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

Diff of /smsdaemon/Util.cpp

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

revision 132 by torben, Sun Dec 7 00:59:05 2008 UTC revision 141 by torben, Sun Dec 7 13:28:52 2008 UTC
# Line 19  using namespace std; Line 19  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 36  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 45  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 87  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 130  namespace Util Line 191  namespace Util
191    
192                  return ::tolower(ch);                  return ::tolower(ch);
193          }          }
194      string str_toupper(string str)          string str_toupper(string str)
195          {          {
196                  for (unsigned i=0; i<str.length(); ++i)                  for (unsigned i=0; i<str.length(); ++i)
197                  {                  {
# Line 185  namespace Util Line 246  namespace Util
246    
247          string str_gsm2latin(string str)          string str_gsm2latin(string str)
248          {          {
249                  str = str_replace_char(str, 0x1c, 198); //AE                  for (unsigned i=0; i<str.size(); i++) {
250                  str = str_replace_char(str, 0x0b, 216); //OE                          str.at(i) = char_def_alphabet_decode(str.at(i));
251                  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  
252                  return str;                  return str;
253          }          }
254    
255          string str_latin2gsm(string str)          string str_latin2gsm(string str)
256          {          {
257                  str = str_replace_char(str, 198, 0x1c); //AE                  for (unsigned i=0; i<str.size(); i++) {
258                  str = str_replace_char(str, 216, 0x0b); //OE                          str.at(i) = char_def_alphabet_encode(str.at(i));
259                  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  
260                  return str;                  return str;
261          }          }
262    
# Line 211  namespace Util Line 264  namespace Util
264          {          {
265                  char buf[128000];                  char buf[128000];
266                  string document;                  string document;
267                    
268                  ostringstream command;                  ostringstream command;
269                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";
270                  int res = my_system( command.str().c_str() );                  int res = my_system( command.str().c_str() );
# Line 221  namespace Util Line 274  namespace Util
274                          throw( std::runtime_error("Error retrieving document"));                          throw( std::runtime_error("Error retrieving document"));
275                  }                  }
276    
277                  if (res>0)                  if (res>0)
278                  {                  {
279                          throw std::runtime_error("Command time out or document not found");                          throw std::runtime_error("Command time out or document not found");
280                  }                  }
# Line 239  namespace Util Line 292  namespace Util
292                  }                  }
293                  in.close();                  in.close();
294                  unlink(tempfile.c_str());                  unlink(tempfile.c_str());
295                    
296                  return document;                  return document;
297          }          }
298    
299            
300    
301          string iconv_wrapper(string _input, string to_format, string from_format)          string iconv_wrapper(string _input, string to_format, string from_format)
302          {          {
303                  char* input,*output,*input_ptr, *output_ptr;                  char* input,*output,*input_ptr, *output_ptr;
304                  input = input_ptr = (char*) malloc(_input.size()+1);                  input = input_ptr = (char*) malloc(_input.size()+1);
305                  strcpy(input, _input.c_str());                  strcpy(input, _input.c_str());
306                    
307                  output = output_ptr = (char*) malloc(_input.size()*2);                  output = output_ptr = (char*) malloc(_input.size()*2);
308    
309    
310                    
311                  unsigned int realinsize,insize,outsize,realsize;                  unsigned int realinsize,insize,outsize,realsize;
312                    
313                  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());
314                  if (icv == (iconv_t)-1)                  if (icv == (iconv_t)-1)
315                  {                  {
# Line 264  namespace Util Line 317  namespace Util
317                          return "";                          return "";
318                  }                  }
319    
320                    
321                  realsize = outsize = _input.size()*2;                  realsize = outsize = _input.size()*2;
322                  realinsize = insize = _input.size();                  realinsize = insize = _input.size();
323                    
324                  iconv(icv,                  iconv(icv,
325                                  &input_ptr,                        &input_ptr,
326                                  &insize,                        &insize,
327                                  &output_ptr,                        &output_ptr,
328                                  &outsize);                        &outsize);
329    
330    
331                  perror(0);                  perror(0);
332  /*              cout << "len=" << len << endl;                  /*              cout << "len=" << len << endl;
333                  cout << "outsize=" <<  outsize << endl;                                  cout << "outsize=" <<  outsize << endl;
334                  cout << "realsize=" << realsize << endl;                                  cout << "realsize=" << realsize << endl;
335                  cout << "insize=" << insize << endl;                                  cout << "insize=" << insize << endl;
336                  cout << "realinsize=" << realinsize << endl;*/                                  cout << "realinsize=" << realinsize << endl;*/
337                  iconv_close(icv);                  iconv_close(icv);
338    
339                  string returnstr;                  string returnstr;
# Line 290  namespace Util Line 343  namespace Util
343                          //cout << " (" << output[i] << ")" << endl;                          //cout << " (" << output[i] << ")" << endl;
344                          returnstr += output[i];                          returnstr += output[i];
345                  }                  }
346              return returnstr;                  return returnstr;
347          }          }
348    
349          std::string convertToUnicode(std::string str)          std::string convertToUnicode(std::string str)
# Line 311  namespace Util Line 364  namespace Util
364          int mTimeDiff(const timeval& then, const timeval& now)          int mTimeDiff(const timeval& then, const timeval& now)
365          {          {
366                  return ( (now.tv_sec - then.tv_sec)*1000) +                  return ( (now.tv_sec - then.tv_sec)*1000) +
367                                  ((now.tv_usec-then.tv_usec)/1000);                         ((now.tv_usec-then.tv_usec)/1000);
368          }          }
369    
370    
# Line 332  namespace Util Line 385  namespace Util
385                  {                  {
386                          std::string output;                          std::string output;
387                          char buf[256];                          char buf[256];
388                          while(!feof(p))                          while (!feof(p))
389                          {                          {
390                                  int len = fread(buf,1,255, p);                                  int len = fread(buf,1,255, p);
391                                  buf[len] = 0;                                  buf[len] = 0;
# Line 347  namespace Util Line 400  namespace Util
400          string readfile(string filename)          string readfile(string filename)
401          {          {
402                  string str;                  string str;
403              ifstream in(filename.c_str());                  ifstream in(filename.c_str());
404          if (in)                  if (in)
405              {                  {
406              char buffer[2048];                          char buffer[2048];
407                  in.read(buffer, 2047);                          in.read(buffer, 2047);
408                          buffer[ in.gcount() ] = 0;                          buffer[ in.gcount() ] = 0;
409                  str = string(buffer);                          str = string(buffer);
410              in.close();                          in.close();
411              }                  }
412          else                  else
413              {                  {
414              string message =  "Could no open ";                          string message =  "Could no open ";
415                  message +=  filename;                          message +=  filename;
416                  throw std::runtime_error(message);                          throw std::runtime_error(message);
417          }                  }
418              return str;                  return str;
419          }          }
420    
421    
422    
423  }  }
424    
   

Legend:
Removed from v.132  
changed lines
  Added in v.141

  ViewVC Help
Powered by ViewVC 1.1.20