Developed MString.
This commit is contained in:
1 parent
91286649cc
commit
59228ad356
4 files changed
+195
-13
No files matched your search
+74
@@ -0,0 +1,74 @@
|
||||
#include "MString.h"
|
||||
#include "Log.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
MString::MString() {
|
||||
data = NULL;
|
||||
length = 0;
|
||||
cursor = NULL;
|
||||
}
|
||||
|
||||
MString::MString(const char *data) {
|
||||
this->length = strlen(data);
|
||||
bufferSize = (this->length % 256) + 256;
|
||||
this->data = (char *)malloc(bufferSize);
|
||||
memcpy(this->data, data, length);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(char *data, size_t length) {
|
||||
this->length = length;
|
||||
bufferSize = (this->length % 256) + 256;
|
||||
this->data = (char *)malloc(bufferSize);
|
||||
memcpy(this->data, data, length);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(const char *data, size_t length) {
|
||||
this->length = length;
|
||||
bufferSize = (this->length % 256) + 256;
|
||||
this->data = (char *)malloc(bufferSize);
|
||||
memcpy(this->data, data, length);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(const MString &mstring) {
|
||||
bufferSize = (mstring.length % 256) + 256;
|
||||
data = (char *)malloc(bufferSize);
|
||||
memcpy(data, mstring.data, mstring.length);
|
||||
length = mstring.length;
|
||||
cursor = data;
|
||||
// Log(LOG_DEBUG_2) << "MString Copy Constructor: ";
|
||||
}
|
||||
|
||||
MString::MString(std::string data) {
|
||||
bufferSize = (data.length() % 256) + 256;
|
||||
this->data = (char *)malloc(bufferSize);
|
||||
memcpy(this->data, (char *)data.c_str(), data.length());
|
||||
length = data.length();
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::~MString() {
|
||||
if(data)
|
||||
free(data);
|
||||
}
|
||||
|
||||
int MString::write(char ch) {
|
||||
if(bufferSize == 0) {
|
||||
bufferSize = 256;
|
||||
data = (char *)malloc(bufferSize);
|
||||
cursor = data;
|
||||
length = 0;
|
||||
} else if(bufferSize < (length + 1)) {
|
||||
bufferSize += 256;
|
||||
data = (char *)realloc(data, bufferSize);
|
||||
cursor = data;
|
||||
}
|
||||
data[length++] = ch;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user