Skip to content

Commit 64e3414

Browse files
committed
Fixed #2 createEmptyFile
1 parent d8cd119 commit 64e3414

9 files changed

+80
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ if (WSJCppCore::removeFile("./file.txt")) {
195195
}
196196
```
197197

198+
### createEmptyFile
199+
200+
Creating empty file. Will return true if file not exists and do created
201+
202+
```
203+
if (WSJCppCore::createEmptyFile("./file.txt")) {
204+
std::cout << "Empty file created" << std::endl;
205+
}
206+
```
207+
198208
### trim
199209

200210
```

src/wsjcpp_core.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,21 @@ bool WSJCppCore::removeFile(const std::string &sFilename) {
364364

365365
// ---------------------------------------------------------------------
366366

367+
bool WSJCppCore::createEmptyFile(const std::string &sFilename) {
368+
if (WSJCppCore::fileExists(sFilename)) {
369+
return false;
370+
}
371+
std::ofstream f(sFilename, std::ios::out | std::ios::binary);
372+
if (!f) {
373+
std::cout << "FAILED could not create file to wtite " << sFilename << std::endl;
374+
return false;
375+
}
376+
f.close();
377+
return true;
378+
}
379+
380+
// ---------------------------------------------------------------------
381+
367382
std::string& WSJCppCore::ltrim(std::string& str, const std::string& chars) {
368383
str.erase(0, str.find_first_not_of(chars));
369384
return str;

src/wsjcpp_core.h

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class WSJCppCore {
4040
static bool readTextFile(const std::string &sFilename, std::string &sOutputContent);
4141
static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize);
4242
static bool removeFile(const std::string &sFilename);
43+
static bool createEmptyFile(const std::string &sFilename);
4344

4445
static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
4546
static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
@@ -59,6 +60,8 @@ class WSJCppCore {
5960

6061
static std::string encodeUriComponent(const std::string& sValue);
6162
static std::string decodeUriComponent(const std::string& sValue);
63+
64+
6265
};
6366

6467

unit-tests.wsjcpp/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ tmp/*
22
logs/*
33
unit-tests
44
.wsjcpp/*
5+
data/empty.txt
56

67
# Vim temp files
78
*.swp

unit-tests.wsjcpp/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_uint2_hex_string
5353
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_uint2_hex_string.cpp")
5454
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_split.h")
5555
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_split.cpp")
56+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_empty_file.h")
57+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_empty_file.cpp")
5658

5759
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5860

unit-tests.wsjcpp/data/testreadfile.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "unit_test_create_empty_file.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
5+
REGISTRY_UNIT_TEST(UnitTestCreateEmptyFile)
6+
7+
UnitTestCreateEmptyFile::UnitTestCreateEmptyFile()
8+
: WSJCppUnitTestBase("UnitTestCreateEmptyFile") {
9+
}
10+
11+
// ---------------------------------------------------------------------
12+
13+
void UnitTestCreateEmptyFile::init() {
14+
// nothing
15+
}
16+
17+
// ---------------------------------------------------------------------
18+
19+
bool UnitTestCreateEmptyFile::run() {
20+
bool bTestSuccess = true;
21+
compareB(bTestSuccess, "./data/", WSJCppCore::dirExists("./data/"), true);
22+
std::string sFilename = "./data/empty.txt";
23+
if (WSJCppCore::fileExists(sFilename)) {
24+
WSJCppCore::removeFile(sFilename);
25+
}
26+
compareB(bTestSuccess, "fileExists 1: " + sFilename, WSJCppCore::fileExists(sFilename), false);
27+
compareB(bTestSuccess, "createEmptyFile 1: " + sFilename, WSJCppCore::createEmptyFile(sFilename), true);
28+
compareB(bTestSuccess, "createEmptyFile 2: " + sFilename, WSJCppCore::createEmptyFile(sFilename), false);
29+
compareB(bTestSuccess, "fileExists 2: " + sFilename, WSJCppCore::fileExists(sFilename), true);
30+
return bTestSuccess;
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_CREATE_EMPTY_FILE_H
2+
#define UNIT_TEST_CREATE_EMPTY_FILE_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestCreateEmptyFile : public WSJCppUnitTestBase {
8+
public:
9+
UnitTestCreateEmptyFile();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_CREATE_EMPTY_FILE_H
15+

wsjcpp.yml

+2
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ unit-tests:
6060
description: "Test convert unsigned int to hex string"
6161
- name: "Split"
6262
description: "Test split function"
63+
- name: "CreateEmptyFile"
64+
description: "Test create empty file"

0 commit comments

Comments
 (0)