16
16
#include < cstdint>
17
17
#include < unistd.h>
18
18
#include < streambuf>
19
+ #include < sys/types.h>
20
+ #include < sys/socket.h>
21
+ #include < arpa/inet.h>
19
22
20
23
// ---------------------------------------------------------------------
21
24
@@ -96,6 +99,34 @@ std::string WSJCppCore::doNormalizePath(const std::string & sPath) {
96
99
97
100
// ---------------------------------------------------------------------
98
101
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
+
99
130
std::string WSJCppCore::getCurrentDirectory () {
100
131
char cwd[PATH_MAX];
101
132
if (getcwd (cwd, sizeof (cwd)) == NULL ) {
@@ -275,20 +306,20 @@ bool WSJCppCore::makeDir(const std::string &sDirname) {
275
306
std::cout << " FAILED create folder " << sDirname << std::endl;
276
307
return false ;
277
308
}
278
- std::cout << " nStatus: " << nStatus << std::endl;
309
+ // std::cout << "nStatus: " << nStatus << std::endl;
279
310
return true ;
280
311
}
281
312
282
313
// ---------------------------------------------------------------------
283
314
284
315
bool WSJCppCore::writeFile (const std::string &sFilename , const std::string &sContent ) {
285
316
286
- std::ofstream f (sFilename , std::ifstream::in);
317
+ // std::ofstream f(sFilename, std::ifstream::in);
318
+ std::ofstream f (sFilename , std::ios::out);
287
319
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 + " ' " ) ;
289
321
return false ;
290
322
}
291
-
292
323
f << sContent << std::endl;
293
324
f.close ();
294
325
return true ;
@@ -325,7 +356,11 @@ bool WSJCppCore::writeFile(const std::string &sFilename, const char *pBuffer, co
325
356
return true ;
326
357
}
327
358
359
+ // ---------------------------------------------------------------------
328
360
361
+ bool WSJCppCore::removeFile (const std::string &sFilename ) {
362
+ return remove (sFilename .c_str ()) == 0 ;
363
+ }
329
364
330
365
// ---------------------------------------------------------------------
331
366
@@ -354,6 +389,14 @@ std::string& WSJCppCore::to_lower(std::string& str) {
354
389
return str;
355
390
}
356
391
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
+ }
357
400
358
401
// ---------------------------------------------------------------------
359
402
@@ -376,6 +419,44 @@ std::string WSJCppCore::createUuid() {
376
419
return sRet ;
377
420
}
378
421
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
+
379
460
// ---------------------------------------------------------------------
380
461
// WSJCppLog
381
462
@@ -492,3 +573,5 @@ void WSJCppLog::add(WSJCppColorModifier &clr, const std::string &sType, const st
492
573
logFile << sLogMessage << std::endl;
493
574
logFile.close ();
494
575
}
576
+
577
+
0 commit comments