예제 #1
0
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_front(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_front(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_front(Rcpp::Named(p.first) = getValue(p.second)); 
            
        } else {
            Rcpp::Rcout << "Other: " << p.first << std::endl;
            sl.push_front(p.first); 
        }
    }
    return Rcpp::as<Rcpp::List>(sl);
}
예제 #2
0
// [[Rcpp::export]]
Rcpp::List tomlparse(const std::string filename, bool verbose=false) {

    if (access(filename.c_str(), R_OK)) {
        Rcpp::stop("Cannot read given file '" + filename + "'.");
    }
            
    cpptoml::table g = cpptoml::parse_file(filename);

    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);
            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);
                sl.push_front (Rcpp::Named(p.first) = getTable(ta, verbose));
                ++ait;
            }

        } 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_front(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_front(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_front(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);
}
예제 #3
0
파일: parse.cpp 프로젝트: cran/RcppTOML
// [[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);
}