deserialize_file
This commit is contained in:
parent
98ddb215e9
commit
6f20af4b2c
3 changed files with 39 additions and 20 deletions
23
main.cpp
23
main.cpp
|
@ -570,28 +570,11 @@ int main(int argc, char *argv[])
|
|||
set_boot_loader(b, bootloader);
|
||||
}
|
||||
else {
|
||||
FILE *fh = fopen(deserialize.c_str(), "r");
|
||||
if (!fh)
|
||||
auto rc = deserialize_file(deserialize);
|
||||
if (rc.has_value() == false)
|
||||
error_exit(true, "Failed to open %s", deserialize.c_str());
|
||||
|
||||
std::string j_in;
|
||||
char buffer[4096];
|
||||
for(;;) {
|
||||
char *rc = fgets(buffer, sizeof buffer, fh);
|
||||
if (!rc)
|
||||
break;
|
||||
|
||||
j_in += buffer;
|
||||
}
|
||||
|
||||
fclose(fh);
|
||||
|
||||
JsonDocument j;
|
||||
DeserializationError error = deserializeJson(j, j_in);
|
||||
if (error)
|
||||
error_exit(true, "State file %s is corrupt: %s", deserialize.c_str(), error.c_str());
|
||||
|
||||
b = bus::deserialize(j, cnsl, &event);
|
||||
b = bus::deserialize(rc.value(), cnsl, &event);
|
||||
|
||||
myusleep(251000);
|
||||
}
|
||||
|
|
32
utils.cpp
32
utils.cpp
|
@ -17,7 +17,9 @@
|
|||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <errno.h>
|
||||
#include <optional>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
|
@ -269,3 +271,33 @@ std::string get_endpoint_name(const int fd)
|
|||
|
||||
return std::string(inet_ntoa(addr.sin_addr)) + "." + format("%d", ntohs(addr.sin_port));
|
||||
}
|
||||
|
||||
std::optional<JsonDocument> deserialize_file(const std::string & filename)
|
||||
{
|
||||
FILE *fh = fopen(filename.c_str(), "r");
|
||||
if (!fh) {
|
||||
DOLOG(warning, false, "Failed to open %s", filename.c_str());
|
||||
return { };
|
||||
}
|
||||
|
||||
std::string j_in;
|
||||
char buffer[4096];
|
||||
for(;;) {
|
||||
char *rc = fgets(buffer, sizeof buffer, fh);
|
||||
if (!rc)
|
||||
break;
|
||||
|
||||
j_in += buffer;
|
||||
}
|
||||
|
||||
fclose(fh);
|
||||
|
||||
JsonDocument j;
|
||||
DeserializationError error = deserializeJson(j, j_in);
|
||||
if (error) {
|
||||
DOLOG(warning, false, "Failed to de-serialize %s", filename.c_str());
|
||||
return { };
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
|
|
4
utils.h
4
utils.h
|
@ -1,6 +1,8 @@
|
|||
// (C) 2018-2024 by Folkert van Heusden
|
||||
// Released under MIT license
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <optional>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
@ -28,3 +30,5 @@ void update_word(uint16_t *const w, const bool msb, const uint8_t v);
|
|||
|
||||
void set_nodelay(const int fd);
|
||||
std::string get_endpoint_name(const int fd);
|
||||
|
||||
std::optional<JsonDocument> deserialize_file(const std::string & filename);
|
||||
|
|
Loading…
Add table
Reference in a new issue