root/util.hh

Revision 12f51629cadbce2beb3d924d761ad2cc07e95ba1, 4.9 KB (checked in by Antti-Juhani Kaijanaho <ajk@…>, 2 years ago)

Fix #63

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

  • 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#ifndef GUARD_UTIL_HH
21#define GUARD_UTIL_HH
22
23#include <boost/asio/streambuf.hpp>
24#include <stdexcept>
25#include <string>
26
27namespace util
28{
29        inline std::string to_lower(const std::string s)
30        {
31                std::string rv = s;
32                for (size_t i = 0; i < s.length(); i++)
33                {
34                        if ('A' <= rv[i] && rv[i] <= 'Z') rv[i] += 'a' - 'A';
35                }
36                return rv;
37        }
38
39        /** Strips off any trailing LF or CRLF. */
40        inline void strip_crlf(std::string &s)
41        {
42                if (*(s.rbegin()) != '\n') return;
43                s.erase(s.length()-1);
44                if (*(s.rbegin()) != '\r') return;
45                s.erase(s.length()-1);
46        }
47
48        inline void strip(std::string &s)
49        {
50                while (!s.empty() &&
51                       (s[0] == ' '  || s[0] == '\t' ||
52                        s[0] == '\r' || s[0] == '\n'))
53                        s.erase(0,1);
54                while (!s.empty() &&
55                       (s[s.length()-1] == ' '  || s[s.length()-1] == '\t' ||
56                        s[s.length()-1] == '\r' || s[s.length()-1] == '\n'))
57                        s.erase(s.length()-1);
58        }
59
60        class parse_exception : public std::exception {
61        public:
62                const char *what() const throw() {
63                        return "parse error";
64                }
65        };
66
67        /* Converts the string s (which must be a sequence of digits)
68         * into a numeric value.  */
69        template <typename Int>
70        Int parse_nonnegative_integer(std::string s)
71                throw (parse_exception)
72        {
73                Int rv = 0;
74
75                for (size_t i = 0; i < s.length();i++) {
76                        if (!('0' <= s[i] && s[i] <= '9')) {
77                                throw parse_exception();
78                        }
79                        rv = 10*rv + (s[i] - '0');
80                }
81                return rv;
82        }
83
84        /** Splits off the first token (which is the longest string
85         * not containing any of the sep characters), and returns it.
86         * Any leading sep characters are erased.  The rest of the
87         * string is stored in s. */
88        std::string split(std::string &s, const char *sep = " \t\r\n");
89
90        /** Splits off the first field and its delimeter (one
91         * character only!).  If there is no delim, the field
92         * encompasses the whole string. */
93        std::string split1R(std::string &s, const char *delim);
94
95        /** Splits off the first field and its delimeter (one
96         * character only!).  If there is no delim, the field is
97         * empty. */
98        std::string split1L(std::string &s, const char *delim);
99
100        /** Returns true if s is a sequence of digits. */
101        bool is_number(std::string s);
102
103        /** Returns true if c is an ASCII digit (note: locale has no
104         * effect).  */
105        inline bool is_digit(char c)
106        {
107                return '0' <= c && c <= '9';
108        }
109
110        // assuming that i is at the start of a character, change it
111        // to be at the start of the next character
112        inline void advance_utf8(std::string s, size_t &i)
113        {
114                unsigned char c = s[i];
115                /**/ if (c < 0xc0) i += 1;
116                else if (c < 0xd0) i += 2;
117                else if (c < 0xe0) i += 3;
118                else               i += 4;
119        }
120
121        /* Returns the current date-time in IMF (RFC 5322) format */
122        std::string current_imf_date();
123
124        // URI-safe base64 - this is _not_ MIME base64
125        inline char b64_char(unsigned val)
126        {
127                if (val < 26) return val + 'A';
128                if (val < 52) return val - 26 + 'a';
129                if (val < 62) return val - 52 + '0';
130                if (val == 62) return '-';
131                if (val == 63) return '.';
132                throw std::logic_error("b64_char");
133        }
134
135        /* WARNING: The resulting string may be longer than n.  If you
136           need exactly n, truncate the string afterward by using the resize(n)
137           method. */
138        std::string streambuf_to_string(boost::asio::streambuf &sbuf, size_t n);
139}
140
141#endif /* GUARD_UTIL_HH */
Note: See TracBrowser for help on using the browser.