Skip to content

Commit

Permalink
Implement folder input
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4n1um committed Jun 23, 2019
1 parent b4f0d6e commit 9dd6ff4
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 92 deletions.
4 changes: 2 additions & 2 deletions rton-json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# semver
set(VERSION_MAJOR 2)
set(VERSION_MINOR 6)
set(VERSION_PATCH 2)
set(VERSION_MINOR 7)
set(VERSION_PATCH 0)

# configure a header file to pass some of the CMake settings
# to the source code
Expand Down
11 changes: 6 additions & 5 deletions rton-json/src/json2rton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ std::unordered_map <std::string, uint64_t> map_0x93;
int write_RTON(json js);

int not_json(){
std::cerr << "ERROR! THIS FILE IS NOT JSON FORMAT!!!" << std::endl;
std::cerr << "Error! This file is not JSON format!!!" << std::endl;
debug << std::setw(4) << debug_js;
std::cin.get();
exit(1);
return 4;
}

//https://en.wikipedia.org/wiki/UTF-8#Examples
Expand Down Expand Up @@ -205,7 +204,7 @@ int write_RTON_block(json js){
}
//error
default:{
return not_json();
throw not_json();
break;
}
}
Expand All @@ -223,12 +222,14 @@ int write_RTON(json js){
}

