/[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 108 by torben, Fri Jul 4 07:58:26 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    
14  #include "util.h"  #include "util.h"
15    
# Line 42  namespace Util Line 43  namespace Util
43                  return str;                  return str;
44          }          }
45    
46                std::string str_replace_char(std::string str, char search, char replace)
47            {
48                    unsigned int pos = 0;
49    
50                    while ( (pos = str.find(search,pos)) != string::npos)
51                    {
52                            str.replace(pos, 1, 1, replace);
53                    }
54                    return str;
55            }
56    
57            bool my_isspace(char ch)
58            {
59                    return (isspace(ch) || ch == 0);
60            }
61    
62          string str_trim(string str)          string str_trim(string str)
63          {          {
64                  while (str.length() > 0 && isspace(str.at(0)))                  while (str.length() > 0 && my_isspace(str.at(0)))
65                          str.erase(0,1);                          str.erase(0,1);
66                  while (str.length() > 0 && isspace(str.at(str.length()-1)))                  while (str.length() > 0 && my_isspace(str.at(str.length()-1)))
67                          str.erase(str.length()-1,1);                          str.erase(str.length()-1,1);
68                  return str;                  return str;
69          }          }
# Line 90  namespace Util Line 105  namespace Util
105                  return retval;                  return retval;
106          }          }
107    
108            char my_toupper(char ch)
109            {
110                    if (ch == 'æ')
111                            return 'Æ';
112                    if (ch == 'ø')
113                            return 'Ø';
114                    if (ch == 'å')
115                            return 'Å';
116    
117                    return ::toupper(ch);
118            }
119    
120            char my_tolower(char ch)
121            {
122                    if (ch == 'Æ')
123                            return 'æ';
124                    if (ch == 'Ø')
125                            return 'ø';
126                    if (ch == 'Å')
127                            return 'å';
128    
129                    return ::tolower(ch);
130            }
131        string str_toupper(string str)
132            {
133                    for (unsigned i=0; i<str.length(); ++i)
134                    {
135                            str.replace(i, 1 ,1, my_toupper(str.at(i)));
136                    }
137                    return str;
138            }
139    
140            string str_tolower(string str)
141            {
142                    for (unsigned i=0; i<str.length(); ++i)
143                    {
144                            str.replace(i, 1 ,1, my_tolower(str.at(i)));
145                    }
146                    return str;
147            }
148    
149    
150            string str_formatint(int i)
151            {
152                    std::ostringstream os;
153                    os << i;
154                    return os.str();
155            }
156    
157    
158          string str_characters(string str)          string str_characters(string str)
159          {          {
160                  string rep;                  string rep;
# Line 115  namespace Util Line 180  namespace Util
180                  return str;                  return str;
181          }          }
182    
183    
184            string str_gsm2latin(string str)
185            {
186                    str = str_replace_char(str, 0x1c, 198); //AE
187                    str = str_replace_char(str, 0x0b, 216); //OE
188                    str = str_replace_char(str, 0x0e, 197); //AA
189    
190                    str = str_replace_char(str, 0x1d, 230); //ae
191                    str = str_replace_char(str, 0x0c, 248); //oe
192                    str = str_replace_char(str, 0x0f, 229); //aa
193                    return str;
194            }
195    
196            string str_latin2gsm(string str)
197            {
198                    str = str_replace_char(str, 198, 0x1c); //AE
199                    str = str_replace_char(str, 216, 0x0b); //OE
200                    str = str_replace_char(str, 197, 0x0e); //AA
201    
202                    str = str_replace_char(str, 230, 0x1d); //ae
203                    str = str_replace_char(str, 248, 0x0c); //oe
204                    str = str_replace_char(str, 229, 0x0f); //aa
205                    return str;
206            }
207    
208          string readUrl(string url, string tempfile)          string readUrl(string url, string tempfile)
209          {          {
210                  char buf[128000];                  char buf[128000];
211                  string document;                  string document;
212                                    
213                  ostringstream command;                  ostringstream command;
214                  command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\"";                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";
215                  system( command.str().c_str() );                  int res = my_system( command.str().c_str() );
216    
217                    if (res<0)
218                    {
219                            throw( std::runtime_error("Error retrieving document"));
220                    }
221    
222                    if (res>0)
223                    {
224                            throw std::runtime_error("Command time out or document not found");
225                    }
226    
227                  ifstream in( tempfile.c_str() );                  ifstream in( tempfile.c_str() );
228    
# Line 141  namespace Util Line 241  namespace Util
241                  return document;                  return document;
242          }          }
243    
244          string convertToUnicode(string _input)          
245    
246            string iconv_wrapper(string _input, string to_format, string from_format)
247          {          {
248                  char* input,*output,*input_ptr, *output_ptr;                  char* input,*output,*input_ptr, *output_ptr;
249                  input = input_ptr = (char*) malloc(_input.size()+1);                  input = input_ptr = (char*) malloc(_input.size()+1);
# Line 153  namespace Util Line 255  namespace Util
255                                    
256                  unsigned int realinsize,insize,outsize,realsize;                  unsigned int realinsize,insize,outsize,realsize;
257                                    
258                  iconv_t icv = iconv_open("UTF8", "ISO8859-1");                  iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str());
259                  if (icv == (iconv_t)-1)                  if (icv == (iconv_t)-1)
260                  {                  {
261                          perror(0);                          perror(0);
# Line 189  namespace Util Line 291  namespace Util
291              return returnstr;              return returnstr;
292          }          }
293    
294            std::string convertToUnicode(std::string str)
295            {
296                    return iconv_wrapper(str, "UTF-8", "ISO8859-1");
297            }
298    
299            std::string convertFromUnicode(std::string str)
300            {
301                    return iconv_wrapper(str, "ISO8859-1", "UTF-8");
302            }
303    
304          int uTimeDiff(const timeval& then, const timeval& now)          int uTimeDiff(const timeval& then, const timeval& now)
305          {          {
306                  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);
# Line 196  namespace Util Line 308  namespace Util
308    
309          int mTimeDiff(const timeval& then, const timeval& now)          int mTimeDiff(const timeval& then, const timeval& now)
310          {          {
311                  return uTimeDiff(then,now) / 1000;                  return ( (now.tv_sec - then.tv_sec)*1000) +
312                                    ((now.tv_usec-then.tv_usec)/1000);
313            }
314    
315    
316            timeval GetTimeOfDay()
317            {
318                    timeval now;
319                    gettimeofday(&now,0);
320                    return now;
321            }
322    
323            int my_system(const char* cmd, std::string* response)
324            {
325                    FILE* p;
326                    if ((p = popen(cmd,"r")) == NULL)
327                            return (-1);
328    
329                    if (response)
330                    {
331                            std::string output;
332                            char buf[256];
333                            while(!feof(p))
334                            {
335                                    int len = fread(buf,1,255, p);
336                                    buf[len] = 0;
337                                    output += buf;
338                            }
339                            *response = output;
340                    }
341    
342                    return (pclose(p));
343          }          }
344    
345  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20