Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 69.17 KB | None | 0 0
  1. #pragma once
  2. #ifndef SPARKLYCHAR_H
  3. #define SPARKLYCHAR_H
  4. #define STR_SEARCH_LOOP_SIZE 16384
  5. #define isnum(a) ( a >= 48 && a <= 57 )
  6. #define tonum(a) a -= 48;
  7. #define islow(a) ( a >= 97 && a <= 122 )
  8. #define isup(a) ( a >= 65 && a <= 90 )
  9. #define tolow(a) a = 97 + ( a - 65 )
  10. #define toup(a) a = 65 + ( a - 97 )
  11. #include "sparklycore.h"
  12.  
  13. #if defined SPwindow
  14. #include <tchar.h>
  15. #endif
  16.  
  17. #include "lz4\lz4.h"
  18. #include "lzma.h"
  19.  
  20. namespace str
  21. {
  22. inline unsigned char *decompress(unsigned char *in, unsigned int *outsize)
  23. {
  24. unsigned char *c;
  25. int osize;
  26. int osize3;
  27. int omax;
  28. bool du = 0;
  29. *outsize = 0;
  30. unsigned int osize2;
  31. switch( in[0] )
  32. {
  33. case 32:
  34. {
  35. osize = (in[4] << 24) | (in[3] << 16) | (in[2] << 8) | (in[1]);
  36. omax = (in[8] << 24) | (in[7] << 16) | (in[6] << 8) | (in[5]);
  37. c = new unsigned char[omax];
  38. osize3 = LZ4_decompress_safe((char *)&in[9], (char *)c, osize, omax);
  39. *outsize =osize3;
  40. return c;
  41. break;
  42. }
  43. case 64:
  44. {
  45. in[0] = 0;
  46. du = fp_uncompress( in, &c, &osize2 );
  47. if( !du ) return 0;
  48. *outsize = osize2;
  49. return c;
  50. break;
  51. }
  52. }
  53. return 0;
  54. }
  55.  
  56. inline unsigned char *compress( int type, unsigned char *in, unsigned int insize, unsigned int *outsize)
  57. {
  58. int osize = 0, a;
  59. unsigned int osize2 = 0;
  60. unsigned char *c;
  61. char *b;
  62. *outsize = 0;
  63. switch(type)
  64. {
  65. case 0:
  66. {
  67. c = new unsigned char[insize+9];
  68. osize = LZ4_compress((char *)in, (char *)&c[9], insize);
  69. b = (char *)&osize;
  70. c[0] = 32;
  71. for( a = 0; a < 4; a++) c[a+1] = b[a];
  72. b = (char *)&insize;
  73. for( a = 0; a < 4; a++) c[a+5] = b[a];
  74. *outsize = osize+9;
  75. return c;
  76. break;
  77. }
  78. case 1:
  79. {
  80. c = fp_compress( in, insize, &osize2, 18 );
  81. c[0] = 64;
  82. *outsize = osize2;
  83. return c;
  84. break;
  85. }
  86. }
  87. return 0;
  88. }
  89.  
  90. inline int size( char * in )
  91. {
  92. int a;
  93. for( a = 0; a < STR_SEARCH_LOOP_SIZE; a++) { if( !in[a] ) return a; }
  94. return 0;
  95. }
  96.  
  97. inline int ref( char *in )
  98. {
  99. int ref = 0;
  100. for( int a = 0; a < STR_SEARCH_LOOP_SIZE; a++ ) { if( !in[a] ) break; ref += in[a]; }
  101. }
  102.  
  103. inline int ref( char *in, int isize )
  104. {
  105. int ref = 0;
  106. for( int a = 0; a < isize; a++ ) ref += in[a];
  107. }
  108.  
  109. inline int add( char *in, int ilen, char *what )
  110. {
  111. int s = size(in);
  112. int s2 = size(what);
  113. int addsize = 0;
  114. if( s >= ilen ) return s;
  115. if( s + s2 >= ilen ) addsize = ilen - 1;
  116. else addsize = s + s2;
  117. for( int a = s; a < addsize; a++ ) in[a] = what[a - s];
  118. in[addsize] = 0;
  119. return addsize;;
  120. }
  121.  
  122. inline int add( char *in, int isize, int ilen, char *what )
  123. {
  124. int s2 = size(what);
  125. int addsize = 0;
  126. if( isize >= ilen ) return isize;
  127. if( isize + s2 >= ilen ) addsize = ilen - 1;
  128. else addsize = isize + s2;
  129. for( int a = isize; a < addsize; a++ ) in[a] = what[a - isize];
  130. in[addsize] = 0;
  131. return addsize;
  132. }
  133.  
  134. inline int add( char *in, int ilen, char *what, int iwhat )
  135. {
  136. int s = size(in);
  137. int addsize = 0;
  138. if( s >= ilen ) return s;
  139. if( s + iwhat >= ilen ) addsize = ilen - 1;
  140. else addsize = s + iwhat;
  141. for( int a = s; a < addsize; a++ ) in[a] = what[a - s];
  142. in[addsize] = 0;
  143. return addsize;
  144. }
  145.  
  146. inline int add( char *in, int isize, int ilen, char *what, int iwhat )
  147. {
  148. int addsize = 0;
  149. if( isize >= ilen ) return isize;
  150. if( isize + iwhat >= ilen ) addsize = ilen - 1;
  151. else addsize = isize + iwhat;
  152. for( int a = isize; a < addsize; a++ ) in[a] = what[a - isize];
  153. in[addsize] = 0;
  154. return addsize;
  155. }
  156.  
  157. inline int add( int pos, char *in, int ilen, char *what )
  158. {
  159. int s = size(in), a, s2 = size(what);
  160. if( s >= ilen ) return s;
  161. if( s + s2 >= ilen ) s += ilen - (s + s2) - 1;
  162. for( a = s; a >= pos; a-- ) in[a+s2] = in[a];
  163. for( a = pos; a < pos + s2; a++ ) in[a] = what[a - pos];
  164. in[s+s2] = 0;
  165. return s+s2;
  166. }
  167.  
  168. inline int add( int pos, char *in, int ilen, char *what, int iwhat )
  169. {
  170. int s = size(in), a;
  171. if( s >= ilen ) return s;
  172. if( s + iwhat >= ilen ) s += ilen - (s + iwhat) - 1;
  173. for( a = s; a >= pos; a-- ) in[a+iwhat] = in[a];
  174. for( a = pos; a < pos + iwhat; a++ ) in[a] = what[a - pos];
  175. in[s+iwhat] = 0;
  176. return s+iwhat;
  177. }
  178.  
  179. inline int set( char *line, int ilen, char *in )
  180. {
  181. int s = size(in), a;
  182. for( a = 0; a < s; a++ ) line[a] = in[a];
  183. line[s] = 0;
  184. }
  185.  
  186. inline int set( char *line, int ilen, char *in, int iin )
  187. {
  188. int a;
  189. for( a = 0; a < iin; a++ ) line[a] = in[a];
  190. line[iin] = 0;
  191. }
  192.  
  193. inline int remove( char *line, int start, int end )
  194. {
  195. int s = size(line);
  196. if( end == -1 ) end = s - 1;
  197. if( end > s ) end = s;
  198. if( start > end ) start = end - 1;
  199. int r = (end - start) + 1;
  200. for( int a = start; a <= s - r; a++ ) line[a] = line[a+r];
  201. s -= r;
  202. return s;
  203. }
  204.  
  205. inline int remove( char *line, int iline, int start, int end )
  206. {
  207. if( end == -1 ) end = iline - 1;
  208. if( end > iline ) end = iline;
  209. if( start > end ) start = end - 1;
  210. int r = (end - start) + 1;
  211. for( int a = start; a <= iline - r; a++ ) line[a] = line[a+r];
  212. iline -= r;
  213. return iline;
  214. }
  215.  
  216. inline int find( char *line, char *what )
  217. {
  218. int a, s = size(what), c = 0, s2 = size(line);
  219. for( a = 0; a < s2; a++ )
  220. {
  221. if( c >= s ) return a - s;
  222. if( line[a] == what[c] )
  223. {
  224. c++;
  225. continue;
  226. }
  227. if( c ) a--;
  228. c = 0;
  229. }
  230. if( c == s ) return a - s;
  231. return -1;
  232. }
  233.  
  234. inline int find( char *line, char *what, int iwhat )
  235. {
  236. int a, c = 0, s2 = size(line);
  237. for( a = 0; a < s2; a++ )
  238. {
  239. if( c >= iwhat ) return a - iwhat;
  240. if( line[a] == what[c] )
  241. {
  242. c++;
  243. continue;
  244. }
  245. if( c ) a--;
  246. c = 0;
  247. }
  248. if( c == iwhat ) return a - iwhat;
  249. return -1;
  250. }
  251.  
  252. inline int find( char *line, int iline, char *what, int iwhat )
  253. {
  254. int a, c = 0;
  255. for( a = 0; a < iline; a++ )
  256. {
  257. if( c >= iwhat ) return a - iwhat;
  258. if( line[a] == what[c] )
  259. {
  260. c++;
  261. continue;
  262. }
  263. if( c ) a--;
  264. c = 0;
  265. }
  266. if( c == iwhat ) return a - iwhat;
  267. return -1;
  268. }
  269.  
  270. inline int find( int pos, char *line, char *what )
  271. {
  272. int a, s = size(what), c = 0, s2 = size(line);
  273. if( pos >= s2 ) return -1;
  274. for( a = pos; a < s2; a++ )
  275. {
  276. if( c >= s ) return a - s;
  277. if( line[a] == what[c] )
  278. {
  279. c++;
  280. continue;
  281. }
  282. if( c ) a--;
  283. c = 0;
  284. }
  285. if( c == s ) return a - s;
  286. return -1;
  287. }
  288.  
  289. inline int find( int pos, char *line, char *what, int iwhat )
  290. {
  291. int a, c = 0, s2 = size(line);
  292. if( pos >= s2 ) return -1;
  293. for( a = pos; a < s2; a++ )
  294. {
  295. if( c >= iwhat ) return a - iwhat;
  296. if( line[a] == what[c] )
  297. {
  298. c++;
  299. continue;
  300. }
  301. if( c ) a--;
  302. c = 0;
  303. }
  304. if( c == iwhat ) return a - iwhat;
  305. return -1;
  306. }
  307.  
  308. inline int find( int pos, char *line, int iline, char *what, int iwhat )
  309. {
  310. int a, c = 0;
  311. if( pos >= iline ) return -1;
  312. for( a = pos; a < iline; a++ )
  313. {
  314. if( c >= iwhat ) return a - iwhat;
  315. if( line[a] == what[c] )
  316. {
  317. c++;
  318. continue;
  319. }
  320. if( c ) a--;
  321. c = 0;
  322. }
  323. if( c == iwhat ) return a - iwhat;
  324. return -1;
  325. }
  326.  
  327. inline bool replace( char *line, int ilen, char *what, char *with )
  328. {
  329. int s1 = size(what);
  330. int s0 = size(line);
  331. int i = find( line, s0, what, s1 );
  332. if( i == -1 ) return 0;
  333. int s2 = str::size(with);
  334. remove(line, s0, i, i + s1 - 1);
  335. add( i, line, ilen, with, s2 );
  336. return 1;
  337. }
  338.  
  339. inline void trim( char *line, char what )
  340. {
  341. int r1e = 0, a, s = size(line);
  342. for( a = 0; a < s; a++ )
  343. {
  344. if( line[a] != what ) break;
  345. r1e++;
  346. }
  347.  
  348. if( r1e > 0 ) s = remove( line, s, 0, r1e - 1 );
  349. if( !s ) return;
  350. int r2s = s;
  351. for( a = s - 1; a >= 0; a-- )
  352. {
  353. if( line[a] != what ) break;
  354. r2s--;
  355. }
  356. if( r2s == s ) return;
  357. remove( line, s, r2s, s - 1);
  358. }
  359.  
  360. inline void trim( char *line, int iline, char what )
  361. {
  362. int r1e = 0, a;
  363. for( a = 0; a < iline; a++ )
  364. {
  365. if( line[a] != what ) break;
  366. r1e++;
  367. }
  368.  
  369. if( r1e > 0 ) iline = remove( line, iline, 0, r1e - 1 );
  370. if( !iline ) return;
  371. int r2s = iline;
  372. for( a = iline - 1; a >= 0; a-- )
  373. {
  374. if( line[a] != what ) break;
  375. r2s--;
  376. }
  377. if( r2s == iline ) return;
  378. remove( line, iline, r2s, iline - 1);
  379. }
  380.  
  381. inline bool same( char *line, char *line2 )
  382. {
  383. int s1 = size(line), s2 = size(line2);
  384. if( s1 != s2 ) return 0;
  385. for( int a = 0; a < s1; a++ ) if( line[a] != line2[a] ) return 0;
  386. return 1;
  387. }
  388.  
  389. inline bool same( char *line, char *line2, int iline2 )
  390. {
  391. int s1 = size(line);
  392. if( s1 != iline2 ) return 0;
  393. for( int a = 0; a < s1; a++ ) if( line[a] != line2[a] ) return 0;
  394. return 1;
  395. }
  396.  
  397. inline bool same( char *line, int iline, char *line2, int iline2 )
  398. {
  399. if( iline != iline2 ) return 0;
  400. for( int a = 0; a < iline; a++ ) if( line[a] != line2[a] ) return 0;
  401. return 1;
  402. }
  403.  
  404. inline bool same2( char *line, char *line2 )
  405. {
  406. char low[2];
  407. int s1 = size(line), s2 = size(line2);
  408. if( s1 != s2 ) return 0;
  409. for( int a = 0; a < s1; a++ )
  410. {
  411. low[0] = line[a];
  412. low[1] = line2[a];
  413. if( isup(low[0]) ) tolow(low[0]);
  414. if( isup(low[1]) ) tolow(low[1]);
  415. if( low[0] != low[1] ) return 0;
  416. }
  417. return 1;
  418. }
  419.  
  420. inline bool same2( char *line, char *line2, int iline2 )
  421. {
  422. char low[2];
  423. int s1 = size(line);
  424. if( s1 != iline2 ) return 0;
  425. for( int a = 0; a < s1; a++ )
  426. {
  427. low[0] = line[a];
  428. low[1] = line2[a];
  429. if( isup(low[0]) ) tolow(low[0]);
  430. if( isup(low[1]) ) tolow(low[1]);
  431. if( low[0] != low[1] ) return 0;
  432. }
  433. return 1;
  434. }
  435.  
  436. inline bool same2( char *line, int iline, char *line2, int iline2 )
  437. {
  438. char low[2];
  439. if( iline != iline2 ) return 0;
  440. for( int a = 0; a < iline; a++ )
  441. {
  442. low[0] = line[a];
  443. low[1] = line2[a];
  444. if( isup(low[0]) ) tolow(low[0]);
  445. if( isup(low[1]) ) tolow(low[1]);
  446. if( low[0] != low[1] ) return 0;
  447. }
  448. return 1;
  449. }
  450.  
  451. inline int find2( char *line, char *what )
  452. {
  453. int a, s = size(what), c = 0, s2 = size(line);
  454. char low[2];
  455. for( a = 0; a < s2; a++ )
  456. {
  457. if( c >= s ) return a - s;
  458. low[0] = line[a];
  459. low[1] = what[c];
  460. if( isup(low[0]) ) tolow(low[0]);
  461. if( isup(low[1]) ) tolow(low[1]);
  462. if( low[0] == low[1] )
  463. {
  464. c++;
  465. continue;
  466. }
  467. if( c ) a--;
  468. c = 0;
  469. }
  470. if( c == s ) return a - s;
  471. return -1;
  472. }
  473.  
  474. inline int find2( char *line, char *what, int iwhat )
  475. {
  476. int a, c = 0, s2 = size(line);
  477. char low[2];
  478. for( a = 0; a < s2; a++ )
  479. {
  480. if( c >= iwhat ) return a - iwhat;
  481. low[0] = line[a];
  482. low[1] = what[c];
  483. if( isup(low[0]) ) tolow(low[0]);
  484. if( isup(low[1]) ) tolow(low[1]);
  485. if( low[0] == low[1] )
  486. {
  487. c++;
  488. continue;
  489. }
  490. if( c ) a--;
  491. c = 0;
  492. }
  493. if( c == iwhat ) return a - iwhat;
  494. return -1;
  495. }
  496.  
  497. inline int find2( char *line, int iline, char *what, int iwhat )
  498. {
  499. int a, c = 0;
  500. char low[2];
  501. for( a = 0; a < iline; a++ )
  502. {
  503. if( c >= iwhat ) return a - iwhat;
  504. low[0] = line[a];
  505. low[1] = what[c];
  506. if( isup(low[0]) ) tolow(low[0]);
  507. if( isup(low[1]) ) tolow(low[1]);
  508. if( low[0] == low[1] )
  509. {
  510. c++;
  511. continue;
  512. }
  513. if( c ) a--;
  514. c = 0;
  515. }
  516. if( c == iwhat ) return a - iwhat;
  517. return -1;
  518. }
  519.  
  520. inline int find2( int pos, char *line, char *what )
  521. {
  522. int a, s = size(what), c = 0, s2 = size(line);
  523. if( pos >= s2 ) return -1;
  524. char low[2];
  525. for( a = pos; a < s2; a++ )
  526. {
  527. if( c >= s ) return a - s;
  528. low[0] = line[a];
  529. low[1] = what[c];
  530. if( isup(low[0]) ) tolow(low[0]);
  531. if( isup(low[1]) ) tolow(low[1]);
  532. if( low[0] == low[1] )
  533. {
  534. c++;
  535. continue;
  536. }
  537. if( c ) a--;
  538. c = 0;
  539. }
  540. if( c == s ) return a - s;
  541. return -1;
  542. }
  543.  
  544. inline int find2( int pos, char *line, char *what, int iwhat )
  545. {
  546. int a, c = 0, s2 = size(line);
  547. if( pos >= s2 ) return -1;
  548. char low[2];
  549. for( a = pos; a < s2; a++ )
  550. {
  551. if( c >= iwhat ) return a - iwhat;
  552. low[0] = line[a];
  553. low[1] = what[c];
  554. if( isup(low[0]) ) tolow(low[0]);
  555. if( isup(low[1]) ) tolow(low[1]);
  556. if( low[0] == low[1] )
  557. {
  558. c++;
  559. continue;
  560. }
  561. if( c ) a--;
  562. c = 0;
  563. }
  564. if( c == iwhat ) return a - iwhat;
  565. return -1;
  566. }
  567.  
  568. inline int find2( int pos, char *line, int iline, char *what, int iwhat )
  569. {
  570. int a, c = 0;
  571. if( pos >= iline ) return -1;
  572. char low[2];
  573. for( a = pos; a < iline; a++ )
  574. {
  575. if( c >= iwhat ) return a - iwhat;
  576. low[0] = line[a];
  577. low[1] = what[c];
  578. if( isup(low[0]) ) tolow(low[0]);
  579. if( isup(low[1]) ) tolow(low[1]);
  580. if( low[0] == low[1] )
  581. {
  582. c++;
  583. continue;
  584. }
  585. if( c ) a--;
  586. c = 0;
  587. }
  588. if( c == iwhat ) return a - iwhat;
  589. return -1;
  590. }
  591.  
  592. inline bool breakstr( char *line, char *out, int outsize, char *token )
  593. {
  594. int lsize = size(line);
  595. if( !lsize ) { out[0] = 0; return 0; }
  596. outsize--;
  597. int a;
  598. int tsize = size(token);
  599. int tpos = find(line, lsize, token, tsize);
  600. int wsize = 0;
  601. if( tpos == -1 )
  602. {
  603. if( lsize >= outsize ) { wsize = outsize; out[outsize - 1] = 0; }
  604. else { wsize = lsize; out[wsize] = 0; }
  605. for( a = 0; a < wsize; a++ ) out[a] = line[a];
  606. remove( line, lsize, 0, lsize - 1 );
  607. return 0;
  608. }
  609. if( tpos >= outsize ) { wsize = outsize; out[outsize - 1] = 0; }
  610. else { wsize = tpos; out[wsize] = 0; }
  611. for( a = 0; a < wsize; a++ ) out[a] = line[a];
  612. remove( line, lsize, 0, tpos + (tsize - 1) );
  613. return 1;
  614. }
  615.  
  616. inline bool breakstr( char *line, int from, char *out, int outlen )
  617. {
  618. int s = size(line), a;
  619. if( s >= from ) { out[0] = 0; return 0; }
  620. if( s - from >= outlen - 1 ) s += ((outlen - 1) - (s - from));
  621. for( a = from; a < s; a++ ) { out[a - from] = line[a]; }
  622. line[from] = 0;
  623. out[s - from] = 0;
  624. }
  625.  
  626. inline void to_low(char *in)
  627. {
  628. int a, s = size(in);
  629. for( a = 0; a < s; a++ ) if( isup(in[a]) ) tolow(in[a]);
  630. }
  631.  
  632. inline void to_up(char *in)
  633. {
  634. int a, s = size(in);
  635. for( a = 0; a < s; a++ ) if( islow(in[a]) ) toup(in[a]);
  636. }
  637.  
  638. inline void to_low(char *in, int isize)
  639. {
  640. int a;
  641. for( a = 0; a < isize; a++ ) if( isup(in[a]) ) tolow(in[a]);
  642. }
  643.  
  644. inline void to_up(char *in, int isize)
  645. {
  646. int a;
  647. for( a = 0; a < isize; a++ ) if( islow(in[a]) ) toup(in[a]);
  648. }
  649.  
  650. inline int get_num(char *in)
  651. {
  652. int res = 0, sign = 1, i = 0;
  653. if (in[0] == '-') { sign = -1; i++; }
  654. int s = size(in);
  655. for(; i < s; ++i )
  656. {
  657. if( !isnum(in[i]) ) break;
  658. res = res*10 + in[i] - '0';
  659. }
  660. return sign*res;
  661. }
  662.  
  663. inline int get_num(char *in, int isize)
  664. {
  665. int res = 0, sign = 1, i = 0;
  666. if (in[0] == '-') { sign = -1; i++; }
  667. for(; i < isize; ++i )
  668. {
  669. if( !isnum(in[i]) ) break;
  670. res = res*10 + in[i] - '0';
  671. }
  672. return sign*res;
  673. }
  674.  
  675. inline float get_float(char *in)
  676. {
  677. int isize = size(in);
  678. float res = 0.0f, sign = 1.0f; int i = 0;
  679. if (in[0] == '-') { sign = -1.0f; i++; }
  680.  
  681. for(; i < isize; ++i )
  682. {
  683. if( !isnum(in[i]) ) break;
  684. res = res*10.0f + float(in[i] - '0');
  685. }
  686. if( in[i] == '.' )
  687. {
  688. i++;
  689. float lres = 0.1f;
  690. for(; i < isize; ++i )
  691. {
  692. if( !isnum(in[i]) ) break;
  693. res += lres * float(in[i] - '0');
  694. lres /= 10.0f;
  695. }
  696. }
  697. return sign*res;
  698. }
  699.  
  700. inline float get_float(char *in, int isize)
  701. {
  702. float res = 0.0f, sign = 1.0f; int i = 0;
  703. if (in[0] == '-') { sign = -1.0f; i++; }
  704.  
  705. for(; i < isize; ++i )
  706. {
  707. if( !isnum(in[i]) ) break;
  708. res = res*10.0f + float(in[i] - '0');
  709. }
  710. if( in[i] == '.' )
  711. {
  712. i++;
  713. float lres = 0.1f;
  714. for(; i < isize; ++i )
  715. {
  716. if( !isnum(in[i]) ) break;
  717. res += lres * float(in[i] - '0');
  718. lres /= 10.0f;
  719. }
  720. }
  721. return sign*res;
  722. }
  723.  
  724. inline double get_double(char *in)
  725. {
  726. int isize = size(in);
  727. double res = 0.0, sign = 1.0; int i = 0;
  728. if (in[0] == '-') { sign = -1.0; i++; }
  729.  
  730. for(; i < isize; ++i )
  731. {
  732. if( !isnum(in[i]) ) break;
  733. res = res*10.0 + double(in[i] - '0');
  734. }
  735. if( in[i] == '.' )
  736. {
  737. i++;
  738. double lres = 0.1;
  739. for(; i < isize; ++i )
  740. {
  741. if( !isnum(in[i]) ) break;
  742. res += lres * double(in[i] - '0');
  743. lres /= 10.0;
  744. }
  745. }
  746. return sign*res;
  747. }
  748.  
  749. inline double get_double(char *in, int isize)
  750. {
  751. double res = 0.0, sign = 1.0; int i = 0;
  752. if (in[0] == '-') { sign = -1.0; i++; }
  753.  
  754. for(; i < isize; ++i )
  755. {
  756. if( !isnum(in[i]) ) break;
  757. res = res*10.0 + double(in[i] - '0');
  758. }
  759. if( in[i] == '.' )
  760. {
  761. i++;
  762. double lres = 0.1;
  763. for(; i < isize; ++i )
  764. {
  765. if( !isnum(in[i]) ) break;
  766. res += lres * double(in[i] - '0');
  767. lres /= 10.0;
  768. }
  769. }
  770. return sign*res;
  771. }
  772.  
  773. inline void reverse(char *in)
  774. {
  775. char t = 0, isize = size(in);
  776. for( int a = 0; a < isize / 2; a++ )
  777. {
  778. t = in[a];
  779. in[a] = in[(isize - 1) - a];
  780. in[(isize - 1) - a] = t;
  781. }
  782. }
  783.  
  784. inline void reverse(char *in, int isize)
  785. {
  786. char t = 0;
  787. for( int a = 0; a < isize / 2; a++ )
  788. {
  789. t = in[a];
  790. in[a] = in[(isize - 1) - a];
  791. in[(isize - 1) - a] = t;
  792. }
  793. }
  794.  
  795. inline int to_num( char *line, int num )
  796. {
  797. int i = 0, base = 10;
  798. bool isNegative = false;
  799.  
  800. if (num == 0)
  801. {
  802. line[i++] = '0';
  803. line[i] = 0;
  804. return i;
  805. }
  806.  
  807. if (num < 0 && base == 10)
  808. {
  809. isNegative = true;
  810. num = -num;
  811. }
  812.  
  813. while (num != 0)
  814. {
  815. int rem = num % base;
  816. line[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
  817. num = num/base;
  818. }
  819. if (isNegative) line[i++] = '-';
  820. line[i] = 0;
  821. reverse(line, i);
  822. return i;
  823. }
  824.  
  825. inline int to_num( char *line, int num, int base )
  826. {
  827. int i = 0;
  828. bool isNegative = false;
  829.  
  830. if (num == 0)
  831. {
  832. line[i++] = '0';
  833. line[i] = 0;
  834. return i;
  835. }
  836.  
  837. if (num < 0 && base == 10)
  838. {
  839. isNegative = true;
  840. num = -num;
  841. }
  842.  
  843. while (num != 0)
  844. {
  845. int rem = num % base;
  846. line[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
  847. num = num/base;
  848. }
  849. if (isNegative) line[i++] = '-';
  850. line[i] = 0;
  851. reverse(line, i);
  852. return i;
  853. }
  854.  
  855. inline int to_long( char *line, long num )
  856. {
  857. int i = 0; long base = 10;
  858. bool isNegative = false;
  859.  
  860. if (num == 0)
  861. {
  862. line[i++] = '0';
  863. line[i] = 0;
  864. return i;
  865. }
  866.  
  867. if (num < 0 && base == 10)
  868. {
  869. isNegative = true;
  870. num = -num;
  871. }
  872.  
  873. while (num != 0)
  874. {
  875. long rem = num % base;
  876. line[i++] = (char)((rem > 9)? (rem-10) + 'a' : rem + '0');
  877. num = num/base;
  878. }
  879. if (isNegative) line[i++] = '-';
  880. line[i] = 0;
  881. reverse(line, i);
  882. return i;
  883. }
  884.  
  885. inline int to_longlong( char *line, long long num )
  886. {
  887. int i = 0; long long base = 10;
  888. bool isNegative = false;
  889.  
  890. if (num == 0)
  891. {
  892. line[i++] = '0';
  893. line[i] = 0;
  894. return i;
  895. }
  896.  
  897. if (num < 0 && base == 10)
  898. {
  899. isNegative = true;
  900. num = -num;
  901. }
  902.  
  903. while (num != 0)
  904. {
  905. long long rem = num % base;
  906. line[i++] = (char)((rem > 9)? (rem-10) + 'a' : rem + '0');
  907. num = num/base;
  908. }
  909. if (isNegative) line[i++] = '-';
  910. line[i] = 0;
  911. reverse(line, i);
  912. return i;
  913. }
  914.  
  915. inline int to_long( char *line, long num, long base )
  916. {
  917. int i = 0;
  918. bool isNegative = false;
  919.  
  920. if (num == 0)
  921. {
  922. line[i++] = '0';
  923. line[i] = 0;
  924. return i;
  925. }
  926.  
  927. if (num < 0 && base == 10)
  928. {
  929. isNegative = true;
  930. num = -num;
  931. }
  932.  
  933. while (num != 0)
  934. {
  935. long rem = num % base;
  936. line[i++] = char((rem > 9)? (rem-10) + 'a' : rem + '0');
  937. num = num/base;
  938. }
  939. if (isNegative) line[i++] = '-';
  940. line[i] = 0;
  941. reverse(line, i);
  942. return i;
  943. }
  944.  
  945. inline int to_longlong( char *line, long long num, long long base )
  946. {
  947. int i = 0;
  948. bool isNegative = false;
  949.  
  950. if (num == 0)
  951. {
  952. line[i++] = '0';
  953. line[i] = 0;
  954. return i;
  955. }
  956.  
  957. if (num < 0 && base == 10)
  958. {
  959. isNegative = true;
  960. num = -num;
  961. }
  962.  
  963. while (num != 0)
  964. {
  965. long long rem = num % base;
  966. line[i++] = char((rem > 9)? (rem-10) + 'a' : rem + '0');
  967. num = num/base;
  968. }
  969. if (isNegative) line[i++] = '-';
  970. line[i] = 0;
  971. reverse(line, i);
  972. return i;
  973. }
  974.  
  975. inline int to_float( char *line, float fnum )
  976. {
  977. char f1[20] = "", f2[20] = "";
  978. bool isnegative = 0;
  979. if( fnum < 0.0f )
  980. {
  981. fnum *= -1.0f;
  982. isnegative = 1;
  983. }
  984. int num = (int)fnum;
  985. int lnum = to_num( f1, num );
  986. fnum -= (float)num;
  987. int a, d = 0;
  988. int rnum = 6 - lnum;
  989. if( lnum == 1 && f1[0] == '0' ) rnum--;
  990. int i = 0;
  991. float add = 0.5f;
  992. if( rnum == 0 )
  993. {
  994. fnum *= 10.0f;
  995. if( int(fnum) + '0' > '4') f1[lnum - 1]++;
  996. }
  997. else
  998. {
  999. for( a = 0; a < rnum; a++ ) add /= 10.0f;
  1000. fnum += add;
  1001. }
  1002. for( a = 0; a < rnum; a++ )
  1003. {
  1004. fnum *= 10.0f;
  1005. d = int(fnum);
  1006. f2[i] = '0'+ d;
  1007. fnum -= (float)d;
  1008. i++;
  1009. }
  1010. int l = 0;
  1011. if( isnegative ) { line[l] = '-'; l++; }
  1012. for( a = 0; a < lnum; a++ ) { line[l] = f1[a]; l++; }
  1013. line[l] = '.'; l++;
  1014. for( a = 0; a < i; a++ ) { line[l] = f2[a]; l++; }
  1015. for( a = l - 1; a >= 0; a-- )
  1016. {
  1017. if( line[a] != '.' && line[a] != '0' ) break;
  1018. line[a] = 0;
  1019. l--;
  1020. }
  1021. if( l <= 0 )
  1022. {
  1023. line[0] = '0';
  1024. line[1] = '.';
  1025. line[2] = '0';
  1026. l = 3;
  1027. }
  1028. line[l] = 0;
  1029. return l;
  1030. }
  1031.  
  1032. inline int to_float( char *line, float fnum, int p )
  1033. {
  1034. char f1[20] = "", f2[20] = "";
  1035. bool isnegative = 0;
  1036. if( fnum < 0.0f )
  1037. {
  1038. fnum *= -1.0f;
  1039. isnegative = 1;
  1040. }
  1041. int num = (int)fnum;
  1042. int lnum = to_num( f1, num );
  1043. fnum -= (float)num;
  1044. int a, d = 0;
  1045. int rnum = 6 - lnum;
  1046. if( rnum > p ) rnum = p;
  1047. if( lnum == 1 && f1[0] == '0' ) rnum--;
  1048. int i = 0;
  1049. float add = 0.5f;
  1050. if( rnum == 0 )
  1051. {
  1052. fnum *= 10.0f;
  1053. if( int(fnum) + '0' > '4') f1[lnum - 1]++;
  1054. }
  1055. else
  1056. {
  1057. for( a = 0; a < rnum; a++ ) add /= 10.0f;
  1058. fnum += add;
  1059. }
  1060. for( a = 0; a < rnum; a++ )
  1061. {
  1062. fnum *= 10.0f;
  1063. d = int(fnum);
  1064. f2[i] = '0'+ d;
  1065. fnum -= (float)d;
  1066. i++;
  1067. }
  1068. int l = 0;
  1069. if( isnegative ) { line[l] = '-'; l++; }
  1070. for( a = 0; a < lnum; a++ ) { line[l] = f1[a]; l++; }
  1071. line[l] = '.'; l++;
  1072. for( a = 0; a < i; a++ ) { line[l] = f2[a]; l++; }
  1073. for( a = l - 1; a >= 0; a-- )
  1074. {
  1075. if( line[a] != '.' && line[a] != '0' ) break;
  1076. line[a] = 0;
  1077. l--;
  1078. }
  1079. if( l <= 0 )
  1080. {
  1081. line[0] = '0';
  1082. line[1] = '.';
  1083. line[2] = '0';
  1084. l = 3;
  1085. }
  1086. line[l] = 0;
  1087. return l;
  1088. }
  1089.  
  1090. inline int to_double( char *line, double fnum )
  1091. {
  1092. char f1[20] = "", f2[20] = "";
  1093. bool isnegative = 0;
  1094. if( fnum < 0.0 )
  1095. {
  1096. fnum *= -1.0;
  1097. isnegative = 1;
  1098. }
  1099. int num = (int)fnum;
  1100. int lnum = to_num( f1, num );
  1101. fnum -= (double)num;
  1102. int a, d = 0;
  1103. int rnum = 6 - lnum;
  1104. if( lnum == 1 && f1[0] == '0' ) rnum--;
  1105. int i = 0;
  1106. double add = 0.5;
  1107. if( rnum == 0 )
  1108. {
  1109. fnum *= 10.0;
  1110. if( int(fnum) + '0' > '4') f1[lnum - 1]++;
  1111. }
  1112. else
  1113. {
  1114. for( a = 0; a < rnum; a++ ) add /= 10.0;
  1115. fnum += add;
  1116. }
  1117. for( a = 0; a < rnum; a++ )
  1118. {
  1119. fnum *= 10.0;
  1120. d = int(fnum);
  1121. f2[i] = '0'+ d;
  1122. fnum -= (double)d;
  1123. i++;
  1124. }
  1125. int l = 0;
  1126. if( isnegative ) { line[l] = '-'; l++; }
  1127. for( a = 0; a < lnum; a++ ) { line[l] = f1[a]; l++; }
  1128. line[l] = '.'; l++;
  1129. for( a = 0; a < i; a++ ) { line[l] = f2[a]; l++; }
  1130. for( a = l - 1; a >= 0; a-- )
  1131. {
  1132. if( line[a] != '.' && line[a] != '0' ) break;
  1133. line[a] = 0;
  1134. l--;
  1135. }
  1136. if( l <= 0 )
  1137. {
  1138. line[0] = '0';
  1139. line[1] = '.';
  1140. line[2] = '0';
  1141. l = 3;
  1142. }
  1143. line[l] = 0;
  1144. return l;
  1145. }
  1146.  
  1147. inline int to_double( char *line, double fnum, int p )
  1148. {
  1149. char f1[20] = "", f2[20] = "";
  1150. bool isnegative = 0;
  1151. if( fnum < 0.0 )
  1152. {
  1153. fnum *= -1.0;
  1154. isnegative = 1;
  1155. }
  1156. int num = (int)fnum;
  1157. int lnum = to_num( f1, num );
  1158. fnum -= (double)num;
  1159. int a, d = 0;
  1160. int rnum = 6 - lnum;
  1161. if( rnum > p ) rnum = p;
  1162. if( lnum == 1 && f1[0] == '0' ) rnum--;
  1163. int i = 0;
  1164. double add = 0.5;
  1165. if( rnum == 0 )
  1166. {
  1167. fnum *= 10.0;
  1168. if( int(fnum) + '0' > '4') f1[lnum - 1]++;
  1169. }
  1170. else
  1171. {
  1172. for( a = 0; a < rnum; a++ ) add /= 10.0;
  1173. fnum += add;
  1174. }
  1175. for( a = 0; a < rnum; a++ )
  1176. {
  1177. fnum *= 10.0;
  1178. d = int(fnum);
  1179. f2[i] = '0'+ d;
  1180. fnum -= (double)d;
  1181. i++;
  1182. }
  1183. int l = 0;
  1184. if( isnegative ) { line[l] = '-'; l++; }
  1185. for( a = 0; a < lnum; a++ ) { line[l] = f1[a]; l++; }
  1186. line[l] = '.'; l++;
  1187. for( a = 0; a < i; a++ ) { line[l] = f2[a]; l++; }
  1188. for( a = l - 1; a >= 0; a-- )
  1189. {
  1190. if( line[a] != '.' && line[a] != '0' ) break;
  1191. line[a] = 0;
  1192. l--;
  1193. }
  1194. if( l <= 0 )
  1195. {
  1196. line[0] = '0';
  1197. line[1] = '.';
  1198. line[2] = '0';
  1199. l = 3;
  1200. }
  1201. line[l] = 0;
  1202. return l;
  1203. }
  1204.  
  1205. inline int to_double2( char *line, double fnum )
  1206. {
  1207. char f1[20] = "", f2[20] = "";
  1208. bool isnegative = 0;
  1209. if( fnum < 0.0 )
  1210. {
  1211. fnum *= -1.0;
  1212. isnegative = 1;
  1213. }
  1214. int num = (int)fnum;
  1215. int lnum = to_num( f1, num );
  1216. fnum -= (double)num;
  1217. int a, d = 0;
  1218. int rnum = 6;
  1219. if( lnum == 1 && f1[0] == '0' ) rnum--;
  1220. int i = 0;
  1221. double add = 0.5;
  1222. if( rnum == 0 )
  1223. {
  1224. fnum *= 10.0;
  1225. if( int(fnum) + '0' > '4') f1[lnum - 1]++;
  1226. }
  1227. else
  1228. {
  1229. for( a = 0; a < rnum; a++ ) add /= 10.0;
  1230. fnum += add;
  1231. }
  1232. for( a = 0; a < rnum; a++ )
  1233. {
  1234. fnum *= 10.0;
  1235. d = int(fnum);
  1236. f2[i] = '0'+ d;
  1237. fnum -= (double)d;
  1238. i++;
  1239. }
  1240. int l = 0;
  1241. if( isnegative ) { line[l] = '-'; l++; }
  1242. for( a = 0; a < lnum; a++ ) { line[l] = f1[a]; l++; }
  1243. line[l] = '.'; l++;
  1244. for( a = 0; a < i; a++ ) { line[l] = f2[a]; l++; }
  1245. for( a = l - 1; a >= 0; a-- )
  1246. {
  1247. if( line[a] != '.' && line[a] != '0' ) break;
  1248. line[a] = 0;
  1249. l--;
  1250. }
  1251. if( l <= 0 )
  1252. {
  1253. line[0] = '0';
  1254. line[1] = '.';
  1255. line[2] = '0';
  1256. l = 3;
  1257. }
  1258. line[l] = 0;
  1259. return l;
  1260. }
  1261.  
  1262. inline int to_double2( char *line, double fnum, int dsize )
  1263. {
  1264. char f1[20] = "", f2[20] = "";
  1265. bool isnegative = 0;
  1266. if( fnum < 0.0 )
  1267. {
  1268. fnum *= -1.0;
  1269. isnegative = 1;
  1270. }
  1271. int num = (int)fnum;
  1272. int lnum = to_num( f1, num );
  1273. fnum -= (double)num;
  1274. int a, d = 0;
  1275. int rnum = dsize;
  1276. if( lnum == 1 && f1[0] == '0' ) rnum--;
  1277. int i = 0;
  1278. double add = 0.5;
  1279. if( rnum == 0 )
  1280. {
  1281. fnum *= 10.0;
  1282. if( int(fnum) + '0' > '4') f1[lnum - 1]++;
  1283. }
  1284. else
  1285. {
  1286. for( a = 0; a < rnum; a++ ) add /= 10.0;
  1287. fnum += add;
  1288. }
  1289. for( a = 0; a < rnum; a++ )
  1290. {
  1291. fnum *= 10.0;
  1292. d = int(fnum);
  1293. f2[i] = '0'+ d;
  1294. fnum -= (double)d;
  1295. i++;
  1296. }
  1297. int l = 0;
  1298. if( isnegative ) { line[l] = '-'; l++; }
  1299. for( a = 0; a < lnum; a++ ) { line[l] = f1[a]; l++; }
  1300. line[l] = '.'; l++;
  1301. for( a = 0; a < i; a++ ) { line[l] = f2[a]; l++; }
  1302. for( a = l - 1; a >= 0; a-- )
  1303. {
  1304. if( line[a] != '.' && line[a] != '0' ) break;
  1305. line[a] = 0;
  1306. l--;
  1307. }
  1308. if( l <= 0 )
  1309. {
  1310. line[0] = '0';
  1311. line[1] = '.';
  1312. line[2] = '0';
  1313. l = 3;
  1314. }
  1315. line[l] = 0;
  1316. return l;
  1317. }
  1318.  
  1319. #if defined SPwindow
  1320. inline int wchar_to_char( char *line, int ilen, WCHAR *in )
  1321. {
  1322. int a, s = 0;
  1323. line[ilen - 1] = 0;
  1324. for( a = 0; a < ilen - 1; a++)
  1325. {
  1326. if( !in[a] )
  1327. {
  1328. line[a] = 0;
  1329. break;
  1330. }
  1331. line[a] = (char)in[a];
  1332. }
  1333. return a - 1;
  1334. }
  1335.  
  1336. inline int char_to_wchar( WCHAR *line, int ilen, char *in )
  1337. {
  1338. int a, s = 0;
  1339. char * o = (char *)&line;
  1340. o[(ilen - 1) * 2] = 0;
  1341. o[((ilen - 1) * 2) + 1] = 0;
  1342. int b = 0;
  1343. for( a = 0; a < ilen - 1; a++)
  1344. {
  1345. if( !in[a] )
  1346. {
  1347. o[b] = 0;
  1348. o[b+1] = 0;
  1349. break;
  1350. }
  1351. o[b] = in[a];
  1352. o[b+1] = 0;
  1353. b++;
  1354. }
  1355. return a - 1;
  1356. }
  1357. #endif
  1358.  
  1359. inline int format( char *line, int ilen, char *in, ... )
  1360. {
  1361. va_list args;
  1362. int a = 0, b = 0, c = 0, i;
  1363. va_start(args, in);
  1364. int s = size(in);
  1365. int l = 0;
  1366. char temp[64];
  1367. char *cp;
  1368. for( a = 0; a < s; a++ )
  1369. {
  1370. if( l >= ilen - 1 ) { line[l - 1] = 0; va_end(args); return l; }
  1371. if( in[a] == '%' )
  1372. {
  1373. switch( in[a+1] )
  1374. {
  1375. case 'i':
  1376. i = to_num(temp, va_arg(args, int));
  1377. l = add( line, l, ilen, temp, i);
  1378. a++;
  1379. break;
  1380. case 'd':
  1381. i = to_num(temp, va_arg(args, int));
  1382. l = add( line, l, ilen, temp, i);
  1383. a++;
  1384. break;
  1385. case 'l':
  1386. {
  1387. i = to_longlong(temp, va_arg(args, long long));
  1388. l = add( line, l, ilen, temp, i);
  1389. a++;
  1390. break;
  1391. }
  1392. case 'L':
  1393. {
  1394. i = to_long(temp, va_arg(args, long));
  1395. l = add( line, l, ilen, temp, i);
  1396. a++;
  1397. break;
  1398. }
  1399. case '.':
  1400. {
  1401. if( in[a+3] == 'f' )
  1402. {
  1403. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  1404. {
  1405. int d = in[a + 2] - '0';
  1406. if( d > 6 ) i = to_double2(temp, va_arg(args, double), d);
  1407. else i = to_double(temp, va_arg(args, double), d);
  1408. l = add( line, l, ilen, temp, i);
  1409. a +=3;
  1410. }
  1411. }
  1412. else if( in[a+3] == 'F' )
  1413. {
  1414. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  1415. {
  1416. int d = in[a + 2] - '0';
  1417. i = to_double2(temp, va_arg(args, double), d);
  1418. l = add( line, l, ilen, temp, i);
  1419. a +=3;
  1420. }
  1421. }
  1422. else
  1423. {
  1424. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  1425. {
  1426. if( in[a+3] >= '0' && in[a+3] <= '9' )
  1427. {
  1428. if( in[a+4] == 'f' || in[a+4] == 'F' )
  1429. {
  1430. int d = ( (in[a + 2] - '0') * 10 ) + (in[a + 3] - '0' );
  1431. i = to_double2(temp, va_arg(args, double), d);
  1432. l = add( line, l, ilen, temp, i);
  1433. a +=4;
  1434. }
  1435. }
  1436. }
  1437. }
  1438. break;
  1439. }
  1440. case 'f':
  1441. {
  1442. i = to_double(temp, va_arg(args, double));
  1443. l = add( line, l, ilen, temp, i);
  1444. a++;
  1445. break;
  1446. }
  1447. case 'F':
  1448. {
  1449. i = to_double2(temp, va_arg(args, double));
  1450. l = add( line, l, ilen, temp, i);
  1451. a++;
  1452. break;
  1453. }
  1454. case '%':
  1455. {
  1456. line[l] = '%'; l++;
  1457. a++;
  1458. break;
  1459. }
  1460. case 's':
  1461. {
  1462. l = add( line, l, ilen, va_arg(args, char *));
  1463. a++;
  1464. break;
  1465. }
  1466. case 'S':
  1467. {
  1468. int t = l;
  1469. l = add( line, l, ilen, va_arg(args, char *));
  1470. if( islow(line[l - ( l - t )] )) toup(line[l - ( l - t )] );
  1471. a++;
  1472. break;
  1473. }
  1474. case 'w':
  1475. {
  1476. cp = va_arg(args, char *);
  1477. for( b = 0; b < STR_SEARCH_LOOP_SIZE; b += 2 )
  1478. {
  1479. if( l >= ilen - 1 ) break;
  1480. if( cp[b] == 0 ) break;
  1481. line[l] = cp[b];
  1482. l++;
  1483. }
  1484. line[l] = 0;
  1485. a++;
  1486. break;
  1487. }
  1488. case 'c':
  1489. {
  1490. char o = va_arg(args, char);
  1491. line[l] = o;
  1492. l++;
  1493. a++;
  1494. break;
  1495. }
  1496. case 'C':
  1497. {
  1498. char o = va_arg(args, char);
  1499. line[l] = o;
  1500. if( islow(line[l] )) toup(line[l] );
  1501. l++;
  1502. a++;
  1503. break;
  1504. }
  1505. case 'p':
  1506. {
  1507. void *p = va_arg(args, void *);
  1508. long long v = (long long)p;
  1509. i = to_longlong(temp, v);
  1510. l = add( line, l, ilen, temp, i);
  1511. a++;
  1512. break;
  1513. }
  1514. case 'n':
  1515. {
  1516. i = va_arg(args, int);
  1517. a++;
  1518. break;
  1519. }
  1520. case 'x':
  1521. {
  1522. char *p = va_arg(args, char *);
  1523. l = add( line, l, ilen, "{", 1 );
  1524. for( b = 0; b < 3; b++ )
  1525. {
  1526. i = to_num(temp, (unsigned char)p[b]);
  1527. l = add( line, l, ilen, temp, i);
  1528. l = add( line, l, ilen, "|", 1 );
  1529. }
  1530. i = to_num(temp, (unsigned char)p[b]);
  1531. l = add( line, l, ilen, temp, i);
  1532. l = add( line, l, ilen, "}", 1 );
  1533. a++;
  1534. break;
  1535. }
  1536. case 'b':
  1537. {
  1538. char *p = va_arg(args, char *);
  1539. l = add( line, l, ilen, "{", 1 );
  1540. for( b = 0; b < 4*8; b++ )
  1541. {
  1542. if( p[b/8] & ( 1 << (b % 8) ) ) temp[b] = '1';
  1543. else temp[b] = '0';
  1544.  
  1545. }
  1546. l = add( line, l, ilen, temp, b);
  1547. l = add( line, l, ilen, "}", 1 );
  1548. a++;
  1549. break;
  1550. }
  1551. default:
  1552. {
  1553. int y = 0, m = ( (ilen - 1 ) - l ); char j[10];
  1554. if( m > 8 ) m = 8;
  1555. for( y = 1; y < m; y++ )
  1556. {
  1557. if( in[a + y] >= '0' && in[ a + y ] <= '9' )
  1558. {
  1559. j[y - 1] = in[a + y];
  1560. }
  1561. else
  1562. {
  1563. if( y == 1 ) break;
  1564. else
  1565. {
  1566. j[y - 1] = 0;
  1567. i = get_num( j, y - 1);
  1568. switch( in[y + a] )
  1569. {
  1570. case 'n':
  1571. {
  1572. struct ad1{ char a1; };
  1573. struct ad2{ char a1; char a2; };
  1574. struct ad4{ ad2 a1; ad2 a2; };
  1575. struct ad8{ ad4 a1; ad4 a2; };
  1576. struct ad16{ ad8 a1; ad8 a2; };
  1577. struct ad32{ ad16 a1; ad16 a2; };
  1578. struct ad64{ ad32 a1; ad32 a2; };
  1579. struct ad128{ ad64 a1; ad64 a2; };
  1580. switch(i)
  1581. {
  1582. case 1: va_arg(args, ad1); break;
  1583. case 2: va_arg(args, ad2); break;
  1584. case 4: va_arg(args, ad4); break;
  1585. case 8: va_arg(args, ad8); break;
  1586. case 16: va_arg(args, ad16); break;
  1587. case 32: va_arg(args, ad32); break;
  1588. case 64: va_arg(args, ad64); break;
  1589. case 128: va_arg(args, ad128); break;
  1590. }
  1591. a += y;
  1592. break;
  1593. }
  1594. case 's':
  1595. {
  1596. char *p = va_arg(args, char *);
  1597. int cs = size(p);
  1598. if( i > cs - 1 ) i = cs - 1;
  1599. l = add( line, l, ilen, p, i);
  1600. line[l] = 0;
  1601. a += y;
  1602. break;
  1603. }
  1604. case 'S':
  1605. {
  1606. int t = l;
  1607. char *p = va_arg(args, char *);
  1608. int cs = size(p);
  1609. if( i > cs - 1 ) i = cs - 1;
  1610. l = add( line, l, ilen, p, i);
  1611. line[l] = 0;
  1612. if( islow(line[l - ( l - t )] )) toup(line[l - ( l - t )] );
  1613. a += y;
  1614. break;
  1615. }
  1616. case 'x':
  1617. {
  1618. char *p = va_arg(args, char *);
  1619. l = add( line, l, ilen, "{", 1 );
  1620. for( b = 0; b < i - 1; b++ )
  1621. {
  1622. c = to_num(temp, (unsigned char)p[b]);
  1623. l = add( line, l, ilen, temp, c);
  1624. l = add( line, l, ilen, "|", 1 );
  1625. }
  1626. c = to_num(temp, (unsigned char)p[b]);
  1627. l = add( line, l, ilen, temp, c);
  1628. l = add( line, l, ilen, "}", 1 );
  1629. a += y;
  1630. break;
  1631. }
  1632. case 'b':
  1633. {
  1634. char *p = va_arg(args, char *);
  1635. l = add( line, l, ilen, "{", 1 );
  1636. for( b = 0; b < i*8; b++ )
  1637. {
  1638. if( p[b/8] & ( 1 << (b % 8) ) ) temp[b] = '1';
  1639. else temp[b] = '0';
  1640.  
  1641. }
  1642. l = add( line, l, ilen, temp, b);
  1643. l = add( line, l, ilen, "}", 1 );
  1644. a += y;
  1645. break;
  1646. }
  1647. }
  1648. break;
  1649. }
  1650. }
  1651. }
  1652. }
  1653. }
  1654. }
  1655. else { line[l] = in[a]; l++; line[l] = 0; }
  1656. }
  1657. line[l] = 0;
  1658. va_end(args);
  1659. return l;
  1660. }
  1661.  
  1662. inline int format( va_list args, char *line, int ilen, char *in )
  1663. {
  1664. int a = 0, b = 0, c = 0, i;
  1665. int s = size(in);
  1666. int l = 0;
  1667. char temp[64];
  1668. char *cp;
  1669. for( a = 0; a < s; a++ )
  1670. {
  1671. if( l >= ilen - 1 ) { line[l - 1] = 0; va_end(args); return l; }
  1672. if( in[a] == '%' )
  1673. {
  1674. switch( in[a+1] )
  1675. {
  1676. case 'i':
  1677. i = to_num(temp, va_arg(args, int));
  1678. l = add( line, l, ilen, temp, i);
  1679. a++;
  1680. break;
  1681. case 'd':
  1682. i = to_num(temp, va_arg(args, int));
  1683. l = add( line, l, ilen, temp, i);
  1684. a++;
  1685. break;
  1686. case 'l':
  1687. {
  1688. i = to_longlong(temp, va_arg(args, long long));
  1689. l = add( line, l, ilen, temp, i);
  1690. a++;
  1691. break;
  1692. }
  1693. case 'L':
  1694. {
  1695. i = to_long(temp, va_arg(args, long));
  1696. l = add( line, l, ilen, temp, i);
  1697. a++;
  1698. break;
  1699. }
  1700. case '.':
  1701. {
  1702. if( in[a+3] == 'f' )
  1703. {
  1704. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  1705. {
  1706. int d = in[a + 2] - '0';
  1707. if( d > 6 ) i = to_double2(temp, va_arg(args, double), d);
  1708. else i = to_double(temp, va_arg(args, double), d);
  1709. l = add( line, l, ilen, temp, i);
  1710. a +=3;
  1711. }
  1712. }
  1713. else if( in[a+3] == 'F' )
  1714. {
  1715. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  1716. {
  1717. int d = in[a + 2] - '0';
  1718. i = to_double2(temp, va_arg(args, double), d);
  1719. l = add( line, l, ilen, temp, i);
  1720. a +=3;
  1721. }
  1722. }
  1723. else
  1724. {
  1725. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  1726. {
  1727. if( in[a+3] >= '0' && in[a+3] <= '9' )
  1728. {
  1729. if( in[a+4] == 'f' || in[a+4] == 'F' )
  1730. {
  1731. int d = ( (in[a + 2] - '0') * 10 ) + (in[a + 3] - '0' );
  1732. i = to_double2(temp, va_arg(args, double), d);
  1733. l = add( line, l, ilen, temp, i);
  1734. a +=4;
  1735. }
  1736. }
  1737. }
  1738. }
  1739. break;
  1740. }
  1741. case 'f':
  1742. {
  1743. i = to_double(temp, va_arg(args, double));
  1744. l = add( line, l, ilen, temp, i);
  1745. a++;
  1746. break;
  1747. }
  1748. case 'F':
  1749. {
  1750. i = to_double2(temp, va_arg(args, double));
  1751. l = add( line, l, ilen, temp, i);
  1752. a++;
  1753. break;
  1754. }
  1755. case '%':
  1756. {
  1757. line[l] = '%'; l++;
  1758. a++;
  1759. break;
  1760. }
  1761. case 's':
  1762. {
  1763. l = add( line, l, ilen, va_arg(args, char *));
  1764. a++;
  1765. break;
  1766. }
  1767. case 'S':
  1768. {
  1769. int t = l;
  1770. l = add( line, l, ilen, va_arg(args, char *));
  1771. if( islow(line[l - ( l - t )] )) toup(line[l - ( l - t )] );
  1772. a++;
  1773. break;
  1774. }
  1775. case 'w':
  1776. {
  1777. cp = va_arg(args, char *);
  1778. for( b = 0; b < STR_SEARCH_LOOP_SIZE; b += 2 )
  1779. {
  1780. if( l >= ilen - 1 ) break;
  1781. if( cp[b] == 0 ) break;
  1782. line[l] = cp[b];
  1783. l++;
  1784. }
  1785. line[l] = 0;
  1786. a++;
  1787. break;
  1788. }
  1789. case 'c':
  1790. {
  1791. char o = va_arg(args, char);
  1792. line[l] = o;
  1793. l++;
  1794. a++;
  1795. break;
  1796. }
  1797. case 'C':
  1798. {
  1799. char o = va_arg(args, char);
  1800. line[l] = o;
  1801. if( islow(line[l] )) toup(line[l] );
  1802. l++;
  1803. a++;
  1804. break;
  1805. }
  1806. case 'p':
  1807. {
  1808. void *p = va_arg(args, void *);
  1809. long long v = (long long)p;
  1810. i = to_longlong(temp, v);
  1811. l = add( line, l, ilen, temp, i);
  1812. a++;
  1813. break;
  1814. }
  1815. case 'n':
  1816. {
  1817. i = va_arg(args, int);
  1818. a++;
  1819. break;
  1820. }
  1821. case 'x':
  1822. {
  1823. char *p = va_arg(args, char *);
  1824. l = add( line, l, ilen, "{", 1 );
  1825. for( b = 0; b < 3; b++ )
  1826. {
  1827. i = to_num(temp, (unsigned char)p[b]);
  1828. l = add( line, l, ilen, temp, i);
  1829. l = add( line, l, ilen, "|", 1 );
  1830. }
  1831. i = to_num(temp, (unsigned char)p[b]);
  1832. l = add( line, l, ilen, temp, i);
  1833. l = add( line, l, ilen, "}", 1 );
  1834. a++;
  1835. break;
  1836. }
  1837. case 'b':
  1838. {
  1839. char *p = va_arg(args, char *);
  1840. l = add( line, l, ilen, "{", 1 );
  1841. for( b = 0; b < 4*8; b++ )
  1842. {
  1843. if( p[b/8] & ( 1 << (b % 8) ) ) temp[b] = '1';
  1844. else temp[b] = '0';
  1845.  
  1846. }
  1847. l = add( line, l, ilen, temp, b);
  1848. l = add( line, l, ilen, "}", 1 );
  1849. a++;
  1850. break;
  1851. }
  1852. default:
  1853. {
  1854. int y = 0, m = ( (ilen - 1 ) - l ); char j[10];
  1855. if( m > 8 ) m = 8;
  1856. for( y = 1; y < m; y++ )
  1857. {
  1858. if( in[a + y] >= '0' && in[ a + y ] <= '9' )
  1859. {
  1860. j[y - 1] = in[a + y];
  1861. }
  1862. else
  1863. {
  1864. if( y == 1 ) break;
  1865. else
  1866. {
  1867. j[y - 1] = 0;
  1868. i = get_num( j, y - 1);
  1869. switch( in[y + a] )
  1870. {
  1871. case 'n':
  1872. {
  1873. struct ad1{ char a1; };
  1874. struct ad2{ char a1; char a2; };
  1875. struct ad4{ ad2 a1; ad2 a2; };
  1876. struct ad8{ ad4 a1; ad4 a2; };
  1877. struct ad16{ ad8 a1; ad8 a2; };
  1878. struct ad32{ ad16 a1; ad16 a2; };
  1879. struct ad64{ ad32 a1; ad32 a2; };
  1880. struct ad128{ ad64 a1; ad64 a2; };
  1881. switch(i)
  1882. {
  1883. case 1: va_arg(args, ad1); break;
  1884. case 2: va_arg(args, ad2); break;
  1885. case 4: va_arg(args, ad4); break;
  1886. case 8: va_arg(args, ad8); break;
  1887. case 16: va_arg(args, ad16); break;
  1888. case 32: va_arg(args, ad32); break;
  1889. case 64: va_arg(args, ad64); break;
  1890. case 128: va_arg(args, ad128); break;
  1891. }
  1892. a += y;
  1893. break;
  1894. }
  1895. case 's':
  1896. {
  1897. char *p = va_arg(args, char *);
  1898. int cs = size(p);
  1899. if( i > cs - 1 ) i = cs - 1;
  1900. l = add( line, l, ilen, p, i);
  1901. line[l] = 0;
  1902. a += y;
  1903. break;
  1904. }
  1905. case 'S':
  1906. {
  1907. int t = l;
  1908. char *p = va_arg(args, char *);
  1909. int cs = size(p);
  1910. if( i > cs - 1 ) i = cs - 1;
  1911. l = add( line, l, ilen, p, i);
  1912. line[l] = 0;
  1913. if( islow(line[l - ( l - t )] )) toup(line[l - ( l - t )] );
  1914. a += y;
  1915. break;
  1916. }
  1917. case 'x':
  1918. {
  1919. char *p = va_arg(args, char *);
  1920. l = add( line, l, ilen, "{", 1 );
  1921. for( b = 0; b < i - 1; b++ )
  1922. {
  1923. c = to_num(temp, (unsigned char)p[b]);
  1924. l = add( line, l, ilen, temp, c);
  1925. l = add( line, l, ilen, "|", 1 );
  1926. }
  1927. c = to_num(temp, (unsigned char)p[b]);
  1928. l = add( line, l, ilen, temp, c);
  1929. l = add( line, l, ilen, "}", 1 );
  1930. a += y;
  1931. break;
  1932. }
  1933. case 'b':
  1934. {
  1935. char *p = va_arg(args, char *);
  1936. l = add( line, l, ilen, "{", 1 );
  1937. for( b = 0; b < i*8; b++ )
  1938. {
  1939. if( p[b/8] & ( 1 << (b % 8) ) ) temp[b] = '1';
  1940. else temp[b] = '0';
  1941.  
  1942. }
  1943. l = add( line, l, ilen, temp, b);
  1944. l = add( line, l, ilen, "}", 1 );
  1945. a += y;
  1946. break;
  1947. }
  1948. }
  1949. break;
  1950. }
  1951. }
  1952. }
  1953. }
  1954. }
  1955. }
  1956. else { line[l] = in[a]; l++; line[l] = 0; }
  1957. }
  1958. line[l] = 0;
  1959. va_end(args);
  1960. return l;
  1961. }
  1962.  
  1963. inline unsigned char *encrypt( unsigned char *line, unsigned int size, char *password, int *outsize )
  1964. {
  1965. unsigned int a = 0;
  1966. int b = 0, c = 0;
  1967. int s = 0;
  1968. unsigned char *p = new unsigned char[size+8];
  1969. *outsize = size+8;
  1970. for( a = 6; a < size + 6; a++ ) p[a] = line[a-6];
  1971. unsigned char *n = (unsigned char *)&size;
  1972. size += 4;
  1973. p[0] = n[0]; p[1] = n[1]; p[2] = n[2]; p[3] = n[3];
  1974. p[4] = 'S'; p[5] = 'P';
  1975. p[size+2] = 'P'; p[size+3] = 'S';
  1976. for( a = 0; a < 1024; a++ ) { if( !password[a] ) break; s++; }
  1977. for( a = 0; a < size; a++ )
  1978. {
  1979. if( b >= s )
  1980. {
  1981. if( c >= s ) c = 0;
  1982. b = 0;
  1983. c++;
  1984. }
  1985.  
  1986. p[a+4] += password[c] + password[b];
  1987. b++;
  1988. }
  1989. int pos = 0; unsigned char t = 0; b = 0;
  1990. int newpos = 0;
  1991. for( a = 0; a < size; a++ )
  1992. {
  1993. if( b >= s ) b = 0;
  1994. pos += password[b];
  1995. b++;
  1996. newpos = pos % size;
  1997. t = p[newpos+4];
  1998. p[newpos+4] = p[a+4];
  1999. p[a+4] = t;
  2000. }
  2001. return p;
  2002. }
  2003.  
  2004. inline bool decrypt( unsigned char *line, char *password, unsigned char **out, int *outsize )
  2005. {
  2006. int size = (line[3] << 24) | (line[2] << 16) | (line[1] << 8) | (line[0]);
  2007. if( size < 0 || size > 16777216 )
  2008. {
  2009. *outsize = 0;
  2010. return 0;
  2011. }
  2012. unsigned int a = 0;
  2013. int b = 0, c = 0;
  2014. int s = 0;
  2015. for( a = 0; a < 1024; a++ ) { if( !password[a] ) break; s++; }
  2016. unsigned char *p = new unsigned char[size];
  2017. for( a = 0; a < size; a++ ) p[a] = line[a + 4];
  2018.  
  2019. int pos = 0; unsigned char t = 0; b = 0;
  2020. int newpos = 0;
  2021. for( a = 0; a < size; a++ )
  2022. {
  2023. if( b >= s ) b = 0;
  2024. pos += password[b];
  2025. b++;
  2026. }
  2027. b--;
  2028. if( b == -1 ) b = s - 1;
  2029.  
  2030. int a2;
  2031. for( a2 = size-1; a2 >= 0; a2-- )
  2032. {
  2033. if( b == -1 ) b = s - 1;
  2034. newpos = pos % size;
  2035. t = p[newpos];
  2036. p[newpos] = p[a2];
  2037. p[a2] = t;
  2038. pos -= password[b];
  2039. b--;
  2040. }
  2041.  
  2042. b = 0, c = 0;
  2043. for( a = 0; a < size; a++ )
  2044. {
  2045. if( b >= s )
  2046. {
  2047. if( c >= s ) c = 0;
  2048. b = 0;
  2049. c++;
  2050. }
  2051.  
  2052. p[a] -= password[b] + password[c];
  2053. b++;
  2054. }
  2055. if( p[0] != 'S' || p[1] != 'P' || p[size - 2] != 'P' || p[size - 1] != 'S' ) return 0;
  2056. *outsize = size - 4;
  2057. unsigned char *u = new unsigned char[size-4];
  2058. for( a = 2; a < size - 2; a++ ) u[a-2] = p[a];
  2059. delete [] p;
  2060. *out = u;
  2061. return 1;
  2062. }
  2063. };
  2064.  
  2065. #define vformat(line, ilen, in) { va_list args; va_start(args, in); str::format(args, line, ilen, in); }
  2066.  
  2067. class spchar
  2068. {
  2069. public:
  2070. char *data;
  2071. int size;
  2072. int reserved;
  2073. int reserv_step;
  2074. int fpos;
  2075. bool isbinary;
  2076. spchar(bool binary)
  2077. {
  2078. size = 0;
  2079. reserved = 0;
  2080. reserv_step = 0;
  2081. fpos = 0;
  2082. isbinary = binary;
  2083. }
  2084.  
  2085. void free_reserved()
  2086. {
  2087. char *p;
  2088. if( !reserved ) return;
  2089. if( !size )
  2090. {
  2091. delete [] data;
  2092. reserved = 0;
  2093. return;
  2094. }
  2095. if( isbinary ) p = new char[size];
  2096. else { p = new char[size+1]; p[size] = 0; }
  2097. for( int a = 0; a < size; a++ ) p[a] = data[a];
  2098. delete [] data; data = p;
  2099. reserved = 0;
  2100. }
  2101.  
  2102. bool same( spchar in )
  2103. {
  2104. if( size != in.size ) return 0;
  2105. for( int a = 0; a < size; a++ ) { if( in.data[a] != data[a] ) return 0; }
  2106. return 1;
  2107. }
  2108.  
  2109. void makeroom(int howmuch)
  2110. {
  2111. int newsize = howmuch+size+reserved+reserv_step;
  2112. int a;
  2113. char *p;
  2114. if( size+reserved == howmuch+size+reserved+reserv_step) return;
  2115. if(!isbinary)
  2116. {
  2117. p = new char[newsize + 1];
  2118. p[newsize] = 0;
  2119. }
  2120. else p = new char[newsize];
  2121. for( a = 0; a < size; a++ ) p[a] = data[a];
  2122. if( size || reserved ) delete[] data;
  2123. reserved += howmuch+reserv_step;
  2124. data = p;
  2125. }
  2126.  
  2127. spchar(bool binary, int isize)
  2128. {
  2129. size = 0;
  2130. reserved = 0;
  2131. reserv_step = 1;
  2132. fpos = 0;
  2133. isbinary = binary;
  2134. makeroom(isize);
  2135. }
  2136.  
  2137. int add( char *in )
  2138. {
  2139. int s = str::size(in);
  2140. if( s >= reserved ) makeroom(s);
  2141. for( int a = size; a < size+s; a++ ) data[a] = in[a - size];
  2142. if( !isbinary ) { data[size+s] = 0; reserved -= s; }
  2143. else reserved -= s;
  2144. size += s;
  2145. return size;
  2146. }
  2147.  
  2148. spchar(bool binary, char *in)
  2149. {
  2150. size = 0;
  2151. reserved = size;
  2152. reserv_step = 1;
  2153. fpos = 0;
  2154. isbinary = binary;
  2155. add(in);
  2156. }
  2157.  
  2158. int add( char *in, int isize )
  2159. {
  2160. if( isize >= reserved ) makeroom(isize);
  2161. for( int a = size; a < size+isize; a++ ) data[a] = in[a - size];
  2162. if( !isbinary ) { data[size+isize] = 0; reserved -= isize; }
  2163. else reserved -= isize;
  2164. size += isize;
  2165. return size;
  2166. }
  2167.  
  2168. spchar(bool binary, char *in, int size)
  2169. {
  2170. size = 0;
  2171. reserved = size;
  2172. reserv_step = 1;
  2173. fpos = 0;
  2174. isbinary = binary;
  2175. add(in, size);
  2176. }
  2177.  
  2178. int add( int pos, char *in )
  2179. {
  2180. int s = str::size(in);
  2181. int addsize = 0, empty = 0, a;
  2182. addsize -= reserved;
  2183. addsize += s;
  2184. if( pos >= size ) empty = pos - size;
  2185. addsize += empty;
  2186.  
  2187. if( addsize > 0 ) makeroom( addsize );
  2188. for( a = size; a >= pos; a-- ) data[a+s] = data[a];
  2189. for( a = pos; a < pos + s; a++ ) data[a] = in[a - pos];
  2190. if( empty ) for( a = size; a < pos; a++ ) data[a] = 0;
  2191. size += s + empty;
  2192. if( isbinary ) reserved -= s + empty;
  2193. else
  2194. {
  2195. reserved -= s + empty;
  2196. data[size] = 0;
  2197. }
  2198. return size;
  2199. }
  2200.  
  2201. int add( int pos, char *in, int isize )
  2202. {
  2203. int addsize = 0, empty = 0, a;
  2204. addsize -= reserved;
  2205. addsize += isize;
  2206. if( pos >= size ) empty = pos - size;
  2207. addsize += empty;
  2208.  
  2209. if( addsize > 0 ) makeroom( addsize );
  2210. for( a = size; a >= pos; a-- ) data[a+isize] = data[a];
  2211. for( a = pos; a < pos + isize; a++ ) data[a] = in[a - pos];
  2212. if( empty ) for( a = size; a < pos; a++ ) data[a] = 0;
  2213. size += isize + empty;
  2214. if( isbinary ) reserved -= isize + empty;
  2215. else
  2216. {
  2217. reserved -= isize + empty;
  2218. data[size] = 0;
  2219. }
  2220. return size;
  2221. }
  2222.  
  2223. ~spchar() { if( size || reserved ) delete [] data; }
  2224.  
  2225. void set( char *in )
  2226. {
  2227. if( size || reserved ) { delete [] data; }
  2228. size = 0;
  2229. reserved = 0;
  2230. int s = str::size(in);
  2231. if( isbinary ) { makeroom(s); reserved -= s; }
  2232. else { makeroom(s); reserved -= s; data[s] = 0; }
  2233. for( int a = 0; a < s; a++ ) data[a] = in[a];
  2234. size = s;
  2235. }
  2236.  
  2237. void set( char *in, int isize )
  2238. {
  2239. if( size || reserved ) { delete [] data; }
  2240. size = 0;
  2241. reserved = 0;
  2242. if( isbinary ) { makeroom(isize); reserved -= isize; }
  2243. else { makeroom(isize); reserved -= isize; data[isize] = 0; }
  2244. for( int a = 0; a < isize; a++ ) data[a] = in[a];
  2245. size = isize;
  2246. }
  2247.  
  2248. int remove( int start, int end )
  2249. {
  2250. int r = (end - start) + 1;
  2251. if( r <= 0 || start >= size || end > size) return size;
  2252. reserved += r;
  2253. for( int a = start; a <= size - r; a++ ) data[a] = data[a+r];
  2254. size -= r;
  2255. return size;
  2256. }
  2257.  
  2258. int find( char *what )
  2259. {
  2260. int a, s = str::size(what), c = 0;
  2261. for( a = 0; a < size; a++ )
  2262. {
  2263. if( c >= s ) return a - s;
  2264. if( data[a] == what[c] )
  2265. {
  2266. c++;
  2267. continue;
  2268. }
  2269. if( c ) a--;
  2270. c = 0;
  2271. }
  2272. if( c == s ) return a - s;
  2273. return -1;
  2274. }
  2275.  
  2276. int find( int pos, char *what )
  2277. {
  2278. if( pos >= size ) return -1;
  2279. int a, s = str::size(what), c = 0;
  2280. for( a = pos; a < size; a++ )
  2281. {
  2282. if( c >= s ) return a - s;
  2283. if( data[a] == what[c] )
  2284. {
  2285. c++;
  2286. continue;
  2287. }
  2288. if( c ) a--;
  2289. c = 0;
  2290. }
  2291. if( c == s ) return a - s;
  2292. return -1;
  2293. }
  2294.  
  2295. int find( char *what, int isize )
  2296. {
  2297. int a, c = 0;
  2298. for( a = 0; a < size; a++ )
  2299. {
  2300. if( c >= isize ) return a - isize;
  2301. if( data[a] == what[c] )
  2302. {
  2303. c++;
  2304. continue;
  2305. }
  2306. if( c ) a--;
  2307. c = 0;
  2308. }
  2309. if( c == isize ) return a - isize;
  2310. return -1;
  2311. }
  2312.  
  2313. int find( int pos, char *what, int isize )
  2314. {
  2315. if( pos >= size ) return -1;
  2316. int a, c = 0;
  2317. for( a = pos; a < size; a++ )
  2318. {
  2319. if( c >= isize ) return a - isize;
  2320. if( data[a] == what[c] )
  2321. {
  2322. c++;
  2323. continue;
  2324. }
  2325. if( c ) a--;
  2326. c = 0;
  2327. }
  2328. if( c == isize ) return a - isize;
  2329. return -1;
  2330. }
  2331.  
  2332. int find2( char *what )
  2333. {
  2334. int a, s = str::size(what), c = 0;
  2335. char low[2];
  2336. for( a = 0; a < size; a++ )
  2337. {
  2338. if( c >= s ) return a - s;
  2339. low[0] = data[a];
  2340. low[1] = what[c];
  2341. if( isup(low[0]) ) tolow(low[0]);
  2342. if( isup(low[1]) ) tolow(low[1]);
  2343. if( low[0] == low[1] )
  2344. {
  2345. c++;
  2346. continue;
  2347. }
  2348. if( c ) a--;
  2349. c = 0;
  2350. }
  2351. if( c == s ) return a - s;
  2352. return -1;
  2353. }
  2354.  
  2355. int find2( int pos, char *what )
  2356. {
  2357. if( pos >= size ) return -1;
  2358. int a, s = str::size(what), c = 0;
  2359. char low[2];
  2360. for( a = pos; a < size; a++ )
  2361. {
  2362. if( c >= s ) return a - s;
  2363. low[0] = data[a];
  2364. low[1] = what[c];
  2365. if( isup(low[0]) ) tolow(low[0]);
  2366. if( isup(low[1]) ) tolow(low[1]);
  2367. if( low[0] == low[1] )
  2368. {
  2369. c++;
  2370. continue;
  2371. }
  2372. if( c ) a--;
  2373. c = 0;
  2374. }
  2375. if( c == s ) return a - s;
  2376. return -1;
  2377. }
  2378.  
  2379. int find2( char *what, int isize )
  2380. {
  2381. int a, c = 0;
  2382. char low[2];
  2383. for( a = 0; a < size; a++ )
  2384. {
  2385. if( c >= isize ) return a - isize;
  2386. low[0] = data[a];
  2387. low[1] = what[c];
  2388. if( isup(low[0]) ) tolow(low[0]);
  2389. if( isup(low[1]) ) tolow(low[1]);
  2390. if( low[0] == low[1] )
  2391. {
  2392. c++;
  2393. continue;
  2394. }
  2395. if( c ) a--;
  2396. c = 0;
  2397. }
  2398. if( c == isize ) return a - isize;
  2399. return -1;
  2400. }
  2401.  
  2402. int find2( int pos, char *what, int isize )
  2403. {
  2404. if( pos >= size ) return -1;
  2405. char low[2];
  2406. int a, c = 0;
  2407. for( a = pos; a < size; a++ )
  2408. {
  2409. if( c >= isize ) return a - isize;
  2410. low[0] = data[a];
  2411. low[1] = what[c];
  2412. if( isup(low[0]) ) tolow(low[0]);
  2413. if( isup(low[1]) ) tolow(low[1]);
  2414. if( low[0] == low[1] )
  2415. {
  2416. c++;
  2417. continue;
  2418. }
  2419. if( c ) a--;
  2420. c = 0;
  2421. }
  2422. if( c == isize ) return a - isize;
  2423. return -1;
  2424. }
  2425.  
  2426. int write( void *in, int insize, int count )
  2427. {
  2428. if( !isbinary ) return size;
  2429. int adsize = insize * count;
  2430. if( reserved - adsize <= 0 ) makeroom(adsize);
  2431. for( int a = size; a < size+adsize; a++ ) data[a] = ((char *)in)[a - size];
  2432. size += adsize;
  2433. reserved -= adsize;
  2434. return size;
  2435. }
  2436.  
  2437. int write( int pos, void *in, int insize, int count )
  2438. {
  2439. if( !isbinary ) return size;
  2440. int adsize = insize * count;
  2441. if( reserved - adsize <= 0 ) makeroom(adsize);
  2442. int a;
  2443. for(a = size; a >= pos; a-- ) data[a + adsize - 1] = data[a];
  2444. for(a = pos; a < pos+adsize; a++ ) data[a] = ((char *)in)[a - pos];
  2445. size += adsize;
  2446. reserved -= adsize;
  2447. return size;
  2448. }
  2449.  
  2450. int read( void *out, int outsize, int count )
  2451. {
  2452. if( fpos >= size ) return -1;
  2453. int s = outsize*count;
  2454. for( int a = fpos; a < s; a++ ) ((char *)out)[a - fpos] = data[a];
  2455. fpos += s;
  2456. return fpos;
  2457. }
  2458.  
  2459. bool replace( char *what, char *with )
  2460. {
  2461. if( !size ) return 0;
  2462. int s1 = str::size(what);
  2463. int i = find( what, s1 );
  2464. if( i == -1 ) return 0;
  2465. int s2 = str::size(with);
  2466. remove(i, i + s1 - 1);
  2467. add(i, with, s2);
  2468. return 1;
  2469. }
  2470.  
  2471. bool replace( char *what, int iwhat, char *with, int iwith )
  2472. {
  2473. if( !size ) return 0;
  2474. int i = find( what, iwhat );
  2475. if( i == -1 ) return 0;
  2476. remove(i, i + iwhat - 1);
  2477. add(i, with, iwith);
  2478. return 1;
  2479. }
  2480.  
  2481. void trim()
  2482. {
  2483. free_reserved();
  2484. }
  2485.  
  2486. void trim( char what )
  2487. {
  2488. int r1e = 0, a;
  2489. for( a = 0; a < size; a++ )
  2490. {
  2491. if( data[a] != what ) break;
  2492. r1e++;
  2493. }
  2494.  
  2495. if( r1e > 0 ) remove( 0, r1e - 1 );
  2496. if( !size ) return;
  2497. int r2s = size;
  2498. for( a = size - 1; a >= 0; a-- )
  2499. {
  2500. if( data[a] != what ) break;
  2501. r2s--;
  2502. }
  2503. if( r2s == size ) return;
  2504. remove( r2s, size - 1);
  2505. }
  2506.  
  2507. bool same( char *in)
  2508. {
  2509. int s = str::size(in);
  2510. if( s != size ) return 0;
  2511. for( int a = 0; a < size; a++ ) if( data[a] != in[a] ) return 0;
  2512. return 1;
  2513. }
  2514.  
  2515. bool same( char *in, int isize)
  2516. {
  2517. if( isize != size ) return 0;
  2518. for( int a = 0; a < size; a++ ) if( data[a] != in[a] ) return 0;
  2519. return 1;
  2520. }
  2521.  
  2522. bool same2( char *in )
  2523. {
  2524. char low[2];
  2525. int s = str::size(in);
  2526. if( s != size ) return 0;
  2527. for( int a = 0; a < s; a++ )
  2528. {
  2529. low[0] = data[a];
  2530. low[1] = in[a];
  2531. if( isup(low[0]) ) tolow(low[0]);
  2532. if( isup(low[1]) ) tolow(low[1]);
  2533. if( low[0] != low[1] ) return 0;
  2534. }
  2535. return 1;
  2536. }
  2537.  
  2538. bool same2( char *in, int isize )
  2539. {
  2540. char low[2];
  2541. if( isize != size ) return 0;
  2542. for( int a = 0; a < size; a++ )
  2543. {
  2544. low[0] = data[a];
  2545. low[1] = in[a];
  2546. if( isup(low[0]) ) tolow(low[0]);
  2547. if( isup(low[1]) ) tolow(low[1]);
  2548. if( low[0] != low[1] ) return 0;
  2549. }
  2550. return 1;
  2551. }
  2552.  
  2553. bool breakstr( char *out, int outsize, char *token )
  2554. {
  2555. if( !size ) { out[0] = 0; return 0; }
  2556. int a;
  2557. int tsize = str::size(token);
  2558. int tpos = find(0, token, tsize);
  2559. int wsize = 0;
  2560. if( tpos == -1 )
  2561. {
  2562. if( size >= outsize ) { wsize = outsize; out[outsize - 1] = 0; }
  2563. else { wsize = size; out[wsize] = 0; }
  2564. for( a = 0; a < wsize; a++ ) out[a] = data[a];
  2565. remove( 0, size - 1 );
  2566. return 0;
  2567. }
  2568. if( tpos >= outsize ) { wsize = outsize; out[outsize - 1] = 0; }
  2569. else { wsize = tpos; out[wsize] = 0; }
  2570. for( a = 0; a < wsize; a++ ) out[a] = data[a];
  2571. remove( 0, tpos + (tsize - 1) );
  2572. return 1;
  2573. }
  2574.  
  2575. bool breakstr( int from, char *out, int outlen )
  2576. {
  2577. if( !size || size >= from ) { out[0] = 0; return 0; }
  2578. int a, s = size;
  2579. if( s - from >= outlen - 1 ) s += ((outlen - 1) - (s - from));
  2580. for( a = from; a < s; a++ ) { out[a - from] = data[a]; }
  2581. data[from] = 0;
  2582. size = from;
  2583. out[s - from] = 0;
  2584. }
  2585.  
  2586. void updatesize()
  2587. {
  2588. if( isbinary ) return;
  2589. size = 0;
  2590. for( int a = 0; a < STR_SEARCH_LOOP_SIZE; a++ ) { size++; if( !data[a] ) break; }
  2591. }
  2592.  
  2593. void to_low()
  2594. {
  2595. int a;
  2596. for( a = 0; a < size; a++ ) if( isup(data[a]) ) tolow(data[a]);
  2597. }
  2598.  
  2599. void to_up()
  2600. {
  2601. int a;
  2602. for( a = 0; a < size; a++ ) if( islow(data[a]) ) toup(data[a]);
  2603. }
  2604.  
  2605. int get_num()
  2606. {
  2607. int res = 0, sign = 1, i = 0;
  2608. if (data[0] == '-') { sign = -1; i++; }
  2609.  
  2610. for(; i < size; ++i )
  2611. {
  2612. if( !isnum(data[i]) ) break;
  2613. res = res*10 + data[i] - '0';
  2614. }
  2615. return sign*res;
  2616. }
  2617.  
  2618. float get_float()
  2619. {
  2620. float res = 0.0, sign = 1.0; int i = 0;
  2621. if (data[0] == '-') { sign = -1.0; i++; }
  2622.  
  2623. for(; i < size; ++i )
  2624. {
  2625. if( !isnum(data[i]) ) break;
  2626. res = res*10.0f + float(data[i] - '0');
  2627. }
  2628. if( data[i] == '.' )
  2629. {
  2630. i++;
  2631. float lres = 0.1f;
  2632. for(; i < size; ++i )
  2633. {
  2634. if( !isnum(data[i]) ) break;
  2635. res += lres * float(data[i] - '0');
  2636. lres /= 10.0f;
  2637. }
  2638. }
  2639. return sign*res;
  2640. }
  2641.  
  2642. double get_double()
  2643. {
  2644. double res = 0.0, sign = 1.0; int i = 0;
  2645. if (data[0] == '-') { sign = -1.0; i++; }
  2646.  
  2647. for(; i < size; ++i )
  2648. {
  2649. if( !isnum(data[i]) ) break;
  2650. res = res*10.0 + double(data[i] - '0');
  2651. }
  2652. if( data[i] == '.' )
  2653. {
  2654. i++;
  2655. double lres = 0.1;
  2656. for(; i < size; ++i )
  2657. {
  2658. if( !isnum(data[i]) ) break;
  2659. res += lres * double(data[i] - '0');
  2660. lres /= 10.0;
  2661. }
  2662. }
  2663. return sign*res;
  2664. }
  2665.  
  2666. int ref()
  2667. {
  2668. int r = 0;
  2669. for( int a = size; a < size; a++ ) r += data[a];
  2670. return r;
  2671. }
  2672.  
  2673. void reverse()
  2674. {
  2675. char t = 0;
  2676. for( int a = 0; a < size / 2; a++ )
  2677. {
  2678. t = data[a];
  2679. data[a] = data[(size - 1) - a];
  2680. data[(size - 1) - a] = t;
  2681. }
  2682. }
  2683.  
  2684. void to_num( int in )
  2685. {
  2686. char temp[64]; int s;
  2687. s = str::to_num(temp, in);
  2688. set(temp, s);
  2689. }
  2690.  
  2691. void to_num( int in, int base )
  2692. {
  2693. char temp[64]; int s;
  2694. s = str::to_num(temp, in, base);
  2695. set(temp, s);
  2696. }
  2697.  
  2698. void to_long( long in )
  2699. {
  2700. char temp[64]; int s;
  2701. s = str::to_long(temp, in);
  2702. set(temp, s);
  2703. }
  2704.  
  2705. void to_longlong( long long in )
  2706. {
  2707. char temp[64]; int s;
  2708. s = str::to_longlong(temp, in);
  2709. set(temp, s);
  2710. }
  2711.  
  2712. void to_float( float in )
  2713. {
  2714. char temp[64]; int s;
  2715. s = str::to_float(temp, in);
  2716. set(temp, s);
  2717. }
  2718.  
  2719. void to_float( float in, int p )
  2720. {
  2721. char temp[64]; int s;
  2722. s = str::to_float(temp, in, p);
  2723. set(temp, s);
  2724. }
  2725.  
  2726. void to_double( double in )
  2727. {
  2728. char temp[64]; int s;
  2729. s = str::to_double(temp, in);
  2730. set(temp, s);
  2731. }
  2732.  
  2733. void to_double( double in, int p )
  2734. {
  2735. char temp[64]; int s;
  2736. s = str::to_double(temp, in, p);
  2737. set(temp, s);
  2738. }
  2739.  
  2740. void to_double2( double in, int p )
  2741. {
  2742. char temp[64]; int s;
  2743. s = str::to_double2(temp, in, p);
  2744. set(temp, s);
  2745. }
  2746. #if defined SPwindow
  2747. void to_wchar( WCHAR *out, int ilen )
  2748. {
  2749. str::char_to_wchar(out, ilen, data);
  2750. }
  2751. #endif
  2752.  
  2753. int format( char *in, ... )
  2754. {
  2755. va_list args;
  2756. int a = 0, b = 0, c = 0, i, s = str::size(in);
  2757. va_start(args, in);
  2758. char temp[64];
  2759. char *cp;
  2760. #if defined SPwindow
  2761. WCHAR *wc;
  2762. #endif
  2763. for( a = 0; a < s; a++ )
  2764. {
  2765. cout << "l:" << in[a] << endl;
  2766. if( in[a] == '%' )
  2767. {
  2768. switch( in[a+1] )
  2769. {
  2770. case 'i':
  2771. i = str::to_num(temp, va_arg(args, int));
  2772. add( temp, i);
  2773. a++;
  2774. break;
  2775. case 'd':
  2776. i = str::to_num(temp, va_arg(args, int));
  2777. add( temp, i);
  2778. a++;
  2779. break;
  2780. case 'l':
  2781. {
  2782. i = str::to_longlong(temp, va_arg(args, long long));
  2783. add( temp, i);
  2784. a++;
  2785. break;
  2786. }
  2787. case 'L':
  2788. {
  2789. i = str::to_long(temp, va_arg(args, long));
  2790. add( temp, i);
  2791. a++;
  2792. break;
  2793. }
  2794. case 'f':
  2795. {
  2796. i = str::to_double(temp, va_arg(args, double));
  2797. add( temp, i);
  2798. a++;
  2799. break;
  2800. }
  2801. case 'F':
  2802. {
  2803. i = str::to_double2(temp, va_arg(args, double));
  2804. add( temp, i);
  2805. a++;
  2806. break;
  2807. }
  2808. case '.':
  2809. {
  2810. if( in[a+3] == 'f' )
  2811. {
  2812. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  2813. {
  2814. int d = in[a + 2] - '0';
  2815. if( d > 6 ) i = str::to_double2(temp, va_arg(args, double), d);
  2816. else i = str::to_double(temp, va_arg(args, double), d);
  2817. add( temp, i);
  2818. a +=3;
  2819. }
  2820. }
  2821. else if( in[a+3] == 'F' )
  2822. {
  2823. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  2824. {
  2825. int d = in[a + 2] - '0';
  2826. i = str::to_double2(temp, va_arg(args, double), d);
  2827. add( temp, i);
  2828. a +=3;
  2829. }
  2830. }
  2831. else
  2832. {
  2833. if( in[a + 2] >= '0' && in[a + 2] <= '9' )
  2834. {
  2835. if( in[a+3] >= '0' && in[a+3] <= '9' )
  2836. {
  2837. if( in[a+4] == 'f' || in[a+4] == 'F' )
  2838. {
  2839. int d = ( (in[a + 2] - '0') * 10 ) + (in[a + 3] - '0' );
  2840. i = str::to_double2(temp, va_arg(args, double), d);
  2841. add( temp, i);
  2842. a +=4;
  2843. }
  2844. }
  2845. }
  2846. }
  2847. break;
  2848. }
  2849. case '%':
  2850. {
  2851. add("%", 1);
  2852. a++;
  2853. break;
  2854. }
  2855. case 's':
  2856. {
  2857. cp = va_arg(args, char *);
  2858. add( cp);
  2859. a++;
  2860. break;
  2861. }
  2862. case 'S':
  2863. {
  2864. cp = va_arg(args, char *);
  2865. if( islow(cp[0] )) toup(cp[0] );
  2866. add(cp);
  2867. a++;
  2868. break;
  2869. }
  2870. #if defined SPwindow
  2871. case 'w':
  2872. {
  2873. wc = va_arg(args, WCHAR *);
  2874. (*this)+=wc;
  2875. a++;
  2876. break;
  2877. }
  2878. #endif
  2879. case 'c':
  2880. {
  2881. char o = va_arg(args, char);
  2882. add(&o, 1);
  2883. a++;
  2884. break;
  2885. }
  2886. case 'C':
  2887. {
  2888. char o = va_arg(args, char);
  2889. if( islow(o )) toup(o );
  2890. add(&o, 1);
  2891. a++;
  2892. break;
  2893. }
  2894. case 'p':
  2895. {
  2896. void *p = va_arg(args, void *);
  2897. long long v = (long long)p;
  2898. i = str::to_longlong(temp, v);
  2899. add( temp, i);
  2900. a++;
  2901. break;
  2902. }
  2903. case 'n':
  2904. {
  2905. i = va_arg(args, int);
  2906. a++;
  2907. break;
  2908. }
  2909. case 'x':
  2910. {
  2911. char *p = va_arg(args, char *);
  2912. add( "{", 1);
  2913. for( b = 0; b < 3; b++ )
  2914. {
  2915. i = str::to_num(temp, (unsigned char)p[b]);
  2916. add( temp, i);
  2917. add( "|", 1 );
  2918. }
  2919. i = str::to_num(temp, (unsigned char)p[b]);
  2920. add( temp, i);
  2921. add( "}", 1 );
  2922. a++;
  2923. break;
  2924. }
  2925. case 'b':
  2926. {
  2927. char *p = va_arg(args, char *);
  2928. add( "{", 1);
  2929. for( b = 0; b < 4*8; b++ )
  2930. {
  2931. if( p[b/8] & ( 1 << (b % 8) ) ) temp[b] = '1';
  2932. else temp[b] = '0';
  2933.  
  2934. }
  2935. add( temp, i);
  2936. add( "}", 1);
  2937. a++;
  2938. break;
  2939. }
  2940. default:
  2941. {
  2942. int y = 0; char j[10];
  2943. for( y = 1; y < 8; y++ )
  2944. {
  2945. if( in[a + y] >= '0' && in[ a + y ] <= '9' )
  2946. {
  2947. j[y - 1] = in[a + y];
  2948. }
  2949. else
  2950. {
  2951. if( y == 1 ) break;
  2952. else
  2953. {
  2954. j[y - 1] = 0;
  2955. i = str::get_num( j, y - 1);
  2956. switch( in[y + a] )
  2957. {
  2958. case 'n':
  2959. {
  2960. struct ad1{ char a1; };
  2961. struct ad2{ char a1; char a2; };
  2962. struct ad4{ ad2 a1; ad2 a2; };
  2963. struct ad8{ ad4 a1; ad4 a2; };
  2964. struct ad16{ ad8 a1; ad8 a2; };
  2965. struct ad32{ ad16 a1; ad16 a2; };
  2966. struct ad64{ ad32 a1; ad32 a2; };
  2967. struct ad128{ ad64 a1; ad64 a2; };
  2968. switch(i)
  2969. {
  2970. case 1: va_arg(args, ad1); break;
  2971. case 2: va_arg(args, ad2); break;
  2972. case 4: va_arg(args, ad4); break;
  2973. case 8: va_arg(args, ad8); break;
  2974. case 16: va_arg(args, ad16); break;
  2975. case 32: va_arg(args, ad32); break;
  2976. case 64: va_arg(args, ad64); break;
  2977. case 128: va_arg(args, ad128); break;
  2978. }
  2979. a += y;
  2980. break;
  2981. }
  2982. case 's':
  2983. {
  2984. char *p = va_arg(args, char *);
  2985. int cs = str::size(p);
  2986. if( i > cs - 1 ) i = cs - 1;
  2987. add(p, i);
  2988. a += y;
  2989. break;
  2990. }
  2991. case 'S':
  2992. {
  2993. char *p = va_arg(args, char *);
  2994. if( islow(p[0] )) toup(p[0] );
  2995. int cs = str::size(p);
  2996. if( i > cs - 1 ) i = cs - 1;
  2997. add(p, i);
  2998. a += y;
  2999. break;
  3000. }
  3001. case 'x':
  3002. {
  3003. char *p = va_arg(args, char *);
  3004. add( "{", 1 );
  3005. for( b = 0; b < i - 1; b++ )
  3006. {
  3007. c = str::to_num(temp, (unsigned char)p[b]);
  3008. add( temp, c );
  3009. add( "|", 1 );
  3010. }
  3011. c = str::to_num(temp, (unsigned char)p[b]);
  3012. add( temp, c );
  3013. add( "}", 1 );
  3014. a += y;
  3015. break;
  3016. }
  3017. case 'b':
  3018. {
  3019. char *p = va_arg(args, char *);
  3020. add( "}", 1 );
  3021. for( b = 0; b < i*8; b++ )
  3022. {
  3023. if( p[b/8] & ( 1 << (b % 8) ) ) temp[b] = '1';
  3024. else temp[b] = '0';
  3025.  
  3026. }
  3027. add( temp, b );
  3028. add( "}", 1 );
  3029. a += y;
  3030. break;
  3031. }
  3032. }
  3033. break;
  3034. }
  3035. }
  3036. }
  3037. }
  3038. }
  3039. }
  3040. else
  3041. {
  3042. if( reserved <= 1 ) makeroom(16);
  3043. data[size] = in[a];
  3044. size++;
  3045. reserved--;
  3046. data[size + 1] = 0;
  3047. }
  3048. }
  3049. data[size] = 0;
  3050. va_end(args);
  3051. return size;
  3052. }
  3053.  
  3054. #if defined SPwindow
  3055. const spchar &operator=(WCHAR *in)
  3056. {
  3057. if( this->size || this->reserved ) { delete [] this->data; }
  3058. this->size = 0;
  3059. this->reserved = 0;
  3060. int s;
  3061. for( s = 0; s < STR_SEARCH_LOOP_SIZE; s++ ) { if( !in[s] ) break; }
  3062. if( isbinary ) { this->makeroom(s); this->reserved -= s; }
  3063. else { this->makeroom(s); this->reserved -= s; this->data[s] = 0; }
  3064. for( int a = 0; a < s; a++ ) this->data[a] = (char)in[a];
  3065. this->size = s;
  3066. return (*this);
  3067. }
  3068.  
  3069. const spchar &operator+=(WCHAR *in)
  3070. {
  3071. int s = 0;
  3072. for( s = 0; s < STR_SEARCH_LOOP_SIZE; s++ ) { if( !in[s] ) break; }
  3073. char *p = new char[s+1];
  3074. p[s] = 0;
  3075. for( int a = 0; a < s; a++ ) p[a] = (char)in[a];
  3076. this->add(p, s);
  3077. return (*this);
  3078. }
  3079. #endif
  3080.  
  3081. void encrypt( char *password )
  3082. {
  3083. if(!size) return;
  3084. int outsize = 0;
  3085. unsigned char *p = str::encrypt( (unsigned char *)data, (unsigned int)size, password, &outsize);
  3086. delete [] data;
  3087. reserved = 0;
  3088. size = outsize;
  3089. data = (char *)p;
  3090. }
  3091.  
  3092. bool decrypt( char *password )
  3093. {
  3094. if(!size) return 0;
  3095. int outsize = 0; bool r = 0;
  3096. unsigned char *p;
  3097. r = str::decrypt( (unsigned char *)data, password, &p, &outsize);
  3098. if( r == 0 ) return 0;
  3099. delete [] data;
  3100. reserved = 0;
  3101. size = outsize;
  3102. data = (char *)p;
  3103. }
  3104.  
  3105. void readfile(char *filename)
  3106. {
  3107. FILE *f = fopen(filename, "rb");
  3108. if(size || reserved) delete [] data;
  3109. fseek(f, 0, SEEK_END);
  3110. size = ftell(f);
  3111. reserved = 0;
  3112. fseek(f, 0, SEEK_SET);
  3113. data = new char[size];
  3114. fread(data, 1, size, f);
  3115. fclose(f);
  3116. }
  3117.  
  3118. void writefile(char *filename)
  3119. {
  3120. if(!size) return;
  3121. FILE *f = fopen(filename, "wb");
  3122. fwrite(data, 1, size, f);
  3123. fclose(f);
  3124. }
  3125.  
  3126. void resize( int newsize )
  3127. {
  3128. if( newsize > size ) makeroom( size - newsize );
  3129. else
  3130. {
  3131. if( newsize == size ) return;
  3132. data[newsize] = 0;
  3133. reserved += size - newsize;
  3134. size = newsize;
  3135. }
  3136. }
  3137.  
  3138. const spchar &operator+=(int in)
  3139. {
  3140. this->makeroom(in);
  3141. return (*this);
  3142. }
  3143.  
  3144. const spchar &operator+=(char in)
  3145. {
  3146. this->add(&in, 1);
  3147. return (*this);
  3148. }
  3149.  
  3150. const spchar &operator-=(char *in)
  3151. {
  3152. int s = str::size(in);
  3153. int p = this->find(in, s);
  3154. if( p == -1 ) return (*this);
  3155. this->remove( p, p+s-1);
  3156. return (*this);
  3157. }
  3158.  
  3159. const spchar &operator-=(int in)
  3160. {
  3161. if( size - in < 0 ) in = size;
  3162. this->resize( size-in );
  3163. return (*this);
  3164. }
  3165.  
  3166. const spchar &operator=(int in)
  3167. {
  3168. this->resize(in);
  3169. return (*this);
  3170. }
  3171.  
  3172. const spchar &operator++(int )
  3173. {
  3174. this->makeroom(1);
  3175. return (*this);
  3176. }
  3177.  
  3178. const spchar &operator+=(char *in)
  3179. {
  3180. this->add(in);
  3181. return (*this);
  3182. }
  3183.  
  3184. const spchar &operator=(char *in)
  3185. {
  3186. this->set(in);
  3187. return (*this);
  3188. }
  3189.  
  3190. const spchar &operator=(const spchar &in)
  3191. {
  3192. if( this == &in ) return (*this);
  3193. if( this->size || this->reserved ) delete [] this ->data;
  3194. this->size = in.size;
  3195. this->reserved = in.reserved;
  3196. this->reserv_step = in.reserv_step;
  3197. this->fpos = in.fpos;
  3198. this->isbinary = in.isbinary;
  3199. int a;
  3200. if( in.size || in.reserved )
  3201. {
  3202. if( in.isbinary )
  3203. {
  3204. this->data = new char[in.size+in.reserved];
  3205. for( a = 0; a < in.size; a++ ) this->data[a] = in.data[a];
  3206. }
  3207. else
  3208. {
  3209. this->data = new char[in.size+in.reserved+1];
  3210. for( a = 0; a < in.size; a++ ) this->data[a] = in.data[a];
  3211. this->data[in.size] = 0;
  3212. }
  3213. }
  3214. return (*this);
  3215. }
  3216.  
  3217. spchar( spchar &in )
  3218. {
  3219. if( this == &in ) return;
  3220. this->size = in.size;
  3221. this->reserved = in.reserved;
  3222. this->reserv_step = in.reserv_step;
  3223. this->fpos = in.fpos;
  3224. this->isbinary = in.isbinary;
  3225. int a;
  3226. if( in.size || in.reserved )
  3227. {
  3228. if( in.isbinary )
  3229. {
  3230. this->data = new char[in.size+in.reserved];
  3231. for( a = 0; a < in.size; a++ ) this->data[a] = in.data[a];
  3232. }
  3233. else
  3234. {
  3235. this->data = new char[in.size+in.reserved+1];
  3236. for( a = 0; a < in.size; a++ ) this->data[a] = in.data[a];
  3237. this->data[in.size] = 0;
  3238. }
  3239. }
  3240. }
  3241. };
  3242. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement