@@ -364,6 +364,21 @@ bool WSJCppCore::removeFile(const std::string &sFilename) {
364364
365365// ---------------------------------------------------------------------
366366
367+ bool WSJCppCore::createEmptyFile (const std::string &sFilename ) {
368+ if (WSJCppCore::fileExists (sFilename )) {
369+ return false ;
370+ }
371+ std::ofstream f (sFilename , std::ios::out | std::ios::binary);
372+ if (!f) {
373+ std::cout << " FAILED could not create file to wtite " << sFilename << std::endl;
374+ return false ;
375+ }
376+ f.close ();
377+ return true ;
378+ }
379+
380+ // ---------------------------------------------------------------------
381+
367382std::string& WSJCppCore::ltrim (std::string& str, const std::string& chars) {
368383 str.erase (0 , str.find_first_not_of (chars));
369384 return str;
@@ -383,10 +398,12 @@ std::string& WSJCppCore::trim(std::string& str, const std::string& chars) {
383398}
384399
385400// ---------------------------------------------------------------------
401+ // will worked only with latin
386402
387- std::string& WSJCppCore::to_lower (std::string& str) {
388- std::transform (str.begin (), str.end (), str.begin (), ::tolower);
389- return str;
403+ std::string WSJCppCore::toLower (const std::string& str) {
404+ std::string sRet = str;
405+ std::transform (sRet .begin (), sRet .end (), sRet .begin (), ::tolower);
406+ return sRet ;
390407}
391408
392409// ---------------------------------------------------------------------
@@ -400,6 +417,44 @@ std::string WSJCppCore::toUpper(const std::string& str) {
400417
401418// ---------------------------------------------------------------------
402419
420+ void WSJCppCore::replaceAll (std::string& str, const std::string& sFrom , const std::string& sTo ) {
421+ if (sFrom .empty ()) {
422+ return ;
423+ }
424+ size_t start_pos = 0 ;
425+ while ((start_pos = str.find (sFrom , start_pos)) != std::string::npos) {
426+ str.replace (start_pos, sFrom .length (), sTo );
427+ start_pos += sTo .length (); // In case 'to' contains 'sFrom', like replacing 'x' with 'yx'
428+ }
429+ }
430+
431+ // ---------------------------------------------------------------------
432+
433+ std::vector<std::string> WSJCppCore::split (const std::string& sWhat , const std::string& sDelim ) {
434+ std::vector<std::string> vRet;
435+ int nPos = 0 ;
436+ int nLen = sWhat .length ();
437+ int nDelimLen = sDelim .length ();
438+ while (nPos < nLen) {
439+ std::size_t nFoundPos = sWhat .find (sDelim , nPos);
440+ if (nFoundPos != std::string::npos) {
441+ std::string sToken = sWhat .substr (nPos, nFoundPos - nPos);
442+ vRet.push_back (sToken );
443+ nPos = nFoundPos + nDelimLen;
444+ if (nFoundPos + nDelimLen == nLen) { // last delimiter
445+ vRet.push_back (" " );
446+ }
447+ } else {
448+ std::string sToken = sWhat .substr (nPos, nLen - nPos);
449+ vRet.push_back (sToken );
450+ break ;
451+ }
452+ }
453+ return vRet;
454+ }
455+
456+ // ---------------------------------------------------------------------
457+
403458void WSJCppCore::initRandom () {
404459 std::srand (std::rand () + std::time (0 ));
405460}
@@ -421,9 +476,20 @@ std::string WSJCppCore::createUuid() {
421476
422477// ---------------------------------------------------------------------
423478
479+ std::string WSJCppCore::uint2hexString (unsigned int n) {
480+ std::string sRet ;
481+ for (int i = 0 ; i < 8 ; i++) {
482+ sRet += " 0123456789abcdef" [n % 16 ];
483+ n >>= 4 ;
484+ }
485+ return std::string (sRet .rbegin (), sRet .rend ());
486+ }
487+
488+ // ---------------------------------------------------------------------
489+
424490unsigned long WSJCppCore::convertVoidToULong (void *p) {
425- unsigned long ret = *( unsigned long *)p ;
426- return ret;
491+ std:: uintptr_t ret = reinterpret_cast <std:: uintptr_t >(p) ;
492+ return ( unsigned long ) ret;
427493}
428494
429495// ---------------------------------------------------------------------
@@ -447,6 +513,61 @@ std::string WSJCppCore::extractURLProtocol(const std::string& sValue) {
447513 return sRet ;
448514}
449515
516+ // ---------------------------------------------------------------------
517+
518+ bool WSJCppCore::getEnv (const std::string& sName , std::string& sValue ) {
519+ if (const char * env_p = std::getenv (sName .c_str ())) {
520+ sValue = std::string (env_p);
521+ return true ;
522+ }
523+ return false ;
524+ }
525+
526+ // ---------------------------------------------------------------------
527+
528+ std::string WSJCppCore::encodeUriComponent (const std::string& sValue ) {
529+ std::stringstream ssRet;
530+ for (int i = 0 ; i < sValue .length (); i++) {
531+ char c = sValue [i];
532+ if (
533+ c == ' -' || c == ' _' || c == ' .' || c == ' !'
534+ || c == ' ~' || c == ' *' || c == ' \' '
535+ || c == ' (' || c == ' )' || (c >= ' 0' && c <= ' 9' )
536+ || (c >= ' a' && c <= ' z' ) || (c >= ' A' && c <= ' Z' )
537+ ) {
538+ ssRet << c;
539+ } else {
540+ ssRet << ' %' << std::uppercase << std::hex << (int )c;
541+ }
542+ }
543+ return ssRet.str ();
544+ }
545+
546+ // ---------------------------------------------------------------------
547+
548+ std::string WSJCppCore::decodeUriComponent (const std::string& sValue ) {
549+ std::string sRet = " " ;
550+ std::string sHex = " " ;
551+ int nLen = sValue .length ();
552+ for (int i = 0 ; i < sValue .length (); i++) {
553+ char c = sValue [i];
554+ if (c == ' %' ) {
555+ if (i+2 >= nLen) {
556+ WSJCppLog::throw_err (" WSJCppCore::decodeUriElement" , " Wrong format of string" );
557+ }
558+ sHex = " 0x" ;
559+ sHex += sValue [i+1 ];
560+ sHex += sValue [i+2 ];
561+ i = i + 2 ;
562+ char c1 = std::stoul (sHex , nullptr , 16 );
563+ sRet += c1;
564+ } else {
565+ sRet += c;
566+ }
567+ }
568+ return sRet ;
569+ }
570+
450571// ---------------------------------------------------------------------
451572// WSJCppLog
452573
0 commit comments