diff --git a/rton-json/CMakeLists.txt b/rton-json/CMakeLists.txt index a0c02de..0945ce4 100644 --- a/rton-json/CMakeLists.txt +++ b/rton-json/CMakeLists.txt @@ -28,7 +28,7 @@ find_package(nlohmann_json 3.7.0 REQUIRED) # semver set(VERSION_MAJOR 2) set(VERSION_MINOR 7) -set(VERSION_PATCH 4) +set(VERSION_PATCH 5) # configure a header file to pass some of the CMake settings # to the source code diff --git a/rton-json/include/RTON_number.hpp b/rton-json/include/RTON_number.hpp index 382b1c6..cbba49c 100644 --- a/rton-json/include/RTON_number.hpp +++ b/rton-json/include/RTON_number.hpp @@ -5,5 +5,7 @@ using namespace std; constexpr double log256(double q); -vector uint64_t2uRTON_t(uint64_t q); + uint64_t uRTON_t2uint64_t(vector q); + +vector uint64_t2uRTON_t(uint64_t q); diff --git a/rton-json/include/error.hpp b/rton-json/include/error.hpp index 2dca2a8..2aacccc 100644 --- a/rton-json/include/error.hpp +++ b/rton-json/include/error.hpp @@ -1,7 +1,7 @@ #pragma once int bytecode_error(uint8_t bytecode); -int key_error(); -int out_of_range_error(uint8_t bytecode); int eof_error(char footer[5]); +int key_error(); int not_supported_json(); +int out_of_range_error(uint8_t bytecode); diff --git a/rton-json/include/json2rton.hpp b/rton-json/include/json2rton.hpp index 5f9e603..68ab46e 100644 --- a/rton-json/include/json2rton.hpp +++ b/rton-json/include/json2rton.hpp @@ -5,8 +5,9 @@ using namespace std; -int write_RTON(json js); -int get_utf8_size(string q); -int write_uRTON_t(vector a); int rton_encode(); +int write_RTON(json js); int write_RTON_block(json js); +int write_uRTON_t(vector a); + +size_t get_utf8_size(string q); diff --git a/rton-json/include/rton2json.hpp b/rton-json/include/rton2json.hpp index e133772..6624aba 100644 --- a/rton-json/include/rton2json.hpp +++ b/rton-json/include/rton2json.hpp @@ -15,7 +15,9 @@ using json = nlohmann::basic_json; template T read(); -json json_decode(); + vector read_uRTON_t(); + +json json_decode(); json read_RTON(); json read_RTON_block(); diff --git a/rton-json/src/json2rton.cpp b/rton-json/src/json2rton.cpp index 9e7bc28..d0bf44f 100644 --- a/rton-json/src/json2rton.cpp +++ b/rton-json/src/json2rton.cpp @@ -25,8 +25,8 @@ unordered_map map_0x91; unordered_map map_0x93; //https://en.wikipedia.org/wiki/UTF-8#Examples -int get_utf8_size(string q){ - int utf8_size = 0; +size_t get_utf8_size(string q){ + size_t utf8_size = 0; for (uint8_t i : q){ if (i <= 0177) utf8_size += 1; if (i >= 0302 && i <= 0337) utf8_size += 1; @@ -65,7 +65,7 @@ int write_RTON_block(json js){ temp.erase(0, 5); string first_string = temp.substr(temp.find("@") + 1), - second_string = temp.substr(0, temp.find("@")); + second_string = temp.substr(0, temp.find("@")); uint8_t subset; if (regex_match(second_string, regex("\\d+\\.\\d+\\.[0-9a-fA-F]+"))) subset = 0x2; @@ -77,11 +77,13 @@ int write_RTON_block(json js){ write_uRTON_t(uint64_t2uRTON_t(first_string.size())); output << first_string; if (subset == 0x2){ - uint64_t first_uid = strtoull(second_string.c_str(), NULL, 10); - second_string = second_string.substr(second_string.find(".") + 1); - uint64_t second_uid = strtoull(second_string.c_str(), NULL, 10); - second_string = second_string.substr(second_string.find(".") + 1); - int32_t third_uid = stoi(second_string, nullptr, 16); + stringstream raw_ss(second_string), ss; + string tmp_str; + uint64_t first_uid, second_uid; + uint32_t third_uid; + + while(getline(raw_ss, tmp_str, '.')) ss << tmp_str << " "; + ss >> first_uid >> second_uid >> hex >> third_uid; write_uRTON_t(uint64_t2uRTON_t(second_uid)); write_uRTON_t(uint64_t2uRTON_t(first_uid)); @@ -93,43 +95,52 @@ int write_RTON_block(json js){ output << second_string; } } - //normal string + //NaN + else if (temp.find("NaN") != string::npos){ + double dnan = numeric_limits::signaling_NaN(); + output.write(reinterpret_cast (&float64), sizeof float64); + output.write(reinterpret_cast (&dnan), sizeof dnan); + } + //Infinity + else if (temp.find("Infinity") != string::npos){ + double dinf = numeric_limits::infinity(); + if (temp[0] == '-') dinf = -dinf; + output.write(reinterpret_cast (&float64), sizeof float64); + output.write(reinterpret_cast (&dinf), sizeof dinf); + } + //normal ascii string + else if (get_utf8_size(temp) == temp.size()){ + if (map_0x91[temp] == 0){ + debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(ascii); + output.write(reinterpret_cast (&ascii), sizeof ascii); + write_uRTON_t(uint64_t2uRTON_t(temp.size())); + output << temp; + + debug_js["RTON stats"]["0x91 stack"][to_hex_string(uint64_t2uRTON_t(map_0x91.size() - 1))] = temp; + map_0x91[temp] = map_0x91.size(); + } + else{ + debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(ascii_stack); + output.write(reinterpret_cast (&ascii_stack), sizeof ascii_stack); + write_uRTON_t(uint64_t2uRTON_t(map_0x91[temp] - 1)); + } + } + //normal utf-8 string else{ - size_t utf8_size = get_utf8_size(temp); - //ascii - if (utf8_size == temp.size()){ - if (map_0x91[temp] == 0){ - debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(ascii); - output.write(reinterpret_cast (&ascii), sizeof ascii); - write_uRTON_t(uint64_t2uRTON_t(temp.size())); - output << temp; - - debug_js["RTON stats"]["0x91 stack"][to_hex_string(uint64_t2uRTON_t(map_0x91.size() - 1))] = temp; - map_0x91[temp] = map_0x91.size(); - } - else{ - debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(ascii_stack); - output.write(reinterpret_cast (&ascii_stack), sizeof ascii_stack); - write_uRTON_t(uint64_t2uRTON_t(map_0x91[temp] - 1)); - } + if (map_0x93[temp] == 0){ + debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(utf8); + output.write(reinterpret_cast (&utf8), sizeof utf8); + write_uRTON_t(uint64_t2uRTON_t(get_utf8_size(temp))); + write_uRTON_t(uint64_t2uRTON_t(temp.size())); + output << temp; + + debug_js["RTON stats"]["0x93 stack"][to_hex_string(uint64_t2uRTON_t(map_0x93.size() - 1))] = temp; + map_0x93[temp] = map_0x93.size(); } - //utf-8 else{ - if (map_0x93[temp] == 0){ - debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(utf8); - output.write(reinterpret_cast (&utf8), sizeof utf8); - write_uRTON_t(uint64_t2uRTON_t(utf8_size)); - write_uRTON_t(uint64_t2uRTON_t(temp.size())); - output << temp; - - debug_js["RTON stats"]["0x93 stack"][to_hex_string(uint64_t2uRTON_t(map_0x93.size() - 1))] = temp; - map_0x93[temp] = map_0x93.size(); - } - else{ - debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(utf8_stack); - output.write(reinterpret_cast (&utf8_stack), sizeof utf8_stack); - write_uRTON_t(uint64_t2uRTON_t(map_0x93[temp] - 1)); - } + debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(utf8_stack); + output.write(reinterpret_cast (&utf8_stack), sizeof utf8_stack); + write_uRTON_t(uint64_t2uRTON_t(map_0x93[temp] - 1)); } } break; diff --git a/rton-json/src/main.cpp b/rton-json/src/main.cpp index 1634edb..b65439d 100644 --- a/rton-json/src/main.cpp +++ b/rton-json/src/main.cpp @@ -201,10 +201,10 @@ int main(const int argc, const char *argv[]){ json_list["Total files"] = json_list["JSON files"].size(); //log processed json & rton - debug.open(path / "json2rton" / "json2rton.json"); + debug.open(path / "log" / "json2rton.json"); debug << setw(4) << json_list; debug.close(); - debug.open(path / "rton2json" / "rton2json.json"); + debug.open(path / "log" / "rton2json.json"); debug << setw(4) << rton_list; debug.close(); diff --git a/rton-json/src/rton2json.cpp b/rton-json/src/rton2json.cpp index d7b8a18..ac74b81 100644 --- a/rton-json/src/rton2json.cpp +++ b/rton-json/src/rton2json.cpp @@ -46,73 +46,33 @@ json read_RTON_block(){ res.push_back(read()); break; } - //0??? - case 0x9:{ - res.push_back(0); - break; - } //uint8_t case 0xa:{ res.push_back(read()); break; } - //0??? - case 0xb:{ - res.push_back(0); - break; - } //int16_t case 0x10:{ res.push_back(read()); break; } - //0??? - case 0x11:{ - res.push_back(0); - break; - } //uint16_t case 0x12:{ res.push_back(read()); break; } - //0??? - case 0x13:{ - res.push_back(0); - break; - } //int32_t case 0x20:{ res.push_back(read()); break; } - //0??? - case 0x21:{ - res.push_back(0); - break; - } //float32 case 0x22:{ - res.push_back(read()); - break; - } - //0.0??? - case 0x23:{ - res.push_back(0.0); - break; - } - //unsigned RTON number - case 0x24:{ - res.push_back(uRTON_t2uint64_t(read_uRTON_t())); - break; - } - //RTON number - case 0x25:{ - int64_t num = uRTON_t2uint64_t(read_uRTON_t()); - if (num % 2) num = -(num + 1); - num /= 2; - - res.push_back(num); + float tmp = read(); + if (isinf(tmp)) res.push_back(tmp > 0 ? "Infinity" : "-Infinity"); + else + if (isnan(tmp)) res.push_back("NaN"); + else res.push_back(tmp); break; } //uint32_t @@ -120,57 +80,18 @@ json read_RTON_block(){ res.push_back(read()); break; } - //0??? - case 0x27:{ - res.push_back(0); - break; - } - //unsigned RTON number??? - case 0x28:{ - res.push_back(uRTON_t2uint64_t(read_uRTON_t())); - break; - } - //RTON number??? - case 0x29:{ - int64_t num = uRTON_t2uint64_t(read_uRTON_t()); - if (num % 2) num = -(num + 1); - num /= 2; - - res.push_back(num); - break; - } //int64_t case 0x40:{ res.push_back(read()); break; } - //0??? - case 0x41:{ - res.push_back(0); - break; - } //float64 case 0x42:{ - res.push_back(read()); - break; - } - //0.0??? - case 0x43:{ - res.push_back(0.0); - break; - } - //unsigned RTON number??? - case 0x44:{ - res.push_back(uRTON_t2uint64_t(read_uRTON_t())); - break; - } - //RTON number??? - case 0x45:{ - int64_t num = uRTON_t2uint64_t(read_uRTON_t()); - if (num % 2) num = -(num + 1); - num /= 2; - - res.push_back(num); + double tmp = read(); + if (isinf(tmp)) res.push_back(tmp > 0 ? "Infinity" : "-Infinity"); + else + if (isnan(tmp)) res.push_back("NaN"); + else res.push_back(tmp); break; } //uint64_t @@ -178,25 +99,6 @@ json read_RTON_block(){ res.push_back(read()); break; } - //0??? - case 0x47:{ - res.push_back(0); - break; - } - //unsigned RTON number??? - case 0x48:{ - res.push_back(uRTON_t2uint64_t(read_uRTON_t())); - break; - } - //RTON number??? - case 0x49:{ - int64_t num = uRTON_t2uint64_t(read_uRTON_t()); - if (num % 2) num = -(num + 1); - num /= 2; - - res.push_back(num); - break; - } //string case 0x81:{ uint64_t buffer = uRTON_t2uint64_t(read_uRTON_t()); @@ -235,7 +137,7 @@ json read_RTON_block(){ uint64_t second_uid = uRTON_t2uint64_t(read_uRTON_t()); uint64_t first_uid = uRTON_t2uint64_t(read_uRTON_t()); - int32_t third_uid = read(); + uint32_t third_uid = read(); stringstream ss; ss << dec << first_uid << '.' << second_uid << '.' << hex << third_uid; @@ -348,6 +250,49 @@ json read_RTON_block(){ case 0xFF:{ break; } + + //0??? + case 0x9: + case 0xb: + case 0x11: + case 0x13: + case 0x21: + case 0x27: + case 0x41: + case 0x47:{ + res.push_back(0); + break; + } + + //0.0??? + case 0x23: + case 0x43:{ + res.push_back(0.0); + break; + } + + //unsigned RTON number + case 0x24: + case 0x28: + case 0x44: + case 0x48:{ + res.push_back(uRTON_t2uint64_t(read_uRTON_t())); + break; + } + + //RTON number + case 0x25: + case 0x29: + case 0x45: + case 0x49:{ + int64_t num = uRTON_t2uint64_t(read_uRTON_t()); + if (num % 2) num = -(num + 1); + num /= 2; + + res.push_back(num); + break; + } + //else just exit error default:{ throw bytecode_error(bytecode);