virtual Node* getNode(const char* key) { std::string path = key; YAML::Node result; for(size_t i = 0; i < entries.size(); i++) { YAML::Node node = entries[i]; size_t pos = 0; while(pos < path.size()) { std::string subpath; size_t end = path.find('.', pos); if(end == std::string::npos) { subpath = path.substr(pos); pos = path.size(); } else { subpath = path.substr(pos, end - pos); pos = end + 1; } if(!node.IsNull()) { node.reset(node[subpath]); } } if(!node.IsNull() && node.IsDefined()) { return new NodeImpl(node); } } return NULL; }
bool get_field(doid_t do_id, const Field* field, val_t &value) { m_log->trace() << "Getting field on obj-" << do_id << endl; YAML::Node document; if(!load(do_id, document)) { return false; } // Get the fields from the file that are not being updated YAML::Node node = document["fields"][field->get_name()]; if(!node.IsDefined() || node.IsNull()) { return false; } m_log->trace() << "Found requested field: " + field->get_name() << endl; value = read_yaml_field(field, node, do_id); if(value.size() > 0) { return true; } return false; }
bool YamlPath::get(YAML::Node root, YAML::Node& out) { size_t beginToken = 0, endToken = 0, pathSize = codedPath.size(); auto delimiter = MAP_DELIM; // First token must be a map key. while (endToken < pathSize) { if (!root.IsDefined()) { return false; // A node before the end of the path was mising, quit! } beginToken = endToken; endToken = pathSize; endToken = std::min(endToken, codedPath.find(SEQ_DELIM, beginToken)); endToken = std::min(endToken, codedPath.find(MAP_DELIM, beginToken)); if (delimiter == SEQ_DELIM) { int index = std::stoi(&codedPath[beginToken]); if (root.IsSequence()) { root.reset(root[index]); } else { return false; } } else if (delimiter == MAP_DELIM) { auto key = codedPath.substr(beginToken, endToken - beginToken); if (root.IsMap()) { root.reset(root[key]); } else { return false; } } else { return false; // Path is malformed, return null node. } delimiter = codedPath[endToken]; // Get next character as the delimiter. ++endToken; // Move past the delimiter. } // Success! Assign the result. out.reset(root); return true; }
inline bool load(doid_t do_id, YAML::Node &document) { ifstream stream(filename(do_id)); document = YAML::Load(stream); if(!document.IsDefined() || document.IsNull()) { m_log->error() << "obj-" << do_id << " does not exist in database." << endl; return false; } if(!document["class"].IsDefined() || document["class"].IsNull()) { m_log->error() << filename(do_id) << " does not contain the 'class' key." << endl; return false; } if(!document["fields"].IsDefined() || document["fields"].IsNull()) { m_log->error() << filename(do_id) << " does not contain the 'fields' key." << endl; return false; } // Read object's DistributedClass string dc_name = document["class"].as<string>(); if(!g_dcf->get_class_by_name(dc_name)) { m_log->error() << "Class '" << dc_name << "', loaded from '" << filename(do_id) << "', does not exist." << endl; return false; } return true; }
virtual void parseFile(const char* path, const char* key) { try { YAML::Node part = YAML::LoadFile(path)[key]; if(!part.IsNull() && part.IsDefined()) entries.push_back(part); } catch(std::exception& e) { throw exception(e.what()); } }
int yaml_get_node(const config_t &config, const std::string &key, const bool optional, YAML::Node &node) { ASSERT(config.ok == true, "Config file is not loaded!"); // Recurse down config key std::vector<YAML::Node> traversal; traversal.push_back(config.root); std::istringstream iss(key); std::string element; while (std::getline(iss, element, '.')) { traversal.push_back(traversal.back()[element]); } node = traversal.back(); // Note: // // yaml_node = yaml_node["some_level_deeper"]; // // YAML::Node is mutable, by doing the above it destroys the parsed yaml // tree/graph, to avoid this problem we store the visited YAML::Node into // a std::vector and return the last visited YAML::Node // Check key if (node.IsDefined() == false && optional == false) { mexErrMsgIdAndTxt("prototype:error", "Opps [%s] missing in yaml file [%s]!", key.c_str(), config.file_path.c_str()); return -1; } else if (node.IsDefined() == false && optional == true) { return -1; } return 0; }
void GraphIO::loadNodes(const YAML::Node& doc, SemanticVersion version) { TimerPtr timer = getProfiler()->getTimer("load graph"); YAML::Node nodes = doc["nodes"]; if (nodes.IsDefined()) { for (std::size_t i = 0, total = nodes.size(); i < total; ++i) { const YAML::Node& n = nodes[i]; auto interlude = timer->step(n["uuid"].as<std::string>()); loadNode(n, version); } } }
std::unordered_map<UUID, UUID, UUID::Hasher> GraphIO::loadIntoGraph(const Snippet& snippet, const Point& position, SemanticVersion version) { double min_x = std::numeric_limits<double>::infinity(); double min_y = std::numeric_limits<double>::infinity(); YAML::Node blueprint; snippet.toYAML(blueprint); YAML::Node nodes = blueprint["nodes"]; if (nodes.IsDefined()) { for (std::size_t i = 0, total = nodes.size(); i < total; ++i) { const YAML::Node& n = nodes[i]; std::string type = n["type"].as<std::string>(); UUID new_uuid = graph_.getLocalGraph()->generateUUID(type); UUID blue_print_uuid = UUIDProvider::makeUUID_without_parent(n["uuid"].as<std::string>()); old_node_uuid_to_new_[blue_print_uuid] = new_uuid; if (n["pos"].IsDefined()) { double x = n["pos"][0].as<double>(); double y = n["pos"][1].as<double>(); if (x < min_x) { min_x = x; } if (y < min_y) { min_y = y; } } } } if (min_x != std::numeric_limits<double>::infinity() && min_y != std::numeric_limits<double>::infinity()) { position_offset_x_ = position.x - min_x; position_offset_y_ = position.y - min_y; } loadNodes(blueprint, version); loadConnections(blueprint, version); auto res = old_node_uuid_to_new_; old_node_uuid_to_new_.clear(); position_offset_x_ = 0; position_offset_y_ = 0; return res; }
TableRow::TableRow( YAML::Node config ) { this->readRequiredEntry<std::string>( table, config["table"], "rows::table" ); this->readEntry<bool>( recycleRows, config["recycleRows"], false ); this->readEntry<callback>( callbackPreUpdate, config["callbackPreUpdate"], "~" ); this->readEntry<callback>( callbackPostUpdate, config["callbackPostUpdate"], "~" ); this->readEntry<callback>( callbackPreInsert, config["callbackPreInsert"], "~" ); this->readEntry<callback>( callbackPostInsert, config["callbackPostInsert"], "~" ); this->readEntry<callback>( callbackPreDelete, config["callbackPreDelete"], "~" ); this->readEntry<callback>( callbackPostDelete, config["callbackPostDelete"], "~" ); Logger::debug( std::string( "* Table : " ) + table + "\n" + "- Recyclage de rows : " + ( recycleRows ? "Oui" : "Non" ) + "\n" + "- Callback update : " + callbackPreUpdate + " / " + callbackPostUpdate + "\n" + "- Callback insert : " + callbackPreInsert + " / " + callbackPostInsert + "\n" + "- Callback delete : " + callbackPreDelete + " / " + callbackPostDelete + "\n" + "- Champs : \n" ); YAML::Node fields = config["fields"]; for( uint i = 0; i < fields.size(); i++ ) { this->fields.push_back( TableRow_Col( fields[i] ) ); } Logger::debug( "\n" ); YAML::Node subrows = config["subRows"]; if( subrows.IsDefined() && !subrows.IsNull() ) { for( uint i = 0; i < subrows.size(); i++ ) { this->subRows.push_back( TableRow( subrows[i] ) ); } } }
YAMLDatabase(ConfigNode dbeconfig, doid_t min_id, doid_t max_id) : DatabaseBackend(dbeconfig, min_id, max_id), m_next_id(min_id), m_free_ids(), m_foldername(foldername.get_rval(m_config)) { stringstream log_name; log_name << "Database-YAML" << "(Range: [" << min_id << ", " << max_id << "])"; m_log = new LogCategory("yamldb", log_name.str()); // Open database info file ifstream infostream(m_foldername + "/info.yaml"); YAML::Node document = YAML::Load(infostream); if(document.IsDefined() && !document.IsNull()) { // Read next available id YAML::Node key_next = document["next"]; if(key_next.IsDefined() && !key_next.IsNull()) { m_next_id = document["next"].as<doid_t>(); } // Read available freed ids YAML::Node key_free = document["free"]; if(key_free.IsDefined() && !key_free.IsNull()) { for(doid_t i = 0; i < key_free.size(); i++) { m_free_ids.push_back(key_free[i].as<doid_t>()); } } } // Close database info file infostream.close(); }
T get(const std::string& path, const T& fallback = T()) { for(size_t i = 0; i < entries.size(); i++) { YAML::Node node = entries[i]; size_t pos = 0; while(pos < path.size()) { std::string subpath; size_t end = path.find('.', pos); if(end == std::string::npos) { subpath = path.substr(pos); pos = path.size(); } else { subpath = path.substr(pos, end - pos); pos = end + 1; } if(!node.IsNull()) { node.reset(node[subpath]); } } if(!node.IsNull() && node.IsDefined()) { return node.as<T>(); } } return fallback; }
parameters (YAML::Node& node, std::string i_path = "") { YAML::Node::operator= (node); path = i_path; defined = node.IsDefined (); }
static bool hasValue( const YAML::Node& v ) { return v.IsDefined() && !v.IsNull(); }