1616#include < cstdint>
1717#include < unistd.h>
1818#include < streambuf>
19+ #include < sys/types.h>
20+ #include < sys/socket.h>
21+ #include < arpa/inet.h>
1922
2023// ---------------------------------------------------------------------
2124
@@ -96,6 +99,34 @@ std::string WSJCppCore::doNormalizePath(const std::string & sPath) {
9699
97100// ---------------------------------------------------------------------
98101
102+ std::string WSJCppCore::extractFilename (const std::string &sPath ) {
103+ // split path by /
104+ std::vector<std::string> vNames;
105+ std::string s = " " ;
106+ int nStrLen = sPath .length ();
107+ for (int i = 0 ; i < sPath .length (); i++) {
108+ if (sPath [i] == ' /' ) {
109+ vNames.push_back (s);
110+ s = " " ;
111+ if (i == nStrLen-1 ) {
112+ vNames.push_back (" " );
113+ }
114+ } else {
115+ s += sPath [i];
116+ }
117+ }
118+ if (s != " " ) {
119+ vNames.push_back (s);
120+ }
121+ std::string sRet ;
122+ if (vNames.size () > 0 ) {
123+ sRet = vNames[vNames.size ()-1 ];
124+ }
125+ return sRet ;
126+ }
127+
128+ // ---------------------------------------------------------------------
129+
99130std::string WSJCppCore::getCurrentDirectory () {
100131 char cwd[PATH_MAX ];
101132 if (getcwd (cwd, sizeof (cwd)) == NULL ) {
@@ -275,20 +306,20 @@ bool WSJCppCore::makeDir(const std::string &sDirname) {
275306 std::cout << " FAILED create folder " << sDirname << std::endl;
276307 return false ;
277308 }
278- std::cout << " nStatus: " << nStatus << std::endl;
309+ // std::cout << "nStatus: " << nStatus << std::endl;
279310 return true ;
280311}
281312
282313// ---------------------------------------------------------------------
283314
284315bool WSJCppCore::writeFile (const std::string &sFilename , const std::string &sContent ) {
285316
286- std::ofstream f (sFilename , std::ifstream::in);
317+ // std::ofstream f(sFilename, std::ifstream::in);
318+ std::ofstream f (sFilename , std::ios::out);
287319 if (!f) {
288- std::cout << " FAILED could not create file to wtite " << sFilename << std::endl ;
320+ WSJCppLog::err ( " WSJCppCore " , " Could not create file to write ' " + sFilename + " ' " ) ;
289321 return false ;
290322 }
291-
292323 f << sContent << std::endl;
293324 f.close ();
294325 return true ;
@@ -325,7 +356,11 @@ bool WSJCppCore::writeFile(const std::string &sFilename, const char *pBuffer, co
325356 return true ;
326357}
327358
359+ // ---------------------------------------------------------------------
328360
361+ bool WSJCppCore::removeFile (const std::string &sFilename ) {
362+ return remove (sFilename .c_str ()) == 0 ;
363+ }
329364
330365// ---------------------------------------------------------------------
331366
@@ -354,6 +389,14 @@ std::string& WSJCppCore::to_lower(std::string& str) {
354389 return str;
355390}
356391
392+ // ---------------------------------------------------------------------
393+ // will worked only with latin
394+
395+ std::string WSJCppCore::toUpper (const std::string& str) {
396+ std::string sRet = str;
397+ std::transform (sRet .begin (), sRet .end (), sRet .begin (), ::toupper);
398+ return sRet ;
399+ }
357400
358401// ---------------------------------------------------------------------
359402
@@ -376,6 +419,44 @@ std::string WSJCppCore::createUuid() {
376419 return sRet ;
377420}
378421
422+ // ---------------------------------------------------------------------
423+
424+ bool WSJCppCore::isIPv4 (const std::string& str) {
425+ int n = 0 ;
426+ std::string s[4 ] = {" " , " " , " " , " " };
427+ for (int i = 0 ; i < str.length (); i++) {
428+ char c = str[i];
429+ if (n > 3 ) {
430+ return false ;
431+ }
432+ if (c >= ' 0' && c <= ' 9' ) {
433+ s[n] += c;
434+ } else if (c == ' .' ) {
435+ n++;
436+ } else {
437+ return false ;
438+ }
439+ }
440+ for (int i = 0 ; i < 4 ; i++) {
441+ if (s[i].length () > 3 ) {
442+ return false ;
443+ }
444+ int p = std::stoi (s[i]);
445+ if (p > 255 || p < 0 ) {
446+ return false ;
447+ }
448+ }
449+ return true ;
450+ }
451+
452+ // ---------------------------------------------------------------------
453+
454+ bool WSJCppCore::isIPv6 (const std::string& str) {
455+ unsigned char buf[sizeof (struct in6_addr )];
456+ bool isValid = inet_pton (AF_INET6 , str.c_str (), buf);
457+ return isValid;
458+ }
459+
379460// ---------------------------------------------------------------------
380461// WSJCppLog
381462
@@ -492,3 +573,5 @@ void WSJCppLog::add(WSJCppColorModifier &clr, const std::string &sType, const st
492573 logFile << sLogMessage << std::endl;
493574 logFile.close ();
494575}
576+
577+
0 commit comments