Functionnal json 2 bin example
This commit is contained in:
commit
8466ba02ac
|
@ -0,0 +1,2 @@
|
|||
build
|
||||
json2bin_converter
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"files.associations": {
|
||||
"complex": "cpp",
|
||||
"string": "cpp",
|
||||
"iterator": "cpp",
|
||||
"string_view": "cpp",
|
||||
"random": "cpp",
|
||||
"fstream": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"istream": "cpp",
|
||||
"ostream": "cpp",
|
||||
"sstream": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"system_error": "cpp"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"label": "Build",
|
||||
"detail": "Build project",
|
||||
"type": "shell",
|
||||
"command": "make",
|
||||
"group": "build",
|
||||
"presentation": {
|
||||
"echo": true,
|
||||
"reveal": "always",
|
||||
"focus": true,
|
||||
"panel": "shared",
|
||||
"showReuseMessage": true,
|
||||
"clear": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "Clean",
|
||||
"detail": "Clean build directory",
|
||||
"type": "shell",
|
||||
"command": "make",
|
||||
"args": [
|
||||
"clean"
|
||||
],
|
||||
"group": "build",
|
||||
"presentation": {
|
||||
"echo": true,
|
||||
"reveal": "never",
|
||||
"focus": false,
|
||||
"panel": "shared",
|
||||
"showReuseMessage": true,
|
||||
"clear": false
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
# Compiler flags
|
||||
CFLAGS = -std=c++17
|
||||
|
||||
# Defaul target
|
||||
all: build_dir json2bin_converter
|
||||
|
||||
# Create build directory
|
||||
build_dir:
|
||||
mkdir -p build
|
||||
|
||||
# Link target
|
||||
json2bin_converter: build/main.o build/bin.o
|
||||
g++ -o json2bin_converter build/main.o build/bin.o
|
||||
|
||||
# Compile objects
|
||||
build/main.o: src/main.cpp src/bin.hpp src/json/json.hpp
|
||||
g++ $(CFLAGS) -c src/main.cpp -o build/main.o
|
||||
|
||||
build/bin.o: src/bin.cpp src/bin.hpp src/json/json.hpp
|
||||
g++ $(CFLAGS) -c src/bin.cpp -o build/bin.o
|
||||
|
||||
# Clean
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf build/* json2bin_converter
|
|
@ -0,0 +1,7 @@
|
|||
#include "bin.hpp"
|
||||
|
||||
void load_json_into_bin(json *json_input, Bin *bin_output)
|
||||
{
|
||||
bin_output->value1 = (*json_input)["value1"].get<uint8_t>();
|
||||
bin_output->value2 = (*json_input)["value2"].get<uint8_t>();
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef BIN_H
|
||||
#define BIN_H
|
||||
|
||||
#include "json/json.hpp"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
struct Bin {
|
||||
uint8_t value1;
|
||||
uint8_t value2;
|
||||
};
|
||||
|
||||
void load_json_into_bin(json *json_input, Bin *bin_output);
|
||||
|
||||
#endif // BIN_H
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,43 @@
|
|||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "bin.hpp"
|
||||
|
||||
void error(std::string error);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if(argc != 3)
|
||||
{
|
||||
error("You need to give an input and output file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
std::ifstream json_input_file {argv[1]};
|
||||
if(!json_input_file)
|
||||
{
|
||||
error("Couldn't open input file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
std::ofstream bin_output_file {argv[2], std::ios::binary};
|
||||
if(!bin_output_file)
|
||||
{
|
||||
error("Couldn't create output file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
json json_input = json::parse(json_input_file);
|
||||
Bin bin_output;
|
||||
|
||||
load_json_into_bin(&json_input, &bin_output);
|
||||
|
||||
bin_output_file.write(reinterpret_cast<char*>(&bin_output), sizeof(Bin));
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void error(std::string error)
|
||||
{
|
||||
std::cerr << "\033[0;1;31mError\033[m : " << error << "\n";
|
||||
}
|
Loading…
Reference in New Issue