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

Annotation of /smsdaemon/Util.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 132 - (hide annotations) (download)
Sun Dec 7 00:59:05 2008 UTC (15 years, 5 months ago) by torben
Original Path: smsdaemon/util.cpp
File size: 6905 byte(s)
Added spooling (queing) function, with a standalone application(smsqueue) to put new messages 
into the spool dir.

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

  ViewVC Help
Powered by ViewVC 1.1.20