Skip to content

Commit d01722f

Browse files
authored
Merge pull request #15 from Dannode36/dev
v2.2
2 parents cd350e6 + 2196fa8 commit d01722f

File tree

10 files changed

+58
-53
lines changed

10 files changed

+58
-53
lines changed

formats/dan.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

formats/example.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"array": "std::vector<{}>",
1313
"structStart": "struct {} {{",
1414
"structEnd": "};",
15-
"usingArray": "#include <vector>",
16-
"usingString": "#include <string>",
15+
"usings": {
16+
"std::string": "#include <string>",
17+
"std::vector": "#include <vector>"
18+
},
1719
"extension": "h"
1820
}

formats/template.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"array": "",
1313
"structStart": "",
1414
"structEnd": "",
15-
"usingArray": "",
16-
"usingString": "",
15+
"usings": {
16+
17+
},
1718
"extension": ""
1819
}

src/LangFormat.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void LangFormat::parseFormatByName(std::string name)
1010
std::ifstream formatFile(USER_FORMATS_PATH + name + ".json");
1111

1212
if (!formatFile.is_open()) {
13-
throw std::exception(fmt::format("\"{}\" is not a supported language or could not be found in {}\n", name, USER_FORMATS_PATH).c_str());
13+
throw std::exception(fmt::format("\"{}\" could not be found in {}\n", name, USER_FORMATS_PATH).c_str());
1414
}
1515

1616
//Read file contents into a string
@@ -40,7 +40,10 @@ void LangFormat::parseFormatByName(std::string name)
4040
this->array_format = doc["array"].GetString();
4141
this->structS_format = doc["structStart"].GetString();
4242
this->structE_format = doc["structEnd"].GetString();
43-
this->using_array = doc["usingArray"].GetString();
44-
this->using_string = doc["usingString"].GetString();
4543
this->file_extension = doc["extension"].GetString();
44+
45+
auto usings = doc["usings"].GetObject();
46+
for (auto i = usings.MemberBegin(); i != usings.MemberEnd(); i++) {
47+
this->usings.insert({ i->name.GetString(), i->value.GetString() });
48+
};
4649
}

src/LangFormat.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,18 @@ struct LangFormat
1919
std::string array_format;
2020
std::string structS_format; //Start of struct declaration
2121
std::string structE_format; //End of struct declaration
22-
23-
std::string using_array;
24-
std::string using_string;
25-
22+
std::map<std::string, std::string> usings;
2623
std::string file_extension;
2724

2825
LangFormat() = default;
29-
3026
void parseFormatByName(std::string name);
3127
};
3228

3329
const std::map<std::string, LangFormat> globalFormats
3430
{
35-
{ "cpp", { "int", "unsigned int", "long long", "unsigned long long", "float", "double", "bool", "std::string", "void*", "{0} {1};", "std::vector<{}>", "struct {} {{", "};", "#include <vector>", "#include <string>", "h" } },
36-
{ "csharp", { "int", "uint", "long", "ulong", "float", "double", "bool", "string", "object", "{0} {1};", "List<{}>", "class {}\n{{", "}", "using System.Collections.Generic;", "", "cs" } },
37-
{ "java", { "int", "int", "long", "long", "float", "double", "bool", "String", "Object", "{0} {1};", "ArrayList<{}>", "class {}\n{{", "}", "", "", "java" } },
38-
{ "kotlin", { "int", "UInt", "long", "ULong", "float", "double", "bool", "String", "Object", "val {1}: {0};", "List<{}>", "class {}\n{{", "}", "", "", "kt" } },
39-
{ "rust", { "i32", "u32", "i64", "u64", "f32", "f64", "bool", "String", "", "{1}: {0},", "Vec<{}>", "struct {}\n{{", "}", "", "", "rs" } },
31+
{ "cpp", { "int", "unsigned int", "long long", "unsigned long long", "float", "double", "bool", "std::string", "void*", "{0} {1};", "std::vector<{}>", "struct {} {{", "};", {{"std::string", "#include <string>"}, {"std::vector", "#include <vector>"}}, "h" } },
32+
{ "csharp", { "int", "uint", "long", "ulong", "float", "double", "bool", "string", "object", "{0} {1};", "List<{}>", "class {}\n{{", "}", {{"List", "using System.Collections.Generic;"}}, "cs" } },
33+
{ "java", { "int", "int", "long", "long", "float", "double", "bool", "String", "Object", "{0} {1};", "ArrayList<{}>", "class {}\n{{", "}", {}, "java" } },
34+
{ "kotlin", { "int", "UInt", "long", "ULong", "float", "double", "bool", "String", "Object", "val {1}: {0};", "List<{}>", "class {}\n{{", "}", {}, "kt" } },
35+
{ "rust", { "i32", "u32", "i64", "u64", "f32", "f64", "bool", "String", "", "{1}: {0},", "Vec<{}>", "struct {}\n{{", "}", {}, "rs" } },
4036
};

src/Options.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ namespace CLOptions {
7878
outputDir = std::wstring(args[i].begin(), args[i].end());
7979
}}}
8080
};
81-
82-
void parse(std::vector<std::string>& args)
81+
82+
//Returns: Wether the current command should continue execution. e.g. "help" will return false
83+
bool parse(std::vector<std::string>& args)
8384
{
85+
if (args.size() > 0 && args[0] == "help") {
86+
CLOptions::help();
87+
return false;
88+
}
89+
8490
if (args.size() < 2) {
8591
throw std::exception("Correct syntax is \"<file-path> <language> <options>\"");
8692
}
@@ -95,10 +101,6 @@ namespace CLOptions {
95101
}
96102
} //This might be too hard (Probably need to use IFileDialog (that shit is wack))
97103

