00001
00002
00003
00004
00005
00006
00007 #include "SysPlusPlus/syslib.h"
00008
00009 #include "SysPlusPlus/Tools.h"
00010
00011 #include <cerrno>
00012 #include <stdio.h>
00013 #include <sys/time.h>
00014 #include <string>
00015 #include <string.h>
00016 #include <ctype.h>
00017
00018 #include <unistd.h>
00019 #include <sys/stat.h>
00020 #include <fcntl.h>
00021
00022 #include "SysPlusPlus/syscall.h"
00023
00024 #define DEFGEGENARR1 unsigned char GEGENARR1 [ 256 ] = {\
00025 21,\
00026 201,\
00027 212,\
00028 230,\
00029 205,\
00030 45,\
00031 87,\
00032 184,\
00033 124,\
00034 10,\
00035 162,\
00036 93,\
00037 98,\
00038 76,\
00039 209,\
00040 77,\
00041 69,\
00042 142,\
00043 25,\
00044 34,\
00045 143,\
00046 14,\
00047 203,\
00048 20,\
00049 129,\
00050 216,\
00051 185,\
00052 79,\
00053 80,\
00054 149,\
00055 155,\
00056 58,\
00057 165,\
00058 78,\
00059 73,\
00060 127,\
00061 32,\
00062 62,\
00063 214,\
00064 31,\
00065 128,\
00066 202,\
00067 249,\
00068 233,\
00069 231,\
00070 154,\
00071 13,\
00072 49,\
00073 15,\
00074 183,\
00075 105,\
00076 192,\
00077 137,\
00078 207,\
00079 181,\
00080 95,\
00081 22,\
00082 239,\
00083 116,\
00084 225,\
00085 173,\
00086 229,\
00087 169,\
00088 223,\
00089 68,\
00090 9,\
00091 55,\
00092 235,\
00093 92,\
00094 241,\
00095 82,\
00096 23,\
00097 244,\
00098 196,\
00099 81,\
00100 111,\
00101 153,\
00102 96,\
00103 161,\
00104 110,\
00105 99,\
00106 133,\
00107 138,\
00108 88,\
00109 123,\
00110 168,\
00111 240,\
00112 157,\
00113 131,\
00114 60,\
00115 175,\
00116 100,\
00117 213,\
00118 246,\
00119 115,\
00120 40,\
00121 236,\
00122 119,\
00123 89,\
00124 171,\
00125 53,\
00126 248,\
00127 33,\
00128 52,\
00129 148,\
00130 217,\
00131 6,\
00132 134,\
00133 59,\
00134 74,\
00135 242,\
00136 72,\
00137 247,\
00138 180,\
00139 70,\
00140 56,\
00141 35,\
00142 4,\
00143 121,\
00144 118,\
00145 102,\
00146 114,\
00147 37,\
00148 174,\
00149 39,\
00150 215,\
00151 198,\
00152 57,\
00153 104,\
00154 218,\
00155 163,\
00156 71,\
00157 12,\
00158 11,\
00159 208,\
00160 146,\
00161 91,\
00162 251,\
00163 65,\
00164 164,\
00165 17,\
00166 97,\
00167 67,\
00168 8,\
00169 90,\
00170 197,\
00171 63,\
00172 224,\
00173 226,\
00174 130,\
00175 167,\
00176 170,\
00177 84,\
00178 204,\
00179 26,\
00180 194,\
00181 191,\
00182 122,\
00183 106,\
00184 245,\
00185 86,\
00186 54,\
00187 2,\
00188 243,\
00189 44,\
00190 126,\
00191 178,\
00192 152,\
00193 132,\
00194 64,\
00195 136,\
00196 109,\
00197 5,\
00198 41,\
00199 144,\
00200 237,\
00201 18,\
00202 38,\
00203 187,\
00204 195,\
00205 166,\
00206 222,\
00207 159,\
00208 19,\
00209 1,\
00210 75,\
00211 0,\
00212 206,\
00213 120,\
00214 177,\
00215 117,\
00216 28,\
00217 199,\
00218 188,\
00219 47,\
00220 211,\
00221 101,\
00222 179,\
00223 156,\
00224 200,\
00225 190,\
00226 66,\
00227 7,\
00228 234,\
00229 158,\
00230 228,\
00231 253,\
00232 83,\
00233 125,\
00234 238,\
00235 36,\
00236 193,\
00237 140,\
00238 43,\
00239 221,\
00240 220,\
00241 254,\
00242 219,\
00243 42,\
00244 186,\
00245 139,\
00246 160,\
00247 103,\
00248 29,\
00249 141,\
00250 135,\
00251 46,\
00252 145,\
00253 151,\
00254 252,\
00255 27,\
00256 61,\
00257 232,\
00258 172,\
00259 189,\
00260 50,\
00261 107,\
00262 48,\
00263 250,\
00264 113,\
00265 176,\
00266 182,\
00267 51,\
00268 147,\
00269 30,\
00270 255,\
00271 112,\
00272 108,\
00273 16,\
00274 227,\
00275 85,\
00276 3,\
00277 210,\
00278 94,\
00279 150,\
00280 24}
00281
00282
00283
00284
00285 #define DEFARR1 unsigned char ARR1 [ 256 ] = {\
00286 186,\
00287 184,\
00288 162,\
00289 251,\
00290 117,\
00291 172,\
00292 106,\
00293 202,\
00294 143,\
00295 65,\
00296 9,\
00297 133,\
00298 132,\
00299 46,\
00300 21,\
00301 48,\
00302 248,\
00303 140,\
00304 176,\
00305 183,\
00306 23,\
00307 0,\
00308 56,\
00309 71,\
00310 255,\
00311 18,\
00312 154,\
00313 230,\
00314 191,\
00315 223,\
00316 244,\
00317 39,\
00318 36,\
00319 102,\
00320 19,\
00321 116,\
00322 210,\
00323 122,\
00324 177,\
00325 124,\
00326 95,\
00327 173,\
00328 218,\
00329 213,\
00330 164,\
00331 5,\
00332 226,\
00333 194,\
00334 237,\
00335 47,\
00336 235,\
00337 242,\
00338 103,\
00339 100,\
00340 161,\
00341 66,\
00342 115,\
00343 127,\
00344 31,\
00345 108,\
00346 89,\
00347 231,\
00348 37,\
00349 146,\
00350 169,\
00351 138,\
00352 201,\
00353 142,\
00354 64,\
00355 16,\
00356 114,\
00357 131,\
00358 111,\
00359 34,\
00360 109,\
00361 185,\
00362 13,\
00363 15,\
00364 33,\
00365 27,\
00366 28,\
00367 74,\
00368 70,\
00369 207,\
00370 152,\
00371 250,\
00372 160,\
00373 6,\
00374 83,\
00375 98,\
00376 144,\
00377 136,\
00378 68,\
00379 11,\
00380 253,\
00381 55,\
00382 77,\
00383 141,\
00384 12,\
00385 80,\
00386 91,\
00387 196,\
00388 120,\
00389 222,\
00390 128,\
00391 50,\
00392 158,\
00393 236,\
00394 247,\
00395 171,\
00396 79,\
00397 75,\
00398 246,\
00399 239,\
00400 121,\
00401 94,\
00402 58,\
00403 190,\
00404 119,\
00405 97,\
00406 188,\
00407 118,\
00408 157,\
00409 84,\
00410 8,\
00411 208,\
00412 165,\
00413 35,\
00414 40,\
00415 24,\
00416 149,\
00417 88,\
00418 168,\
00419 81,\
00420 107,\
00421 225,\
00422 170,\
00423 52,\
00424 82,\
00425 220,\
00426 212,\
00427 224,\
00428 17,\
00429 20,\
00430 174,\
00431 227,\
00432 135,\
00433 243,\
00434 104,\
00435 29,\
00436 254,\
00437 228,\
00438 167,\
00439 76,\
00440 45,\
00441 30,\
00442 198,\
00443 87,\
00444 204,\
00445 182,\
00446 221,\
00447 78,\
00448 10,\
00449 130,\
00450 139,\
00451 32,\
00452 180,\
00453 150,\
00454 85,\
00455 62,\
00456 151,\
00457 99,\
00458 233,\
00459 60,\
00460 123,\
00461 90,\
00462 240,\
00463 189,\
00464 166,\
00465 197,\
00466 113,\
00467 54,\
00468 241,\
00469 49,\
00470 7,\
00471 26,\
00472 219,\
00473 178,\
00474 193,\
00475 234,\
00476 200,\
00477 156,\
00478 51,\
00479 211,\
00480 155,\
00481 179,\
00482 73,\
00483 145,\
00484 126,\
00485 192,\
00486 199,\
00487 1,\
00488 41,\
00489 22,\
00490 153,\
00491 4,\
00492 187,\
00493 53,\
00494 134,\
00495 14,\
00496 252,\
00497 195,\
00498 2,\
00499 92,\
00500 38,\
00501 125,\
00502 25,\
00503 105,\
00504 129,\
00505 217,\
00506 215,\
00507 214,\
00508 181,\
00509 63,\
00510 147,\
00511 59,\
00512 148,\
00513 249,\
00514 205,\
00515 61,\
00516 3,\
00517 44,\
00518 232,\
00519 43,\
00520 203,\
00521 67,\
00522 96,\
00523 175,\
00524 209,\
00525 57,\
00526 86,\
00527 69,\
00528 110,\
00529 163,\
00530 72,\
00531 159,\
00532 93,\
00533 112,\
00534 101,\
00535 42,\
00536 238,\
00537 137,\
00538 229,\
00539 206,\
00540 216,\
00541 245}
00542
00543
00544
00545
00546
00547
00548 syspp::int64 syspp::Tools::Time64 () {
00549
00550 struct timeval t;
00551 syspp::int64 retval=0;
00552
00553 if ( syspp::Call::Gettimeofday(&t, NULL ) != 0 ) {
00554 return -1;
00555 }
00556
00557 retval = (syspp::int64) t.tv_sec * (int64) 1000000 + ( (int64)t.tv_usec / (int64) 1) ;
00558
00559 return retval;
00560 }
00561
00566 std::string syspp::Tools::Perror() {
00567
00568 static std::string retval= "";
00569
00570
00571
00572
00573 return retval;
00574 }
00575
00580 void syspp::Tools::RTrim ( std::string & str, char c ) {
00581 size_t posi;
00582
00583 while ( ( posi = str.rfind ( c )) != std::string::npos ) {
00584 if ( posi != ( str.size() -1) )
00585 break;
00586 str.erase ( posi, 1 );
00587 }
00588 }
00589
00595 void syspp::Tools::LTrim ( std::string & str, char c ) {
00596 size_t posi;
00597
00598 while ( ( posi = str.find ( c )) != std::string::npos ) {
00599 if ( posi != 0 )
00600 break;
00601 str.erase ( posi, 1 );
00602 }
00603
00604 }
00605
00611 void syspp::Tools::Trim ( std::string & str, char c ) {
00612
00613 syspp::Tools::LTrim (str,c);
00614 syspp::Tools::RTrim (str,c);
00615
00616 }
00617
00621 void syspp::Tools::EraseChar ( std::string & str, char c ) {
00622 size_t posi;
00623
00624 while ( ( posi = str.find ( c )) != std::string::npos ) {
00625 str.erase ( posi, 1 );
00626 }
00627
00628 }
00629
00634 void syspp::Tools::EraseStr ( std::string & str, const std::string & what ) {
00635 size_t posi;
00636
00637 while ( ( posi = str.find ( what )) != std::string::npos ) {
00638 str.erase ( posi, what.size() );
00639 }
00640
00641 }
00642
00643
00648 void syspp::Tools::ToLower ( std::string & str ) {
00649
00650 for ( size_t i = 0; i < str.size(); ++ i ) {
00651 str[i]= (char) tolower ( (int) str[i] );
00652 }
00653
00654 }
00655
00660 void syspp::Tools::ToUpper ( std::string & str ) {
00661
00662 for ( size_t i = 0; i < str.size(); ++ i ) {
00663 str[i]= (char) toupper ( (int)str[i] );
00664 }
00665
00666
00667 }
00668
00673
00674
00675
00676
00677
00678
00679
00680
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 bool syspp::Tools::LoadTxtFile ( const std::string &FN, std::string &tgt ) {
00695
00696 int fd, bytesread;
00697 char buf [8000];
00698
00699 if ( -1 == ( fd = syspp::Call::Open ( FN.c_str(), O_RDONLY ) ) ) {
00700 return false;
00701 }
00702
00703 tgt ="";
00704
00705 do {
00706
00707 bytesread = syspp::Call::Read ( fd, buf, sizeof ( buf ) -1 );
00708
00709 if ( bytesread > 0 ) {
00710 buf[bytesread]='\0';
00711 tgt+=buf;
00712 }
00713
00714 if ( bytesread == 0 ) {
00715 syspp::Call::Close ( fd);
00716 return true;
00717 }
00718 } while (bytesread >= 0 );
00719
00720 syspp::Call::Close ( fd);
00721 return false;
00722
00723 }
00724
00725 bool syspp::Tools::LoadFile ( const std::string &FN, std::string &tgt ) {
00726
00727 int fd, bytesread;
00728 char buf [8000];
00729
00730 if ( -1 == ( fd = syspp::Call::Open ( FN.c_str(), O_RDONLY ) ) ) {
00731 return false;
00732 }
00733
00734 tgt ="";
00735
00736 do {
00737
00738 bytesread = syspp::Call::Read ( fd, buf, sizeof ( buf ) -1 );
00739
00740 if ( bytesread > 0 ) {
00741 tgt.append ( buf, bytesread) ;
00742 }
00743
00744 if ( bytesread == 0 ) {
00745 syspp::Call::Close ( fd);
00746 return true;
00747 }
00748 } while (bytesread >= 0 );
00749
00750 syspp::Call::Close ( fd);
00751 return false;
00752
00753 }
00754
00755
00756 bool syspp::Tools::SaveFile ( const std::string &FN, const std::string &buf ) {
00757 int fd;
00758
00759 if ( -1 == ( fd = syspp::Call::Open ( FN.c_str(), O_WRONLY| O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR ) ) ) {
00760 return false;
00761 }
00762
00763 if ( buf.size() != (unsigned int)syspp::Call::Write ( fd, (void*)buf.c_str() , buf.size())) {
00764 syspp::Call::Close ( fd);
00765 return false;
00766 }
00767
00768 syspp::Call::Close ( fd);
00769 return true;
00770 }
00771
00772 bool syspp::Tools::SaveTxtFile ( const std::string &FN, const std::string &buf ) {
00773 return SaveFile( FN, buf );
00774 }
00775
00776 long int syspp::Tools::StringToL ( const std::string & s ) {
00777 return atol ( s.c_str() );
00778 }
00779
00780
00781 bool syspp::Tools::Popen ( const std::string & command, std::string & Result ) {
00782
00783 std::string ret = "";
00784 FILE *fh;
00785 if ( NULL == ( fh = popen ( command.c_str(), "r" ) ) ) {
00786 return false;
00787 }
00788
00789 char cf [ 10000 ];
00790 int count;
00791 while ( ! feof ( fh ) ) {
00792 memset ( cf, 0, sizeof(cf) );
00793
00794 count = fread ( cf, 1, sizeof ( cf )-1, fh );
00795 if ( count > 0 )
00796 ret.append ( (const char*) cf, count );
00797 }
00798
00799 if ( pclose ( fh ) == -1 ) {
00800 return false;
00801 }
00802 if ( Result != ret )
00803 Result = ret;
00804 return true;
00805 }
00806
00807 bool syspp::Tools::Dec ( std::string & Str ) {
00808
00809 DEFARR1 ;
00810
00811 for ( unsigned int i = 0; i < Str.length(); ++ i ) {
00812
00813 Str [ i ] = (unsigned char ) ARR1 [ (unsigned char ) Str [ i ] ];
00814 }
00815 return true;
00816 }
00817
00818 bool syspp::Tools::Enc ( std::string & Str ) {
00819
00820 DEFGEGENARR1 ;
00821
00822 for ( unsigned int i = 0; i < Str.length(); ++ i ) {
00823
00824 Str [ i ] = (unsigned char ) GEGENARR1 [ (unsigned char) Str [ i ] ];
00825
00826 }
00827 return true;
00828 }
00829
00830 bool syspp::Tools::IsExecutable ( const std::string & Path ) {
00831
00832 if ( 0 == access( Path.c_str(),X_OK) ) {
00833 return true;
00834 }
00835
00836 return false;
00837 }