1 /*
2  * (C) 2006 Oleg V. Palij <o.palij@gmail.com>
3  * Released under the GNU GPL, see the COPYING file in the source distribution for its full text.
4  */
5 
6 //stringstream
7 #include <sstream>
8 //setprecision
9 #include <iomanip>
10 //inet_ntoa
11 #include <arpa/inet.h>
12 //transform
13 #include <algorithm>
14 //errno
15 #include <cerrno>
16 //LONG_MIN, LONG_MAX
17 #include <climits>
18 
19 #include "Utils.hpp"
20 
21 using std::string;
22 using std::vector;
23 
SplitString(string str,string delim)24 vector<string> Utils::SplitString(string str, string delim) {
25    vector<string> result;
26    std::string::size_type found = str.find_first_of(delim);
27    while (found != string::npos) {
28       result.push_back(str.substr(0, found));
29       str = str.substr(found+1);
30       found = str.find_first_of(delim);
31    }
32    if (str.size() != 0) {
33       result.push_back(str);
34    }
35    return result;
36 }
37 
SplitIPPort(string ipport)38 std::pair <string, string> Utils::SplitIPPort(string ipport) {
39    std::pair <string, string> result;
40    std::string::size_type found = ipport.find_last_of(":");
41    if (found != string::npos) {
42       result.first = ipport.substr(0, found);
43       result.second = ipport.substr(found+1);
44    }
45    return result;
46 }
47 
JoinVector(vector<string> inv,string delim)48 string Utils::JoinVector(vector<string> inv, string delim) {
49    string result = "";
50    for (vector<string>::iterator it = inv.begin(); it != inv.end(); ++it)
51       result += (*it + delim);
52    if (!result.empty())
53       result.erase(result.size()-delim.size(), delim.size());
54    return result;
55 }
56 
stol(string s)57 long int Utils::stol(string s) {
58    errno = 0;
59    char *endptr;
60    int base = 10;
61    long int val = strtol(s.c_str(), &endptr, base);
62    if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
63       || (errno != 0 && val == 0)) {
64       throw string("Unacceptable input");
65    }
66    string end = string(endptr);
67    if (end.size() != 0) {
68       throw string("Invalid input '"+end+"'");
69    }
70    return val;
71 }
72 
itos(long long num)73 string Utils::itos(long long num) {
74     std::stringstream ss;
75     ss << num;
76     return (ss.str());
77 }
78 
ftos(double num,int prec)79 string Utils::ftos(double num, int prec) {
80     std::stringstream ss;
81     ss << std::setiosflags(std::ios::fixed) << std::setprecision(prec) << num;
82     return (ss.str());
83 }
84 
UsernamesToStr(vector<string> & in)85 string Utils::UsernamesToStr(vector<string>& in) {
86    string result = "";
87    for (vector<string>::iterator it = in.begin(); it != in.end(); ++it)
88         result += *it + ", ";
89    result.resize(result.size()-2);
90    return result;
91 }
92 
ConvertTime(long etime)93 string Utils::ConvertTime(long etime) {
94     string result="";
95     long hours   = etime/3600;
96     long minutes = (etime/60) % 60;
97     long seconds = etime % 60;
98     if (hours != 0) {
99         result = itos(hours) + "h ";
100     }
101     if (minutes != 0) {
102         result += itos(minutes) + "m ";
103     }
104     if (seconds != 0) {
105         result += itos(seconds) + "s";
106     } else if ((minutes == 0) && (hours == 0)) {
107         result = "0s";
108     }
109     return result;
110 }
111 
ConvertSize(long long esize)112 string Utils::ConvertSize(long long esize) {
113     string result="";
114     long long gb = esize/1024/1024/1024;
115     long long mb = esize/1024/1024 - gb*1024;
116     long long kb = (esize/1024) % 1024;
117     if (gb != 0) {
118         result += itos(gb) + "GB ";
119     }
120     if (mb != 0) {
121         result += itos(mb) + "MB ";
122     }
123     result += itos(kb) + "KB";
124     return result;
125 }
126 
ConvertSpeedPair(long long speed)127 std::pair <string, string> Utils::ConvertSpeedPair(long long speed) {
128    std::pair <string, string> result;
129    long long mb = speed/1024/1024;
130    //long kb = speed/1024;
131    if (mb != 0) {
132        result.first = ftos(speed/1024.0/1024.0, 2);
133        result.second = "MB/s";
134    } else {
135        result.first = ftos(speed/1024.0, 1);
136        result.second = "KB/s";
137    }
138    return result;
139 }
140 
ConvertSpeed(long long speed)141 string Utils::ConvertSpeed(long long speed) {
142    std::pair <string, string> result = Utils::ConvertSpeedPair(speed);
143    return result.first+" "+result.second;
144 }
145 
VectorFindSubstr(vector<string> & v,string & str)146 bool Utils::VectorFindSubstr(vector<string>& v, string& str) {
147    for(vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
148       if ((*it).find(str) != string::npos) return true;
149    }
150    return false;
151 }
152 
MemberOf(vector<string> & v,string & str)153 bool Utils::MemberOf(vector<string>& v, string& str) {
154      if (find(v.begin(), v.end(), str) == v.end())
155         return false;
156      else return true;
157 }
158 
VectorDeleteStr(vector<string> & v,string & str)159 void Utils::VectorDeleteStr(vector<string>& v, string& str)
160 {
161    vector<string>::iterator vItr = v.begin();
162    while ( vItr != v.end() ) {
163       if ( (*vItr) == str ) {
164          vItr = v.erase( vItr ); // Will return next valid iterator
165          break;
166       } else {
167          vItr++;
168       }
169    }
170 }
171 
IPMemberOf(vector<string> & v,string & ip_in)172 bool Utils::IPMemberOf(vector<string>& v, string& ip_in) {
173      for (vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
174         vector<string> ip_mask = SplitString(*it, "/");
175         unsigned long int ip = inet_addr(ip_mask[0].c_str());
176         unsigned long int mask;
177         if (ip_mask.size() > 1) {
178            if (ip_mask[1].size() > 2) {
179                mask = inet_addr(ip_mask[1].c_str());
180            } else {
181                std::stringstream ss(ip_mask[1]);
182                int mask_bits;
183                ss >> mask_bits;
184                mask = mask_bits ? htonl(0xfffffffful << (32 - mask_bits)) : 0;
185            }
186         } else {
187            mask = inet_addr("255.255.255.255");
188         }
189         if ((inet_addr (ip_in.c_str()) & mask) == ip)
190            return true;
191     }
192     return false;
193 }
194 
ToLower(string & rData)195 void Utils::ToLower(string& rData) {
196      transform(rData.begin(), rData.end(), rData.begin(), ::tolower);
197 }
198 
UserMemberOf(vector<string> & v,vector<string> & users)199 bool Utils::UserMemberOf(vector<string>& v, vector<string>& users) {
200      for (vector<string>::iterator it = users.begin(); it != users.end(); ++it) {
201          if (MemberOf(v, *it))
202             return true;
203      }
204      return false;
205 }
206 
replace(string text,string s,string d)207 string Utils::replace(string text, string s, string d)
208 {
209   for(std::string::size_type index=0; index=text.find(s, index), index!=std::string::npos;)
210   {
211     text.erase(index, s.length());
212     text.insert(index, d);
213     index+=d.length();
214   }
215   return text;
216 }
217 
218 // vim: ai ts=3 sts=3 et sw=3 expandtab
219