/[projects]/smsdaemon/Util.cpp
ViewVC logotype

Annotation of /smsdaemon/Util.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (hide annotations) (download)
Fri Jul 4 07:58:26 2008 UTC (15 years, 10 months ago) by torben
Original Path: smsdaemon/util.cpp
File size: 6443 byte(s)
Make str_toupper and str_tolower handle danish characters correctly.

1 torben 26
2     #include <string>
3     #include <sstream>
4     #include <vector>
5 torben 38 #include <cctype>
6    
7 torben 26 #include <iostream>
8     #include <fstream>
9     #include <iconv.h>
10 torben 49 #include <stdexcept>
11 torben 26 #include <time.h>
12 torben 89 #include <sys/time.h>
13 torben 26
14     #include "util.h"
15    
16     using namespace std;
17    
18     namespace Util
19     {
20     void str_dump(const string& str)
21     {
22     for (unsigned i=0; i<str.length(); ++i)
23     {
24     cout.width(2);
25     cout.fill('0');
26     cout << hex << static_cast<int>(str.at(i)) << " ";
27     }
28     cout.width(1);
29     cout << dec << endl;
30     }
31    
32    
33    
34 torben 38 string str_replace(string str, string search, string replace)
35 torben 26 {
36     unsigned int pos = 0;
37    
38 torben 38 while ( (pos = str.find(search,pos)) != string::npos)
39 torben 26 {
40 torben 38 str.replace(pos, search.size(), replace);
41 torben 26 pos += replace.size();
42     }
43 torben 38 return str;
44 torben 26 }
45    
46 torben 42 std::string str_replace_char(std::string str, char search, char replace)
47     {
48     unsigned int pos = 0;
49    
50     while ( (pos = str.find(search,pos)) != string::npos)
51     {
52     str.replace(pos, 1, 1, replace);
53     }
54     return str;
55     }
56    
57 torben 78 bool my_isspace(char ch)
58     {
59     return (isspace(ch) || ch == 0);
60     }
61 torben 38
62     string str_trim(string str)
63     {
64 torben 78 while (str.length() > 0 && my_isspace(str.at(0)))
65 torben 38 str.erase(0,1);
66 torben 78 while (str.length() > 0 && my_isspace(str.at(str.length()-1)))
67 torben 38 str.erase(str.length()-1,1);
68     return str;
69     }
70    
71     string str_striptags(string str)
72     {
73     unsigned int pos=0;
74    
75     while ( (pos=str.find("<",pos)) != string::npos)
76     {
77     unsigned int endpos = str.find(">",pos);
78     if (endpos == string::npos)
79     break;
80     str.erase(pos, (endpos-pos)+1);
81     }
82    
83     return str;
84     }
85    
86 torben 26 vector<string> str_split(string input, string delimiter)
87     {
88     vector<string> retval;
89    
90     while ( 1 )
91     {
92     unsigned int endpos = input.find(delimiter);
93    
94     string entry = input.substr(0,endpos);
95     retval.push_back( entry );
96    
97    
98     if (endpos == string::npos)
99     break;
100    
101     endpos += delimiter.length();
102     input = input.substr(endpos, input.length() - endpos);
103     }
104    
105     return retval;
106     }
107    
108 torben 108 char my_toupper(char ch)
109     {
110     if (ch == 'æ')
111     return 'Æ';
112     if (ch == 'ø')
113     return 'Ø';
114     if (ch == 'å')
115     return 'Å';
116 torben 43
117 torben 108 return ::toupper(ch);
118     }
119    
120     char my_tolower(char ch)
121     {
122     if (ch == 'Æ')
123     return 'æ';
124     if (ch == 'Ø')
125     return 'ø';
126     if (ch == 'Å')
127     return 'å';
128    
129     return ::tolower(ch);
130     }
131 torben 43 string str_toupper(string str)
132     {
133     for (unsigned i=0; i<str.length(); ++i)
134     {
135 torben 108 str.replace(i, 1 ,1, my_toupper(str.at(i)));
136 torben 43 }
137     return str;
138     }
139    
140     string str_tolower(string str)
141     {
142     for (unsigned i=0; i<str.length(); ++i)
143     {
144 torben 108 str.replace(i, 1 ,1, my_tolower(str.at(i)));
145 torben 43 }
146     return str;
147     }
148    
149    
150 torben 59 string str_formatint(int i)
151     {
152     std::ostringstream os;
153     os << i;
154     return os.str();
155     }
156    
157    
158 torben 38 string str_characters(string str)
159     {
160     string rep;
161     rep.append(1,197);
162     str = str_replace(str, "&#197;", rep);
163    
164     rep.at(0) = 198;
165     str = str_replace(str, "&#198;", rep);
166    
167     rep.at(0) = 216;
168     str = str_replace(str, "&#216;", rep);
169    
170    
171     rep.at(0) = 229;
172     str = str_replace(str, "&#229;", rep);
173    
174     rep.at(0) = 230;
175     str = str_replace(str, "&#230;", rep);
176    
177     rep.at(0) = 248;
178     str = str_replace(str, "&#248;", rep);
179    
180     return str;
181     }
182    
183 torben 42
184 torben 43 string str_gsm2latin(string str)
185 torben 42 {
186     str = str_replace_char(str, 0x1c, 198); //AE
187     str = str_replace_char(str, 0x0b, 216); //OE
188     str = str_replace_char(str, 0x0e, 197); //AA
189    
190     str = str_replace_char(str, 0x1d, 230); //ae
191     str = str_replace_char(str, 0x0c, 248); //oe
192     str = str_replace_char(str, 0x0f, 229); //aa
193     return str;
194     }
195    
196 torben 43 string str_latin2gsm(string str)
197     {
198     str = str_replace_char(str, 198, 0x1c); //AE
199     str = str_replace_char(str, 216, 0x0b); //OE
200     str = str_replace_char(str, 197, 0x0e); //AA
201    
202     str = str_replace_char(str, 230, 0x1d); //ae
203     str = str_replace_char(str, 248, 0x0c); //oe
204     str = str_replace_char(str, 229, 0x0f); //aa
205     return str;
206     }
207    
208 torben 26 string readUrl(string url, string tempfile)
209     {
210     char buf[128000];
211     string document;
212    
213     ostringstream command;
214 torben 49 command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null \"" << url << "\"";
215 torben 86 int res = my_system( command.str().c_str() );
216 torben 79
217 torben 86 if (res<0)
218     {
219     throw( std::runtime_error("Error retrieving document"));
220     }
221    
222 torben 80 if (res>0)
223 torben 79 {
224 torben 86 throw std::runtime_error("Command time out or document not found");
225 torben 80 }
226 torben 26
227     ifstream in( tempfile.c_str() );
228    
229     if (!in)
230     return "";
231    
232     while (!in.eof() )
233     {
234     in.getline(buf,128000-1);
235     document += buf;
236     document += "\n";
237     }
238     in.close();
239     unlink(tempfile.c_str());
240    
241     return document;
242     }
243    
244 torben 42
245    
246     string iconv_wrapper(string _input, string to_format, string from_format)
247 torben 26 {
248     char* input,*output,*input_ptr, *output_ptr;
249     input = input_ptr = (char*) malloc(_input.size()+1);
250     strcpy(input, _input.c_str());
251    
252     output = output_ptr = (char*) malloc(_input.size()*2);
253    
254    
255    
256     unsigned int realinsize,insize,outsize,realsize;
257    
258 torben 42 iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str());
259 torben 26 if (icv == (iconv_t)-1)
260     {
261     perror(0);
262     return "";
263     }
264    
265    
266     realsize = outsize = _input.size()*2;
267     realinsize = insize = _input.size();
268    
269     iconv(icv,
270     &input_ptr,
271     &insize,
272     &output_ptr,
273     &outsize);
274    
275    
276     perror(0);
277     /* cout << "len=" << len << endl;
278     cout << "outsize=" << outsize << endl;
279     cout << "realsize=" << realsize << endl;
280     cout << "insize=" << insize << endl;
281     cout << "realinsize=" << realinsize << endl;*/
282     iconv_close(icv);
283    
284     string returnstr;
285     for (unsigned int i=0; i<realsize - outsize; i++)
286     {
287     //cout << i << ":" << (unsigned short) output[i] << "|" << (unsigned short) input[i];
288     //cout << " (" << output[i] << ")" << endl;
289     returnstr += output[i];
290     }
291     return returnstr;
292     }
293    
294 torben 42 std::string convertToUnicode(std::string str)
295     {
296     return iconv_wrapper(str, "UTF-8", "ISO8859-1");
297     }
298    
299     std::string convertFromUnicode(std::string str)
300     {
301     return iconv_wrapper(str, "ISO8859-1", "UTF-8");
302     }
303    
304 torben 26 int uTimeDiff(const timeval& then, const timeval& now)
305     {
306     return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec);
307     }
308    
309     int mTimeDiff(const timeval& then, const timeval& now)
310     {
311 torben 84 return ( (now.tv_sec - then.tv_sec)*1000) +
312     ((now.tv_usec-then.tv_usec)/1000);
313 torben 26 }
314    
315 torben 89
316     timeval GetTimeOfDay()
317     {
318     timeval now;
319     gettimeofday(&now,0);
320     return now;
321     }
322    
323 torben 107 int my_system(const char* cmd, std::string* response)
324 torben 86 {
325     FILE* p;
326 torben 107 if ((p = popen(cmd,"r")) == NULL)
327 torben 86 return (-1);
328 torben 107
329     if (response)
330     {
331     std::string output;
332     char buf[256];
333     while(!feof(p))
334     {
335     int len = fread(buf,1,255, p);
336     buf[len] = 0;
337     output += buf;
338     }
339     *response = output;
340     }
341    
342 torben 86 return (pclose(p));
343     }
344    
345 torben 26 }

  ViewVC Help
Powered by ViewVC 1.1.20