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

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

  ViewVC Help
Powered by ViewVC 1.1.20