11#include " wsjcpp_core.h"
2- #include < dirent.h>
2+
3+ #ifndef _MSC_VER
4+ #include < dirent.h>
5+ #include < sys/time.h>
6+ #include < unistd.h>
7+ #include < arpa/inet.h>
8+ #else
9+ #include < direct.h>
10+ #define PATH_MAX 256
11+ #endif
12+
13+ #include < filesystem>
314#include < sys/stat.h>
415#include < sys/types.h>
516#include < iostream>
617#include < sstream>
718#include < fstream>
8- #include < sys/time.h>
919#include < time.h>
1020#include < ctime>
1121#include < math.h>
1525#include < cstdlib>
1626#include < thread>
1727#include < cstdint>
18- #include < unistd.h>
1928#include < streambuf>
2029// #include <sys/socket.h>
21- #include < arpa/inet.h>
2230#include < random>
2331#include < iomanip>
2432
@@ -248,7 +256,6 @@ uint16_t WsjcppFilePermissions::toUInt16() const {
248256 return nRet;
249257}
250258
251-
252259// ---------------------------------------------------------------------
253260// WsjcppCore
254261
@@ -271,8 +278,8 @@ std::string WsjcppCore::doNormalizePath(const std::string & sPath) {
271278 // split path by /
272279 std::vector<std::string> vNames;
273280 std::string s = " " ;
274- int nStrLen = sPath .length ();
275- for (int i = 0 ; i < sPath .length (); i++) {
281+ size_t nStrLen = sPath .length ();
282+ for (size_t i = 0 ; i < sPath .length (); i++) {
276283 if (sPath [i] == ' /' ) {
277284 vNames.push_back (s);
278285 s = " " ;
@@ -288,9 +295,9 @@ std::string WsjcppCore::doNormalizePath(const std::string & sPath) {
288295 }
289296
290297 // fildered
291- int nLen = vNames.size ();
298+ size_t nLen = vNames.size ();
292299 std::vector<std::string> vNewNames;
293- for (int i = 0 ; i < nLen; i++) {
300+ for (size_t i = 0 ; i < nLen; i++) {
294301 std::string sCurrent = vNames[i];
295302 if (sCurrent == " " && i == nLen-1 ) {
296303 vNewNames.push_back (sCurrent );
@@ -316,9 +323,9 @@ std::string WsjcppCore::doNormalizePath(const std::string & sPath) {
316323 }
317324 }
318325 std::string sRet = " " ;
319- int nNewLen = vNewNames.size ();
320- int nLastNew = nNewLen-1 ;
321- for (int i = 0 ; i < nNewLen; i++) {
326+ size_t nNewLen = vNewNames.size ();
327+ size_t nLastNew = nNewLen-1 ;
328+ for (size_t i = 0 ; i < nNewLen; i++) {
322329 sRet += vNewNames[i];
323330 if (i != nLastNew) {
324331 sRet += " /" ;
@@ -333,8 +340,8 @@ std::string WsjcppCore::extractFilename(const std::string &sPath) {
333340 // split path by /
334341 std::vector<std::string> vNames;
335342 std::string s = " " ;
336- int nStrLen = sPath .length ();
337- for (int i = 0 ; i < sPath .length (); i++) {
343+ size_t nStrLen = sPath .length ();
344+ for (size_t i = 0 ; i < sPath .length (); i++) {
338345 if (sPath [i] == ' /' ) {
339346 vNames.push_back (s);
340347 s = " " ;
@@ -355,7 +362,11 @@ std::string WsjcppCore::extractFilename(const std::string &sPath) {
355362 return sRet ;
356363}
357364
358- // ---------------------------------------------------------------------
365+ std::string WsjcppCore::extractDirpath (const std::string &sFullPath ) {
366+ std::vector<std::string> vDirs = WsjcppCore::split (sFullPath , " /" );
367+ vDirs.pop_back ();
368+ return WsjcppCore::join (vDirs, " /" );
369+ }
359370
360371std::string WsjcppCore::getCurrentDirectory () {
361372 char cwd[PATH_MAX];
@@ -365,8 +376,6 @@ std::string WsjcppCore::getCurrentDirectory() {
365376 return std::string (cwd) + " /" ;
366377}
367378
368- // ---------------------------------------------------------------------
369-
370379long WsjcppCore::getCurrentTimeInMilliseconds () {
371380 long nTimeStart = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now ().time_since_epoch ()).count ();
372381 return nTimeStart;
@@ -416,8 +425,6 @@ std::string WsjcppCore::getThreadId() {
416425 return std::string (stream.str ());
417426}
418427
419- // ---------------------------------------------------------------------
420-
421428std::string WsjcppCore::formatTimeForWeb (long nTimeInSec) {
422429 std::time_t tm_ = long (nTimeInSec);
423430 // struct tm tstruct = *localtime(&tm_);
@@ -432,10 +439,6 @@ std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) {
432439 return std::string (buf);
433440}
434441
435-
436-
437- // ---------------------------------------------------------------------
438-
439442std::string WsjcppCore::formatTimeForFilename (long nTimeInSec) {
440443 std::time_t tm_ = long (nTimeInSec);
441444 // struct tm tstruct = *localtime(&tm_);
@@ -487,12 +490,6 @@ bool WsjcppCore::dirExists(const std::string &sDirname) {
487490 return false ;
488491}
489492
490- // ---------------------------------------------------------------------
491-
492- std::vector<std::string> WsjcppCore::listOfDirs (const std::string &sDirname ) {
493- WsjcppLog::warn (" listOfDirs" , " Deprecated. Use a WsjcppCore::getListOfDirs" );
494- return WsjcppCore::getListOfDirs (sDirname );
495- }
496493
497494// ---------------------------------------------------------------------
498495
@@ -501,51 +498,29 @@ std::vector<std::string> WsjcppCore::getListOfDirs(const std::string &sDirname)
501498 if (!WsjcppCore::dirExists (sDirname )) {
502499 return vDirs;
503500 }
504- DIR *dir = opendir (sDirname .c_str ());
505- if (dir != NULL ) {
506- struct dirent *entry = readdir (dir);
507- while (entry != NULL ) {
508- if (entry->d_type == DT_DIR) {
509- std::string sDir (entry->d_name );
510- if (sDir != " ." && sDir != " .." ) {
511- vDirs.push_back (sDir );
512- }
513- }
514- entry = readdir (dir);
501+ for (auto & entry : std::filesystem::directory_iterator (sDirname )) {
502+ if (entry.is_directory ()) {
503+ std::string sPath = entry.path ();
504+ sPath = sPath .substr (sDirname .length ()+1 );
505+ vDirs.push_back (sPath );
515506 }
516- closedir (dir);
517507 }
518508 std::sort (vDirs.begin (), vDirs.end ());
519509 return vDirs;
520510}
521511
522512// ---------------------------------------------------------------------
523513
524- std::vector<std::string> WsjcppCore::listOfFiles (const std::string &sDirname ) {
525- WsjcppLog::warn (" listOfFiles" , " Deprecated. Use a WsjcppCore::getListOfFiles" );
526- return WsjcppCore::getListOfFiles (sDirname );
527- }
528-
529- // ---------------------------------------------------------------------
530-
531514std::vector<std::string> WsjcppCore::getListOfFiles (const std::string &sDirname ) {
532515 std::vector<std::string> vFiles;
533516 if (!WsjcppCore::dirExists (sDirname )) {
534517 return vFiles;
535518 }
536- DIR *dir = opendir (sDirname .c_str ());
537- if (dir != NULL ) {
538- struct dirent *entry = readdir (dir);
539- while (entry != NULL ) {
540- if (entry->d_type != DT_DIR) {
541- std::string sDir (entry->d_name );
542- if (sDir != " ." && sDir != " .." ) {
543- vFiles.push_back (sDir );
544- }
545- }
546- entry = readdir (dir);
519+ for (auto & entry: std::filesystem::directory_iterator (sDirname )) {
520+ if (!entry.is_directory ()) {
521+ std::string sPath = entry.path ();
522+ vFiles.push_back (sPath );
547523 }
548- closedir (dir);
549524 }
550525 return vFiles;
551526}
@@ -554,6 +529,10 @@ std::vector<std::string> WsjcppCore::getListOfFiles(const std::string &sDirname)
554529
555530bool WsjcppCore::makeDir (const std::string &sDirname ) {
556531 struct stat st;
532+
533+ const std::filesystem::path dir{sDirname };
534+ std::filesystem::create_directory (dir);
535+
557536 int nStatus = mkdir (sDirname .c_str (), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
558537 if (nStatus == 0 ) {
559538 return true ;
@@ -566,7 +545,25 @@ bool WsjcppCore::makeDir(const std::string &sDirname) {
566545 return true ;
567546}
568547
569- // ---------------------------------------------------------------------
548+ bool WsjcppCore::makeDirsPath (const std::string &sDirname ) {
549+ std::string sDirpath = WsjcppCore::doNormalizePath (sDirname );
550+ std::vector<std::string> vDirs = WsjcppCore::split (sDirpath , " /" );
551+ std::string sDirpath2 = " " ;
552+ for (int i = 0 ; i < vDirs.size (); i++) {
553+ if (vDirs[i] == " " ) {
554+ continue ;
555+ }
556+ sDirpath2 += vDirs[i] + " /" ;
557+ if (WsjcppCore::dirExists (sDirpath2 )) {
558+ continue ;
559+ } else {
560+ if (!WsjcppCore::makeDir (sDirpath2 )) {
561+ return false ;
562+ }
563+ }
564+ }
565+ return true ;
566+ }
570567
571568bool WsjcppCore::writeFile (const std::string &sFilename , const std::string &sContent ) {
572569
@@ -744,9 +741,9 @@ void WsjcppCore::replaceAll(std::string& str, const std::string& sFrom, const st
744741
745742std::vector<std::string> WsjcppCore::split (const std::string& sWhat , const std::string& sDelim ) {
746743 std::vector<std::string> vRet;
747- int nPos = 0 ;
748- int nLen = sWhat .length ();
749- int nDelimLen = sDelim .length ();
744+ size_t nPos = 0 ;
745+ size_t nLen = sWhat .length ();
746+ size_t nDelimLen = sDelim .length ();
750747 while (nPos < nLen) {
751748 std::size_t nFoundPos = sWhat .find (sDelim , nPos);
752749 if (nFoundPos != std::string::npos) {
@@ -781,7 +778,8 @@ std::string WsjcppCore::join(const std::vector<std::string> &vWhat, const std::s
781778// ---------------------------------------------------------------------
782779
783780void WsjcppCore::initRandom () {
784- std::srand (std::time (0 ));
781+ time_t t = std::time (0 );
782+ std::srand ((unsigned int )t);
785783}
786784
787785// ---------------------------------------------------------------------
@@ -828,7 +826,7 @@ std::string WsjcppCore::getPointerAsHex(void *p) {
828826
829827std::string WsjcppCore::extractURLProtocol (const std::string& sValue ) {
830828 std::string sRet = " " ;
831- int nPosProtocol = sValue .find (" ://" );
829+ size_t nPosProtocol = sValue .find (" ://" );
832830 if (nPosProtocol == std::string::npos) {
833831 return sRet ;
834832 }
@@ -992,7 +990,7 @@ bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
992990
993991bool WsjcppCore::setFilePermissions (const std::string& sFilePath , const WsjcppFilePermissions &filePermissions, std::string& sError ) {
994992
995- mode_t m;
993+ mode_t m = 0x0 ;
996994
997995 // owner
998996 m |= filePermissions.getOwnerReadFlag () ? S_IRUSR : 0x0 ;
@@ -1055,21 +1053,21 @@ bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePerm
10551053
10561054// ---------------------------------------------------------------------
10571055
1058- std::string WsjcppCore::doPadLeft (const std::string& sIn , char cWhat, int nLength) {
1056+ std::string WsjcppCore::doPadLeft (const std::string& sIn , char cWhat, size_t nLength) {
10591057 std::string sRet ;
1060- int nPadLen = nLength - sIn .length ();
1061- for (int i = 0 ; i < nPadLen; i++) {
1058+ size_t nPadLen = nLength - sIn .length ();
1059+ for (size_t i = 0 ; i < nPadLen; i++) {
10621060 sRet += cWhat;
10631061 }
10641062 return sRet + sIn ;
10651063}
10661064
10671065// ---------------------------------------------------------------------
10681066
1069- std::string WsjcppCore::doPadRight (const std::string& sIn , char cWhat, int nLength) {
1067+ std::string WsjcppCore::doPadRight (const std::string& sIn , char cWhat, size_t nLength) {
10701068 std::string sRet ;
1071- int nPadLen = nLength - sIn .length ();
1072- for (int i = 0 ; i < nPadLen; i++) {
1069+ size_t nPadLen = nLength - sIn .length ();
1070+ for (size_t i = 0 ; i < nPadLen; i++) {
10731071 sRet += cWhat;
10741072 }
10751073 return sIn + sRet ;
@@ -1220,9 +1218,6 @@ WsjcppResourceFile::WsjcppResourceFile() {
12201218 WsjcppResourcesManager::add (this );
12211219}
12221220
1223- // ---------------------------------------------------------------------
1224-
1225-
12261221// ---------------------------------------------------------------------
12271222// WsjcppResourcesManager
12281223
0 commit comments