JSON file changes

This commit is contained in:
brad Arant 2025-05-17 20:47:09 -07:00
parent 9cd8381845
commit a87ce0bab7
5 changed files with 105 additions and 46 deletions

View File

@ -1,33 +0,0 @@
#ifndef __JSONFile_h__
#define __JSONFile_h__
#include "JString.h"
#include "File.h"
#include <iostream>
#include <sstream>
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

20
MFile.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "MFile.h"
#include "Exception.h"
#include <sys/stat.h>
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();
}
}

35
MFile.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef __MFile_h__
#define __MFile_h__
#include "MString.h"
#include <fstream>
#include <iostream>
///
/// 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

View File

@ -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() {}
}

View File

@ -165,6 +165,12 @@ namespace coreutils {
// MString &operator<<(std::ostream (*f)(std::ostream&) );
MString &operator<<(std::basic_ostream<char> (*pf)(std::basic_ostream<char>&));
///
///
///
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;