Compare commits

..

No commits in common. "c40e288a671a6d90d3fd88185f421144e0aa4363" and "d1e5e41ab291af4f2d5a9b4a589d3c15d7bfa1b3" have entirely different histories.

14 changed files with 33 additions and 328 deletions

View File

@ -34,7 +34,7 @@ namespace coreutils {
File::~File() { File::~File() {
close(fd); close(fd);
free(buffer); setBufferSize(0);
} }
void File::setBufferSize(size_t size) { void File::setBufferSize(size_t size) {

2
File.h
View File

@ -17,7 +17,7 @@ namespace coreutils {
public: public:
File(ZString fileName, int mode = O_RDONLY, int authority = 0664); File(ZString fileName, int mode = O_RDONLY, int authority = 0664);
virtual ~File(); ~File();
void setBufferSize(size_t size); void setBufferSize(size_t size);
int read(); int read();
int readLine(); int readLine();

View File

@ -19,12 +19,12 @@ namespace coreutils {
JSONFile(ZString path): JString(), File(path, O_RDWR, 0644) { JSONFile(ZString path): JString(), File(path, O_RDWR, 0644) {
} }
virtual ~JSONFile() { virtual ~JSONFile() {
write(*this); write(*this);
} }
void changed(ZString key, ZString value) overide;
}; };

View File

@ -169,54 +169,5 @@ namespace coreutils {
} }
reset(); reset();
} }
MString JString::pretty() {
MString output;
reset();
char ch;
int index = 0;
while(!eod()) {
ch = nextChar();
if(ch == '{') {
index += 3;
output << ch << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
}
else if(ch == '[') {
index += 3;
output << ch << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
}
else if(ch == ':') {
output << ": ";
}
else if(ch == '}') {
index -= 3;
output << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
output << ch;
}
else if(ch == ']') {
index -= 3;
output << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
output << ch;
}
else if(ch == ',') {
output << ch << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
}
else
output << ch;
}
return output;
}
void JString::changed(ZString key, ZString value) {}
} }

View File

@ -67,9 +67,8 @@ namespace coreutils {
return jstring.find(key).str(); return jstring.find(key).str();
} }
Proxy& operator=(coreutils::MString value) { Proxy& operator=(coreutils::ZString value) {
jstring.set(key, value); jstring.set(key, value);
jstring.changed(key, value);
return *this; return *this;
} }
@ -83,10 +82,6 @@ namespace coreutils {
} }
JString & operator=(coreutils:: ZString value); JString & operator=(coreutils:: ZString value);
MString pretty();
virtual void changed(ZString key, ZString value);
private: private:
void removeWhitespace(); void removeWhitespace();

View File

@ -1,7 +1,6 @@
#include "MString.h" #include "MString.h"
#include "Log.h" #include "Log.h"
#include "uuid/uuid.h" #include "uuid/uuid.h"
#include "Exception.h"
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <format> #include <format>
@ -187,11 +186,13 @@ namespace coreutils {
return *this; return *this;
} }
MString &MString::operator<<(char value) { // MString &MString::operator<<(std::string value) {
setSize(length + 1); // int temp = length;
data[length - 1] = value; // int len = length + value.length();
return *this; // setSize(len);
} // memcpy(data + temp, value.c_str(), value.length());
// return *this;
// }
// MString &MString::operator<<(std::ostream (*f)(std::ostream&)) { // MString &MString::operator<<(std::ostream (*f)(std::ostream&)) {
MString &MString::operator<<(std::basic_ostream<char> (*pf)(std::basic_ostream<char>&)) { MString &MString::operator<<(std::basic_ostream<char> (*pf)(std::basic_ostream<char>&)) {
@ -280,130 +281,5 @@ namespace coreutils {
int MString::offset() { int MString::offset() {
return cursor - data; return cursor - data;
} }
MString MString::toBinary() {
push();
reset();
MString target;
char temp;
while(!eod()) {
temp = nextChar();
if(strchr("\\'\".\0\r\n", temp))
target.write('\\');
target.write(temp);
}
pop();
return target;
}
MString MString::fromBinary() {
push();
reset();
MString target;
while(!eod()) {
if(ifNext("\\r"))
target.write(13);
else if(ifNext("\\n"))
target.write(10);
else if(ifNext("\\0"))
target.write(0);
else if(ifNext("\\\\"))
target.write("\\");
else if(ifNext("\\."))
target.write(".");
else if(ifNext("\\\""))
target.write("\"");
else if(ifNext("\\\'"))
target.write("'");
else
target.write(nextChar());
}
pop();
return target;
}
MString MString::toHex() {
push();
reset();
MString target;
char temp;
while(!eod()) {
temp = nextChar();
char temp2 = temp;
temp >>= 4;
target.write(hexChar(temp));
target.write(hexChar(temp2));
}
pop();
return target;
}
MString MString::fromHex() {
push();
reset();
MString target;
while(!eod()) {
char ch1 = nextChar();
ch1 -= 48;
if(ch1 > 9)
ch1 -= 7;
ch1 <<= 4;
ch1 &= 240;
if(eod())
coreutils::Exception("conversion from hex requires even number of characters.");
char ch2 = nextChar();
ch2 -= 48;
if(ch2 > 9)
ch2 -= 7;
ch2 &= 15;
ch1 |= ch2;
target.write(ch1);
}
pop();
return target;
}
MString MString::toBase64() {
MString target;
return target;
}
MString MString::fromBase64() {
MString target;
return target;
}
MString MString::toUpper() {
MString target;
return target;
}
MString MString::toLower() {
MString target;
return target;
}
MString MString::toCGI() {
MString target;
return target;
}
MString MString::fromCGI() {
MString target;
return target;
}
char MString::hexChar(char c) {
c &= 15;
c += 48;
if(c > 57)
c += 7;
return c;
}
} }

View File

@ -157,7 +157,7 @@ namespace coreutils {
/// ///
/// ///
MString &operator<<(char value); // MString &operator<<(std::string value);
/// ///
/// ///
@ -231,81 +231,14 @@ namespace coreutils {
/// ///
MString &read(int fd); MString &read(int fd);
///
///
///
int offset(); int offset();
///
///
///
MString toBinary();
///
///
///
MString fromBinary();
///
///
///
MString toHex();
///
///
///
MString fromHex();
///
///
///
MString toBase64();
///
///
///
MString fromBase64();
///
///
///
MString toUpper();
///
///
///
MString toLower();
///
///
///
MString toCGI();
///
///
///
MString fromCGI();
protected: protected:
void setSize(int size); void setSize(int size);
private: private:
int bufferSize = 0; int bufferSize = 0;
char hexChar(char c);
}; };
} }

