SEXP getArray(const cpptoml::array& arr) { Rcpp::StretchyList sl; bool nonested = true; // ie no embedded array auto it = arr.get().begin(); while (it != arr.get().end()) { if ((*it)->is_array()) { sl.push_back(getArray(*(*it)->as_array())); nonested = false; } else { sl.push_back(getValue(*it)); nonested = true; } it++; } if (nonested) { return collapsedList(Rcpp::as<Rcpp::List>(sl)); } else { return Rcpp::as<Rcpp::List>(sl); } }
SEXP getTable(const std::shared_ptr<cpptoml::table>& t, bool verbose=false) { Rcpp::StretchyList sl; for (auto & p : *t) { if (p.second->is_table()) { auto ga = std::dynamic_pointer_cast<cpptoml::table>(p.second); if (verbose) Rcpp::Rcout << "Table: " << p.first << std::endl; sl.push_back(Rcpp::Named(p.first) = getTable(ga, verbose)); } else if (p.second->is_array()) { auto ga = std::dynamic_pointer_cast<cpptoml::array>(p.second); if (verbose) { Rcpp::Rcout << "Array: " << p.first << std::endl; printArray(Rcpp::Rcout, *ga); } sl.push_back(Rcpp::Named(p.first) = getArray(*ga)); } else if (p.second->is_value()) { if (verbose) { Rcpp::Rcout << "Value: " << p.first << "\n :"; printValue(Rcpp::Rcout, p.second); Rcpp::Rcout << std::endl; } sl.push_back(Rcpp::Named(p.first) = getValue(p.second)); } else if (p.second->is_table_array()) { if (verbose) Rcpp::Rcout << "TableArray: " << p.first << std::endl; Rcpp::StretchyList l; auto arr = t->get_table_array(p.first)->get(); auto ait = arr.begin(); while (ait != arr.end()) { auto ta = std::dynamic_pointer_cast<cpptoml::table>(*ait); l.push_back (getTable(ta, verbose)); ++ait; } sl.push_back(Rcpp::Named(p.first) = Rcpp::as<Rcpp::List>(l)); } else { if (verbose) Rcpp::Rcout << "Other: " << p.first << std::endl; sl.push_back(p.first); } } return Rcpp::as<Rcpp::List>(sl); }
// [[Rcpp::export]] Rcpp::List tomlparseImpl(const std::string input, bool verbose=false, bool fromfile=true, bool includize=false) { if (fromfile && access(input.c_str(), R_OK)) { Rcpp::stop("Cannot read given file '" + input + "'."); } std::shared_ptr<cpptoml::table> g; if (fromfile) { if (includize) { includize::toml_preprocessor pp(input.c_str()); cpptoml::parser included_parser(pp); g = included_parser.parse(); } else { g = cpptoml::parse_file(input.c_str()); } } else { std::stringstream strstream(input); cpptoml::parser p(strstream); g = p.parse(); } if (verbose) { Rcpp::Rcout << "<default print method>\n" << (*g) << "</default print method>\n" << std::endl; } Rcpp::StretchyList sl; for (auto & p : (*g)) { if (p.second->is_table_array()) { if (verbose) Rcpp::Rcout << "TableArray: " << p.first << std::endl; //auto ga = std::dynamic_pointer_cast<cpptoml::table_array>(p.second); Rcpp::StretchyList l; auto arr = g->get_table_array(p.first)->get(); auto ait = arr.begin(); while (ait != arr.end()) { auto ta = std::dynamic_pointer_cast<cpptoml::table>(*ait); l.push_back (getTable(ta, verbose)); ++ait; } sl.push_back(Rcpp::Named(p.first) = Rcpp::as<Rcpp::List>(l)); } else if (p.second->is_table()) { auto ga = std::dynamic_pointer_cast<cpptoml::table>(p.second); if (verbose) Rcpp::Rcout << "Table: " << p.first << std::endl; sl.push_back(Rcpp::Named(p.first) = getTable(ga, verbose)); } else if (p.second->is_array()) { auto ga = std::dynamic_pointer_cast<cpptoml::array>(p.second); if (verbose) Rcpp::Rcout << "Array: " << p.first << std::endl; sl.push_back(Rcpp::Named(p.first) = getArray(*ga)); } else if (p.second->is_value()) { if (verbose) { Rcpp::Rcout << "Value: " << p.first << "\n :"; printValue(Rcpp::Rcout, p.second); Rcpp::Rcout << std::endl; } sl.push_back(Rcpp::Named(p.first) = getValue(p.second)); } else { if (verbose) Rcpp::Rcout << "Other: " << p.first << std::endl; sl.push_front(p.first); } } return Rcpp::as<Rcpp::List>(sl); }