-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringwrapper.cpp
50 lines (43 loc) · 1.42 KB
/
stringwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "stringwrapper.h"
StringWrapper::StringWrapper(): content(nullptr){}
StringWrapper::~StringWrapper(){
Clear();
}
void StringWrapper::Clear(){
if (content != nullptr) delete[] content;
content = nullptr;
}
void StringWrapper::SetContent(const std::string& input){
std::lock_guard<std::mutex> lock(mtx);
Clear();
content = new char[input.length() + 1];
strcpy(content, input.c_str());
}
void StringWrapper::AddContent(const std::string& input) {
std::lock_guard<std::mutex> lock(mtx);
if (content == nullptr) {
content = new char[input.length() + 1];
strcpy(content, input.c_str());
} else {
char* newContent = new char[strlen(content) + input.length() + 1];
strcpy(newContent, content);
strcat(newContent, input.c_str());
delete[] content;
content = newContent;
}
}
int StringWrapper::GetStringSize(){
std::lock_guard<std::mutex> lock(mtx);
int num = 0;
if (content != nullptr) num = strlen(content) + 1;
return num;
}
void StringWrapper::GetString(char* buffer, int bufferSize, bool clear){
std::lock_guard<std::mutex> lock(mtx);
if (buffer != nullptr && content != nullptr && bufferSize > 1){
size_t copySize = std::min(strlen(content), static_cast<size_t>(bufferSize - 1));
if(copySize>0) strncpy(buffer, content, copySize);
buffer[bufferSize - 1] = '\0';
}
if(clear) Clear();
}