View File

@ -66,8 +66,8 @@ namespace coreutils {
} }
ZString::~ZString() { ZString::~ZString() {
if(cdata != NULL) if(cdata != NULL)
free(cdata); free(cdata);
} }
bool ZString::operator<(const ZString &valuex) const { bool ZString::operator<(const ZString &valuex) const {
@ -110,9 +110,9 @@ namespace coreutils {
return (strncmp(data, valuex.data, length) == 0); return (strncmp(data, valuex.data, length) == 0);
} }
bool ZString::operator!=(const ZString valuex) const { bool ZString::operator!=(const ZString &valuex) const {
if(length != valuex.getLength()) // if(length != valuex.getLength())
return true; // return false;
return (strncmp(data, valuex.data, length) != 0); return (strncmp(data, valuex.data, length) != 0);
} }
@ -137,7 +137,7 @@ namespace coreutils {
++cursor; ++cursor;
if(cursor == end) if(cursor == end)
return 0; return 0;
while((*cursor >= '0') && (*cursor <= '9')) { while((*cursor >- '0') && (*cursor <= '9')) {
value *= 10; value *= 10;
value += *cursor - 48; value += *cursor - 48;
++cursor; ++cursor;
@ -232,10 +232,7 @@ namespace coreutils {
} }
char* ZString::c_str() { char* ZString::c_str() {
if(cdata == NULL) cdata = (char *)malloc(length + 1);
cdata = (char *)malloc(length + 1);
else
cdata = (char *)realloc(cdata, length + 1);
strncpy(cdata, data, length); strncpy(cdata, data, length);
cdata[length] = '\0'; cdata[length] = '\0';
return cdata; return cdata;
@ -255,8 +252,9 @@ namespace coreutils {
char *startChar = data + start; char *startChar = data + start;
char *endChar = startChar + len; char *endChar = startChar + len;
char newlen = endChar > end ? endChar - end: len; char newlen = endChar > end ? endChar - end: len;
if(startChar < end) if(startChar < end) {
return ZString(startChar, newlen); return ZString(startChar, newlen);
}
return ZString(); return ZString();
} }
@ -362,12 +360,6 @@ namespace coreutils {
return cursor >= data + length; return cursor >= data + length;
} }
int ZString::goeod() {
char *temp = cursor;
cursor = data + length;
return cursor - temp;
}
bool ZString::startsWith(const char *value) { bool ZString::startsWith(const char *value) {
return strncmp(cursor, value, strlen(value)) == 0; return strncmp(cursor, value, strlen(value)) == 0;
} }
@ -452,28 +444,11 @@ namespace coreutils {
} }
int ZString::skipWhitespace() { int ZString::skipWhitespace() {
char *end = data + length; int len = strspn(cursor, " \n\t");
int len = 0; cursor += len;
while((cursor < end) && ((*cursor == ' ') || (*cursor == '\n') || (*cursor == '\t'))) {
++cursor;
++len;
}
return len; return len;
} }
int ZString::trimTrailingWhitespace() {
goeod();
int len = 0;
--cursor;
while((cursor > data) && ((*cursor == ' ') || (*cursor == '\n') || (*cursor == '\t'))) {
--cursor;
--length;
++len;
}
++cursor;
return len;
}
bool ZString::lineIsWhitespace() { bool ZString::lineIsWhitespace() {
char *end = data + length; char *end = data + length;
char *temp = cursor; char *temp = cursor;
@ -515,8 +490,6 @@ namespace coreutils {
} }
ZString ZString::trim() { ZString ZString::trim() {
trimTrailingWhitespace();
reset();
skipWhitespace(); skipWhitespace();
return unparsed(); return unparsed();
} }

View File

@ -84,7 +84,7 @@ namespace coreutils {
/// Destructor for the ZString. /// Destructor for the ZString.
/// ///
virtual ~ZString(); ~ZString();
/// ///
/// A friend method used to write the value of the ZString /// A friend method used to write the value of the ZString
@ -115,7 +115,7 @@ namespace coreutils {
bool operator<=(const ZString &valuex) const; bool operator<=(const ZString &valuex) const;
bool operator>=(const ZString &valuex) const; bool operator>=(const ZString &valuex) const;
bool operator==(const ZString valuex); bool operator==(const ZString valuex);
bool operator!=(const ZString valuex) const; bool operator!=(const ZString &valuex) const;
bool operator!=(const char *valuex) const; bool operator!=(const char *valuex) const;
/// ///
@ -248,14 +248,6 @@ namespace coreutils {
/// ///
bool eod(); bool eod();
///
/// Go to the end of data.
///
/// Return number of bytes the cursor was moved.
///
int goeod();
/// ///
/// ///
@ -320,7 +312,7 @@ namespace coreutils {
/// ///
/// Advance the cursor through the ZString for each whitespace character /// Advance the cursor through the ZString for each whitespace character
/// encountered. /// encountered.
/// ///
int skipWhitespace(); int skipWhitespace();
@ -329,12 +321,6 @@ namespace coreutils {
/// ///
/// ///
int trimTrailingWhitespace();
///
///
///
bool lineIsWhitespace(); bool lineIsWhitespace();
/// ///
@ -468,9 +454,9 @@ namespace coreutils {
void moveBackToLineStart(); void moveBackToLineStart();
protected: protected:
char *data = NULL; char *data;
char *cursor = NULL; char *cursor;
size_t length = 0; size_t length;
char *cdata = NULL; char *cdata = NULL;
std::vector<ZString> list; std::vector<ZString> list;

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils #g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils
g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils

Binary file not shown.

View File

@ -27,7 +27,6 @@ int main(int argc, char **argv) {
test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}"; test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}";
test1["object1.attr2"] = "{\"xattr1\":\"xvalue1\",\"xattr2\":\"xvalue2\",\"xattr3\":\"xvalue3\"}"; test1["object1.attr2"] = "{\"xattr1\":\"xvalue1\",\"xattr2\":\"xvalue2\",\"xattr3\":\"xvalue3\"}";
test1["object1.attr3"] = "Im not an object"; test1["object1.attr3"] = "Im not an object";
// test1["age"] = 64; future
std::cout << test1 << std::endl; std::cout << test1 << std::endl;
@ -59,8 +58,6 @@ int main(int argc, char **argv) {
test9 = test1["object1"]; test9 = test1["object1"];
std::cout << test9 << std::endl; std::cout << test9 << std::endl;
std::cout << test9["attr3"] << std::endl; std::cout << test9["attr3"] << std::endl;
std::cout << test1.pretty() << std::endl;
return 0; return 0;
} }

View File

@ -94,9 +94,6 @@ int main(int argc, char **argv) {
int test29 = 402; int test29 = 402;
test28 = test29; test28 = test29;
std::cout << "int: " << test28 << std::endl; std::cout << "int: " << test28 << std::endl;
coreutils::MString test30 = "ABCDEF";
std::cout << test30 << " to hex " << test30.toHex() << " from hex " << test30.toHex().fromHex() << std::endl;
return 0; return 0;
} }

View File

@ -122,8 +122,5 @@ int main(int argc, char **argv) {
std::cout << "integer '53524534' is [" << test24 << "]" << std::endl; std::cout << "integer '53524534' is [" << test24 << "]" << std::endl;
coreutils::ZString test25("-543"); coreutils::ZString test25("-543");
std::cout << "integer '-543' is [" << test25 << "]" << std::endl; std::cout << "integer '-543' is [" << test25 << "]" << std::endl;
coreutils::ZString test26(" this is a trim test ");
std::cout << "trimmed [" << test26.trim() << "]" << std::endl;
} }