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

Diff of /smsdaemon/Util.cpp

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

revision 79 by torben, Sun Jun 15 16:05:14 2008 UTC revision 183 by torben, Fri Dec 12 13:57:10 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 <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)          std::string str_replace_char(std::string str, char search, char replace)
110          {          {
111                  unsigned int pos = 0;                  unsigned int pos = 0;
112    
# Line 84  namespace Util Line 148  namespace Util
148    
149          vector<string> str_split(string input, string delimiter)          vector<string> str_split(string input, string delimiter)
150          {          {
151          vector<string> retval;                  vector<string> retval;
152    
153                  while ( 1 )                  while ( 1 )
154                  {                  {
# Line 104  namespace Util Line 168  namespace Util
168                  return retval;                  return retval;
169          }          }
170    
171            char danish_map[3][2] = { {198,230}, {216,248}, {197,229} }; // aelig, oslash, aring
172    
173            unsigned char my_toupper(unsigned char ch)
174            {
175                    if (ch == 230)
176                            return 198;
177                    if (ch == 248)
178                            return 216;
179                    if (ch == 229)
180                            return 197;
181    
182                    return ::toupper(ch);
183            }
184    
185            unsigned char my_tolower(unsigned char ch)
186            {
187                    if (ch == 198)
188                            return 230;
189                    if (ch == 216)
190                            return 248;
191                    if (ch == 197)
192                            return 229;
193    
194      string str_toupper(string str)                  return ::tolower(ch);
195            }
196            string str_toupper(string str)
197          {          {
198                  for (unsigned i=0; i<str.length(); ++i)                  for (unsigned i=0; i<str.length(); ++i)
199                  {                  {
200                          str.replace(i, 1 ,1, ::toupper(str.at(i)));                          str.replace(i, 1 ,1, my_toupper(str.at(i)));
201                  }                  }
202                  return str;                  return str;
203          }          }
# Line 118  namespace Util Line 206  namespace Util
206          {          {
207                  for (unsigned i=0; i<str.length(); ++i)                  for (unsigned i=0; i<str.length(); ++i)
208                  {                  {
209                          str.replace(i, 1 ,1, ::tolower(str.at(i)));                          str.replace(i, 1 ,1, my_tolower(str.at(i)));
210                  }                  }
211                  return str;                  return str;
212          }          }
# Line 160  namespace Util Line 248  namespace Util
248    
249          string str_gsm2latin(string str)          string str_gsm2latin(string str)
250          {          {
251                  str = str_replace_char(str, 0x1c, 198); //AE                  for (unsigned i=0; i<str.size(); i++) {
252                  str = str_replace_char(str, 0x0b, 216); //OE                          str.at(i) = char_def_alphabet_decode(str.at(i));
253                  str = str_replace_char(str, 0x0e, 197); //AA                  }
   
                 str = str_replace_char(str, 0x1d, 230); //ae  
                 str = str_replace_char(str, 0x0c, 248); //oe  
                 str = str_replace_char(str, 0x0f, 229); //aa  
254                  return str;                  return str;
255          }          }
256    
257          string str_latin2gsm(string str)          string str_latin2gsm(string str)
258          {          {
259                  str = str_replace_char(str, 198, 0x1c); //AE                  for (unsigned i=0; i<str.size(); i++) {
260                  str = str_replace_char(str, 216, 0x0b); //OE                          str.at(i) = char_def_alphabet_encode(str.at(i));
261                  str = str_replace_char(str, 197, 0x0e); //AA                  }
   
                 str = str_replace_char(str, 230, 0x1d); //ae  
                 str = str_replace_char(str, 248, 0x0c); //oe  
                 str = str_replace_char(str, 229, 0x0f); //aa  
262                  return str;                  return str;
263          }          }
264    
# Line 186  namespace Util Line 266  namespace Util
266          {          {
267                  char buf[128000];                  char buf[128000];
268                  string document;                  string document;
269                    
270                  ostringstream command;                  ostringstream command;
271                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";
272                  int res = system( command.str().c_str() );                  int res = my_system( command.str().c_str() );
273    
274  /*              if (res)                  if (res<0)
275                  {                  {
276                          throw std::runtime_error("Command time out");                          throw( std::runtime_error("Error retrieving document"));
277                  }*/                  }
278    
279                    if (res>0)
280                    {
281                            throw std::runtime_error("Command time out or document not found");
282                    }
283    
284                  ifstream in( tempfile.c_str() );                  ifstream in( tempfile.c_str() );
285    
# Line 209  namespace Util Line 294  namespace Util
294                  }                  }
295                  in.close();                  in.close();
296                  unlink(tempfile.c_str());                  unlink(tempfile.c_str());
297                    
298                  return document;                  return document;
299          }          }
300    
301            
302    
303          string iconv_wrapper(string _input, string to_format, string from_format)          string iconv_wrapper(string _input, string to_format, string from_format)
304          {          {
305                  char* input,*output,*input_ptr, *output_ptr;                  char* input,*output,*input_ptr, *output_ptr;
306                  input = input_ptr = (char*) malloc(_input.size()+1);                  input = input_ptr = (char*) malloc(_input.size()+1);
307                  strcpy(input, _input.c_str());                  strcpy(input, _input.c_str());
308                    
309                  output = output_ptr = (char*) malloc(_input.size()*2);                  output = output_ptr = (char*) malloc(_input.size()*2);
310    
311    
312                    
313                  unsigned int realinsize,insize,outsize,realsize;                  unsigned int realinsize,insize,outsize,realsize;
314                    
315                  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());
316                  if (icv == (iconv_t)-1)                  if (icv == (iconv_t)-1)
317                  {                  {
# Line 234  namespace Util Line 319  namespace Util
319                          return "";                          return "";
320                  }                  }
321    
322                    
323                  realsize = outsize = _input.size()*2;                  realsize = outsize = _input.size()*2;
324                  realinsize = insize = _input.size();                  realinsize = insize = _input.size();
325                    
326                  iconv(icv,                  iconv(icv,
327                                  &input_ptr,                        &input_ptr,
328                                  &insize,                        &insize,
329                                  &output_ptr,                        &output_ptr,
330                                  &outsize);                        &outsize);
331    
332    
333                  perror(0);                  perror(0);
334  /*              cout << "len=" << len << endl;                  /*              cout << "len=" << len << endl;
335                  cout << "outsize=" <<  outsize << endl;                                  cout << "outsize=" <<  outsize << endl;
336                  cout << "realsize=" << realsize << endl;                                  cout << "realsize=" << realsize << endl;
337                  cout << "insize=" << insize << endl;                                  cout << "insize=" << insize << endl;
338                  cout << "realinsize=" << realinsize << endl;*/                                  cout << "realinsize=" << realinsize << endl;*/
339                  iconv_close(icv);                  iconv_close(icv);
340    
341                  string returnstr;                  string returnstr;
# Line 260  namespace Util Line 345  namespace Util
345                          //cout << " (" << output[i] << ")" << endl;                          //cout << " (" << output[i] << ")" << endl;
346                          returnstr += output[i];                          returnstr += output[i];
347                  }                  }
348              return returnstr;                  return returnstr;
349          }          }
350    
351          std::string convertToUnicode(std::string str)          std::string convertToUnicode(std::string str)
# Line 280  namespace Util Line 365  namespace Util
365    
366          int mTimeDiff(const timeval& then, const timeval& now)          int mTimeDiff(const timeval& then, const timeval& now)
367          {          {
368                  return uTimeDiff(then,now) / 1000;                  return ( (now.tv_sec - then.tv_sec)*1000) +
369                           ((now.tv_usec-then.tv_usec)/1000);
370          }          }
371    
 }  