98-
if (args[0] == "help") {
99-
help();
100-
}
101-
102104
//Get filepath
103105
r_filePath = args[0];
104106

@@ -132,6 +134,8 @@ namespace CLOptions {
132134
//State errors
133135
if (o_forcePerfection && o_forceThroughErrors) { throw std::exception("-f and -p cannot be set simultaneously"); }
134136
}
137+
138+
return true;
135139
}
136140

137141
std::string filePath()
@@ -166,9 +170,11 @@ namespace CLOptions {
166170
}
167171
void help(std::string arg)
168172
{
173+
fmt::print("Syntax: json2x <file-path> <language> <options>\n");
174+
169175
for (auto& option : commandStringToId)
170176
{
171-
fmt::print(" " + option.second.description + '\n');
177+
fmt::print(option.second.description + '\n');
172178
}
173179
}
174180
}

src/Options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "LangFormat.h"
55

66
namespace CLOptions {
7-
void parse(std::vector<std::string>& args);
7+
bool parse(std::vector<std::string>& args);
88

99
std::string filePath();
1010
LangFormat& langFormat();

src/jsonConverters.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ std::string CodeGenerator::DeserializeJsonObject(rapidjson::Value* jsonValue, st
126126

127127
std::string typeString = getType(&member.value, nameOfMember, depth + 1);
128128

129+
//If type has a using, add it to an array for later, then remove it from the format map
130+
auto itr = format.usings.lower_bound(typeString);
131+
if (itr != format.usings.end()) {
132+
usings.push_back(itr->second);
133+
format.usings.erase(itr->first);
134+
}
135+
129136
if (typeString == format.placeholder_t) {
130137
sstruct.isComplete = false;
131138
}
@@ -186,13 +193,12 @@ std::string CodeGenerator::DeserializeJsonObject(rapidjson::Value* jsonValue, st
186193
}
187194

188195
std::string CodeGenerator::GenerateCode() {
189-
190196
int completedClasses = 0;
191197
for (auto& object : structureList)
192198
{
193199
if (!object.isComplete) {
194200
if (CLOptions::isForcePerfection()) {
195-
throw std::exception("Some objects could not be parsed to completion (-p)");
201+
throw std::exception("Some objects could not be parsed to completion (-p was set)");
196202
}
197203
continue;
198204
}
@@ -208,9 +214,13 @@ std::string CodeGenerator::GenerateCode() {
208214
}
209215

210216
std::string text;
211-
text += usingVectors ? format.using_array + "\n" : "";
212-
text += usingStrings ? format.using_string + "\n" : "";
213-
text += usingStrings || usingVectors ? "\n" : "";
217+
218+
if (!usings.empty()) {
219+
for (auto& u : usings) {
220+
text += u + "\n";
221+
}
222+
text += "\n";
223+
}
214224

215225
try {
216226
for (auto& sstruct : structureList)

src/jsonConverters.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class CodeGenerator {
4343
std::vector<ObjectData> structureList; //ObjectData ID (hash) mapped to itself
4444
std::unordered_map<std::string, int> classNameCounters;
4545

46+
std::vector<std::string> usings;
47+
4648
std::string DeserializeJsonObject(rapidjson::Value* jsonValue, std::string memberName, int depth);
4749
std::string getType(rapidjson::Value* jsonValue, std::string memberName, int depth);
4850
std::string GenerateCode();

src/main.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ int main(int argc, char* argv[]) {
1515
const std::wstring outFileName = L"\\output";
1616
bool running = true;
1717

18-
std::cout << "json2x " << VERSION_MAJOR << "." << VERSION_MINOR << "\n";
19-
2018
while (running)
2119
{
2220
try
@@ -31,14 +29,17 @@ int main(int argc, char* argv[]) {
3129
throw std::exception("Correct syntax is \"json2x <file-path> <language> <options>\"\n");
3230
}
3331

34-
for (int i = 0; i < argc; i++) {
32+
for (int i = 1; i < argc; i++) {
3533
args.push_back(argv[i]);
3634
}
3735

3836
running = false; //Run once
3937
argc = 0; //Prevent args being used again
4038
}
4139
else {
40+
41+
std::cout << "json2x " << VERSION_MAJOR << "." << VERSION_MINOR << " (shell mode) " << "\n";
42+
4243
std::cout << ">> ";
4344

4445
std::string tempInput;
@@ -56,7 +57,9 @@ int main(int argc, char* argv[]) {
5657
}
5758
}
5859

59-
CLOptions::parse(args);
60+
if (CLOptions::parse(args) != true) {
61+
continue;
62+
}
6063
}
6164

6265
//Load JSON file
@@ -73,7 +76,7 @@ int main(int argc, char* argv[]) {
7376
//Construct a code generator object with an indent style (4 spaces) and an initial class name ("MyClass")
7477
CodeGenerator generator(CLOptions::indent(), className);
7578

76-
//Write generated code to a file if no parsing error occured
79+
//Get generated code if no parsing error occured
7780
std::string genOutput = generator.convertJson(json, CLOptions::langFormat());
7881

7982
//Write generated code to file

0 commit comments

Comments
 (0)