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

Diff of /smsdaemon/Util.cpp

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

revision 84 by torben, Mon Jun 16 06:12:45 2008 UTC revision 203 by torben, Fri Dec 19 07:03:34 2008 UTC
# Line 9  Line 9 
9  #include <iconv.h>  #include <iconv.h>
10  #include <stdexcept>  #include <stdexcept>
11  #include <time.h>  #include <time.h>
12    #include <sys/time.h>
13    #include <sys/types.h>
14    #include <sys/wait.h>
15    #include <errno.h>
16    #include <stdlib.h>
17    
18  #include "util.h"  #include <cstring>
19    #include "Util.h"
20    
21  using namespace std;  using namespace std;
22    
23  namespace Util  namespace Util
24  {  {
25            const int GN_CHAR_ALPHABET_SIZE = 128;
26            unsigned char gsm_default_alphabet[GN_CHAR_ALPHABET_SIZE] =
27            {
28    
29                    /* ETSI GSM 03.38, version 6.0.1, section 6.2.1; Default alphabet */
30                    /* Characters in hex position 10, [12 to 1a] and 24 are not present on
31                    *        latin1 charset, so we cannot reproduce on the screen, however they are
32                    *               greek symbol not present even on my Nokia */
33    
34                    '@',  0xa3, '$',  0xa5, 0xe8, 0xe9, 0xf9, 0xec,
35                    0xf2, 0xc7, '\n', 0xd8, 0xf8, '\r', 0xc5, 0xe5,
36                    '?',  '_',  '?',  '?',  '?',  '?',  '?',  '?',
37                    '?',  '?',  '?',  '?',  0xc6, 0xe6, 0xdf, 0xc9,
38                    ' ',  '!',  '\"', '#',  0xa4,  '%',  '&',  '\'',
39                    '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
40                    '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
41                    '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
42                    0xa1, 'A',  'B',  'C',  'D',  'E',  'F',  'G',
43                    'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
44                    'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
45                    'X',  'Y',  'Z',  0xc4, 0xd6, 0xd1, 0xdc, 0xa7,
46                    0xbf, 'a',  'b',  'c',  'd',  'e',  'f',  'g',
47                    'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
48                    'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
49                    'x',  'y',  'z',  0xe4, 0xf6, 0xf1, 0xfc, 0xe0
50            };
51    
52            static unsigned char gsm_reverse_default_alphabet[256];
53            static bool reversed = false;
54    
55            static void tbl_setup_reverse()
56            {
57                    int i;
58    
59                    if (reversed) return;
60                    memset(gsm_reverse_default_alphabet, 0x3f, 256);
61                    for (i = GN_CHAR_ALPHABET_SIZE - 1; i >= 0; i--)
62                            gsm_reverse_default_alphabet[ gsm_default_alphabet[i] ] = i;
63                    gsm_reverse_default_alphabet['?'] = 0x3f;
64                    reversed = true;
65            }
66    
67            unsigned char char_def_alphabet_encode(unsigned char value)
68            {
69                    tbl_setup_reverse();
70                    return gsm_reverse_default_alphabet[value];
71            }
72    
73            unsigned char char_def_alphabet_decode(unsigned char value)
74            {
75                    if (value < GN_CHAR_ALPHABET_SIZE)
76                    {
77                            return gsm_default_alphabet[value];
78                    }
79                    else
80                    {
81                            return '?';
82                    }
83            }
84    
85    
86          void str_dump(const string& str)          void str_dump(const string& str)
87          {          {
88              for (unsigned i=0; i<str.length(); ++i)                  for (unsigned i=0; i<str.length(); ++i)
89              {                  {
90                          cout.width(2);                          cout.width(2);
91                          cout.fill('0');                          cout.fill('0');
92                          cout << hex << static_cast<int>(str.at(i)) << " ";                          cout << hex << static_cast<int>(str.at(i)) << " ";
# Line 33  namespace Util Line 100  namespace Util
100          string str_replace(string str, string search, string replace)          string str_replace(string str, string search, string replace)
101          {          {
102                  unsigned int pos = 0;                  unsigned int pos = 0;
103                    
104                  while ( (pos = str.find(search,pos)) != string::npos)                  while ( (pos = str.find(search,pos)) != string::npos)
105                  {                  {
106                          str.replace(pos, search.size(), replace);                          str.replace(pos, search.size(), replace);
# Line 42  namespace Util Line 109  namespace Util
109                  return str;                  return str;
110          }          }
111    
112      std::string str_replace_char(std::string str, char search, char replace)          std::string str_replace_char(std::string str, char search, char replace)
113          {          {
114                  unsigned int pos = 0;                  unsigned int pos = 0;
115    
# Line 82  namespace Util Line 149  namespace Util
149                  return str;                  return str;
150          }          }
151    
152    
153            vector<string>  str_split(string input)
154            {
155                    string buf;
156                    stringstream ss(input);
157                    vector<string> tokens;
158                    while (ss >> buf)
159                            tokens.push_back(buf);
160                    return tokens;
161            }
162    
163    
164          vector<string> str_split(string input, string delimiter)          vector<string> str_split(string input, string delimiter)
165          {          {
166          vector<string> retval;                  vector<string> retval;
167    
168                  while ( 1 )                  while ( 1 )
169                  {                  {
# Line 104  namespace Util Line 183  namespace Util
183                  return retval;                  return retval;
184          }          }
185    
186            char danish_map[3][2] = { {198,230}, {216,248}, {197,229} }; // aelig, oslash, aring
187    
188      string str_toupper(string str)          unsigned char my_toupper(unsigned char ch)
189            {
190                    if (ch == 230)
191                            return 198;
192                    if (ch == 248)
193                            return 216;
194                    if (ch == 229)
195                            return 197;
196    
197                    return ::toupper(ch);
198            }
199    
200            unsigned char my_tolower(unsigned char ch)
201            {
202                    if (ch == 198)
203                            return 230;
204                    if (ch == 216)
205                            return 248;
206                    if (ch == 197)
207                            return 229;
208    
209                    return ::tolower(ch);
210            }
211            string str_toupper(string str)
212          {          {
213                  for (unsigned i=0; i<str.length(); ++i)                  for (unsigned i=0; i<str.length(); ++i)
214                  {                  {
215                          str.replace(i, 1 ,1, ::toupper(str.at(i)));                          str.replace(i, 1 ,1, my_toupper(str.at(i)));
216                  }                  }
217                  return str;                  return str;
218          }          }
# Line 118  namespace Util Line 221  namespace Util
221          {          {
222                  for (unsigned i=0; i<str.length(); ++i)                  for (unsigned i=0; i<str.length(); ++i)
223                  {                  {
224                          str.replace(i, 1 ,1, ::tolower(str.at(i)));                          str.replace(i, 1 ,1, my_tolower(str.at(i)));
225                  }                  }
226                  return str;                  return str;
227          }          }
# Line 137  namespace Util Line 240  namespace Util
240                  string rep;                  string rep;
241                  rep.append(1,197);                  rep.append(1,197);
242                  str = str_replace(str, "&#197;", rep);                  str = str_replace(str, "&#197;", rep);
243                    str = str_replace(str, "&Aring;", rep);
244    
245                  rep.at(0) = 198;                  rep.at(0) = 198;
246                  str = str_replace(str, "&#198;", rep);                  str = str_replace(str, "&#198;", rep);
247                    str = str_replace(str, "&AElig;", rep);
248    
249                  rep.at(0) = 216;                  rep.at(0) = 216;
250                  str = str_replace(str, "&#216;", rep);                  str = str_replace(str, "&#216;", rep);
251                    str = str_replace(str, "&Oslash;", rep);
252    
253    
254                  rep.at(0) = 229;                  rep.at(0) = 229;
255                  str = str_replace(str, "&#229;", rep);                  str = str_replace(str, "&#229;", rep);
256                    str = str_replace(str, "&aring;", rep);
257    
258                  rep.at(0) = 230;                  rep.at(0) = 230;
259                  str = str_replace(str, "&#230;", rep);                  str = str_replace(str, "&#230;", rep);
260                    str = str_replace(str, "&aelig;", rep);
261    
262                  rep.at(0) = 248;                  rep.at(0) = 248;
263                  str = str_replace(str, "&#248;", rep);                  str = str_replace(str, "&#248;", rep);
264                    str = str_replace(str, "&oslash;", rep);
265    
266                  return str;                  return str;
267          }          }
# Line 160  namespace Util Line 269  namespace Util
269    
270          string str_gsm2latin(string str)          string str_gsm2latin(string str)
271          {          {
272                  str = str_replace_char(str, 0x1c, 198); //AE                  for (unsigned i=0; i<str.size(); i++)
273                  str = str_replace_char(str, 0x0b, 216); //OE                  {
274                  str = str_replace_char(str, 0x0e, 197); //AA                          str.at(i) = char_def_alphabet_decode(str.at(i));
275                    }
                 str = str_replace_char(str, 0x1d, 230); //ae  
                 str = str_replace_char(str, 0x0c, 248); //oe  
                 str = str_replace_char(str, 0x0f, 229); //aa  
276                  return str;                  return str;
277          }          }
278    
279          string str_latin2gsm(string str)          string str_latin2gsm(string str)
280          {          {
281                  str = str_replace_char(str, 198, 0x1c); //AE                  for (unsigned i=0; i<str.size(); i++)
282                  str = str_replace_char(str, 216, 0x0b); //OE                  {
283                  str = str_replace_char(str, 197, 0x0e); //AA                          str.at(i) = char_def_alphabet_encode(str.at(i));
284                    }
                 str = str_replace_char(str, 230, 0x1d); //ae  
                 str = str_replace_char(str, 248, 0x0c); //oe  
                 str = str_replace_char(str, 229, 0x0f); //aa  
285                  return str;                  return str;
286          }          }
287    
288            /* deprecated, use HttpClient instead
289          string readUrl(string url, string tempfile)          string readUrl(string url, string tempfile)
290          {          {
291                  char buf[128000];                  char buf[128000];
292                  string document;                  string document;
293                    
294                  ostringstream command;                  ostringstream command;
295                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";
296                  int res = system( command.str().c_str() );                  int res = my_system( command.str().c_str() );
297    
298                    if (res<0)
299                    {
300                            throw( std::runtime_error("Error retrieving document"));
301                    }
302    
303                  if (res>0)                  if (res>0)
304                  {                  {
305                          throw std::runtime_error("Command time out");                          throw std::runtime_error("Command time out or document not found");
306                  }                  }
307    
308                  ifstream in( tempfile.c_str() );                  ifstream in( tempfile.c_str() );
# Line 209  namespace Util Line 318  namespace Util
318                  }                  }
319                  in.close();                  in.close();
320                  unlink(tempfile.c_str());                  unlink(tempfile.c_str());
321                    
322                  return document;                  return document;
323          }          }*/
324    
325    
           
326    
327          string iconv_wrapper(string _input, string to_format, string from_format)          string iconv_wrapper(string _input, string to_format, string from_format)
328          {          {
329                  char* input,*output,*input_ptr, *output_ptr;                  char* input,*output,*input_ptr, *output_ptr;
330                  input = input_ptr = (char*) malloc(_input.size()+1);                  input = input_ptr = (char*) malloc(_input.size()+1);
331                  strcpy(input, _input.c_str());                  strcpy(input, _input.c_str());
332                    
333                  output = output_ptr = (char*) malloc(_input.size()*2);                  output = output_ptr = (char*) malloc(_input.size()*2);
334    
335    
336                    
337                  unsigned int realinsize,insize,outsize,realsize;                  unsigned int realinsize,insize,outsize,realsize;
338                    
339                  iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str());                  iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str());
340                  if (icv == (iconv_t)-1)                  if (icv == (iconv_t)-1)
341                  {                  {
# Line 234  namespace Util Line 343  namespace Util
343                          return "";                          return "";
344                  }                  }
345    
346                    
347                  realsize = outsize = _input.size()*2;                  realsize = outsize = _input.size()*2;
348                  realinsize = insize = _input.size();                  realinsize = insize = _input.size();
349                    
350                  iconv(icv,                  iconv(icv,
351                                  &input_ptr,                        &input_ptr,
352                                  &insize,                        &insize,
353                                  &output_ptr,                        &output_ptr,
354                                  &outsize);                        &outsize);
355    
356    
357                  perror(0);                  perror(0);
358  /*              cout << "len=" << len << endl;                  /*              cout << "len=" << len << endl;
359                  cout << "outsize=" <<  outsize << endl;                                  cout << "outsize=" <<  outsize << endl;
360                  cout << "realsize=" << realsize << endl;                                  cout << "realsize=" << realsize << endl;
361                  cout << "insize=" << insize << endl;                                  cout << "insize=" << insize << endl;
362                  cout << "realinsize=" << realinsize << endl;*/                                  cout << "realinsize=" << realinsize << endl;*/
363                  iconv_close(icv);                  iconv_close(icv);
364    
365                  string returnstr;                  string returnstr;
# Line 260  namespace Util Line 369  namespace Util
369                          //cout << " (" << output[i] << ")" << endl;                          //cout << " (" << output[i] << ")" << endl;
370                          returnstr += output[i];                          returnstr += output[i];
371                  }                  }
372              return returnstr;                  return returnstr;
373          }          }
374    
375          std::string convertToUnicode(std::string str)          std::string convertToUnicode(std::string str)
# Line 281  namespace Util Line 390  namespace Util
390          int mTimeDiff(const timeval& then, const timeval& now)          int mTimeDiff(const timeval& then, const timeval& now)
391          {          {
392                  return ( (now.tv_sec - then.tv_sec)*1000) +                  return ( (now.tv_sec - then.tv_sec)*1000) +
393                                  ((now.tv_usec-then.tv_usec)/1000);                         ((now.tv_usec-then.tv_usec)/1000);
394          }          }
395    
 }  
396    
397            timeval GetTimeOfDay()
398            {
399                    timeval now;
400                    gettimeofday(&now,0);
401                    return now;
402            }
403    
404            int my_system(const char* cmd, std::string* response)
405            {
406                    FILE* p;
407                    if ((p = popen(cmd,"r")) == NULL)
408                            return (-1);
409    
410                    if (response)
411                    {
412                            std::string output;
413                            char buf[256];
414                            while (!feof(p))
415                            {
416                                    int len = fread(buf,1,255, p);
417                                    buf[len] = 0;
418                                    output += buf;
419                            }
420                            *response = output;
421                    }
422    
423                    return (pclose(p));
424            }
425    
426            int my_system2 (const char *command)
427            {
428                    int pid, status;
429    
430                    if (command == 0)
431                            return 1;
432            //      printf("forking ...\n");
433                    pid = fork();
434                    if (pid == -1)
435                            return -1;
436                    if (pid == 0)
437                    {
438                            char *argv[4];
439                            argv[0] = (char*) "sh";
440                            argv[1] = (char*) "-c";
441                            argv[2] = (char *) command;
442                            argv[3] = 0;
443            //              printf("chaining ...\n");
444                            execve("/bin/sh", argv, environ);
445            //              printf("Chaining failed!\n");
446                            exit(127);
447                    }
448                    do
449                    {
450            //              printf("Waiting on PID: %d\n",pid);
451                            if (waitpid(pid, &status, 0) == -1)
452                            {
453            //                      printf("erc: %d\n",errno);
454                                    if (errno != EINTR)
455                                            return -1;
456                            }
457                            else
458                            {
459            //                      printf("status: %d\n",status);
460                                    return status;
461                            }
462                    }
463                    while (1);
464            }
465    
466    
467            string readfile(string filename)
468            {
469                    string str;
470                    ifstream in(filename.c_str());
471                    if (in)
472                    {
473                            char buffer[4096];
474                            in.read(buffer, 4095);
475                            buffer[ in.gcount() ] = 0;
476                            str = string(buffer);
477                            in.close();
478                    }
479                    else
480                    {
481                            string message =  "Could not open ";
482                            message +=  filename;
483                            throw std::runtime_error(message);
484                    }
485                    return str;
486            }
487    
488    
489    
490    }
491    

Legend:
Removed from v.84  
changed lines
  Added in v.203

  ViewVC Help
Powered by ViewVC 1.1.20