<< operator now outputs fulll ZString.

This commit is contained in:
Brad Arant 2022-04-04 13:35:15 -07:00
parent 59228ad356
commit b25650c5a7
4 changed files with 83 additions and 43 deletions

View File

@ -1,6 +1,7 @@
#include "MString.h" #include "MString.h"
#include "Log.h" #include "Log.h"
#include <iostream> #include <iostream>
#include <stdlib.h>
namespace coreutils { namespace coreutils {
@ -12,7 +13,7 @@ namespace coreutils {
MString::MString(const char *data) { MString::MString(const char *data) {
this->length = strlen(data); this->length = strlen(data);
bufferSize = (this->length % 256) + 256; setSize(this->length);
this->data = (char *)malloc(bufferSize); this->data = (char *)malloc(bufferSize);
memcpy(this->data, data, length); memcpy(this->data, data, length);
cursor = this->data; cursor = this->data;
@ -20,16 +21,14 @@ namespace coreutils {
MString::MString(char *data, size_t length) { MString::MString(char *data, size_t length) {
this->length = length; this->length = length;
bufferSize = (this->length % 256) + 256; setSize(length);
this->data = (char *)malloc(bufferSize);
memcpy(this->data, data, length); memcpy(this->data, data, length);
cursor = this->data; cursor = this->data;
} }
MString::MString(const char *data, size_t length) { MString::MString(const char *data, size_t length) {
this->length = length; this->length = length;
bufferSize = (this->length % 256) + 256; setSize(length);
this->data = (char *)malloc(bufferSize);
memcpy(this->data, data, length); memcpy(this->data, data, length);
cursor = this->data; cursor = this->data;
} }
@ -44,8 +43,7 @@ namespace coreutils {
} }
MString::MString(std::string data) { MString::MString(std::string data) {
bufferSize = (data.length() % 256) + 256; setSize(data.length());
this->data = (char *)malloc(bufferSize);
memcpy(this->data, (char *)data.c_str(), data.length()); memcpy(this->data, (char *)data.c_str(), data.length());
length = data.length(); length = data.length();
cursor = this->data; cursor = this->data;
@ -53,22 +51,26 @@ namespace coreutils {
MString::~MString() { MString::~MString() {
if(data) if(data)
free(data); free(data);
} }
int MString::write(char ch) { int MString::write(char ch) {
if(bufferSize == 0) { setSize(bufferSize + 1);
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; data[length++] = ch;
return 1; return 1;
} }
int MString::write(ZString &value) {
setSize(bufferSize + value.getLength());
memcpy(data, value.getData(), value.getLength());
length += value.getLength();
return value.getLength();
}
void MString::setSize(int size) {
bufferSize = (size % 256) + 256;
data = (char *)realloc(data, bufferSize);
length = size;
}
} }

View File

@ -49,10 +49,13 @@ namespace coreutils {
~MString(); ~MString();
int write(char ch); int write(char ch);
int write(ZString &value);
private: private:
int bufferSize = 0; int bufferSize = 0;
void setSize(int size);
}; };
} }

View File

@ -1,32 +1,27 @@
#include "ZString.h" #include "ZString.h"
#include "Log.h" #include "Log.h"
#include "Exception.h"
#include <iostream>
namespace coreutils namespace coreutils {
{
std::ostream &operator<<(std::ostream &os, const ZString &zstring) std::ostream &operator<<(std::ostream &os, const ZString &zstring) {
{ for (char *ix = zstring.data; ix < (zstring.data + zstring.length); ++ix)
for (char *ix = zstring.cursor; ix < (zstring.data + zstring.length); ++ix) os << *ix;
{
os << *ix;
}
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, const std::string &string) std::ostream &operator<<(std::ostream &os, const std::string &string) {
{
os << string; os << string;
return os; return os;
} }
std::ostream &operator+(std::ostream &os, const ZString &zstring) std::ostream &operator+(std::ostream &os, const ZString &zstring) {
{
os << zstring; os << zstring;
return os; return os;
} }
ZString::ZString() ZString::ZString() {
{
data = NULL; data = NULL;
length = 0; length = 0;
cursor = data; cursor = data;
@ -38,16 +33,14 @@ namespace coreutils
ZString::ZString(const char *data, size_t length) : data((char *)data), length(length), cursor((char *)data) {} ZString::ZString(const char *data, size_t length) : data((char *)data), length(length), cursor((char *)data) {}
ZString::ZString(const ZString &zstring) ZString::ZString(const ZString &zstring) {
{
data = zstring.data; data = zstring.data;
length = zstring.length; length = zstring.length;
cursor = zstring.cursor; cursor = zstring.cursor;
// Log(LOG_DEBUG_2) << "ZString Copy Constructor: "; // Log(LOG_DEBUG_2) << "ZString Copy Constructor: ";
} }
ZString::ZString(std::string data) ZString::ZString(std::string data) {
{
this->data = (char *)data.c_str(); this->data = (char *)data.c_str();
length = data.length(); length = data.length();
cursor = (char *)data.c_str(); cursor = (char *)data.c_str();
@ -80,6 +73,15 @@ namespace coreutils
return tempInt; return tempInt;
} }
int ZString::incrementCursor(int length) {
char *temp = cursor;
if((cursor + length) > (data + this->length))
cursor = data + this->length;
else
cursor += length;
return cursor - temp;
}
std::string ZString::str() std::string ZString::str()
{ {
return std::string(data, length); return std::string(data, length);
@ -279,6 +281,12 @@ namespace coreutils
char *ZString::getCursor() { char *ZString::getCursor() {
return cursor; return cursor;
} }
void ZString::setCursor(char *cursor) {
if((cursor < data) || (cursor > (data + length)))
throw Exception("Cursor out of range on setCursor.");
this->cursor = cursor;
}
size_t ZString::getLength() size_t ZString::getLength()
{ {
@ -292,10 +300,14 @@ namespace coreutils
cursor = data; cursor = data;
} }
ZString ZString::getParsed() { ZString ZString::parsed() {
return ZString(data, cursor - data); return ZString(data, cursor - data);
} }
ZString ZString::unparsed() {
return ZString(cursor, data + length - cursor);
}
void ZString::reset() void ZString::reset()
{ {
cursor = data; cursor = data;

View File

@ -1,3 +1,4 @@
#ifndef __ZString_h__ #ifndef __ZString_h__
#define __ZString_h__ #define __ZString_h__
@ -23,6 +24,12 @@ namespace coreutils {
/// ZString and is used to point to the current parsing point. Several methods are /// ZString and is used to point to the current parsing point. Several methods are
/// provided to advance through the parsing based upon content and rule checks. /// provided to advance through the parsing based upon content and rule checks.
/// ///
/// 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
/// contents of the ZString will be output regardless of parsing activity.
///
class ZString { class ZString {
@ -87,6 +94,12 @@ namespace coreutils {
int asInteger(); int asInteger();
///
///
///
int incrementCursor(int length);
/// ///
/// Return the value of the ZString from the cursor to the end. /// Return the value of the ZString from the cursor to the end.
/// ///
@ -213,6 +226,8 @@ namespace coreutils {
char* getCursor(); char* getCursor();
void setCursor(char *cursor);
/// ///
/// Return the length of the ZString. /// Return the length of the ZString.
/// ///
@ -227,10 +242,18 @@ namespace coreutils {
void setZString(ZString zstring); void setZString(ZString zstring);
/// ///
/// /// Use the parsed method to return a ZString that represents the portion
/// ZString that has been parsed. The returned ZString represents the
/// portion of the ZString from the data pointer to the cursor.
/// ///
ZString getParsed(); ZString parsed();
///
///
///
ZString unparsed();
/// ///
/// ///
@ -252,7 +275,7 @@ namespace coreutils {
void nextChar(); void nextChar();
// protected: protected:
char *data; char *data;
char *cursor; char *cursor;
size_t length; size_t length;