Skip to content

Commit 57de149

Browse files
committed
Implemented readFileToBuffer
1 parent 64e3414 commit 57de149

File tree

9 files changed

+100
-3
lines changed

9 files changed

+100
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ if (WSJCppCore::readTextFile("./file.txt", sContent)) {
187187
}
188188
```
189189

190+
### readFileToBuffer
191+
192+
Read file binary content to buffer
193+
194+
```
195+
char *pBuffer = nullptr;
196+
int nBufferSize = 0;
197+
if (WSJCppCore::readFileToBuffer("./data/readFileToBuffer.txt", &pBuffer, nBufferSize)) {
198+
// your can work with buffer here
199+
}
200+
```
201+
190202
### removeFile
191203

192204
```

src.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automaticly generated by [email protected]
22
cmake_minimum_required(VERSION 3.0)
33

4-
add_definitions(-DWSJCPP_VERSION="v0.0.7")
4+
add_definitions(-DWSJCPP_VERSION="v0.0.8")
55
add_definitions(-DWSJCPP_NAME="wsjcpp-core")
66

77
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

src/wsjcpp_core.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,33 @@ bool WSJCppCore::readTextFile(const std::string &sFilename, std::string &sConten
345345

346346
// ---------------------------------------------------------------------
347347

348+
bool WSJCppCore::readFileToBuffer(const std::string &sFilename, char *pBuffer[], int &nBufferSize) {
349+
std::ifstream f(sFilename, std::ifstream::binary);
350+
if (!f) {
351+
return false;
352+
}
353+
354+
// get length of file:
355+
f.seekg (0, f.end);
356+
nBufferSize = f.tellg();
357+
f.seekg (0, f.beg);
358+
359+
*pBuffer = new char [nBufferSize];
360+
361+
// read data as a block:
362+
f.read (*pBuffer, nBufferSize);
363+
if (!f) {
364+
WSJCppLog::err("WSJCppCore::readFileToBuffer", "Only " + std::to_string(f.gcount()) + " could be read");
365+
delete[] pBuffer;
366+
f.close();
367+
return false;
368+
}
369+
f.close();
370+
return true;
371+
}
372+
373+
// ---------------------------------------------------------------------
374+
348375
bool WSJCppCore::writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize) {
349376
std::ofstream f(sFilename, std::ios::out | std::ios::binary);
350377
if (!f) {

src/wsjcpp_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class WSJCppCore {
3838
static bool makeDir(const std::string &sDirname);
3939
static bool writeFile(const std::string &sFilename, const std::string &sContent);
4040
static bool readTextFile(const std::string &sFilename, std::string &sOutputContent);
41+
static bool readFileToBuffer(const std::string &sFilename, char *pBuffer[], int &nBufferSize);
4142
static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize);
4243
static bool removeFile(const std::string &sFilename);
4344
static bool createEmptyFile(const std::string &sFilename);

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
5-
add_definitions(-DWSJCPP_VERSION="ut-v0.0.7")
5+
add_definitions(-DWSJCPP_VERSION="ut-v0.0.8")
66
add_definitions(-DWSJCPP_NAME="unit-tests-wsjcpp-core")
77

88
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -19,7 +19,7 @@ set (WSJCPP_SOURCES "")
1919
find_package(Threads REQUIRED)
2020
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
2121

22-
# wsjcpp-core:v0.0.7
22+
# wsjcpp-core:v0.0.8
2323
list (APPEND WSJCPP_INCLUDE_DIRS "../src")
2424
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.cpp")
2525
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.h")
@@ -55,6 +55,8 @@ 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")
5656
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_empty_file.h")
5757
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_empty_file.cpp")
58+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_file_to_buffer.h")
59+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_file_to_buffer.cpp")
5860

5961
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
6062

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1234567890
2+
qwerty123456
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "unit_test_read_file_to_buffer.h"
2+
#include <vector>
3+
#include <algorithm>
4+
#include <wsjcpp_core.h>
5+
6+
REGISTRY_UNIT_TEST(UnitTestReadFileToBuffer)
7+
8+
UnitTestReadFileToBuffer::UnitTestReadFileToBuffer()
9+
: WSJCppUnitTestBase("UnitTestReadFileToBuffer") {
10+
}
11+
12+
// ---------------------------------------------------------------------
13+
14+
void UnitTestReadFileToBuffer::init() {
15+
// nothing
16+
}
17+
18+
// ---------------------------------------------------------------------
19+
20+
bool UnitTestReadFileToBuffer::run() {
21+
bool bTestSuccess = true;
22+
std::string sExpected = "1234567890\nqwerty123456";
23+
char *pBuffer = nullptr;
24+
int nBufferSize = 0;
25+
WSJCppCore::readFileToBuffer("./data/readFileToBuffer.txt", &pBuffer, nBufferSize);
26+
compareN(bTestSuccess, "Size of readed", nBufferSize, sExpected.length());
27+
int nMin = std::min(nBufferSize, (int)sExpected.length());
28+
for (int i = 0; i < nMin; i++) {
29+
char c_got = pBuffer[i];
30+
char c_expected = sExpected[i];
31+
compareN(bTestSuccess, "Check the " + std::to_string(i), c_got, c_expected);
32+
}
33+
// compareB(bTestSuccess, "Not implemented", true, false);
34+
return bTestSuccess;
35+
}
36+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_READ_FILE_TO_BUFFER_H
2+
#define UNIT_TEST_READ_FILE_TO_BUFFER_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestReadFileToBuffer : public WSJCppUnitTestBase {
8+
public:
9+
UnitTestReadFileToBuffer();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_READ_FILE_TO_BUFFER_H
15+

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ unit-tests:
6262
description: "Test split function"
6363
- name: "CreateEmptyFile"
6464
description: "Test create empty file"
65+
- name: "ReadFileToBuffer"
66+
description: "test for readFileToBuffer"

0 commit comments

Comments
 (0)