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

Diff of /smsdaemon/Util.cpp

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

revision 42 by torben, Tue Jun 10 22:22:19 2008 UTC revision 107 by torben, Thu Jun 19 14:47:52 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 53  namespace Util Line 54  namespace Util
54                  return str;                  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 101  namespace Util Line 105  namespace Util
105                  return retval;                  return retval;
106          }          }
107    
108    
109        string str_toupper(string str)
110            {
111                    for (unsigned i=0; i<str.length(); ++i)
112                    {
113                            str.replace(i, 1 ,1, ::toupper(str.at(i)));
114                    }
115                    return str;
116            }
117    
118            string str_tolower(string str)
119            {
120                    for (unsigned i=0; i<str.length(); ++i)
121                    {
122                            str.replace(i, 1 ,1, ::tolower(str.at(i)));
123                    }
124                    return str;
125            }
126    
127    
128            string str_formatint(int i)
129            {
130                    std::ostringstream os;
131                    os << i;
132                    return os.str();
133            }
134    
135    
136          string str_characters(string str)          string str_characters(string str)
137          {          {
138                  string rep;                  string rep;
# Line 127  namespace Util Line 159  namespace Util
159          }          }
160    
161    
162          string str_gsm2latin1(string str)          string str_gsm2latin(string str)
163          {          {
164                  str = str_replace_char(str, 0x1c, 198); //AE                  str = str_replace_char(str, 0x1c, 198); //AE
165                  str = str_replace_char(str, 0x0b, 216); //OE                  str = str_replace_char(str, 0x0b, 216); //OE
# Line 139  namespace Util Line 171  namespace Util
171                  return str;                  return str;
172          }          }
173    
174            string str_latin2gsm(string str)
175            {
176                    str = str_replace_char(str, 198, 0x1c); //AE
177                    str = str_replace_char(str, 216, 0x0b); //OE
178                    str = str_replace_char(str, 197, 0x0e); //AA
179    
180                    str = str_replace_char(str, 230, 0x1d); //ae
181                    str = str_replace_char(str, 248, 0x0c); //oe
182                    str = str_replace_char(str, 229, 0x0f); //aa
183                    return str;
184            }
185    
186          string readUrl(string url, string tempfile)          string readUrl(string url, string tempfile)
187          {          {
188                  char buf[128000];                  char buf[128000];
189                  string document;                  string document;
190                                    
191                  ostringstream command;                  ostringstream command;
192                  command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\"";                  command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null  \"" << url << "\"";
193                  system( command.str().c_str() );                  int res = my_system( command.str().c_str() );
194    
195                    if (res<0)
196                    {
197                            throw( std::runtime_error("Error retrieving document"));
198                    }
199    
200                    if (res>0)
201                    {
202                            throw std::runtime_error("Command time out or document not found");
203                    }
204    
205                  ifstream in( tempfile.c_str() );                  ifstream in( tempfile.c_str() );
206    
# Line 232  namespace Util Line 286  namespace Util
286    
287          int mTimeDiff(const timeval& then, const timeval& now)          int mTimeDiff(const timeval& then, const timeval& now)
288          {          {
289                  return uTimeDiff(then,now) / 1000;                  return ( (now.tv_sec - then.tv_sec)*1000) +
290                                    ((now.tv_usec-then.tv_usec)/1000);
291            }
292    
293    
294            timeval GetTimeOfDay()
295            {
296                    timeval now;
297                    gettimeofday(&now,0);
298                    return now;
299            }
300    
301            int my_system(const char* cmd, std::string* response)
302            {
303                    FILE* p;
304                    if ((p = popen(cmd,"r")) == NULL)
305                            return (-1);
306    
307                    if (response)
308                    {
309                            std::string output;
310                            char buf[256];
311                            while(!feof(p))
312                            {
313                                    int len = fread(buf,1,255, p);
314                                    buf[len] = 0;
315                                    output += buf;
316                            }
317                            *response = output;
318                    }
319    
320                    return (pclose(p));
321          }          }
322    
323  }  }

Legend:
Removed from v.42  
changed lines
  Added in v.107

  ViewVC Help
Powered by ViewVC 1.1.20