From 57c8ff05f4b19762b6915aac83c1245a54ee7f42 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 2 Apr 2024 17:48:37 +0200 Subject: tools, codec-json: Initial whitespace is also valid json. Also fixed reading for cli manip as well --- modules/tools/examples/cli_mod.cpp | 73 +++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) (limited to 'modules/tools/examples/cli_mod.cpp') diff --git a/modules/tools/examples/cli_mod.cpp b/modules/tools/examples/cli_mod.cpp index 42d4370..45114a0 100644 --- a/modules/tools/examples/cli_mod.cpp +++ b/modules/tools/examples/cli_mod.cpp @@ -8,16 +8,59 @@ namespace schema { using namespace saw::schema; +using AnalyzeInner = Struct< + Member, + Member +>; + using AnalyzeTest = Struct< - Member + Member, + Member >; } +std::deque split_schema_path(const std::string_view& sch_arg){ + std::deque split; + + bool escape = false; + uint64_t last_pick = 0; + for(uint64_t i = 0; i < sch_arg.size(); ++i){ + if(escape){ + escape = false; + } else { + if(sch_arg.at(i) == '\\'){ + escape = true; + }else if(sch_arg.at(i) == '.'){ + std::string sub_str{sch_arg.substr(last_pick, i - last_pick)}; + if(sub_str.empty()){ + /// @todo return error + return {}; + } + split.emplace_back(std::move(sub_str)); + last_pick = i+1u; + } + } + } + + if( ( sch_arg.size() == last_pick ) || escape ){ + return {}; + } + + std::string sub_str{sch_arg.substr(last_pick)}; + split.emplace_back(std::move(sub_str)); + + return split; +} + int main(int argc, char** argv){ /** * Basic checking and wrapping args into an array of string_view + * 1. mode + * 2. file_path + * 3. schema_path + * 4. data if it's write */ - int min_amount_args = 2; + int min_amount_args = 3; if( argc < (min_amount_args + 1) ) { std::cerr<<"Not enough arguments"<(argc) ) ){ + std::cerr<<"Invalid amount argument. Need "< bool { if(args_view.at(1) == "w"){ return false; @@ -55,12 +103,29 @@ int main(int argc, char** argv){ }(); std::string json_data; - std::deque sch_path; - int rc = saw::modify_data_on_cli(is_read_mode, args_view.at(2), sch_path, json_data); + if ( not is_read_mode ){ + json_data = std::string{args_view.at(4)}; + } + + std::deque sch_path = split_schema_path(args_view.at(3)); + { + auto eov = saw::modify_data_on_cli(is_read_mode, args_view.at(2), sch_path, json_data); + if(eov.is_error()){ + auto& err = eov.get_error(); + std::cerr<<"Modification failed: "<