int rton_encode(){
map_0x91.clear();
map_0x93.clear();
json js;
try{
input >> js;
}
catch(nlohmann::detail::parse_error){
return not_json();
throw not_json();
}
output.write("RTON", 4);
const int RTON_ver = 1; //not sure if I ever see RTON version higher than 1
Expand Down
148 changes: 90 additions & 58 deletions rton-json/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ std::string to_hex_string(std::vector <uint8_t> a){

int help(const char* argv[]){
std::cerr << "Usage:" << std::endl
<< '\t' << argv[0] << " <file-path>" << std::endl
<< '\t' << argv[0] << " [options]" << " <file-path>" << std::endl << std::endl
<< '\t' << argv[0] << " <file-or-folder-path>" << std::endl
<< '\t' << argv[0] << " [options]" << " <file-or-folder-path>" << std::endl << std::endl
<< "Options:" << std::endl
<< "\t--help\t\tShow help (the thing you're looking at)" << std::endl
<< "\t--rton2json\tForce covert RTON to JSON" << std::endl
Expand All @@ -56,35 +56,9 @@ int help(const char* argv[]){
return 1;
}

int main(const int argc, const char* argv[]){
std::clog << std::endl << "rton-json made by H3x4n1um" << std::endl
<< "Version: " << ver << std::endl
<< "Compiled on " << __DATE__ << " at " << __TIME__ << std::endl
<< "Credits: nlohmann for his awesome JSON parser and fifo_map" << std::endl << std::endl;

if (argc > 3) return help(argv);
//get file_path
std::filesystem::path file_path;
if (argc == 1){
std::string temp;
std::clog << "Enter file path: ";
getline(std::cin, temp);
file_path = temp;
std::clog << std::endl;
}
else if (argc == 2){
if (strcmp(argv[1], "--help") == 0) return help(argv);
file_path = argv[1];
}
else file_path = argv[2];

//check file exist
if (!std::filesystem::is_regular_file(file_path)){
std::cerr << "ERROR! CAN'T FIND FILE " << file_path << "!!!" << std::endl;
std::cin.get();
return 1;
}

int process_file(std::filesystem::path file_name, const int argc, const char* argv[]){
debug_js.clear();
std::clog << "Processing file " << file_name;
//info
debug_js["Info"]["Log"] = "This log file created by rton-json made by H3x4n1um";
debug_js["Info"]["Executable"] = argv[0];
Expand All @@ -102,52 +76,110 @@ int main(const int argc, const char* argv[]){
//else just mark as json
else{
debug_js["Info"]["Mode"] = "Auto";
input.open(file_path, std::ifstream::binary);
input.open(file_name, std::ifstream::binary);
//check header
char header[5];
input.read(header, 4);
header[4] = 0;
if (strcmp(header, "RTON") == 0) is_rton = true;
input.close();
}
debug_js["Info"]["File"] = file_path.string();
debug_js["Info"]["File"] = file_name.string();

debug.open(file_path.string() + "_log.json");
std::filesystem::create_directory(file_name.parent_path() / "log");
debug.open((file_name.parent_path() / "log" / file_name.filename()).string() + "_log.json");

//init RTON Stats
debug_js["RTON Stats"]["RTON Version"] = 1; //not sure if it ever higher than 1
debug_js["RTON Stats"]["List of Bytecodes"]["Offset"] = "Bytecode";
debug_js["RTON Stats"]["0x91 Stack"]["Unsigned RTON Number"] = "String";
debug_js["RTON Stats"]["0x93 Stack"]["Unsigned RTON Number"] = "UTF-8 String";

//rton2json
if (is_rton){
std::clog << "RTON DETECTED" << std::endl;
//read
input.open(file_path, std::ifstream::binary);
//write
output.open(file_path.string() + ".json");
output << std::setw(4) << json_decode();
//close
try{
//rton2json
if (is_rton){
std::clog << " - RTON Detected" << std::endl;
//read
input.open(file_name, std::ifstream::binary);
//write
std::filesystem::create_directory(file_name.parent_path() / "rton2json");
output.open((file_name.parent_path() / "rton2json" / file_name.stem()).string() + ".json");
output << std::setw(4) << json_decode();
//close
input.close();
output.close();
}
//json2rton
else{
std::clog << " - JSON Detected" << std::endl;
//read
input.open(file_name);
//write
std::filesystem::create_directory(file_name.parent_path() / "json2rton");
output.open((file_name.parent_path() / "json2rton" / file_name.stem()).string() + ".rton", std::ofstream::binary);

rton_encode(); //write directly to file

//close
input.close();
output.close();
}
//log at the end
debug << std::setw(4) << debug_js;
debug.close();
std::clog << "Done" << std::endl << std::endl;
}
catch (int e){
input.close();
output.close();
debug.close();
std::filesystem::path out_file;
if (is_rton) out_file = (file_name.parent_path() / "rton2json" / file_name.stem()).string() + ".json";
else out_file = (file_name.parent_path() / "json2rton" / file_name.stem()).string() + ".rton";
std::filesystem::remove(out_file);
std::clog << "Error code: " << e << std::endl << std::endl;
return e;
}
//json2rton
return 0;
}

int main(const int argc, const char* argv[]){
std::clog << std::endl << "rton-json made by H3x4n1um" << std::endl
<< "Version: " << ver << std::endl
<< "Compiled on " << __DATE__ << " at " << __TIME__ << std::endl
<< "Credits: nlohmann for his awesome JSON parser and fifo_map" << std::endl << std::endl;

if (argc > 3) return help(argv);
//get path
std::filesystem::path path;
if (argc == 1){
std::string temp;
std::clog << "Enter file or folder path: ";
getline(std::cin, temp);
path = temp;
std::clog << std::endl;
}
else if (argc == 2){
if (strcmp(argv[1], "--help") == 0) return help(argv);
path = argv[1];
}
else path = argv[2];

//check file exist
if (!std::filesystem::is_regular_file(path) && !std::filesystem::is_directory(path)){
std::cerr << "Error! Can't find file or folder " << path << "!!!" << std::endl;
std::cin.get();
return 1;
}

if (std::filesystem::is_regular_file(path)) process_file(path, argc, argv);
else{
std::clog << "JSON DETECTED" << std::endl;
//read
input.open(file_path);
//write
output.open(file_path.string() + ".rton", std::ofstream::binary);
rton_encode(); //write directly to file
//close
input.close();
output.close();
for (auto &file : std::filesystem::directory_iterator(path)){
if (std::filesystem::is_regular_file(file)){
process_file(file.path(), argc, argv);
}
}
}
std::clog << std::endl << "DONE";
//log at the end
debug << std::setw(4) << debug_js;
debug.close();
std::cin.get();
std::clog << "Finished" << std::endl << std::endl;
return 0;
}
46 changes: 19 additions & 27 deletions rton-json/src/rton2json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ std::vector <std::string> stack_0x91;
std::vector <std::string> stack_0x93;

json read_RTON();
void bytecode_error(uint8_t bytecode);

int not_RTON(){
std::cerr << "ERROR! THIS FILE IS NOT RTON FORMAT!!!" << std::endl;
debug << std::setw(4) << debug_js;
std::cin.get();
return 1;
}
int bytecode_error(uint8_t bytecode);

std::vector <uint8_t> read_RTON_num(){
std::vector <uint8_t> RTON_num;
Expand Down Expand Up @@ -266,7 +259,7 @@ json read_RTON_block(){
res.push_back(std::string("RTID(") + uid + '@' + s + ')');
}
//unknown
else bytecode_error(subset);
else throw bytecode_error(subset);
break;
}
//null
Expand All @@ -291,9 +284,9 @@ json read_RTON_block(){
//check end of array
uint8_t arr_end;
input.read(reinterpret_cast <char*> (&arr_end), sizeof arr_end);
if (arr_end != 0xfe) bytecode_error(arr_end);
if (arr_end != 0xfe) throw bytecode_error(arr_end);
}
else bytecode_error(arr_begin);
else throw bytecode_error(arr_begin);
break;
}
//cached string
Expand Down Expand Up @@ -343,7 +336,7 @@ json read_RTON_block(){
}
//else just exit error
default:{
bytecode_error(bytecode);
throw bytecode_error(bytecode);
break;
}
}
Expand All @@ -355,14 +348,14 @@ json read_RTON(){
json res;
while(true){
std::string key;
json js_key = read_RTON_block();
json js_key;
js_key = read_RTON_block();
if (js_key.size() == 0) return res;
else{
if (!js_key[0].is_string()){
std::cerr << std::endl << "ERROR! KEY IS NOT A STRING!!!" << std::endl;
std::cerr << "Error! Key is not a string!!!" << std::endl;
debug << std::setw(4) << debug_js;
std::cin.get();
exit(1);
throw 3;
}
key = js_key[0];
}
Expand All @@ -373,27 +366,26 @@ json read_RTON(){
}

json json_decode(){
//check header
char header[5];
input.read(header, 4);
header[4] = 0;
if (strcmp(header, "RTON") != 0) exit(not_RTON());
stack_0x91.clear();
stack_0x93.clear();
input.seekg((uint64_t) input.tellg() + 4); //skip RTON

uint32_t RTON_ver;
input.read(reinterpret_cast <char*> (&RTON_ver), sizeof RTON_ver);

json js = read_RTON();
json js;
js = read_RTON();

//check footer
char footer[5];
input.read(footer, 4);
footer[4] = 0;
if (strcmp(footer, "DONE") != 0) std::clog << R"(MISSING "DONE" AT EOF?)" << std::endl;
if (strcmp(footer, "DONE") != 0) std::clog << "Missing \"DONE\" at EOF?" << std::endl;
return js;
}

void bytecode_error(uint8_t bytecode){
std::cerr << std::endl << "ERROR READING BYTECODE " << std::hex << std::showbase << (int) bytecode << " AT " << (uint64_t) input.tellg() - 1 << "!!!" << std::endl;
int bytecode_error(uint8_t bytecode){
std::cerr << "Error reading bytecode " << std::hex << std::showbase << (int) bytecode << " at " << (uint64_t) input.tellg() - 1 << "!!!" << std::endl;
debug << std::setw(4) << debug_js;
std::cin.get();
exit(1);
return 2;
}

0 comments on commit 9dd6ff4

Please sign in to comment.