]> git.stg.codes - stg.git/blob - projects/traffcounter/utils.cpp
Merge branch 'log-unauth-reasons'
[stg.git] / projects / traffcounter / utils.cpp
1 #include <algorithm>
2 #include <functional>
3
4 #include <iostream>
5 #include <cerrno>
6 #include <cstring>
7
8 #include <arpa/inet.h>
9
10 #include "utils.h"
11
12 using namespace std;
13
14 string STG::ToLower(const string & val, const locale & loc)
15 {
16     std::string res;
17     transform(val.begin(),
18               val.end(),
19               back_inserter(res),
20               STG::ToLowerHelper(loc));
21     return res;
22 }
23
24 string STG::ToUpper(const string & val, const locale & loc)
25 {
26     std::string res;
27     transform(val.begin(),
28               val.end(),
29               back_inserter(res),
30               STG::ToUpperHelper(loc));
31     return res;
32 }
33
34 string STG::Trim(const string & val, const locale & loc)
35 {
36     if (val.empty())
37         return std::string();
38     string::const_iterator first(find_if(
39                 val.begin(),
40                 val.end(),
41                 STG::IsNotSpace(loc)));
42     string::const_reverse_iterator last(find_if(
43                 val.rbegin(),
44                 val.rend(),
45                 STG::IsNotSpace(loc)));
46     if (first == val.end())
47         return std::string();
48     return std::string(first, last.base());
49 }
50 std::string inet_ntostring(uint32_t ip)
51 {
52     char buf[INET_ADDRSTRLEN + 1];
53
54     return inet_ntop(AF_INET, &ip, buf, INET_ADDRSTRLEN);
55 }