MString edebugged.

This commit is contained in:
Brad Arant committed 2022-05-20 19:04:48 -07:00
1 parent b25650c5a7
commit e411d31cb6
3 files changed
+41 -17

No files matched your search

+15 -16
View File
@@ -2,6 +2,7 @@
#include "Log.h"
#include <iostream>
#include <stdlib.h>
#include <iomanip>
namespace coreutils {
@@ -12,32 +13,26 @@ namespace coreutils {
}
MString::MString(const char *data) {
this->length = strlen(data);
setSize(this->length);
this->data = (char *)malloc(bufferSize);
setSize(strlen(data));
memcpy(this->data, data, length);
cursor = this->data;
}
MString::MString(char *data, size_t length) {
this->length = length;
setSize(length);
memcpy(this->data, data, length);
cursor = this->data;
}
MString::MString(const char *data, size_t length) {
this->length = length;
setSize(length);
memcpy(this->data, data, length);
cursor = this->data;
}
MString::MString(const MString &mstring) {
bufferSize = (mstring.length % 256) + 256;
data = (char *)malloc(bufferSize);
setSize(mstring.length);
memcpy(data, mstring.data, mstring.length);
length = mstring.length;
cursor = data;
// Log(LOG_DEBUG_2) << "MString Copy Constructor: ";
}
@@ -45,7 +40,6 @@ namespace coreutils {
MString::MString(std::string data) {
setSize(data.length());
memcpy(this->data, (char *)data.c_str(), data.length());
length = data.length();
cursor = this->data;
}
@@ -55,21 +49,26 @@ namespace coreutils {
}
int MString::write(char ch) {
setSize(bufferSize + 1);
data[length++] = ch;
setSize(length + 1);
*(data + length - 1) = ch;
return 1;
}
int MString::write(ZString &value) {
setSize(bufferSize + value.getLength());
memcpy(data, value.getData(), value.getLength());
length += value.getLength();
int len = length;
setSize(length + value.getLength());
memcpy(data + len, value.getData(), value.getLength());
return value.getLength();
}
void MString::setSize(int size) {
bufferSize = (size % 256) + 256;
data = (char *)realloc(data, bufferSize);
int cursorOffset = cursor - data;
int newBufferSize = ((size / 256) + 1) * 256;
if(bufferSize != newBufferSize) {
bufferSize = newBufferSize;
data = (char *)realloc(data, bufferSize);
cursor = data + cursorOffset;
}
length = size;
}