diff options
Diffstat (limited to 'src/lib/convertlib.cpp')
-rwxr-xr-x | src/lib/convertlib.cpp | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/src/lib/convertlib.cpp b/src/lib/convertlib.cpp new file mode 100755 index 0000000..ebbfaff --- /dev/null +++ b/src/lib/convertlib.cpp | |||
@@ -0,0 +1,311 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include "convertlib.h" | ||
3 | |||
4 | bool Convert::isnibble(string x) | ||
5 | { | ||
6 | if ((x >= "0") && (x <= "9")) return true; | ||
7 | if ((x >= "a") && (x <= "f")) return true; | ||
8 | if ((x >= "A") && (x <= "F")) return true; | ||
9 | return false; | ||
10 | } | ||
11 | |||
12 | string* Convert::byte2hex(unsigned char x) | ||
13 | { | ||
14 | int b = x % 16; | ||
15 | int a = x >> 4; | ||
16 | |||
17 | string hex = "0123456789ABCDEF"; | ||
18 | string * newst = new string(""); | ||
19 | *newst = *newst + hex.substr(a, 1); | ||
20 | *newst = *newst + hex.substr(b, 1); | ||
21 | return newst; | ||
22 | } | ||
23 | |||
24 | long Convert::str2long(string decstr) | ||
25 | { | ||
26 | return strtol(decstr.c_str(), 0, 10); | ||
27 | } | ||
28 | |||
29 | double Convert::str2dbl(string decstr) | ||
30 | { | ||
31 | return strtod(decstr.c_str(), 0); | ||
32 | } | ||
33 | |||
34 | string* Convert::int2str(int x, unsigned int pad, string padchar) | ||
35 | { | ||
36 | string* newst = int2str(x); | ||
37 | |||
38 | if (pad > 1) | ||
39 | { | ||
40 | while (newst->length() < pad) | ||
41 | { | ||
42 | *newst = padchar + *newst; | ||
43 | } | ||
44 | } | ||
45 | |||
46 | return newst; | ||
47 | } | ||
48 | |||
49 | string* Convert::int64tostr(__sint64 x) | ||
50 | { | ||
51 | bool isneg=false; | ||
52 | string * newst = new string(""); | ||
53 | if (x == 0) | ||
54 | { | ||
55 | *newst = "0"; | ||
56 | } | ||
57 | else | ||
58 | { | ||
59 | if (x<0) | ||
60 | { | ||
61 | isneg=true; | ||
62 | x=0-x; | ||
63 | } | ||
64 | while (x != 0) | ||
65 | { | ||
66 | long long digit = x % (long long) 10; | ||
67 | string dec = "0123456789"; | ||
68 | *newst = dec.substr(digit, 1) + *newst; | ||
69 | |||
70 | x -= (long long) (x % (long long) 10); | ||
71 | x /= (long long) 10; | ||
72 | } | ||
73 | } | ||
74 | if (isneg) | ||
75 | { | ||
76 | *newst="-"+*newst; | ||
77 | } | ||
78 | return newst; | ||
79 | } | ||
80 | |||
81 | string* Convert::int2str(int x) | ||
82 | { | ||
83 | long long y = x; | ||
84 | return int64tostr(y); | ||
85 | } | ||
86 | |||
87 | string* Convert::int32tostr(__uint32 x) | ||
88 | { | ||
89 | long long y = x; | ||
90 | return int64tostr(y); | ||
91 | } | ||
92 | |||
93 | long Convert::hex2long(string hexstr) | ||
94 | { | ||
95 | return strtol(hexstr.c_str(), 0, 16); | ||
96 | } | ||
97 | |||
98 | unsigned char Convert::hex2byte(string hexstr) | ||
99 | { | ||
100 | long x = hex2long(hexstr); | ||
101 | return (unsigned char) x % 256; | ||
102 | } | ||
103 | |||
104 | unsigned char Convert::safebyte(unsigned char x) | ||
105 | { | ||
106 | if (x < 32) return '.'; | ||
107 | return x; | ||
108 | } | ||
109 | |||
110 | string* Convert::int64tohex(__sint64 x) | ||
111 | { | ||
112 | __sint64 q = x; | ||
113 | unsigned char a = q % 256; q = q >> 8; | ||
114 | unsigned char b = q % 256; q = q >> 8; | ||
115 | unsigned char c = q % 256; q = q >> 8; | ||
116 | unsigned char d = q % 256; q = q >> 8; | ||
117 | unsigned char e = q % 256; q = q >> 8; | ||
118 | unsigned char f = q % 256; q = q >> 8; | ||
119 | unsigned char g = q % 256; q = q >> 8; | ||
120 | unsigned char h = q % 256; | ||
121 | string* b2h8 = byte2hex(h); | ||
122 | string* b2h7 = byte2hex(g); | ||
123 | string* b2h6 = byte2hex(f); | ||
124 | string* b2h5 = byte2hex(e); | ||
125 | string* b2h4 = byte2hex(d); | ||
126 | string* b2h3 = byte2hex(c); | ||
127 | string* b2h2 = byte2hex(b); | ||
128 | string* b2h1 = byte2hex(a); | ||
129 | string* newst = new string(); | ||
130 | *newst += *b2h8 + *b2h7 + ":" + *b2h6 + *b2h5 + "." + *b2h4 + *b2h3 + ":" + *b2h2 + *b2h1; | ||
131 | delete (b2h1); | ||
132 | delete (b2h2); | ||
133 | delete (b2h3); | ||
134 | delete (b2h4); | ||
135 | delete (b2h5); | ||
136 | delete (b2h6); | ||
137 | delete (b2h7); | ||
138 | delete (b2h8); | ||
139 | return newst; | ||
140 | } | ||
141 | |||
142 | string* Convert::int32tohex(unsigned long x) | ||
143 | { | ||
144 | __sint64 q = x; | ||
145 | unsigned char a = q % 256; q = q >> 8; | ||
146 | unsigned char b = q % 256; q = q >> 8; | ||
147 | unsigned char c = q % 256; q = q >> 8; | ||
148 | unsigned char d = q % 256; q = q >> 8; | ||
149 | string* b2h4 = byte2hex(d); | ||
150 | string* b2h3 = byte2hex(c); | ||
151 | string* b2h2 = byte2hex(b); | ||
152 | string* b2h1 = byte2hex(a); | ||
153 | string* newst = new string(); | ||
154 | *newst += *b2h4 + *b2h3 + ":" + *b2h2 + *b2h1; | ||
155 | delete (b2h1); | ||
156 | delete (b2h2); | ||
157 | delete (b2h3); | ||
158 | delete (b2h4); | ||
159 | return newst; | ||
160 | } | ||
161 | |||
162 | unsigned int Convert::getint24(unsigned char * buf, int loc) | ||
163 | { | ||
164 | unsigned int q = 0; | ||
165 | |||
166 | q = buf[loc + 2] | ||
167 | + (buf[loc + 1] << 8) | ||
168 | + (buf [loc + 0] << 16); | ||
169 | return q; | ||
170 | } | ||
171 | |||
172 | |||
173 | unsigned int Convert::getint32(unsigned char * buf, int loc) | ||
174 | { | ||
175 | unsigned int q = 0; | ||
176 | |||
177 | q = buf[loc + 3] | ||
178 | + (buf[loc + 2] << 8) | ||
179 | + (buf [loc + 1] << 16) | ||
180 | + (buf [loc] << 24); | ||
181 | return q; | ||
182 | } | ||
183 | |||
184 | void Convert::setint32(unsigned char * buf, int loc, __uint32 newval) | ||
185 | { | ||
186 | buf[loc + 0] = (newval >> 24) % 256; | ||
187 | buf[loc + 1] = (newval >> 16) % 256; | ||
188 | buf[loc + 2] = (newval >> 8) % 256; | ||
189 | buf[loc + 3] = newval % 256; | ||
190 | } | ||
191 | |||
192 | void Convert::setfloat80(unsigned char * buf, int loc, __uint32 newval) | ||
193 | { | ||
194 | /* Thanks, Erik */ | ||
195 | unsigned int mask = 0x40000000 ; | ||
196 | int count ; | ||
197 | |||
198 | for (int i=0;i<10;i++) | ||
199 | { | ||
200 | buf[loc+i]=0; | ||
201 | } | ||
202 | if (newval <= 1) | ||
203 | { buf[loc+0] = 0x3F ; | ||
204 | buf[loc+1] = 0xFF ; | ||
205 | buf[loc+2] = 0x80 ; | ||
206 | return ; | ||
207 | } ; | ||
208 | |||
209 | buf[loc+0] = 0x40 ; | ||
210 | |||
211 | if (newval >= mask) | ||
212 | { buf[loc+1] = 0x1D ; | ||
213 | return ; | ||
214 | } ; | ||
215 | |||
216 | for (count = 0 ; count <= 32 ; count ++) | ||
217 | { if (newval & mask) | ||
218 | break ; | ||
219 | mask >>= 1 ; | ||
220 | } ; | ||
221 | |||
222 | newval <<= count + 1 ; | ||
223 | buf[loc+1] = 29 - count ; | ||
224 | buf[loc+2] = (newval >> 24) & 0xFF ; | ||
225 | buf[loc+3] = (newval >> 16) & 0xFF ; | ||
226 | buf[loc+4] = (newval >> 8) & 0xFF ; | ||
227 | buf[loc+5] = newval & 0xFF ; | ||
228 | return; | ||
229 | } | ||
230 | |||
231 | string* Convert::readstring(unsigned char * orig, int offset, int len) | ||
232 | { | ||
233 | string * newst = new string(""); | ||
234 | int i; | ||
235 | |||
236 | for (i = offset; i < offset + len; i++) | ||
237 | { | ||
238 | if (i >= offset + len) | ||
239 | return newst; | ||
240 | |||
241 | if (orig[i] == 0) break; | ||
242 | *newst += orig[i]; | ||
243 | } | ||
244 | |||
245 | return newst; | ||
246 | } | ||
247 | |||
248 | string* Convert::padright(string & strinput, int inlen, string strpad) | ||
249 | { | ||
250 | int currlen = strinput.length(); | ||
251 | |||
252 | if (currlen > inlen) | ||
253 | { | ||
254 | strinput=strinput.substr(0,inlen); | ||
255 | return new string(strinput); | ||
256 | } | ||
257 | |||
258 | int pad = inlen - currlen; | ||
259 | if (pad > 0) | ||
260 | { | ||
261 | for (int i = 0; i < pad; i++) | ||
262 | { | ||
263 | strinput += strpad; | ||
264 | } | ||
265 | } | ||
266 | |||
267 | return new string(strinput); | ||
268 | } | ||
269 | |||
270 | string* Convert::padleft(string & strinput, int inlen, string strpad) | ||
271 | { | ||
272 | int currlen = strinput.length(); | ||
273 | |||
274 | if (currlen > inlen) | ||
275 | { | ||
276 | return new string(strinput.substr(0, inlen)); | ||
277 | } | ||
278 | |||
279 | int pad = inlen - currlen; | ||
280 | if (pad > 0) | ||
281 | { | ||
282 | string strtmp = ""; | ||
283 | for (int i = 0; i < pad; i++) | ||
284 | { | ||
285 | strtmp += strpad; | ||
286 | } | ||
287 | strinput = strtmp + strinput; | ||
288 | } | ||
289 | return new string(strinput); | ||
290 | } | ||
291 | |||
292 | string* Convert::trim(string* strinput) | ||
293 | { | ||
294 | if (strinput->length() == 0) | ||
295 | { | ||
296 | string* x = new string(""); | ||
297 | return x; | ||
298 | } | ||
299 | |||
300 | string* strresult = new string(*strinput); | ||
301 | while (strresult->substr(0, 1) == " ") | ||
302 | { | ||
303 | *strresult = strresult->substr(1, strresult->length() - 1); | ||
304 | } | ||
305 | |||
306 | while (strresult->substr(strresult->length() - 1, 1) == " ") | ||
307 | { | ||
308 | *strresult = strresult->substr(0, strresult->length() - 1); | ||
309 | } | ||
310 | return strresult; | ||
311 | } | ||