diff --git a/tools/util_generate_hex_from_json.cpp b/tools/util_generate_hex_from_json.cpp index e09876f..9ba30a5 100644 --- a/tools/util_generate_hex_from_json.cpp +++ b/tools/util_generate_hex_from_json.cpp @@ -1,7 +1,7 @@ // // Created by Eric Passmore on 1/14/23. // Purpose: command line option to generate hex code from JSON -// may be used to generate deserialization tests cases in other languages and external packages +// may be used to generate serialization tests cases in other languages and external packages // #include "../src/abieos.h" @@ -22,27 +22,27 @@ // json: the name and values // verbose: flag to print out step by step messages std::string generate_hex_from_json(const char *abi_definition, const char *contract_name, const char *schema, const char *json, bool verbose) { - if (verbose) fprintf(stderr, "Schema is: %s and json is %s\n\n",schema,json); + if (verbose) std::cerr << "Schema is: " << schema << " and json is " << json << std::endl << std::endl; // create empty context abieos_context_s *context = abieos_create(); if (! context) throw std::runtime_error("unable to create context"); - if (verbose) fprintf(stderr,"step 1 of 4: created empty ABI context\n"); + if (verbose) std::cerr << "step 1 of 4: created empty ABI context" << std::endl; // set the transaction context. // first get the contract_id uint64_t contract_id = abieos_string_to_name(context, contract_name); if (contract_id == 0) { - fprintf(stderr, "Error: abieos_string_to_name %s\n", abieos_get_error(context)); + std::cerr << "Error: abieos_string_to_name " << abieos_get_error(context) << std::endl; throw std::runtime_error("unable to set context"); } // use our id and set the ABI bool successSettingAbi = abieos_set_abi(context, contract_id, abi_definition); if (! successSettingAbi) { - fprintf(stderr, "Error: abieos_set_abi %s\n", abieos_get_error(context)); + std::cerr << "Error: abieos_set_abi " << abieos_get_error(context) << std::endl; throw std::runtime_error("unable to set context"); } - if (verbose) fprintf(stderr,"step 2 of 4: established context for transactions, packed transactions, and state history\n"); + if (verbose) std::cerr << "step 2 of 4: established context for transactions, packed transactions, and state history" << std::endl; // convert from json to binary. binary stored with context // get contract id returns integer for the ABI contract we passed in by name @@ -53,45 +53,36 @@ std::string generate_hex_from_json(const char *abi_definition, const char *contr json ); if (!successJsonToBin) { - fprintf(stderr,"failed in step 3: using context %s\n", contract_name); + std::cerr << "failed in step 3: using context " << contract_name << std::endl; throw std::runtime_error(abieos_get_error(context)); } - if (verbose) fprintf(stderr,"step 3 of 4: completed parsing json to binary\n"); + if (verbose) std::cerr << "step 3 of 4: completed parsing json to binary" << std::endl; // now time to return the hex string std::string hex = abieos_get_bin_hex(context); - if (verbose) fprintf(stderr,"step 4 of 4: converted binary to hex\n\n"); + if (verbose) std::cerr << "step 4 of 4: converted binary to hex" << std::endl << std::endl; return hex; } // prints usage void help(const char* exec_name) { - fprintf(stderr, "Usage %s: -f file -j JSON -x type\n", exec_name); - fprintf(stderr, "\t-f file with ABI definition\n"); - fprintf(stderr, "\t-v verbose, print out steps\n"); - fprintf(stderr, "\t-j json: string to convert to hex\n"); - fprintf(stderr, "\t-x type: a specific data type or schema section (example uint16, action, name, uint8[])\n"); - fprintf(stderr, "\texample: generate_hex_from_json -f ./transaction.abi -x bool -j true\n\n"); + + std::cerr << "Usage " << exec_name << ": -f -j JSON -x type [-v]" << std::endl; + std::cerr << "\t-f file with ABI definition" << std::endl; + std::cerr << "\t-v verbose, print out steps" << std::endl; + std::cerr << "\t-j json: string to convert to hex" << std::endl; + std::cerr << "\t-x type: a specific data type or schema section (example uint16, action, name, uint8[])" << std::endl; + std::cerr << "\texample: generate_hex_from_json -f ./transaction.abi -x bool -j true" << std::endl << std::endl; } // reads file returning string of contents -std::string retrieveFileContents(const std::string &filePath ) { +std::string retrieveFileContents(const std::string &filename ) { try { - size_t fileSize = static_cast(std::filesystem::file_size(filePath)); - - if (fileSize < 6) { - fprintf(stderr, "Too Small: ABI file at path: %s does not have enough content\n", filePath.c_str()); - exit(EXIT_FAILURE); - } - - std::ifstream ifs(filePath.c_str(), std::ios::in); - std::vector bytes(fileSize); - ifs.read(bytes.data(), fileSize); - - return {bytes.data(), static_cast(fileSize)}; - + std::ifstream ifs(filename, std::ios::in); + std::string string_of_file_contents((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + return string_of_file_contents; } catch(std::filesystem::filesystem_error const& ex) { - fprintf(stderr, "unable to read ABI file at path: %s\n", filePath.c_str()); + std::cerr << "unable to read ABI file at path: " << filename << std::endl; exit(EXIT_FAILURE); } } @@ -122,9 +113,9 @@ int main(int argc, char *argv[]) { case 'x': type = optarg; break; case '?': if ('c' == optopt) { - fprintf(stderr, "Option -%c requires an argument.\n", optopt); + std::cerr << "Option -" << optopt << "required an argument" << std::endl; } else if (isprint(optopt)) { - fprintf (stderr, "Unknown option `-%c'.\n", optopt); + std::cerr << "Unknown option -" << optopt << std::endl; } exit(EXIT_FAILURE); default: @@ -148,15 +139,15 @@ int main(int argc, char *argv[]) { type.c_str(), json.c_str(), verbose - ); + ); if (hex.length() > 0) { - printf("%s\n",hex.c_str()); + std::cout << hex << std::endl; } else { - fprintf(stderr,"no hex value\n"); + std::cerr << "no json value" << std::endl; } return 0; } catch (std::exception& e) { - printf("error: %s\n", e.what()); + std::cerr << "no json value: error " << e.what() << std::endl; return 1; } } \ No newline at end of file diff --git a/tools/util_generate_json_from_hex.cpp b/tools/util_generate_json_from_hex.cpp index c2aae42..411e550 100644 --- a/tools/util_generate_json_from_hex.cpp +++ b/tools/util_generate_json_from_hex.cpp @@ -1,6 +1,6 @@ // // Created by Eric Passmore on 1/19/23. -// Purpose: command line option to generate hex code from JSON +// Purpose: command line option to generate JSON from Hex code // may be used to generate deserialization tests cases in other languages and external packages // @@ -21,27 +21,27 @@ // hex: hex encoded schema // verbose: flag to print out step by step messages std::string generate_json_from_hex(const char *abi_definition, const char *contract_name, const char *schema, const char *hex, bool verbose) { - if (verbose) fprintf(stderr, "Schema is: %s and hex is %s\n\n",schema,hex); + if (verbose) std::cerr << "Schema is: " << schema << " and hex is " << hex << std::endl << std::endl; // create empty context abieos_context_s *context = abieos_create(); if (! context) throw std::runtime_error("unable to create context"); - if (verbose) fprintf(stderr,"step 1 of 3: created empty ABI context\n"); + if (verbose) std::cerr << "step 1 of 3: created empty ABI context" << std::endl; // set the transaction context. // first get the contract_id uint64_t contract_id = abieos_string_to_name(context, contract_name); if (contract_id == 0) { - fprintf(stderr, "Error: abieos_string_to_name %s\n", abieos_get_error(context)); + std::cerr << "Error: abieos_string_to_name " << abieos_get_error(context) << std::endl; throw std::runtime_error("unable to set context"); } // use our id and set the ABI bool successSettingAbi = abieos_set_abi(context, contract_id, abi_definition); if (! successSettingAbi) { - fprintf(stderr, "Error: abieos_set_abi %s\n", abieos_get_error(context)); + std::cerr << "Error: abieos_set_abi " << abieos_get_error(context) << std::endl; throw std::runtime_error("unable to set context"); } - if (verbose) fprintf(stderr,"step 2 of 3: established context for transactions, packed transactions, and state history\n"); + if (verbose) std::cerr << "step 2 of 3: established context for transactions, packed transactions, and state history" << std::endl; // convert hex to json std::string json = abieos_hex_to_json( @@ -50,38 +50,28 @@ std::string generate_json_from_hex(const char *abi_definition, const char *contr schema, hex ); - if (verbose) fprintf(stderr,"step 3 of 3: converted hex to json\n\n"); + if (verbose) std::cerr << "step 3 of 3: converted hex to json" << std::endl << std::endl; return json; } // prints usage void help(const char* exec_name) { - fprintf(stderr, "Usage %s: -f -j JSON -x type [-v]\n", exec_name); - fprintf(stderr, "\t-f file with ABI definition\n"); - fprintf(stderr, "\t-v verbose, print out steps\n"); - fprintf(stderr,"\t-h hex: string to convert to json\n"); - fprintf(stderr,"\t-x type: a specific data type or schema section (example uint16, action, name, uint8[])\n"); - fprintf(stderr, "\texample: generate_hex_from_json -f ./transaction.abi -x bool -j true\n\n"); + std::cerr << "Usage " << exec_name << ": -f -h hex -x type [-v]" << std::endl; + std::cerr << "\t-f file with ABI definition" << std::endl; + std::cerr << "\t-v verbose, print out steps" << std::endl; + std::cerr << "\t-h hex: string to convert to json" << std::endl; + std::cerr << "\t-x type: a specific data type or schema section (example uint16, action, name, uint8[])" << std::endl; + std::cerr << "\texample: generate_hex_from_json -f ./transaction.abi -x bool -h 01" << std::endl << std::endl; } // reads file returning string of contents -std::string retrieveFileContents(const std::string &filePath ) { +std::string retrieveFileContents(const std::string &filename ) { try { - size_t fileSize = static_cast(std::filesystem::file_size(filePath)); - - if (fileSize < 6) { - fprintf(stderr, "Too Small: ABI file at path: %s does not have enough content\n", filePath.c_str()); - exit(EXIT_FAILURE); - } - - std::ifstream ifs(filePath.c_str(), std::ios::in); - std::vector bytes(fileSize); - ifs.read(bytes.data(), fileSize); - - return {bytes.data(), static_cast(fileSize)}; - + std::ifstream ifs(filename, std::ios::in); + std::string string_of_file_contents((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + return string_of_file_contents; } catch(std::filesystem::filesystem_error const& ex) { - fprintf(stderr, "unable to read ABI file at path: %s\n", filePath.c_str()); + std::cerr << "unable to read ABI file at path: " << filename << std::endl; exit(EXIT_FAILURE); } } @@ -112,9 +102,9 @@ int main(int argc, char *argv[]) { case 'x': type = optarg; break; case '?': if ('c' == optopt) { - fprintf(stderr, "Option -%c requires an argument.\n", optopt); + std::cerr << "Option -" << optopt << "required an argument" << std::endl; } else if (isprint(optopt)) { - fprintf (stderr, "Unknown option `-%c'.\n", optopt); + std::cerr << "Unknown option -" << optopt << std::endl; } exit(EXIT_FAILURE); default: @@ -140,13 +130,13 @@ int main(int argc, char *argv[]) { verbose ); if (json.length() > 0) { - printf("%s\n",json.c_str()); + std::cout << json << std::endl; } else { - fprintf(stderr,"no json value\n"); + std::cerr << "no json value" << std::endl; } return 0; } catch (std::exception& e) { - printf("error: %s\n", e.what()); + std::cerr << "no json value: error " << e.what() << std::endl; return 1; } } \ No newline at end of file