Skip to content

Commit a725c65

Browse files
committed
Updated wsjcpp-core
1 parent 9398f7d commit a725c65

File tree

9 files changed

+163
-30
lines changed

9 files changed

+163
-30
lines changed

src.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ set (WSJCPP_SOURCES "")
1717
find_package(Threads REQUIRED)
1818
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
1919

20-
# wsjcpp-core:v0.0.5
20+
# wsjcpp-core:v0.0.7
2121
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_core/")
2222
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp")
2323
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.h")

src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_cxx_standard: 11
33
cmake_minimum_required: 3.0
44

55
name: wsjcpp-core
6-
version: v0.0.5
6+
version: v0.0.7
77
description: Basic Utils for wsjcpp
88
issues: https://github.com/wsjcpp/wsjcpp-core/issues
99
repositories:
@@ -17,30 +17,22 @@ authors:
1717
- name: Evgenii Sopov
1818
email: mrseakg@gmail.com
1919

20-
required-libraries:
21-
- pthread
22-
2320
distribution:
2421
- source-file: src/wsjcpp_core.cpp
2522
target-file: wsjcpp_core.cpp
26-
sha1: "d892bfee196af88dcada57b39bc6bd65ce2ce075"
2723
type: "source-code"
2824
- source-file: src/wsjcpp_core.h
2925
target-file: wsjcpp_core.h
30-
sha1: "8f5e7b7ada06814ed123a08e49acffe2e12d398a"
3126
type: "source-code" # todo must be header-file
3227
- source-file: "src/wsjcpp_unit_tests.cpp"
3328
target-file: "wsjcpp_unit_tests.cpp"
3429
type: "unit-tests"
35-
sha1: "4208e039ec2923636655b3ada79ec223cca7bae2"
3630
- source-file: "src/wsjcpp_unit_tests.h"
3731
target-file: "wsjcpp_unit_tests.h"
3832
type: "unit-tests"
39-
sha1: "8d2ec886f23161a639bb2419bb5e4af48278f18b"
4033
- source-file: "src/wsjcpp_unit_tests_main.cpp"
4134
target-file: "wsjcpp_unit_tests_main.cpp"
4235
type: "unit-tests"
43-
sha1: "2c02fb58f51687eeac2076096b7df698cc246c9d"
4436

4537
unit-tests:
4638
cases:
@@ -54,3 +46,19 @@ unit-tests:
5446
description: "String to upper"
5547
- name: "CreateUuid"
5648
description: "Test generation uuids"
49+
- name: "GetEnv"
50+
description: "Test getEnv function"
51+
- name: "ToLower"
52+
description: "Test toLower"
53+
- name: "ReplaceAll"
54+
description: "Test replace all"
55+
- name: "DecodeUriComponent"
56+
description: "Check decoding"
57+
- name: "EncodeUriComponent"
58+
description: "Check encoding"
59+
- name: "Uint2HexString"
60+
description: "Test convert unsigned int to hex string"
61+
- name: "Split"
62+
description: "Test split function"
63+
- name: "CreateEmptyFile"
64+
description: "Test create empty file"

src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp

Lines changed: 126 additions & 5 deletions
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;
@@ -383,10 +398,12 @@ std::string& WSJCppCore::trim(std::string& str, const std::string& chars) {
383398
}
384399

385400
// ---------------------------------------------------------------------
401+
// will worked only with latin
386402

