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

Annotation of /smsdaemon/Util.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 38 - (hide annotations) (download)
Tue Jun 10 20:25:15 2008 UTC (15 years, 11 months ago) by torben
Original Path: smsdaemon/util.cpp
File size: 3772 byte(s)
Added some extra string functions to Util::

Made Util::str_clean into Util::str_replace

1 torben 26
2     #include <string>
3     #include <sstream>
4     #include <vector>
5 torben 38 #include <cctype>
6    
7 torben 26 #include <iostream>
8     #include <fstream>
9     #include <iconv.h>
10    
11     #include <time.h>
12    
13     #include "util.h"
14    
15     using namespace std;
16    
17     namespace Util
18     {
19     void str_dump(const string& str)
20     {
21     for (unsigned i=0; i<str.length(); ++i)
22     {
23     cout.width(2);
24     cout.fill('0');
25     cout << hex << static_cast<int>(str.at(i)) << " ";
26     }
27     cout.width(1);
28     cout << dec << endl;
29     }
30    
31    
32    
33 torben 38 string str_replace(string str, string search, string replace)
34 torben 26 {
35     unsigned int pos = 0;
36    
37 torben 38 while ( (pos = str.find(search,pos)) != string::npos)
38 torben 26 {
39 torben 38 str.replace(pos, search.size(), replace);
40 torben 26 pos += replace.size();
41     }
42 torben 38 return str;
43 torben 26 }
44    
45 torben 38
46    
47     string str_trim(string str)
48     {
49     while (str.length() > 0 && isspace(str.at(0)))
50     str.erase(0,1);
51     while (str.length() > 0 && isspace(str.at(str.length()-1)))
52     str.erase(str.length()-1,1);
53     return str;
54     }
55    
56     string str_striptags(string str)
57     {
58     unsigned int pos=0;
59    
60     while ( (pos=str.find("<",pos)) != string::npos)
61     {
62     unsigned int endpos = str.find(">",pos);
63     if (endpos == string::npos)
64     break;
65     str.erase(pos, (endpos-pos)+1);
66     }
67    
68     return str;
69     }
70    
71 torben 26 vector<string> str_split(string input, string delimiter)
72     {
73     vector<string> retval;
74    
75     while ( 1 )
76     {
77     unsigned int endpos = input.find(delimiter);
78    
79     string entry = input.substr(0,endpos);
80     retval.push_back( entry );
81    
82    
83     if (endpos == string::npos)
84     break;
85    
86     endpos += delimiter.length();
87     input = input.substr(endpos, input.length() - endpos);
88     }
89    
90     return retval;
91     }
92    
93 torben 38 string str_characters(string str)
94     {
95     string rep;
96     rep.append(1,197);
97     str = str_replace(str, "&#197;", rep);
98    
99     rep.at(0) = 198;
100     str = str_replace(str, "&#198;", rep);
101    
102     rep.at(0) = 216;
103     str = str_replace(str, "&#216;", rep);
104    
105    
106     rep.at(0) = 229;
107     str = str_replace(str, "&#229;", rep);
108    
109     rep.at(0) = 230;
110     str = str_replace(str, "&#230;", rep);
111    
112     rep.at(0) = 248;
113     str = str_replace(str, "&#248;", rep);
114    
115     return str;
116     }
117    
118 torben 26 string readUrl(string url, string tempfile)
119     {
120     char buf[128000];
121     string document;
122    
123     ostringstream command;
124     command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\"";
125     system( command.str().c_str() );
126    
127     ifstream in( tempfile.c_str() );
128    
129     if (!in)
130     return "";
131    
132     while (!in.eof() )
133     {
134     in.getline(buf,128000-1);
135     document += buf;
136     document += "\n";
137     }
138     in.close();
139     unlink(tempfile.c_str());
140    
141     return document;
142     }
143    
144     string convertToUnicode(string _input)
145     {
146     char* input,*output,*input_ptr, *output_ptr;
147     input = input_ptr = (char*) malloc(_input.size()+1);
148     strcpy(input, _input.c_str());
149    
150     output = output_ptr = (char*) malloc(_input.size()*2);
151    
152    
153    
154     unsigned int realinsize,insize,outsize,realsize;
155    
156     iconv_t icv = iconv_open("UTF8", "ISO8859-1");
157     if (icv == (iconv_t)-1)
158     {
159     perror(0);
160     return "";
161     }
162    
163    
164     realsize = outsize = _input.size()*2;
165     realinsize = insize = _input.size();
166    
167     iconv(icv,
168     &input_ptr,
169     &insize,
170     &output_ptr,
171     &outsize);
172    
173    
174     perror(0);
175     /* cout << "len=" << len << endl;
176     cout << "outsize=" << outsize << endl;
177     cout << "realsize=" << realsize << endl;
178     cout << "insize=" << insize << endl;
179     cout << "realinsize=" << realinsize << endl;*/
180     iconv_close(icv);
181    
182     string returnstr;
183     for (unsigned int i=0; i<realsize - outsize; i++)
184     {
185     //cout << i << ":" << (unsigned short) output[i] << "|" << (unsigned short) input[i];
186     //cout << " (" << output[i] << ")" << endl;
187     returnstr += output[i];
188     }
189     return returnstr;
190     }
191    
192     int uTimeDiff(const timeval& then, const timeval& now)
193     {
194     return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec);
195     }
196    
197     int mTimeDiff(const timeval& then, const timeval& now)
198     {
199     return uTimeDiff(then,now) / 1000;
200     }
201    
202     }

  ViewVC Help
Powered by ViewVC 1.1.20