MString edebugged.

This commit is contained in:
Brad Arant 2022-05-20 19:04:48 -07:00
parent b25650c5a7
commit e411d31cb6
3 changed files with 41 additions and 17 deletions

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;
}

View File

@ -81,6 +81,15 @@ namespace coreutils {
cursor += length;
return cursor - temp;
}
void ZString::push() {
stack.push(cursor);
}
void ZString::pop() {
cursor = stack.top();
stack.pop();
}
std::string ZString::str()
{

View File

@ -6,6 +6,7 @@
#include <string>
#include <ostream>
#include <vector>
#include <stack>
namespace coreutils {
@ -27,7 +28,7 @@ namespace coreutils {
/// During the parsing process you can retrieve the parsed and unparsed sections of
/// the ZString as ZStrings.
///
/// You can stream the ZString to an output stream usinf the << operator and the entire
/// You can stream the ZString to an output stream using the << operator and the entire
/// contents of the ZString will be output regardless of parsing activity.
///
@ -99,6 +100,20 @@ namespace coreutils {
///
int incrementCursor(int length);
///
/// Use the push method to push the current cursor pointer to a stack.
///
void push();
///
/// Use the pop method to pop a previously pushed cursor pointer from the
/// cursor stack. The cursor will point to the same location in the
/// ZString where the cursor was pushed.
///
void pop();
///
/// Return the value of the ZString from the cursor to the end.
@ -282,6 +297,7 @@ namespace coreutils {
private:
std::vector<ZString> list;
std::stack<char *> stack;
bool isCharacter(char ch, const char *string);
};