Json2Bin_Converter Copy
This commit is contained in:
commit
31000d0126
|
@ -0,0 +1,65 @@
|
||||||
|
# Build folder
|
||||||
|
BUILD_DIR = build
|
||||||
|
|
||||||
|
# Source files
|
||||||
|
SOURCES = \
|
||||||
|
src/main.cpp \
|
||||||
|
src/bin.cpp
|
||||||
|
|
||||||
|
# Output file name
|
||||||
|
OUTPUT = 2D_Engine_Casio_Tool
|
||||||
|
|
||||||
|
# Compiler and flags
|
||||||
|
CXX = g++
|
||||||
|
CXXFLAGS = -std=c++17 -g
|
||||||
|
LDFLAGS =
|
||||||
|
|
||||||
|
# Deduce objects
|
||||||
|
OBJECTS = $(SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
||||||
|
|
||||||
|
# Verbose mode
|
||||||
|
ifeq ($(VERBOSE), 1)
|
||||||
|
Q :=
|
||||||
|
else
|
||||||
|
Q := @
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
GREEN := \033[0;32m
|
||||||
|
YELLOW := \033[1;33m
|
||||||
|
RESET := \033[0m
|
||||||
|
|
||||||
|
# Total number of files to process
|
||||||
|
TOTAL_FILES := $(shell echo $$(($(words $(SOURCES))+1)))
|
||||||
|
CURRENT_FILE = 0
|
||||||
|
|
||||||
|
# Default targets
|
||||||
|
all: $(BUILD_DIR) $(OUTPUT) end
|
||||||
|
|
||||||
|
# Create build directory
|
||||||
|
$(BUILD_DIR):
|
||||||
|
$(Q)mkdir -p $(dir $(OBJECTS))
|
||||||
|
|
||||||
|
# Link target
|
||||||
|
$(OUTPUT): $(OBJECTS)
|
||||||
|
@echo "[100%] $(YELLOW)Linking $(OUTPUT)$(RESET)"
|
||||||
|
$(Q)$(CXX) $(LDFLAGS) -o $@ $(OBJECTS)
|
||||||
|
|
||||||
|
# Build .o files from .cpp
|
||||||
|
$(BUILD_DIR)/%.o: %.cpp
|
||||||
|
$(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE)+1))))
|
||||||
|
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE)*100/$(TOTAL_FILES)))))
|
||||||
|
@echo "[$(PERCENTAGE)%] $(GREEN)Building $@$(RESET)"
|
||||||
|
$(Q)mkdir -p $(dir $@)
|
||||||
|
$(Q)$(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
# Source files dependencies
|
||||||
|
-include $(OBJECTS:.o=.d)
|
||||||
|
|
||||||
|
end:
|
||||||
|
@echo "Built target $(OUTPUT)"
|
||||||
|
|
||||||
|
# Clean
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
$(Q)rm -rf $(BUILD_DIR)
|
|
@ -0,0 +1,17 @@
|
||||||
|
Json2Bin Converter
|
||||||
|
==================
|
||||||
|
|
||||||
|
The library json by nlomann is used to read json files in this program [GitHub](https://github.com/nlohmann/json)
|
||||||
|
|
||||||
|
You can reuse and modify it by anyway, just give credits and i'll be happy !
|
||||||
|
|
||||||
|
Manual
|
||||||
|
======
|
||||||
|
|
||||||
|
Put your struct, load, write and free function into bin.cpp and bin.hpp and compile it with "make", you can then use the ouputed binary "json2bin_converter" with the json input file and the binary output file which will be (re)created when the command is executed.
|
||||||
|
|
||||||
|
Exemple :
|
||||||
|
|
||||||
|
```sh
|
||||||
|
json2bin_converter test.json test.bin
|
||||||
|
```
|
|
@ -0,0 +1,171 @@
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "bin.hpp"
|
||||||
|
|
||||||
|
void load_json_into_bin(json *json_input, bin_t *bin_output)
|
||||||
|
{
|
||||||
|
// value
|
||||||
|
bin_output->value = (*json_input)["value"].get<int>();
|
||||||
|
|
||||||
|
// str
|
||||||
|
std::string str = (*json_input)["str"];
|
||||||
|
bin_output->str_size = str.size();
|
||||||
|
bin_output->str = (char *)malloc(bin_output->str_size * sizeof(char));
|
||||||
|
|
||||||
|
for(size_t i = 0; i < bin_output->str_size; i++)
|
||||||
|
{
|
||||||
|
bin_output->str[i] = str[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// list
|
||||||
|
std::vector<int> list = (*json_input)["list"];
|
||||||
|
bin_output->list_size = list.size();
|
||||||
|
bin_output->list = (int *)malloc(bin_output->list_size * sizeof(int));
|
||||||
|
|
||||||
|
for(size_t i = 0; i < bin_output->list_size; i++)
|
||||||
|
{
|
||||||
|
bin_output->list[i] = list[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// object -> value2
|
||||||
|
bin_output->object.value2 = (*json_input)["object"]["value2"].get<int>();
|
||||||
|
|
||||||
|
// object -> str2
|
||||||
|
std::string str2 = (*json_input)["object"]["str2"];
|
||||||
|
bin_output->object.str2_size = str2.size();
|
||||||
|
bin_output->object.str2 = (char *)malloc(bin_output->object.str2_size * sizeof(char));
|
||||||
|
|
||||||
|
for(size_t i = 0; i < bin_output->object.str2_size; i++)
|
||||||
|
{
|
||||||
|
bin_output->object.str2[i] = str2[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// object -> list2
|
||||||
|
std::vector<int> list2 = (*json_input)["object"]["list2"];
|
||||||
|
bin_output->object.list2_size = list2.size();
|
||||||
|
bin_output->object.list2 = (int *)malloc(bin_output->object.list2_size * sizeof(int));
|
||||||
|
|
||||||
|
for(size_t i = 0; i < bin_output->object.list2_size; i++)
|
||||||
|
{
|
||||||
|
bin_output->object.list2[i] = list2[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// object_list
|
||||||
|
std::vector<json> object_list = (*json_input)["object_list"];
|
||||||
|
bin_output->object_list_size = object_list.size();
|
||||||
|
bin_output->object_list = (person_t *)malloc(bin_output->object_list_size * sizeof(person_t));
|
||||||
|
|
||||||
|
for(size_t i = 0; i < bin_output->object_list_size; i++)
|
||||||
|
{
|
||||||
|
json object = object_list[i];
|
||||||
|
|
||||||
|
std::string name = object["name"];
|
||||||
|
bin_output->object_list[i].name_size = name.size();
|
||||||
|
bin_output->object_list[i].name = (char *)malloc(bin_output->object_list[i].name_size * sizeof(char));
|
||||||
|
|
||||||
|
for(size_t j = 0; j < bin_output->object_list[i].name_size; j++)
|
||||||
|
{
|
||||||
|
bin_output->object_list[i].name[j] = name[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
bin_output->object_list[i].age = object["age"].get<int>();
|
||||||
|
|
||||||
|
std::vector<std::string> passions = object["passions"];
|
||||||
|
bin_output->object_list[i].nb_passions = passions.size();
|
||||||
|
bin_output->object_list[i].passions_size = (size_t *)malloc(bin_output->object_list[i].nb_passions * sizeof(size_t));
|
||||||
|
|
||||||
|
size_t passions_total_size = 0;
|
||||||
|
|
||||||
|
for(size_t j = 0; j < bin_output->object_list[i].nb_passions; j++)
|
||||||
|
{
|
||||||
|
bin_output->object_list[i].passions_size[j] = passions[j].size();
|
||||||
|
passions_total_size += passions[j].size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bin_output->object_list[i].passions = (char *)malloc(passions_total_size * sizeof(char));
|
||||||
|
|
||||||
|
size_t last_passion_pos = 0;
|
||||||
|
for(size_t j = 0; j < bin_output->object_list[i].nb_passions; j++)
|
||||||
|
{
|
||||||
|
for(size_t k = 0; k < bin_output->object_list[i].passions_size[j]; k++)
|
||||||
|
bin_output->object_list[i].passions[k + last_passion_pos] = passions[j][k];
|
||||||
|
|
||||||
|
std::cout << passions[j] << std::endl;
|
||||||
|
std::cout << bin_output->object_list[i].passions << std::endl;
|
||||||
|
|
||||||
|
last_passion_pos += bin_output->object_list[i].passions_size[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_bin_into_file(bin_t *bin_output, std::ofstream *bin_output_file)
|
||||||
|
{
|
||||||
|
// value
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&bin_output->value), sizeof(int));
|
||||||
|
|
||||||
|
// str
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&bin_output->str_size), sizeof(size_t));
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(bin_output->str), bin_output->str_size * sizeof(char));
|
||||||
|
|
||||||
|
// list
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&bin_output->list_size), sizeof(size_t));
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(bin_output->list), bin_output->list_size * sizeof(int));
|
||||||
|
|
||||||
|
// object -> value2
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&bin_output->object.value2), sizeof(int));
|
||||||
|
|
||||||
|
// object -> str2
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&bin_output->object.str2_size), sizeof(size_t));
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(bin_output->object.str2), bin_output->object.str2_size * sizeof(char));
|
||||||
|
|
||||||
|
// object -> list2
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&bin_output->object.list2_size), sizeof(size_t));
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(bin_output->object.list2), bin_output->object.list2_size * sizeof(int));
|
||||||
|
|
||||||
|
// object_list
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&bin_output->object_list_size), sizeof(size_t));
|
||||||
|
for (size_t i = 0; i < bin_output->object_list_size; i++)
|
||||||
|
{
|
||||||
|
person_t& person = bin_output->object_list[i];
|
||||||
|
|
||||||
|
// name
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&person.name_size), sizeof(size_t));
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(person.name), person.name_size * sizeof(char));
|
||||||
|
|
||||||
|
// age
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&person.age), sizeof(int));
|
||||||
|
|
||||||
|
// passions
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(&person.nb_passions), sizeof(size_t));
|
||||||
|
bin_output_file->write(reinterpret_cast<char*>(person.passions_size), person.nb_passions * sizeof(size_t));
|
||||||
|
|
||||||
|
size_t passions_total_size = 0;
|
||||||
|
|
||||||
|
for(size_t j = 0; j < person.nb_passions; j++)
|
||||||
|
{
|
||||||
|
passions_total_size += person.passions_size[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
bin_output_file->write(person.passions, passions_total_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_bin(bin_t *bin)
|
||||||
|
{
|
||||||
|
free(bin->list);
|
||||||
|
bin->list = nullptr;
|
||||||
|
bin->list_size = 0;
|
||||||
|
free(bin->str);
|
||||||
|
bin->str = nullptr;
|
||||||
|
bin->str_size = 0;
|
||||||
|
|
||||||
|
free(bin->object.list2);
|
||||||
|
bin->object.list2 = nullptr;
|
||||||
|
bin->object.list2_size = 0;
|
||||||
|
free(bin->object.str2);
|
||||||
|
bin->object.str2 = nullptr;
|
||||||
|
bin->object.str2_size = 0;
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
#ifndef BIN_H
|
||||||
|
#define BIN_H
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include "json/json.hpp"
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
typedef struct object_t {
|
||||||
|
int value2;
|
||||||
|
size_t list2_size;
|
||||||
|
int *list2;
|
||||||
|
size_t str2_size;
|
||||||
|
char *str2;
|
||||||
|
} object_t;
|
||||||
|
|
||||||
|
typedef struct person_t {
|
||||||
|
size_t name_size;
|
||||||
|
char *name;
|
||||||
|
int age;
|
||||||
|
size_t nb_passions;
|
||||||
|
size_t *passions_size;
|
||||||
|
char *passions;
|
||||||
|
} person_t;
|
||||||
|
|
||||||
|
typedef struct bin_t {
|
||||||
|
int value;
|
||||||
|
size_t list_size;
|
||||||
|
int *list;
|
||||||
|
size_t str_size;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
object_t object;
|
||||||
|
|
||||||
|
size_t object_list_size;
|
||||||
|
person_t *object_list;
|
||||||
|
} bin_t;
|
||||||
|
|
||||||
|
void load_json_into_bin(json *json_input, bin_t *bin_output);
|
||||||
|
|
||||||
|
void write_bin_into_file(bin_t *bin_output, std::ofstream *bin_output_file);
|
||||||
|
|
||||||
|
void free_bin(bin_t *bin);
|
||||||
|
|
||||||
|
#endif // BIN_H
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,44 @@
|
||||||
|
#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_t bin_output;
|
||||||
|
|
||||||
|
load_json_into_bin(&json_input, &bin_output);
|
||||||
|
|
||||||
|
write_bin_into_file(&bin_output, &bin_output_file);
|
||||||
|
|
||||||
|
free_bin(&bin_output);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void error(std::string error)
|
||||||
|
{
|
||||||
|
std::cerr << "\033[0;1;31mError\033[m : " << error << "\n";
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
{
|
||||||
|
"value": 42,
|
||||||
|
"str": "You should eat more bread",
|
||||||
|
"list": [
|
||||||
|
3,
|
||||||
|
5,
|
||||||
|
7
|
||||||
|
],
|
||||||
|
|
||||||
|
"object": {
|
||||||
|
"value2": 24,
|
||||||
|
"str2": "XD",
|
||||||
|
"list2": [
|
||||||
|
4,
|
||||||
|
6,
|
||||||
|
8
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
"object_list": [
|
||||||
|
{
|
||||||
|
"name": "Aaron",
|
||||||
|
"age": 12,
|
||||||
|
"passions": [
|
||||||
|
"Showering",
|
||||||
|
"Eating",
|
||||||
|
"Parking"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Suzann",
|
||||||
|
"age": 27,
|
||||||
|
"passions": [
|
||||||
|
"Sleeping",
|
||||||
|
"Horsing",
|
||||||
|
"Parachuting"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Heliott",
|
||||||
|
"age": 35,
|
||||||
|
"passions": [
|
||||||
|
"Watching",
|
||||||
|
"Baking",
|
||||||
|
"Running"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue