You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
4.1 KiB
195 lines
4.1 KiB
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "modelbahn.h"
|
|
|
|
int Server::Load() {
|
|
string f = DEFAULT_DATADIR;
|
|
f = f + "/track.json";
|
|
|
|
Load (f);
|
|
|
|
return 1;
|
|
};
|
|
|
|
int Server::Save() {
|
|
struct stat sbuf;
|
|
string f = DEFAULT_DATADIR;
|
|
string nf;
|
|
|
|
//
|
|
// check if the folder does exists.
|
|
if (stat (f.c_str(), &sbuf) != 0) {
|
|
debug (DEBUG_ERROR, "* Track::Save could not find directory '%s'. Creating it.", f.c_str());
|
|
if (mkdir (f.c_str(), 0755) != 0) {
|
|
debug (DEBUG_ERROR, "* Track::Save could not create directory '%s'. (%s)", f.c_str(), strerror(errno));
|
|
return 0;
|
|
}
|
|
}
|
|
if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
|
|
debug (DEBUG_ERROR, "* Track::Save could not stat directory: '%s'. (%s)", f.c_str(), strerror(errno));
|
|
if (errno != EINTR) return 0;
|
|
}
|
|
|
|
//
|
|
// check if the file exsists
|
|
f = DEFAULT_DATADIR; f = f + "/track.json";
|
|
nf = DEFAULT_DATADIR; nf = nf + "/track.json.backup." + to_string(time(NULL));
|
|
if (stat (f.c_str(), &sbuf) == 0) {
|
|
debug (DEBUG_INFO, "* Track::Save rename backup file to: %s", nf.c_str());
|
|
if (rename (f.c_str(), nf.c_str())) {
|
|
debug (DEBUG_ERROR, "* Track::Save could not rename file '%s' to '%s'. Error:(%s)",
|
|
f.c_str(), nf.c_str(), strerror(errno));
|
|
}
|
|
}
|
|
|
|
return Save(f);
|
|
};
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
//
|
|
// loading and saving of the file is done here. We do no checks
|
|
// here.
|
|
//
|
|
int Server::Load(string fn) {
|
|
int fd;
|
|
size_t len;
|
|
struct stat sbuf;
|
|
JSONParse json;
|
|
JSONParse jtmp;
|
|
char *buf = NULL;
|
|
string s;
|
|
int x, y, i;
|
|
|
|
debug (DEBUG_INFO, "* Load File:%s", fn.c_str());
|
|
if (stat (fn.c_str(), &sbuf) == 0) {
|
|
buf = (char *) malloc(sbuf.st_size+1);
|
|
memset (buf, 0x0, sbuf.st_size+1);
|
|
fd = open (fn.c_str(), O_RDONLY);
|
|
len = read (fd, buf, sbuf.st_size);
|
|
close (fd);
|
|
|
|
// everything read?
|
|
if (len < sbuf.st_size) {
|
|
free (buf);
|
|
debug (DEBUG_ERROR, "* Reading Track File Failed. (len < filesize)");
|
|
return 0;
|
|
}
|
|
|
|
s = buf;
|
|
json.Set(s);
|
|
}
|
|
else {
|
|
debug (DEBUG_ERROR, "* cloud not stat file (%s)", strerror(errno));
|
|
return 0;
|
|
}
|
|
|
|
//
|
|
// json holds all the data
|
|
|
|
//
|
|
// read track values
|
|
json.GetObject("track", &jtmp);
|
|
jtmp.GetValueInt("width", &x);
|
|
jtmp.GetValueInt("height", &y);
|
|
railways.New (x, y);
|
|
|
|
//
|
|
// read railways
|
|
Railway rw;
|
|
for (i = 0; json.GetObjectIdx("railways", i, &jtmp); i++) {
|
|
rw = railways.GetRailwayFromJSON(&jtmp);
|
|
railways.Change(&rw);
|
|
}
|
|
|
|
//
|
|
// read interfaces
|
|
Interface iface;
|
|
for (i = 0; json.GetObjectIdx("interfaces", i, &jtmp); i++) {
|
|
iface = interfaces.GetInterfaceFromJSON(&jtmp);
|
|
interfaces.Change(&iface);
|
|
}
|
|
|
|
//
|
|
// read locomotives
|
|
Locomotive loco;
|
|
for (i = 0; json.GetObjectIdx("locomotives", i, &jtmp); i++) {
|
|
loco = locomotives.GetLocomotiveFromJSON(&jtmp);
|
|
locomotives.Change(&loco);
|
|
}
|
|
|
|
//
|
|
// read sensors
|
|
Sensor se;
|
|
for (i = 0; json.GetObjectIdx("sensors", i, &jtmp); i++) {
|
|
se = sensors.GetSensorFromJSON(&jtmp);
|
|
sensors.Change(&se);
|
|
}
|
|
|
|
//
|
|
// read turnouts
|
|
Turnout to;
|
|
for (i = 0; json.GetObjectIdx("turnouts", i, &jtmp); i++) {
|
|
to = turnouts.GetTurnoutFromJSON(&jtmp);
|
|
turnouts.Change(&to);
|
|
}
|
|
|
|
|
|
railways.ClearChanged();
|
|
return 1;
|
|
};
|
|
|
|
|
|
int Server::Save(string fn) {
|
|
FILE *f;
|
|
JSONParse json;
|
|
JSONElement je;
|
|
int x, y, cnt;
|
|
|
|
debug (DEBUG_INFO, "* Save File:%s", fn.c_str());
|
|
json.Clear();
|
|
json.AddObject("track", railways.GetJSONTrack());
|
|
|
|
//
|
|
// write all railway data
|
|
// create json object array manualy
|
|
je.type = JSON_T_ARRAY;
|
|
je.name = "railways";
|
|
for (cnt = 0, x = 0; x < railways.GetWidth(); x++) for (y = 0; y < railways.GetHeight(); y++)
|
|
if (railways.Get(x, y).type != RAILWAY_NOTHING) {
|
|
if (cnt != 0) je.value += ","; // not first element
|
|
je.value += railways.GetJSONRailway(x, y).ToString();
|
|
cnt++;
|
|
}
|
|
json.AddObject(je);
|
|
|
|
//
|
|
// write all interfaces
|
|
interfaces.GetJSONAll(&json);
|
|
|
|
//
|
|
// write all locomotives
|
|
locomotives.GetJSONAll(&json);
|
|
|
|
//
|
|
// write all sensors
|
|
sensors.GetJSONAll(&json);
|
|
|
|
//
|
|
// write all turnouts
|
|
turnouts.GetJSONAll(&json);
|
|
|
|
|
|
f = fopen (fn.c_str(), "w");
|
|
fprintf (f, "%s", json.ToString().c_str());
|
|
fclose (f);
|
|
|
|
railways.ClearChanged();
|
|
|
|
return 1;
|
|
};
|
|
|