Developed MString.
This commit is contained in:
parent
91286649cc
commit
59228ad356
74
MString.cpp
Normal file
74
MString.cpp
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
60
MString.h
Normal file
60
MString.h
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef __MString_h__
|
||||
#define __MString_h__
|
||||
|
||||
#include "ZString.h"
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
///
|
||||
/// Use the MString object when you need a permanent backing store on the heap
|
||||
/// for a ZString style functionality.
|
||||
///
|
||||
|
||||
class MString : public ZString {
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
MString();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
MString(const char *data);
|
||||
|
||||
///
|
||||
/// Consructor providing the initial setup of the ZString object.
|
||||
///
|
||||
|
||||
MString(char *data, size_t length);
|
||||
|
||||
MString(const char *data, size_t length);
|
||||
|
||||
///
|
||||
/// Consructor providing a copy of a ZString.
|
||||
///
|
||||
|
||||
MString(const MString &zstring);
|
||||
|
||||
///
|
||||
/// Consructor from a string.
|
||||
///
|
||||
|
||||
MString(std::string string);
|
||||
|
||||
~MString();
|
||||
|
||||
int write(char ch);
|
||||
|
||||
private:
|
||||
int bufferSize = 0;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
39
ZString.cpp
39
ZString.cpp
@ -6,9 +6,9 @@ namespace coreutils
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const ZString &zstring)
|
||||
{
|
||||
for (int ix = 0; ix < (zstring.length - (zstring.cursor - zstring.data)); ++ix)
|
||||
for (char *ix = zstring.cursor; ix < (zstring.data + zstring.length); ++ix)
|
||||
{
|
||||
os << zstring.cursor[ix];
|
||||
os << *ix;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
@ -53,9 +53,8 @@ namespace coreutils
|
||||
cursor = (char *)data.c_str();
|
||||
}
|
||||
|
||||
bool ZString::operator<(const ZString &valuex) const
|
||||
{
|
||||
return (strncmp(cursor, valuex.cursor, (valuex.length <= length ? valuex.length : length) < 0));
|
||||
bool ZString::operator<(const ZString &valuex) const {
|
||||
return (strncmp(cursor, valuex.cursor, (valuex.length <= length ? valuex.length : length)) < 0);
|
||||
}
|
||||
|
||||
bool ZString::operator>(const ZString &valuex) const
|
||||
@ -166,6 +165,10 @@ namespace coreutils
|
||||
return cursor >= data + length;
|
||||
}
|
||||
|
||||
bool ZString::startsWith(const char *value) {
|
||||
return strncmp(cursor, value, strlen(value)) == 0;
|
||||
}
|
||||
|
||||
bool ZString::equals(const char *value)
|
||||
{
|
||||
if (strlen(value) != length)
|
||||
@ -202,6 +205,15 @@ namespace coreutils
|
||||
return test;
|
||||
}
|
||||
|
||||
bool ZString::ifNext(ZString &value) {
|
||||
if (((data + length) - cursor) < value.getLength())
|
||||
return false;
|
||||
bool test = (strncmp(cursor, value.getCursor(), value.getLength()) == 0);
|
||||
if (test)
|
||||
cursor += value.getLength();
|
||||
return test;
|
||||
}
|
||||
|
||||
int ZString::ifEqualsCount(ZString &comparator)
|
||||
{
|
||||
int count = 0;
|
||||
@ -223,7 +235,7 @@ namespace coreutils
|
||||
|
||||
int ZString::skipWhitespace()
|
||||
{
|
||||
int len = strspn(cursor, " \t");
|
||||
int len = strspn(cursor, " \n\t");
|
||||
cursor += len;
|
||||
return len;
|
||||
}
|
||||
@ -264,6 +276,10 @@ namespace coreutils
|
||||
return data;
|
||||
}
|
||||
|
||||
char *ZString::getCursor() {
|
||||
return cursor;
|
||||
}
|
||||
|
||||
size_t ZString::getLength()
|
||||
{
|
||||
return length - (cursor - data);
|
||||
@ -276,6 +292,10 @@ namespace coreutils
|
||||
cursor = data;
|
||||
}
|
||||
|
||||
ZString ZString::getParsed() {
|
||||
return ZString(data, cursor - data);
|
||||
}
|
||||
|
||||
void ZString::reset()
|
||||
{
|
||||
cursor = data;
|
||||
@ -283,7 +303,7 @@ namespace coreutils
|
||||
|
||||
char ZString::charAt(int index)
|
||||
{
|
||||
return *(data + index);
|
||||
return *(cursor + index);
|
||||
}
|
||||
|
||||
bool ZString::ifCRLF()
|
||||
@ -298,4 +318,9 @@ namespace coreutils
|
||||
return len != length;
|
||||
}
|
||||
|
||||
void ZString::nextChar() {
|
||||
if(!eod())
|
||||
++cursor;
|
||||
}
|
||||
|
||||
}
|
||||
|
35
ZString.h
35
ZString.h
@ -29,7 +29,7 @@ namespace coreutils {
|
||||
public:
|
||||
|
||||
///
|
||||
/// Consructor providing an empty ZString..
|
||||
/// Consructor providing an empty ZString.
|
||||
///
|
||||
|
||||
ZString();
|
||||
@ -144,6 +144,12 @@ namespace coreutils {
|
||||
///
|
||||
///
|
||||
|
||||
bool startsWith(const char *value);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
bool equals(const char *value);
|
||||
|
||||
///
|
||||
@ -171,6 +177,8 @@ namespace coreutils {
|
||||
|
||||
bool ifNext(const char *value);
|
||||
|
||||
bool ifNext(ZString &value);
|
||||
|
||||
int ifEqualsCount(ZString &comparator);
|
||||
|
||||
///
|
||||
@ -199,6 +207,12 @@ namespace coreutils {
|
||||
|
||||
char* getData();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
char* getCursor();
|
||||
|
||||
///
|
||||
/// Return the length of the ZString.
|
||||
///
|
||||
@ -216,6 +230,12 @@ namespace coreutils {
|
||||
///
|
||||
///
|
||||
|
||||
ZString getParsed();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
void reset();
|
||||
|
||||
///
|
||||
@ -230,12 +250,15 @@ namespace coreutils {
|
||||
|
||||
bool ifCRLF();
|
||||
|
||||
private:
|
||||
char *data;
|
||||
size_t length;
|
||||
char *cursor;
|
||||
std::vector<ZString> list;
|
||||
void nextChar();
|
||||
|
||||
// protected:
|
||||
char *data;
|
||||
char *cursor;
|
||||
size_t length;
|
||||
|
||||
private:
|
||||
std::vector<ZString> list;
|
||||
bool isCharacter(char ch, const char *string);
|
||||
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user