Skip to content

Commit 327de05

Browse files
committed
Up to c++17 && redesign getListOfDisrs && fixed setFilePermissions
1 parent 7168de9 commit 327de05

File tree

5 files changed

+52
-71
lines changed

5 files changed

+52
-71
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/src.wsjcpp/CMakeLists.txt)
77
#### BEGIN_WSJCPP_APPEND
88
#### END_WSJCPP_APPEND
99

10-
set(CMAKE_CXX_STANDARD 11)
10+
set(CMAKE_CXX_STANDARD 17)
1111
set(EXECUTABLE_OUTPUT_PATH ${wsjcpp-core_SOURCE_DIR})
1212

1313
# Sources

src/wsjcpp_core.cpp

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
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>
@@ -15,10 +25,8 @@
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 = "";
@@ -487,12 +494,6 @@ bool WsjcppCore::dirExists(const std::string &sDirname) {
487494
return false;
488495
}
489496

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-
}
496497

497498
// ---------------------------------------------------------------------
498499

@@ -501,51 +502,29 @@ std::vector<std::string> WsjcppCore::getListOfDirs(const std::string &sDirname)
501502
if (!WsjcppCore::dirExists(sDirname)) {
502503
return vDirs;
503504
}
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);
505+
for (auto& entry : std::filesystem::directory_iterator(sDirname)) {
506+
if (entry.is_directory()) {
507+
std::string sPath = entry.path();
508+
sPath = sPath.substr(sDirname.length()+1);
509+
vDirs.push_back(sPath);
515510
}
516-
closedir(dir);
517511
}
518512
std::sort(vDirs.begin(), vDirs.end());
519513
return vDirs;
520514
}
521515

522516
// ---------------------------------------------------------------------
523517

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-
531518
std::vector<std::string> WsjcppCore::getListOfFiles(const std::string &sDirname) {
532519
std::vector<std::string> vFiles;
533520
if (!WsjcppCore::dirExists(sDirname)) {
534521
return vFiles;
535522
}
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);
523+
for (auto& entry: std::filesystem::directory_iterator(sDirname)) {
524+
if (!entry.is_directory()) {
525+
std::string sPath = entry.path();
526+
vFiles.push_back(sPath);
547527
}
548-
closedir(dir);
549528
}
550529
return vFiles;
551530
}
@@ -554,6 +533,10 @@ std::vector<std::string> WsjcppCore::getListOfFiles(const std::string &sDirname)
554533

555534
bool WsjcppCore::makeDir(const std::string &sDirname) {
556535
struct stat st;
536+
537+
const std::filesystem::path dir{sDirname};
538+
std::filesystem::create_directory(dir);
539+
557540
int nStatus = mkdir(sDirname.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
558541
if (nStatus == 0) {
559542
return true;
@@ -744,9 +727,9 @@ void WsjcppCore::replaceAll(std::string& str, const std::string& sFrom, const st
744727

745728
std::vector<std::string> WsjcppCore::split(const std::string& sWhat, const std::string& sDelim) {
746729
std::vector<std::string> vRet;
747-
int nPos = 0;
748-
int nLen = sWhat.length();
749-
int nDelimLen = sDelim.length();
730+
size_t nPos = 0;
731+
size_t nLen = sWhat.length();
732+
size_t nDelimLen = sDelim.length();
750733
while (nPos < nLen) {
751734
std::size_t nFoundPos = sWhat.find(sDelim, nPos);
752735
if (nFoundPos != std::string::npos) {
@@ -781,7 +764,8 @@ std::string WsjcppCore::join(const std::vector<std::string> &vWhat, const std::s
781764
// ---------------------------------------------------------------------
782765

783766
void WsjcppCore::initRandom() {
784-
std::srand(std::time(0));
767+
time_t t = std::time(0);
768+
std::srand((unsigned int)t);
785769
}
786770

787771
// ---------------------------------------------------------------------
@@ -828,7 +812,7 @@ std::string WsjcppCore::getPointerAsHex(void *p) {
828812

829813
std::string WsjcppCore::extractURLProtocol(const std::string& sValue) {
830814
std::string sRet = "";
831-
int nPosProtocol = sValue.find("://");
815+
size_t nPosProtocol = sValue.find("://");
832816
if (nPosProtocol == std::string::npos) {
833817
return sRet;
834818
}
@@ -992,7 +976,7 @@ bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
992976

993977
bool WsjcppCore::setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError) {
994978

995-
mode_t m;
979+
mode_t m = 0x0;
996980

997981
// owner
998982
m |= filePermissions.getOwnerReadFlag() ? S_IRUSR : 0x0;
@@ -1055,21 +1039,21 @@ bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePerm
10551039

10561040
// ---------------------------------------------------------------------
10571041

1058-
std::string WsjcppCore::doPadLeft(const std::string& sIn, char cWhat, int nLength) {
1042+
std::string WsjcppCore::doPadLeft(const std::string& sIn, char cWhat, size_t nLength) {
10591043
std::string sRet;
1060-
int nPadLen = nLength - sIn.length();
1061-
for (int i = 0; i < nPadLen; i++) {
1044+
size_t nPadLen = nLength - sIn.length();
1045+
for (size_t i = 0; i < nPadLen; i++) {
10621046
sRet += cWhat;
10631047
}
10641048
return sRet + sIn;
10651049
}
10661050

10671051
// ---------------------------------------------------------------------
10681052

1069-
std::string WsjcppCore::doPadRight(const std::string& sIn, char cWhat, int nLength) {
1053+
std::string WsjcppCore::doPadRight(const std::string& sIn, char cWhat, size_t nLength) {
10701054
std::string sRet;
1071-
int nPadLen = nLength - sIn.length();
1072-
for (int i = 0; i < nPadLen; i++) {
1055+
size_t nPadLen = nLength - sIn.length();
1056+
for (size_t i = 0; i < nPadLen; i++) {
10731057
sRet += cWhat;
10741058
}
10751059
return sIn + sRet;
@@ -1220,9 +1204,6 @@ WsjcppResourceFile::WsjcppResourceFile() {
12201204
WsjcppResourcesManager::add(this);
12211205
}
12221206

1223-
// ---------------------------------------------------------------------
1224-
1225-
12261207
// ---------------------------------------------------------------------
12271208
// WsjcppResourcesManager
12281209

src/wsjcpp_core.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ class WsjcppCore {
127127
static bool setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError);
128128
static bool getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError);
129129

130-
static std::string doPadLeft(const std::string& sIn, char cWhat, int nLength);
131-
static std::string doPadRight(const std::string& sIn, char cWhat, int nLength);
130+
static std::string doPadLeft(const std::string& sIn, char cWhat, size_t nLength);
131+
static std::string doPadRight(const std::string& sIn, char cWhat, size_t nLength);
132132

133133
};
134134

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
99
set(MACOSX TRUE)
1010
endif()
1111

12-
set(CMAKE_CXX_STANDARD 11)
12+
set(CMAKE_CXX_STANDARD 17)
1313
set(EXECUTABLE_OUTPUT_PATH ${unit-tests_SOURCE_DIR})
1414

1515
set (WSJCPP_LIBRARIES "")

wsjcpp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
wsjcpp_version: v0.0.1
2-
cmake_cxx_standard: 11
2+
cmake_cxx_standard: 17
33
cmake_minimum_required: 3.0
44

55
name: wsjcpp-core

0 commit comments

Comments
 (0)