<< 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 "Log.h"
#include <iostream>
#include <stdlib.h>
namespace coreutils {
@ -12,7 +13,7 @@ namespace coreutils {
MString::MString(const char *data) {
this->length = strlen(data);
bufferSize = (this->length % 256) + 256;
setSize(this->length);
this->data = (char *)malloc(bufferSize);
memcpy(this->data, data, length);
cursor = this->data;
@ -20,16 +21,14 @@ namespace coreutils {
MString::MString(char *data, size_t length) {
this->length = length;
bufferSize = (this->length % 256) + 256;
this->data = (char *)malloc(bufferSize);
setSize(length);
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);
setSize(length);
memcpy(this->data, data, length);
cursor = this->data;
}
@ -44,8 +43,7 @@ namespace coreutils {
}
MString::MString(std::string data) {
bufferSize = (data.length() % 256) + 256;
this->data = (char *)malloc(bufferSize);
setSize(data.length());
memcpy(this->data, (char *)data.c_str(), data.length());
length = data.length();
cursor = this->data;
@ -57,18 +55,22 @@ namespace coreutils {
}
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;
}
setSize(bufferSize + 1);
data[length++] = ch;
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

@ -50,8 +50,11 @@ namespace coreutils {
int write(char ch);
int write(ZString &value);
private:
int bufferSize = 0;
void setSize(int size);
};

View File

@ -1,32 +1,27 @@
#include "ZString.h"
#include "Log.h"
#include "Exception.h"
#include <iostream>
namespace coreutils
{
namespace coreutils {
std::ostream &operator<<(std::ostream &os, const ZString &zstring)
{
for (char *ix = zstring.cursor; ix < (zstring.data + zstring.length); ++ix)
{
std::ostream &operator<<(std::ostream &os, const ZString &zstring) {
for (char *ix = zstring.data; ix < (zstring.data + zstring.length); ++ix)
os << *ix;
}
return os;
}
std::ostream &operator<<(std::ostream &os, const std::string &string)
{
std::ostream &operator<<(std::ostream &os, const std::string &string) {
os << string;
return os;
}
std::ostream &operator+(std::ostream &os, const ZString &zstring)
{
std::ostream &operator+(std::ostream &os, const ZString &zstring) {
os << zstring;
return os;
}
ZString::ZString()
{
ZString::ZString() {
data = NULL;
length = 0;
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 ZString &zstring)
{
ZString::ZString(const ZString &zstring) {
data = zstring.data;
length = zstring.length;
cursor = zstring.cursor;
// Log(LOG_DEBUG_2) << "ZString Copy Constructor: ";
}
ZString::ZString(std::string data)
{
ZString::ZString(std::string data) {
this->data = (char *)data.c_str();
length = data.length();
cursor = (char *)data.c_str();
@ -80,6 +73,15 @@ namespace coreutils
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()
{
return std::string(data, length);
@ -280,6 +282,12 @@ namespace coreutils
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()
{
return length - (cursor - data);
@ -292,10 +300,14 @@ namespace coreutils
cursor = data;
}
ZString ZString::getParsed() {
ZString ZString::parsed() {
return ZString(data, cursor - data);
}
ZString ZString::unparsed() {
return ZString(cursor, data + length - cursor);
}
void ZString::reset()
{
cursor = data;

View File

@ -1,3 +1,4 @@
#ifndef __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
/// 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 {
@ -87,6 +94,12 @@ namespace coreutils {
int asInteger();
///
///
///
int incrementCursor(int length);
///
/// Return the value of the ZString from the cursor to the end.
///
@ -213,6 +226,8 @@ namespace coreutils {
char* getCursor();
void setCursor(char *cursor);
///
/// Return the length of the ZString.
///
@ -226,11 +241,19 @@ namespace coreutils {
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 parsed();
///
///
///
ZString getParsed();
ZString unparsed();
///
///
@ -252,7 +275,7 @@ namespace coreutils {
void nextChar();
// protected:
protected:
char *data;
char *cursor;
size_t length;