root/util.cc

Revision 43f521291669f483d361949fa6c886c0fbd94daf, 3.5 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 21 months ago)

Add support for paginating thread and article lists.

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009, 2010 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20#include "util.hh"
21
22#include <boost/date_time/posix_time/posix_time.hpp>
23#include <boost/asio/streambuf.hpp>
24
25namespace util
26{
27        std::string split(std::string &s, const char *sep)
28        {
29                if (s.length() == 0) return s;
30                std::string::size_type k1 = s.find_first_not_of(sep);
31                if (k1 == std::string::npos) { s = ""; return ""; }
32                std::string::size_type k2 = s.find_first_of(sep, k1);
33                std::string rv = s.substr(k1, k2-k1);
34                s.erase(0, k2);
35                return rv;
36        }
37
38        bool is_number(std::string s)
39        {
40                if (s.empty()) return false;
41                for (size_t i = 0; i < s.length(); i++) {
42                        if (!is_digit(s[i])) return false;
43                }
44                return true;
45        }
46
47        std::string split1R(std::string &s, const char *delim)
48        {
49                size_t dl = s.find_first_of(delim);
50                std::string rv = s.substr(0, dl);
51                if (dl+1 == 0) s.clear(); else s.erase(0, dl+1);
52                return rv;
53        }
54
55        std::string split1L(std::string &s, const char *delim)
56        {
57                size_t dl = s.find_first_of(delim);
58                if (dl == std::string::npos) return "";
59                std::string rv = s.substr(0, dl);
60                s.erase(0, dl+1);
61                return rv;
62        }
63
64        std::string current_imf_date()
65        {
66                using namespace boost::posix_time;
67                std::ostringstream ss;
68                time_facet *tf = new time_facet
69                        ("%a, %d %b %Y %H:%M:%S -0000");
70                ss.imbue(std::locale(std::locale::classic(), tf));
71                ss << second_clock::universal_time();
72                return ss.str();
73        }
74
75        std::string streambuf_to_string(boost::asio::streambuf &sbuf, size_t n)
76        {
77                std::string recv;
78                recv.reserve(n);
79
80                /* Note: boost.asio 1.35's commit is buggy, it doesn't
81                   update the steambuf state, so we have to do this
82                   the hard way. */
83                sbuf.commit(n);
84                typedef boost::asio::streambuf::const_buffers_type
85                        const_buffers_type;
86                const_buffers_type bufs = sbuf.data();
87                for (const_buffers_type::const_iterator it = bufs.begin();
88                     it != bufs.end(); it++)
89                {
90                        using  boost::asio::buffer_cast;
91                        using  boost::asio::buffer_size;
92                        recv += std::string(buffer_cast<const char *>(*it),
93                                            buffer_size(*it));
94                }
95                sbuf.consume(recv.length());
96                return recv;
97        }
98}
Note: See TracBrowser for help on using the browser.