Load JSON files at compile time as C Strings

This commit is contained in:
Tim Warren 2015-04-15 09:47:20 -04:00
parent 5639d33bb0
commit 61adc35bb0
8 changed files with 157 additions and 81 deletions

View File

@ -4,6 +4,8 @@ SOURCES = $(wildcard include/**/*.cpp src/network/*.cpp src/settings/*.cpp inclu
OBJECTS = $(patsubst %.cpp,%.o, $(SOURCES))
TARGET = build/Tyro.a
JSON_FILES = $(patsubst config/%.json,%.json, $(wildcard config/*.json))
PROGRAM_SRC = $(wildcard src/*.cpp src/widgets/*.cpp)
PROGRAM = build/Tyro
PROGRAM_OBJECTS = $(patsubst %.cpp,%.o, $(PROGRAM_SRC))
@ -17,11 +19,17 @@ CXXFLAGS = -Os
TEST_SRC= $(wildcard tests/*.cpp)
TESTS = $(patsubst %.cpp,%,$(TEST_SRC))
all: build $(TARGET) $(PROGRAM)
all: build json_wrapper $(TARGET) $(PROGRAM)
dev: CXXFLAGS= $(DEV_CXXFLAGS)
dev: all
json_wrapper: json_wrapper_build
$(foreach var, $(JSON_FILES), config/json2c config/$(var) $(patsubst %.json,config/%_json.h,$(var)) $(patsubst %.json,%_json,$(var));)
json_wrapper_build:
$(CC) config/json2c.c -o config/json2c
build:
@mkdir -p build
@ -57,6 +65,8 @@ tests: $(TESTS)
sh ./tests/runtests.sh
clean:
rm config/json2c
rm config/*_json.h
rm -f *.o
rm -rf Tyro.app
rm -rf build $(OBJECTS) $(PROGRAM) $(TARGET) $(TESTS)

66
config/json2c.c Normal file
View File

@ -0,0 +1,66 @@
/*
* bin2c: A Program to convert binary data into C source code
* Copyright 2004 by Adrian Prantl <adrian@f4z.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#include <stdio.h>
#include <stdlib.h>
char* self = 0;
void usage() {
printf("Usage:\n%s input.bin output.h name\n\n", self);
}
void bail_out(const char* s1, const char* s2) {
fprintf(stderr, "%s: FATAL ERROR:\n%s%s\n", self, s1, s2);
exit(1);
}
int main(int argc, char** argv) {
FILE *fi, *fo;
int c, i;
self = argv[0];
if (argc != 4) {
usage();
return 0;
}
if ((fi = fopen(argv[1], "rb")) == 0)
bail_out("Cannot open input file ", argv[1]);
if ((fo = fopen(argv[2], "w")) == 0)
bail_out("Cannot open output file ", argv[2]);
if ((c = fgetc(fi)) != EOF) {
fprintf(fo, "#ifndef %s_H\n", argv[3]);
fprintf(fo, "#define %s_H\n\n", argv[3]);
fprintf(fo, "const char %s[] = {\n", argv[3]);
fprintf(fo, c < 16 ? " 0x%02x" : " 0x%02x", c);
}
i = 1;
while ((c = fgetc(fi)) != EOF) {
if (i < 12)
fprintf(fo, c < 16 ? ", 0x%02x" : ", 0x%02x", c);
else {
fprintf(fo, c < 16 ? ",\n 0x%02x" : ",\n 0x%02x", c);
i = 0;
}
i++;
}
fprintf(fo, "\n};\n\n");
fprintf(fo, "#endif\n");
printf("converted %s\n", argv[1]);
return 0;
}

55
config/languages.json Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

5
config/themes.json Normal file
View File

@ -0,0 +1,5 @@
{
"solarized" : [
]
}

View File

@ -14,7 +14,5 @@
using namespace std;
#define JSON_INCLUDE(a, b) (string (b) = "(#include (a))")
#endif // TYRO_COMMON_H

View File

@ -6,12 +6,14 @@
TyroConfig::TyroConfig()
{
string raw_json;
JSON_INCLUDE("../../config/scintilla.json", raw_json);
// Defines languages_json
#include "../../config/languages_json.h"
string json_string(languages_json);
cout << raw_json << endl;
reader.parse(raw_json, default_root);
if ( ! reader.parse(json_string, default_root))
{
cerr << reader.getFormattedErrorMessages() << endl;
}
}
TyroConfig::~TyroConfig()
@ -26,13 +28,5 @@ JsonValue TyroConfig::GetRoot()
JsonValue TyroConfig::GetLang(string name)
{
JsonValue root = this->GetRoot();
JsonValue lang = root.get("languages", "");
if (lang != "")
{
return lang.get(name, "");
}
return JsonValue("");
return default_root.get(name, JsonValue());
}

View File

@ -35,7 +35,8 @@ bool EditPane::LoadAndHighlight(wxString filePath)
this->StyleSetFaceName(i, "Anonymous Pro");
}
JsonValue keywords_array = config->GetLang("cpp");
// Get the list of keywords for the current language
JsonValue keywords_array = config->GetLang("cpp").get("keywords", JsonValue());
this->StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColor(101, 123, 131));
this->StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColor(147, 161, 161));
@ -71,9 +72,16 @@ bool EditPane::LoadAndHighlight(wxString filePath)
this->StyleSetBold(wxSTC_C_COMMENTDOCKEYWORD, true);
this->StyleSetBold(wxSTC_C_OPERATOR, true);
this->SetKeyWords(0, keywords_array[0].asString());
this->SetKeyWords(1, keywords_array[1].asString());
if (keywords_array.isArray())
{
this->SetKeyWords(0, keywords_array[0].asString());
this->SetKeyWords(1, keywords_array[1].asString());
}
else
{
cerr << "keywords array is not an array" << endl;
cerr << "keyword array is a " << keywords_array.type() << endl;
}
return this->LoadFile(filePath);
}