From a87ce0bab72893196db75cad7b48d503fff39d16 Mon Sep 17 00:00:00 2001 From: brad Arant Date: Sat, 17 May 2025 20:47:09 -0700 Subject: [PATCH] JSON file changes --- JSONFile.h | 33 --------------------------------- MFile.cpp | 20 ++++++++++++++++++++ MFile.h | 35 +++++++++++++++++++++++++++++++++++ MString.cpp | 51 ++++++++++++++++++++++++++++++++++++++------------- MString.h | 12 ++++++++++++ 5 files changed, 105 insertions(+), 46 deletions(-) delete mode 100644 JSONFile.h create mode 100644 MFile.cpp create mode 100644 MFile.h diff --git a/JSONFile.h b/JSONFile.h deleted file mode 100644 index 3f7c112..0000000 --- a/JSONFile.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef __JSONFile_h__ -#define __JSONFile_h__ - -#include "JString.h" -#include "File.h" -#include -#include - -namespace coreutils { - - /// - /// Use the JSONFile object where you need a file based persistent backing store - /// for the JString style object. - /// - - class JSONFile : public JString : public File { - - public: - JSONFile(ZString path): JString(), File(path, O_RDWR, 0644) { - - } - - virtual ~JSONFile() { - write(*this); - } - - void changed(ZString key, ZString value) overide; - - }; - -} - -#endif diff --git a/MFile.cpp b/MFile.cpp new file mode 100644 index 0000000..652c99c --- /dev/null +++ b/MFile.cpp @@ -0,0 +1,20 @@ +#include "MFile.h" +#include "Exception.h" +#include + +namespace coreutils { + + MFile::MFile(coreutils::MString fileName) : fileName(fileName) { + inFile.open(fileName.c_str()); + *this << inFile; + inFile.close(); + } + + void MFile::update() { + outFile.open(fileName.c_str()); + outFile << *this; + outFile.close(); + } + + +} diff --git a/MFile.h b/MFile.h new file mode 100644 index 0000000..fd8e928 --- /dev/null +++ b/MFile.h @@ -0,0 +1,35 @@ +#ifndef __MFile_h__ +#define __MFile_h__ + +#include "MString.h" +#include +#include + +/// +/// MFile +/// +/// An MString with a file backing store. +/// + +namespace coreutils { + + class MFile : public MString { + + public: + MFile(MString fileName); + virtual ~MFile(); + + coreutils::MString fileName; + + private: + std::ifstream inFile; + std::ofstream outFile; + + void onChange() override; + void update(); + + }; + +} + +#endif diff --git a/MString.cpp b/MString.cpp index 73fa121..9ce70fa 100644 --- a/MString.cpp +++ b/MString.cpp @@ -107,27 +107,31 @@ namespace coreutils { setSize(value.getLength()); memcpy(data, value.getData(), value.getLength()); length = value.getLength(); + onChange(); return *this; } - MString &MString::operator=(coreutils::ZString value) { - setSize(value.getLength()); - memcpy(data, value.getData(), value.getLength()); - length = value.getLength(); - return *this; - } - - MString &MString::operator=(std::string value) { - setSize(value.length()); - memcpy(data, value.c_str(), value.length()); - length = value.length(); - return *this; + MString &MString::operator=(coreutils::ZString value) { + setSize(value.getLength()); + memcpy(data, value.getData(), value.getLength()); + length = value.getLength(); + onChange(); + return *this; } + MString &MString::operator=(std::string value) { + setSize(value.length()); + memcpy(data, value.c_str(), value.length()); + length = value.length(); + onChange(); + return *this; + } + MString &MString::operator=(const char *value) { int len = strlen(value); setSize(len); memcpy(data, value, len); + onChange(); return *this; } @@ -135,6 +139,7 @@ namespace coreutils { int len = 1; setSize(1); *data = value; + onChange(); return *this; } @@ -146,6 +151,7 @@ namespace coreutils { setSize(temp.length()); memcpy(this->data, (char *)temp.data(), temp.length()); cursor = this->data; + onChange(); return *this; } @@ -154,6 +160,7 @@ namespace coreutils { setSize(temp.length()); memcpy(this->data, (char *)temp.data(), temp.length()); cursor = this->data; + onChange(); return *this; } @@ -162,6 +169,7 @@ namespace coreutils { int len = strlen(value); setSize(len + length); memcpy(data + temp, value, len); + onChange(); return *this; } @@ -170,6 +178,7 @@ namespace coreutils { temp << value; MString temp2 = temp.str(); write(temp2); + onChange(); return *this; } @@ -186,12 +195,14 @@ namespace coreutils { int len = length + zstring.getLength(); setSize(len); memcpy(data + temp, zstring.getData(), zstring.getLength()); + onChange(); return *this; } MString &MString::operator<<(char value) { setSize(length + 1); data[length - 1] = value; + onChange(); return *this; } @@ -204,12 +215,17 @@ namespace coreutils { std::cout << "xxx" << std::endl; return *this; } + + MString &operator<<(std::ostream &out) { + *this << out; + } void MString::insert(char ch, int offset) { setSize(length + 1); for (int ix = length; ix >= offset; ix--) getData()[ix + 1] = getData()[ix]; data[offset] = ch; + onChange(); } void MString::insert(ZString value, int offset) { @@ -217,6 +233,7 @@ namespace coreutils { for (int ix = length; ix >= offset; ix--) getData()[ix] = getData()[ix - value.getLength()]; std::memcpy(data + offset, value.getData(), value.getLength()); + onChange(); } // void MString::insert(std::string value, int offset) { @@ -240,17 +257,20 @@ namespace coreutils { for (int ix = 0; ix < len; ix++) cursor[ix] = cursor[ix + length]; setSize(this->length - length); + onChange(); } void MString::remove(int offset, int length) { for (int ix = offset; ix < this->length; ix++) getData()[ix] = getData()[ix + length]; setSize(this->length - length); + onChange(); } MString &MString::write(char ch) { setSize(length + 1); *(data + length - 1) = ch; + onChange(); return *this; } @@ -258,6 +278,7 @@ namespace coreutils { int len = length; setSize(length + value.getLength()); memcpy(data + len, value.getData(), value.getLength()); + onChange(); return *this; } @@ -276,6 +297,7 @@ namespace coreutils { char ch; while(::read(fd, &ch, 1)) write(ch); + onChange(); return *this; } @@ -283,6 +305,7 @@ namespace coreutils { char ch; while(count-- && ::read(fd, &ch, 1)) write(ch); + onChange(); return *this; } @@ -418,5 +441,7 @@ namespace coreutils { c += 7; return c; } - + + void MString::onChange() {} + } diff --git a/MString.h b/MString.h index 46f90df..4fa32e9 100644 --- a/MString.h +++ b/MString.h @@ -165,6 +165,12 @@ namespace coreutils { // MString &operator<<(std::ostream (*f)(std::ostream&) ); MString &operator<<(std::basic_ostream (*pf)(std::basic_ostream&)); + + /// + /// + /// + + MString &operator<<(std::ostream &out); /// /// @@ -302,6 +308,12 @@ namespace coreutils { protected: void setSize(int size); + + /// + /// This method is called whenever a change is made to the MString storage. + /// + + virtual void onChange(); private: int bufferSize = 0;