Skip to content

Commit

Permalink
Resolve issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4n1um committed Jun 27, 2019
1 parent ddb49c4 commit f1050df
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
2 changes: 1 addition & 1 deletion rton-json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# semver
set(VERSION_MAJOR 2)
set(VERSION_MINOR 7)
set(VERSION_PATCH 1)
set(VERSION_PATCH 2)

# configure a header file to pass some of the CMake settings
# to the source code
Expand Down
35 changes: 24 additions & 11 deletions rton-json/src/json2rton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ std::unordered_map <std::string, uint64_t> map_0x91;
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;
debug << std::setw(4) << debug_js;
return 4;
}
int not_json();
int not_supported_json();

//https://en.wikipedia.org/wiki/UTF-8#Examples
int get_utf8_size(std::string q){
Expand Down Expand Up @@ -209,12 +205,17 @@ int write_RTON_block(json js){
}

int write_RTON(json js){
for (auto i : js.get<std::map <std::string, json> >()){
write_RTON_block(i.first);
write_RTON_block(i.second);
try{
for (auto i : js.get<std::map <std::string, json> >()){
write_RTON_block(i.first);
write_RTON_block(i.second);
}
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(object_end);
output.write(reinterpret_cast<const char*> (&object_end), 1);
}
catch(json::exception &e){
throw not_supported_json();
}
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(object_end);
output.write(reinterpret_cast<const char*> (&object_end), 1);
return 0;
}

Expand All @@ -238,3 +239,15 @@ int rton_encode(){

return 0;
}

int not_json(){
std::cerr << "Error! This file is not JSON format!!!" << std::endl;
debug << std::setw(4) << debug_js;
return 4;
}

int not_supported_json(){
std::cerr << "Error! This file is a JSON but format is not supported" << std::endl;
debug << std::setw(4) << debug_js;
return 6;
}
2 changes: 1 addition & 1 deletion rton-json/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ int process_file(std::filesystem::path file_name, const int argc, const char *ar
output.close();
debug.close();

//remove unfinish file
//remove unfinished file
std::filesystem::path out_file;
if (file_type) 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";
Expand Down
41 changes: 32 additions & 9 deletions rton-json/src/rton2json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ std::vector <std::string> stack_0x93;

json read_RTON();
int bytecode_error(uint8_t bytecode);
int out_of_range_error(uint8_t bytecode);

std::vector <uint8_t> read_RTON_num(){
std::vector <uint8_t> RTON_num;
Expand Down Expand Up @@ -309,8 +310,12 @@ json read_RTON_block(){
}
//recall
case 0x91:{
//TODO: check stack overflow
res.push_back(stack_0x91[unsigned_RTON_num2int(read_RTON_num())]);
try{
res.push_back(stack_0x91.at(unsigned_RTON_num2int(read_RTON_num())));
}
catch(const std::out_of_range &oor){
throw out_of_range_error(bytecode);
}
break;
}
//cached utf-8 string
Expand All @@ -330,13 +335,16 @@ json read_RTON_block(){
}
//recall
case 0x93:{
//TODO: check stack overflow
res.push_back(stack_0x93[unsigned_RTON_num2int(read_RTON_num())]);
try{
res.push_back(stack_0x93.at(unsigned_RTON_num2int(read_RTON_num())));
}
catch(const std::out_of_range &oor){
throw out_of_range_error(bytecode);
}
break;
}
//end of object
case 0xFF:{
//TODO: check for bracket
break;
}
//else just exit error
Expand Down Expand Up @@ -378,12 +386,21 @@ json json_decode(){
uint32_t RTON_ver;
input.read(reinterpret_cast <char*> (&RTON_ver), sizeof RTON_ver);
debug_js["RTON stats"]["RTON version"] = RTON_ver;

json js;
js = read_RTON();
char footer[5];
input.read(footer, 4);
footer[4] = 0;
if (strcmp(footer, "DONE") != 0) std::clog << "Missing \"DONE\" at EOF?" << std::endl;

if (input.eof()) std::clog << "Missing \"DONE\" at EOF?" << std::endl;
else{
char footer[5];
input.read(footer, 4);
footer[4] = 0;

if (strcmp(footer, "DONE") != 0){
input.seekg((uint8_t) input.tellg() - 3);
throw bytecode_error(footer[0]);
}
}

return js;
}
Expand All @@ -393,3 +410,9 @@ int bytecode_error(uint8_t bytecode){
debug << std::setw(4) << debug_js;
return 2;
}

int out_of_range_error(uint8_t bytecode){
std::cerr << "Error! " << std::hex << std::showbase << (int) bytecode << " stack overflow at " << (uint64_t) input.tellg() - 1 << std::endl;
debug << std::setw(4) << debug_js;
return 5;
}

0 comments on commit f1050df

Please sign in to comment.