387-
std::string& WSJCppCore::to_lower(std::string& str) {
388-
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
389-
return str;
403+
std::string WSJCppCore::toLower(const std::string& str) {
404+
std::string sRet = str;
405+
std::transform(sRet.begin(), sRet.end(), sRet.begin(), ::tolower);
406+
return sRet;
390407
}
391408

392409
// ---------------------------------------------------------------------
@@ -400,6 +417,44 @@ std::string WSJCppCore::toUpper(const std::string& str) {
400417

401418
// ---------------------------------------------------------------------
402419

420+
void WSJCppCore::replaceAll(std::string& str, const std::string& sFrom, const std::string& sTo) {
421+
if (sFrom.empty()) {
422+
return;
423+
}
424+
size_t start_pos = 0;
425+
while ((start_pos = str.find(sFrom, start_pos)) != std::string::npos) {
426+
str.replace(start_pos, sFrom.length(), sTo);
427+
start_pos += sTo.length(); // In case 'to' contains 'sFrom', like replacing 'x' with 'yx'
428+
}
429+
}
430+
431+
// ---------------------------------------------------------------------
432+
433+
std::vector<std::string> WSJCppCore::split(const std::string& sWhat, const std::string& sDelim) {
434+
std::vector<std::string> vRet;
435+
int nPos = 0;
436+
int nLen = sWhat.length();
437+
int nDelimLen = sDelim.length();
438+
while (nPos < nLen) {
439+
std::size_t nFoundPos = sWhat.find(sDelim, nPos);
440+
if (nFoundPos != std::string::npos) {
441+
std::string sToken = sWhat.substr(nPos, nFoundPos - nPos);
442+
vRet.push_back(sToken);
443+
nPos = nFoundPos + nDelimLen;
444+
if (nFoundPos + nDelimLen == nLen) { // last delimiter
445+
vRet.push_back("");
446+
}
447+
} else {
448+
std::string sToken = sWhat.substr(nPos, nLen - nPos);
449+
vRet.push_back(sToken);
450+
break;
451+
}
452+
}
453+
return vRet;
454+
}
455+
456+
// ---------------------------------------------------------------------
457+
403458
void WSJCppCore::initRandom() {
404459
std::srand(std::rand() + std::time(0));
405460
}
@@ -421,9 +476,20 @@ std::string WSJCppCore::createUuid() {
421476

422477
// ---------------------------------------------------------------------
423478

479+
std::string WSJCppCore::uint2hexString(unsigned int n) {
480+
std::string sRet;
481+
for (int i = 0; i < 8; i++) {
482+
sRet += "0123456789abcdef"[n % 16];
483+
n >>= 4;
484+
}
485+
return std::string(sRet.rbegin(), sRet.rend());
486+
}
487+
488+
// ---------------------------------------------------------------------
489+
424490
unsigned long WSJCppCore::convertVoidToULong(void *p) {
425-
unsigned long ret = *(unsigned long *)p;
426-
return ret;
491+
std::uintptr_t ret = reinterpret_cast<std::uintptr_t>(p);
492+
return (unsigned long)ret;
427493
}
428494

429495
// ---------------------------------------------------------------------
@@ -447,6 +513,61 @@ std::string WSJCppCore::extractURLProtocol(const std::string& sValue) {
447513
return sRet;
448514
}
449515

516+
// ---------------------------------------------------------------------
517+
518+
bool WSJCppCore::getEnv(const std::string& sName, std::string& sValue) {
519+
if (const char* env_p = std::getenv(sName.c_str())) {
520+
sValue = std::string(env_p);
521+
return true;
522+
}
523+
return false;
524+
}
525+
526+
// ---------------------------------------------------------------------
527+
528+
std::string WSJCppCore::encodeUriComponent(const std::string& sValue) {
529+
std::stringstream ssRet;
530+
for (int i = 0; i < sValue.length(); i++) {
531+
char c = sValue[i];
532+
if (
533+
c == '-' || c == '_' || c == '.' || c == '!'
534+
|| c == '~' || c == '*' || c == '\''
535+
|| c == '(' || c == ')' || (c >= '0' && c <= '9')
536+
|| (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
537+
) {
538+
ssRet << c;
539+
} else {
540+
ssRet << '%' << std::uppercase << std::hex << (int)c;
541+
}
542+
}
543+
return ssRet.str();
544+
}
545+
546+
// ---------------------------------------------------------------------
547+
548+
std::string WSJCppCore::decodeUriComponent(const std::string& sValue) {
549+
std::string sRet = "";
550+
std::string sHex = "";
551+
int nLen = sValue.length();
552+
for (int i = 0; i < sValue.length(); i++) {
553+
char c = sValue[i];
554+
if (c == '%') {
555+
if (i+2 >= nLen) {
556+
WSJCppLog::throw_err("WSJCppCore::decodeUriElement", "Wrong format of string");
557+
}
558+
sHex = "0x";
559+
sHex += sValue[i+1];
560+
sHex += sValue[i+2];
561+
i = i + 2;
562+
char c1 = std::stoul(sHex, nullptr, 16);
563+
sRet += c1;
564+
} else {
565+
sRet += c;
566+
}
567+
}
568+
return sRet;
569+
}
570+
450571
// ---------------------------------------------------------------------
451572
// WSJCppLog
452573

src.wsjcpp/wsjcpp_core/wsjcpp_core.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,28 @@ 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 ");
4647
static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
47-
static std::string& to_lower(std::string& str);
48+
static std::string toLower(const std::string &str);
4849
static std::string toUpper(const std::string& str);
50+
static void replaceAll(std::string& str, const std::string& from, const std::string& to);
51+
static std::vector<std::string> split(const std::string& sWhat, const std::string& sDelim);
4952

5053
static void initRandom();
5154
static std::string createUuid();
52-
55+
static std::string uint2hexString(unsigned int n);
5356
static unsigned long convertVoidToULong(void *p);
5457
static std::string getPointerAsHex(void *p);
5558
static std::string extractURLProtocol(const std::string& sValue);
59+
static bool getEnv(const std::string& sName, std::string& sValue);
60+
61+
static std::string encodeUriComponent(const std::string& sValue);
62+
static std::string decodeUriComponent(const std::string& sValue);
63+
64+
5665
};
5766

5867

src/wsjcpp_light_web_http_request.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void WSJCppLightWebHttpRequest::appendRecieveRequest(const std::string &sRequest
107107
}
108108
m_vHeaders.push_back(sLine);
109109

110-
WSJCppCore::to_lower(sLine);
110+
sLine = WSJCppCore::toLower(sLine);
111111
if (!sLine.compare(0, sContentLengthPrefix.size(), sContentLengthPrefix)) {
112112
m_nContentLength = atoi(sLine.substr(sContentLengthPrefix.size()).c_str());
113113
// WSJCppLog::warn(TAG, "Content-Length: " + std::to_string(m_nContentLength));
@@ -184,16 +184,12 @@ void WSJCppLightWebHttpRequest::parseFirstLine(const std::string &sHeader) {
184184
std::string sValue = sParam.substr(nFound2+1);
185185
std::string sName = sParam.substr(0, nFound2);
186186
m_vRequestQueryParams.push_back(WSJCppLightWebHttpRequestQueryValue(
187-
this->decodeURIElement(sName),
188-
this->decodeURIElement(sValue)
187+
WSJCppCore::decodeUriComponent(sName),
188+
WSJCppCore::decodeUriComponent(sValue)
189189
));
190190
}
191191
}
192192
}
193193

194194
// ----------------------------------------------------------------------
195195

196-
std::string WSJCppLightWebHttpRequest::decodeURIElement(const std::string &sElement) { // TODO move to WSJCppCore
197-
WSJCppLog::warn(TAG, "TODO Implement decodeURIElement");
198-
return sElement;
199-
}

src/wsjcpp_light_web_http_request.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class WSJCppLightWebHttpRequest {
4343
std::string TAG;
4444

4545
void parseFirstLine(const std::string &sHeader);
46-
std::string decodeURIElement(const std::string &sElement); // TODO move to WSJCppCore
4746

4847
enum EnumParserState {
4948
START,

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.5
22+
# wsjcpp-core:v0.0.7
2323
list (APPEND WSJCPP_INCLUDE_DIRS "../src.wsjcpp/wsjcpp_core/")
2424
list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp")
2525
list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_core.h")

unit-tests.wsjcpp/src/unit_test_parse_http_request.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ bool UnitTestParseHttpRequest::run() {
9999
}, "/2/", "POST", "{\"some\":1}", "HTTP/1.1", {}));
100100

101101
tests.push_back(LTest(4, "some-address4", {
102-
LPartsOfRequest("GET /query?somebook=Hello%20sss&somebook=Hello%20ddd&somebook2=dmsf&p4=11_-%dk123 HTTP/1.1\n", false),
102+
LPartsOfRequest("GET /query?somebook=Hello%20sss&somebook=Hello%20ddd&somebook2=dmsf&p4=11_-%25dk123 HTTP/1.1\n", false),
103103
LPartsOfRequest("Host: www.w3.org\n", false),
104104
LPartsOfRequest("\n", true),
105105
}, "/query", "GET", "", "HTTP/1.1", {
106-
LPartsOfRequestQueryParam("somebook", "Hello%20sss"),
107-
LPartsOfRequestQueryParam("somebook", "Hello%20ddd"),
106+
LPartsOfRequestQueryParam("somebook", "Hello sss"),
107+
LPartsOfRequestQueryParam("somebook", "Hello ddd"),
108108
LPartsOfRequestQueryParam("somebook2", "dmsf"),
109109
LPartsOfRequestQueryParam("p4", "11_-%dk123")
110110
}));

wsjcpp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ keywords:
1919

2020
dependencies:
2121
- name: "wsjcpp-core"
22-
version: "v0.0.5"
22+
version: "v0.0.7"
2323
url: "https://github.com/wsjcpp/wsjcpp-core:master"
2424
origin: "https://github.com/"
2525
installation-dir: "./src.wsjcpp/wsjcpp_core"

0 commit comments

Comments
 (0)