/[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 49 by torben, Wed Jun 11 10:21:47 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    
13  #include "util.h"  #include "util.h"
# Line 42  namespace Util Line 42  namespace Util
42                  return str;                  return str;
43          }          }
44    
45        std::string str_replace_char(std::string str, char search, char replace)
46            {
47                    unsigned int pos = 0;
48    
49                    while ( (pos = str.find(search,pos)) != string::npos)
50                    {
51                            str.replace(pos, 1, 1, replace);
52                    }
53                    return str;
54            }
55    
56                    
57    
58          string str_trim(string str)          string str_trim(string str)
# Line 90  namespace Util Line 101  namespace Util
101                  return retval;                  return retval;
102          }          }
103    
104    
105        string str_toupper(string str)
106            {
107                    for (unsigned i=0; i<str.length(); ++i)
108                    {
109                            str.replace(i, 1 ,1, ::toupper(str.at(i)));
110                    }
111                    return str;
112            }
113    
114            string str_tolower(string str)
115            {
116                    for (unsigned i=0; i<str.length(); ++i)
117                    {
118                            str.replace(i, 1 ,1, ::tolower(str.at(i)));
119                    }
120                    return str;
121            }
122    
123    
124          string str_characters(string str)          string str_characters(string str)
125          {          {
126                  string rep;                  string rep;
# Line 115  namespace Util Line 146  namespace Util
146                  return str;                  return str;
147          }          }
148    
149    
150            string str_gsm2latin(string str)
151            {
152                    str = str_replace_char(str, 0x1c, 198); //AE
153                    str = str_replace_char(str, 0x0b, 216); //OE
154                    str = str_replace_char(str, 0x0e, 197); //AA
155    
156                    str = str_replace_char(str, 0x1d, 230); //ae
157                    str = str_replace_char(str, 0x0c, 248); //oe
158                    str = str_replace_char(str, 0x0f, 229); //aa
159                    return str;
160            }
161    
162            string str_latin2gsm(string str)
163            {
164                    str = str_replace_char(str, 198, 0x1c); //AE
165                    str = str_replace_char(str, 216, 0x0b); //OE
166                    str = str_replace_char(str, 197, 0x0e); //AA
167    
168                    str = str_replace_char(str, 230, 0x1d); //ae
169                    str = str_replace_char(str, 248, 0x0c); //oe
170                    str = str_replace_char(str, 229, 0x0f); //aa
171                    return str;
172            }
173    
174          string readUrl(string url, string tempfile)          string readUrl(string url, string tempfile)
175          {          {
176                  char buf[128000];                  char buf[128000];
177                  string document;                  string document;
178                                    
179                  ostringstream command;                  ostringstream command;
180                  command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\"";                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";
181                  system( command.str().c_str() );                  int res = system( command.str().c_str() );
182                    if (res)
183                            throw std::runtime_error("Command time out");
184    
185                  ifstream in( tempfile.c_str() );                  ifstream in( tempfile.c_str() );
186    
# Line 141  namespace Util Line 199  namespace Util
199                  return document;                  return document;
200          }          }
201    
202          string convertToUnicode(string _input)          
203    
204            string iconv_wrapper(string _input, string to_format, string from_format)
205          {          {
206                  char* input,*output,*input_ptr, *output_ptr;                  char* input,*output,*input_ptr, *output_ptr;
207                  input = input_ptr = (char*) malloc(_input.size()+1);                  input = input_ptr = (char*) malloc(_input.size()+1);
# Line 153  namespace Util Line 213  namespace Util
213                                    
214                  unsigned int realinsize,insize,outsize,realsize;                  unsigned int realinsize,insize,outsize,realsize;
215                                    
216                  iconv_t icv = iconv_open("UTF8", "ISO8859-1");                  iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str());
217                  if (icv == (iconv_t)-1)                  if (icv == (iconv_t)-1)
218                  {                  {
219                          perror(0);                          perror(0);
# Line 189  namespace Util Line 249  namespace Util
249              return returnstr;              return returnstr;
250          }          }
251    
252            std::string convertToUnicode(std::string str)
253            {
254                    return iconv_wrapper(str, "UTF-8", "ISO8859-1");
255            }
256    
257            std::string convertFromUnicode(std::string str)
258            {
259                    return iconv_wrapper(str, "ISO8859-1", "UTF-8");
260            }
261    
262          int uTimeDiff(const timeval& then, const timeval& now)          int uTimeDiff(const timeval& then, const timeval& now)
263          {          {
264                  return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec);                  return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec);

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

  ViewVC Help
Powered by ViewVC 1.1.20