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

Diff of /smsdaemon/Util.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 38 by torben, Tue Jun 10 20:25:15 2008 UTC revision 185 by torben, Mon Dec 15 09:10:05 2008 UTC
# Line 7  Line 7 
7  #include <iostream>  #include <iostream>
8  #include <fstream>  #include <fstream>
9  #include <iconv.h>  #include <iconv.h>
10    #include <stdexcept>
11  #include <time.h>  #include <time.h>
12    #include <sys/time.h>
13    #include <stdlib.h>
14    
15  #include "util.h"  #include <cstring>
16    #include "Util.h"
17    
18  using namespace std;  using namespace std;
19    
20  namespace Util  namespace Util
21  {  {
22            const int GN_CHAR_ALPHABET_SIZE = 128;
23            unsigned char gsm_default_alphabet[GN_CHAR_ALPHABET_SIZE] =
24            {
25    
26                    /* ETSI GSM 03.38, version 6.0.1, section 6.2.1; Default alphabet */
27                    /* Characters in hex position 10, [12 to 1a] and 24 are not present on
28                    *        latin1 charset, so we cannot reproduce on the screen, however they are
29                    *               greek symbol not present even on my Nokia */
30    
31                    '@',  0xa3, '$',  0xa5, 0xe8, 0xe9, 0xf9, 0xec,
32                    0xf2, 0xc7, '\n', 0xd8, 0xf8, '\r', 0xc5, 0xe5,
33                    '?',  '_',  '?',  '?',  '?',  '?',  '?',  '?',
34                    '?',  '?',  '?',  '?',  0xc6, 0xe6, 0xdf, 0xc9,
35                    ' ',  '!',  '\"', '#',  0xa4,  '%',  '&',  '\'',
36                    '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
37                    '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
38                    '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
39                    0xa1, 'A',  'B',  'C',  'D',  'E',  'F',  'G',
40                    'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
41                    'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
42                    'X',  'Y',  'Z',  0xc4, 0xd6, 0xd1, 0xdc, 0xa7,
43                    0xbf, 'a',  'b',  'c',  'd',  'e',  'f',  'g',
44                    'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
45                    'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
46                    'x',  'y',  'z',  0xe4, 0xf6, 0xf1, 0xfc, 0xe0
47            };
48    
49            static unsigned char gsm_reverse_default_alphabet[256];
50            static bool reversed = false;
51    
52            static void tbl_setup_reverse()
53            {
54                    int i;
55    
56                    if (reversed) return;
57                    memset(gsm_reverse_default_alphabet, 0x3f, 256);
58                    for (i = GN_CHAR_ALPHABET_SIZE - 1; i >= 0; i--)
59                            gsm_reverse_default_alphabet[ gsm_default_alphabet[i] ] = i;
60                    gsm_reverse_default_alphabet['?'] = 0x3f;
61                    reversed = true;
62            }
63    
64            unsigned char char_def_alphabet_encode(unsigned char value)
65            {
66                    tbl_setup_reverse();
67                    return gsm_reverse_default_alphabet[value];
68            }
69    
70            unsigned char char_def_alphabet_decode(unsigned char value)
71            {
72                    if (value < GN_CHAR_ALPHABET_SIZE)
73                    {
74                            return gsm_default_alphabet[value];
75                    }
76                    else
77                    {
78                            return '?';
79                    }
80            }
81    
82    
83          void str_dump(const string& str)          void str_dump(const string& str)
84          {          {
85              for (unsigned i=0; i<str.length(); ++i)                  for (unsigned i=0; i<str.length(); ++i)
86              {                  {
87                          cout.width(2);                          cout.width(2);
88                          cout.fill('0');                          cout.fill('0');
89                          cout << hex << static_cast<int>(str.at(i)) << " ";                          cout << hex << static_cast<int>(str.at(i)) << " ";
# Line 33  namespace Util Line 97  namespace Util
97          string str_replace(string str, string search, string replace)          string str_replace(string str, string search, string replace)
98          {          {
99                  unsigned int pos = 0;                  unsigned int pos = 0;
100                    
101                  while ( (pos = str.find(search,pos)) != string::npos)                  while ( (pos = str.find(search,pos)) != string::npos)
102                  {                  {
103                          str.replace(pos, search.size(), replace);                          str.replace(pos, search.size(), replace);
# Line 42  namespace Util Line 106  namespace Util
106                  return str;                  return str;
107          }          }
108    
109                    std::string str_replace_char(std::string str, char search, char replace)
110            {
111                    unsigned int pos = 0;
112    
113                    while ( (pos = str.find(search,pos)) != string::npos)
114                    {
115                            str.replace(pos, 1, 1, replace);
116                    }
117                    return str;
118            }
119    
120            bool my_isspace(char ch)
121            {
122                    return (isspace(ch) || ch == 0);
123            }
124    
125          string str_trim(string str)          string str_trim(string str)
126          {          {
127                  while (str.length() > 0 && isspace(str.at(0)))                  while (str.length() > 0 && my_isspace(str.at(0)))
128                          str.erase(0,1);                          str.erase(0,1);
129                  while (str.length() > 0 && isspace(str.at(str.length()-1)))                  while (str.length() > 0 && my_isspace(str.at(str.length()-1)))
130                          str.erase(str.length()-1,1);                          str.erase(str.length()-1,1);
131                  return str;                  return str;
132          }          }
# Line 68  namespace Util Line 146  namespace Util
146                  return str;                  return str;
147          }          }
148    
149    
150            vector<string>  str_split(string input)
151            {
152                    string buf;
153                    stringstream ss(input);
154                    vector<string> tokens;
155                    while (ss >> buf)
156                            tokens.push_back(buf);
157                    return tokens;
158            }
159    
160    
161          vector<string> str_split(string input, string delimiter)          vector<string> str_split(string input, string delimiter)
162          {          {
163          vector<string> retval;                  vector<string> retval;
164    
165                  while ( 1 )                  while ( 1 )
166                  {                  {
# Line 90  namespace Util Line 180  namespace Util
180                  return retval;                  return retval;
181          }          }
182    
183            char danish_map[3][2] = { {198,230}, {216,248}, {197,229} }; // aelig, oslash, aring
184    
185            unsigned char my_toupper(unsigned char ch)
186            {
187                    if (ch == 230)
188                            return 198;
189                    if (ch == 248)
190                            return 216;
191                    if (ch == 229)
192                            return 197;
193    
194                    return ::toupper(ch);
195            }
196    
197            unsigned char my_tolower(unsigned char ch)
198            {
199                    if (ch == 198)
200                            return 230;
201                    if (ch == 216)
202                            return 248;
203                    if (ch == 197)
204                            return 229;
205    
206                    return ::tolower(ch);
207            }
208            string str_toupper(string str)
209            {
210                    for (unsigned i=0; i<str.length(); ++i)
211                    {
212                            str.replace(i, 1 ,1, my_toupper(str.at(i)));
213                    }
214                    return str;
215            }
216    
217            string str_tolower(string str)
218            {
219                    for (unsigned i=0; i<str.length(); ++i)
220                    {
221                            str.replace(i, 1 ,1, my_tolower(str.at(i)));
222                    }
223                    return str;
224            }
225    
226    
227            string str_formatint(int i)
228            {
229                    std::ostringstream os;
230                    os << i;
231                    return os.str();
232            }
233    
234    
235          string str_characters(string str)          string str_characters(string str)
236          {          {
237                  string rep;                  string rep;
# Line 115  namespace Util Line 257  namespace Util
257                  return str;                  return str;
258          }          }
259    
260    
261            string str_gsm2latin(string str)
262            {
263                    for (unsigned i=0; i<str.size(); i++) {
264                            str.at(i) = char_def_alphabet_decode(str.at(i));
265                    }
266                    return str;
267            }
268    
269            string str_latin2gsm(string str)
270            {
271                    for (unsigned i=0; i<str.size(); i++) {
272                            str.at(i) = char_def_alphabet_encode(str.at(i));
273                    }
274                    return str;
275            }
276    
277          string readUrl(string url, string tempfile)          string readUrl(string url, string tempfile)
278          {          {
279                  char buf[128000];                  char buf[128000];
280                  string document;                  string document;
281                    
282                  ostringstream command;                  ostringstream command;
283                  command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\"";                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";
284                  system( command.str().c_str() );                  int res = my_system( command.str().c_str() );
285    
286                    if (res<0)
287                    {
288                            throw( std::runtime_error("Error retrieving document"));
289                    }
290    
291                    if (res>0)
292                    {
293                            throw std::runtime_error("Command time out or document not found");
294                    }
295    
296                  ifstream in( tempfile.c_str() );                  ifstream in( tempfile.c_str() );
297    
# Line 137  namespace Util Line 306  namespace Util
306                  }                  }
307                  in.close();                  in.close();
308                  unlink(tempfile.c_str());                  unlink(tempfile.c_str());
309                    
310                  return document;                  return document;
311          }          }
312    
313          string convertToUnicode(string _input)  
314    
315            string iconv_wrapper(string _input, string to_format, string from_format)
316          {          {
317                  char* input,*output,*input_ptr, *output_ptr;                  char* input,*output,*input_ptr, *output_ptr;
318                  input = input_ptr = (char*) malloc(_input.size()+1);                  input = input_ptr = (char*) malloc(_input.size()+1);
319                  strcpy(input, _input.c_str());                  strcpy(input, _input.c_str());
320                    
321                  output = output_ptr = (char*) malloc(_input.size()*2);                  output = output_ptr = (char*) malloc(_input.size()*2);
322    
323    
324                    
325                  unsigned int realinsize,insize,outsize,realsize;                  unsigned int realinsize,insize,outsize,realsize;
326                    
327                  iconv_t icv = iconv_open("UTF8", "ISO8859-1");                  iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str());
328                  if (icv == (iconv_t)-1)                  if (icv == (iconv_t)-1)
329                  {                  {
330                          perror(0);                          perror(0);
331                          return "";                          return "";
332                  }                  }
333    
334                    
335                  realsize = outsize = _input.size()*2;                  realsize = outsize = _input.size()*2;
336                  realinsize = insize = _input.size();                  realinsize = insize = _input.size();
337                    
338                  iconv(icv,                  iconv(icv,
339                                  &input_ptr,                        &input_ptr,
340                                  &insize,                        &insize,
341                                  &output_ptr,                        &output_ptr,
342                                  &outsize);                        &outsize);
343    
344    
345                  perror(0);                  perror(0);
346  /*              cout << "len=" << len << endl;                  /*              cout << "len=" << len << endl;
347                  cout << "outsize=" <<  outsize << endl;                                  cout << "outsize=" <<  outsize << endl;
348                  cout << "realsize=" << realsize << endl;                                  cout << "realsize=" << realsize << endl;
349                  cout << "insize=" << insize << endl;                                  cout << "insize=" << insize << endl;
350                  cout << "realinsize=" << realinsize << endl;*/                                  cout << "realinsize=" << realinsize << endl;*/
351                  iconv_close(icv);                  iconv_close(icv);
352    
353                  string returnstr;                  string returnstr;
# Line 186  namespace Util Line 357  namespace Util
357                          //cout << " (" << output[i] << ")" << endl;                          //cout << " (" << output[i] << ")" << endl;
358                          returnstr += output[i];                          returnstr += output[i];
359                  }                  }
360              return returnstr;                  return returnstr;
361            }
362    
363            std::string convertToUnicode(std::string str)
364            {
365                    return iconv_wrapper(str, "UTF-8", "ISO8859-1");
366            }
367    
368            std::string convertFromUnicode(std::string str)
369            {
370                    return iconv_wrapper(str, "ISO8859-1", "UTF-8");
371          }          }
372    
373          int uTimeDiff(const timeval& then, const timeval& now)          int uTimeDiff(const timeval& then, const timeval& now)
# Line 196  namespace Util Line 377  namespace Util
377    
378          int mTimeDiff(const timeval& then, const timeval& now)          int mTimeDiff(const timeval& then, const timeval& now)
379          {          {
380                  return uTimeDiff(then,now) / 1000;                  return ( (now.tv_sec - then.tv_sec)*1000) +
381                           ((now.tv_usec-then.tv_usec)/1000);
382          }          }
383    
 }  
384    
385            timeval GetTimeOfDay()
386            {
387                    timeval now;
388                    gettimeofday(&now,0);
389                    return now;
390            }
391    
392            int my_system(const char* cmd, std::string* response)
393            {
394                    FILE* p;
395                    if ((p = popen(cmd,"r")) == NULL)
396                            return (-1);
397    
398                    if (response)
399                    {
400                            std::string output;
401                            char buf[256];
402                            while (!feof(p))
403                            {
404                                    int len = fread(buf,1,255, p);
405                                    buf[len] = 0;
406                                    output += buf;
407                            }
408                            *response = output;
409                    }
410    
411                    return (pclose(p));
412            }
413    
414            string readfile(string filename)
415            {
416                    string str;
417                    ifstream in(filename.c_str());
418                    if (in)
419                    {
420                            char buffer[4096];
421                            in.read(buffer, 4095);
422                            buffer[ in.gcount() ] = 0;
423                            str = string(buffer);
424                            in.close();
425                    }
426                    else
427                    {
428                            string message =  "Could not open ";
429                            message +=  filename;
430                            throw std::runtime_error(message);
431                    }
432                    return str;
433            }
434    
435    
436    
437    }
438    

Legend:
Removed from v.38  
changed lines
  Added in v.185

  ViewVC Help
Powered by ViewVC 1.1.20