372    
373            timeval GetTimeOfDay()
374            {
375                    timeval now;
376                    gettimeofday(&now,0);
377                    return now;
378            }
379    
380            int my_system(const char* cmd, std::string* response)
381            {
382                    FILE* p;
383                    if ((p = popen(cmd,"r")) == NULL)
384                            return (-1);
385    
386                    if (response)
387                    {
388                            std::string output;
389                            char buf[256];
390                            while (!feof(p))
391                            {
392                                    int len = fread(buf,1,255, p);
393                                    buf[len] = 0;
394                                    output += buf;
395                            }
396                            *response = output;
397                    }
398    
399                    return (pclose(p));
400            }
401    
402            string readfile(string filename)
403            {
404                    string str;
405                    ifstream in(filename.c_str());
406                    if (in)
407                    {
408                            char buffer[4096];
409                            in.read(buffer, 4095);
410                            buffer[ in.gcount() ] = 0;
411                            str = string(buffer);
412                            in.close();
413                    }
414                    else
415                    {
416                            string message =  "Could not open ";
417                            message +=  filename;
418                            throw std::runtime_error(message);
419                    }
420                    return str;
421            }
422    
423    
424    
425    }
426    

Legend:
Removed from v.79  
changed lines
  Added in v.183

  ViewVC Help
Powered by ViewVC 1.1.20