Skip to content

Commit 07f4bc8

Browse files
committed
Fixed #1 added new WsjcppCore::recoursiveCopyFiles
1 parent f78eeca commit 07f4bc8

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ if (WsjcppCore::removeFile("./file.txt")) {
207207
}
208208
```
209209

210+
### copyFile
211+
212+
```
213+
if (WsjcppCore::copyFile("./file.txt", "./file1.txt")) {
214+
std::cout << "File copied!" << std::endl;
215+
}
216+
```
217+
210218
### createEmptyFile
211219

212220
Creating empty file. Will return true if file not exists and do created
@@ -391,4 +399,15 @@ std::cout << "Size: " << sResult << std::endl;
391399
Example output:
392400
```
393401
Size: 12K
402+
```
403+
404+
### recoursiveCopyFiles
405+
406+
Recoursive copy files
407+
*If target folders does not exists then it will be created*
408+
409+
```
410+
if (WsjcppCore::recoursiveCopyFiles("./folder1", "./folder2")) {
411+
// everything ok
412+
}
394413
```

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ int main(int argc, char* argv[]) {
2626
"Evgenii Sopov",
2727
""
2828
);
29+
WsjcppCore::recoursiveCopyFiles("./tmp", "./tmp2");
30+
2931
return 0;
3032
}

src/wsjcpp_core.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ bool WsjcppCore::dirExists(const std::string &sDirname) {
255255
// ---------------------------------------------------------------------
256256

257257
std::vector<std::string> WsjcppCore::listOfDirs(const std::string &sDirname) {
258+
WsjcppLog::warn("listOfDirs", "Deprecated. Use a WsjcppCore::getListOfDirs");
259+
return WsjcppCore::getListOfDirs(sDirname);
260+
}
261+
262+
// ---------------------------------------------------------------------
263+
264+
std::vector<std::string> WsjcppCore::getListOfDirs(const std::string &sDirname) {
258265
std::vector<std::string> vDirs;
259266
if (!WsjcppCore::dirExists(sDirname)) {
260267
return vDirs;
@@ -280,6 +287,13 @@ std::vector<std::string> WsjcppCore::listOfDirs(const std::string &sDirname) {
280287
// ---------------------------------------------------------------------
281288

282289
std::vector<std::string> WsjcppCore::listOfFiles(const std::string &sDirname) {
290+
WsjcppLog::warn("listOfFiles", "Deprecated. Use a WsjcppCore::getListOfFiles");
291+
return WsjcppCore::getListOfFiles(sDirname);
292+
}
293+
294+
// ---------------------------------------------------------------------
295+
296+
std::vector<std::string> WsjcppCore::getListOfFiles(const std::string &sDirname) {
283297
std::vector<std::string> vFiles;
284298
if (!WsjcppCore::dirExists(sDirname)) {
285299
return vFiles;
@@ -396,6 +410,25 @@ bool WsjcppCore::removeFile(const std::string &sFilename) {
396410

397411
// ---------------------------------------------------------------------
398412

413+
bool WsjcppCore::copyFile(const std::string &sSourceFilename, const std::string &sTargetFilename) {
414+
if (!WsjcppCore::fileExists(sSourceFilename)) {
415+
WsjcppLog::err("copyFile", "File '" + sSourceFilename + "' did not exists");
416+
return false;
417+
}
418+
419+
if (WsjcppCore::fileExists(sTargetFilename)) {
420+
WsjcppLog::err("copyFile", "File '" + sTargetFilename + "' already exists");
421+
return false;
422+
}
423+
424+
std::ifstream src(sSourceFilename, std::ios::binary);
425+
std::ofstream dst(sTargetFilename, std::ios::binary);
426+
dst << src.rdbuf();
427+
return true;
428+
}
429+
430+
// ---------------------------------------------------------------------
431+
399432
bool WsjcppCore::createEmptyFile(const std::string &sFilename) {
400433
if (WsjcppCore::fileExists(sFilename)) {
401434
return false;
@@ -636,6 +669,48 @@ std::string WsjcppCore::getHumanSizeBytes(long nBytes) {
636669
return std::to_string(nBytes) + "PB";
637670
}
638671

672+
// ---------------------------------------------------------------------
673+
674+
bool WsjcppCore::recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir) {
675+
if (!WsjcppCore::dirExists(sSourceDir)) {
676+
WsjcppLog::err("recoursiveCopyFiles", "Source Dir '" + sSourceDir + "' did not exists");
677+
return false;
678+
}
679+
680+
if (!WsjcppCore::dirExists(sTargetDir)) {
681+
if (!WsjcppCore::makeDir(sTargetDir)) {
682+
WsjcppLog::err("recoursiveCopyFiles", "Could not create target dir '" + sTargetDir + "'");
683+
return false;
684+
}
685+
}
686+
687+
std::vector<std::string> vFiles = WsjcppCore::getListOfFiles(sSourceDir);
688+
for (int i = 0; i < vFiles.size(); i++) {
689+
std::string sSourceFile = sSourceDir + "/" + vFiles[i];
690+
std::string sTargetFile = sTargetDir + "/" + vFiles[i];
691+
if (!WsjcppCore::copyFile(sSourceFile, sTargetFile)) {
692+
return false;
693+
}
694+
}
695+
696+
std::vector<std::string> vDirs = WsjcppCore::getListOfDirs(sSourceDir);
697+
for (int i = 0; i < vDirs.size(); i++) {
698+
std::string sSourceDir2 = sSourceDir + "/" + vDirs[i];
699+
std::string sTargetDir2 = sTargetDir + "/" + vDirs[i];
700+
if (!WsjcppCore::dirExists(sTargetDir2)) {
701+
if (!WsjcppCore::makeDir(sTargetDir2)) {
702+
WsjcppLog::err("recoursiveCopyFiles", "Could not create target subdir '" + sTargetDir2 + "'");
703+
return false;
704+
}
705+
}
706+
707+
if (!WsjcppCore::recoursiveCopyFiles(sSourceDir2, sTargetDir2)) {
708+
return false;
709+
}
710+
}
711+
return true;
712+
}
713+
639714
// ---------------------------------------------------------------------
640715
// WsjcppLog
641716

src/wsjcpp_core.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ class WsjcppCore {
3434
static bool dirExists(const std::string &sFilename);
3535
static bool fileExists(const std::string &sFilename);
3636
static std::vector<std::string> listOfDirs(const std::string &sDirname);
37-
static std::vector<std::string> listOfFiles(const std::string &sDirname);
37+
static std::vector<std::string> getListOfDirs(const std::string &sDirname);
38+
static std::vector<std::string> listOfFiles(const std::string &sDirname);
39+
static std::vector<std::string> getListOfFiles(const std::string &sDirname);
3840
static bool makeDir(const std::string &sDirname);
3941
static bool writeFile(const std::string &sFilename, const std::string &sContent);
4042
static bool readTextFile(const std::string &sFilename, std::string &sOutputContent);
4143
static bool readFileToBuffer(const std::string &sFilename, char *pBuffer[], int &nBufferSize);
4244
static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize);
4345
static bool removeFile(const std::string &sFilename);
46+
static bool copyFile(const std::string &sSourceFilename, const std::string &sTargetFilename);
47+
4448
static bool createEmptyFile(const std::string &sFilename);
4549

4650
static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
@@ -64,6 +68,8 @@ class WsjcppCore {
6468
static std::string decodeUriComponent(const std::string& sValue);
6569

6670
static std::string getHumanSizeBytes(long nBytes);
71+
72+
static bool recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir);
6773
};
6874

6975

unit-tests.wsjcpp/src/unit_test_list_of_dirs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void UnitTestListOfDirs::init() {
1919

2020
bool UnitTestListOfDirs::run() {
2121
bool bTestSuccess = true;
22-
std::vector<std::string> vDirs = WsjcppCore::listOfDirs("./data/list_of_dirs");
22+
std::vector<std::string> vDirs = WsjcppCore::getListOfDirs("./data/list_of_dirs");
2323

2424
compareN(bTestSuccess, "size", vDirs.size(), 4);
2525
if (vDirs.size() == 4) {

0 commit comments

Comments
 (0)