JSON file changes
This commit is contained in:
parent
9cd8381845
commit
a87ce0bab7
33
JSONFile.h
33
JSONFile.h
@ -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
20
MFile.cpp
Normal 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
35
MFile.h
Normal 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
|
25
MString.cpp
25
MString.cpp
@ -107,6 +107,7 @@ namespace coreutils {
|
||||
setSize(value.getLength());
|
||||
memcpy(data, value.getData(), value.getLength());
|
||||
length = value.getLength();
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -114,6 +115,7 @@ namespace coreutils {
|
||||
setSize(value.getLength());
|
||||
memcpy(data, value.getData(), value.getLength());
|
||||
length = value.getLength();
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -121,6 +123,7 @@ namespace coreutils {
|
||||
setSize(value.length());
|
||||
memcpy(data, value.c_str(), value.length());
|
||||
length = value.length();
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -128,6 +131,7 @@ namespace coreutils {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -205,11 +216,16 @@ namespace coreutils {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -419,4 +442,6 @@ namespace coreutils {
|
||||
return c;
|
||||
}
|
||||
|
||||
void MString::onChange() {}
|
||||
|
||||
}
|
||||
|
12
MString.h
12
MString.h
@ -170,6 +170,12 @@ namespace coreutils {
|
||||
///
|
||||
///
|
||||
|
||||
MString &operator<<(std::ostream &out);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
void insert(char ch, int offset);
|
||||
|
||||
///
|
||||
@ -303,6 +309,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;
|
||||
char hexChar(char c);
|
||||
|
Loading…
x
Reference in New Issue
Block a user