Exemplo n.º 1
0
static Value toValue(const YAML::Node& node, NTA_BasicType dataType)
{
    if (node.Type() == YAML::NodeType::Map || node.Type() == YAML::NodeType::Null)
    {
        NTA_THROW << "YAML string does not not represent a value.";
    }
    if (node.Type() == YAML::NodeType::Scalar)
    {
        if (dataType == NTA_BasicType_Byte)
        {
            // node >> *str;
            std::string val;
            node.Read(val);
            boost::shared_ptr<std::string> str(new std::string(val));
            Value v(str);
            return v;
        } else {
            boost::shared_ptr<Scalar> s(new Scalar(dataType));
            _toScalar(node, s);
            Value v(s);
            return v;
        }
    } else {
        // array
        boost::shared_ptr<Array> a(new Array(dataType));
        _toArray(node, a);
        Value v(a);
        return v;
    }
}
Exemplo n.º 2
0
Drawable parse_drawable(const YAML::Node& node) {
	const char* err =
			"Expected drawable object, of form \n"
					"     type: image -or- animation -or- directional -or- lua \n"
					"     file: <filename>\n"
					"-or- frames: <drawable list or file pattern>\n"
					"         Note: file patterns are of the form filename(min-max).extension\n"
					"-or- function: <lua function>";
	if (node.Type() == YAML::NodeType::Scalar) {
		return Drawable(new Image(parse_str(node)));
	}

	if (node.Type() != YAML::NodeType::Map) {
		throw YAML::RepresentationException(node.GetMark(), err);
	}

	std::string type = parse_optional(node, "type", std::string());

	if ((type.empty() && yaml_has_node(node, "file")) || type == "image") {
		return Drawable(new Image(parse_image(node)));
	} else if (type == "animation") {
		return Drawable(new Animation(parse_animation(node)));
	} else if (type == "directional") {
		return Drawable(new DirectionalDrawable(parse_directional(node)));
	} else if (type == "lua") {
		return Drawable(new LuaDrawable(parse_luadrawable(node)));
	} else {
		throw YAML::RepresentationException(node.GetMark(), err);
	}

}
Exemplo n.º 3
0
JSValue parseSceneGlobals(JSScope& jsScope, const YAML::Node& node) {
    switch(node.Type()) {
    case YAML::NodeType::Scalar: {
        auto& scalar = node.Scalar();
        if (scalar.compare(0, 8, "function") == 0) {
            return pushYamlScalarAsJsFunctionOrString(jsScope, node);
        }
        return pushYamlScalarAsJsPrimitive(jsScope, node);
    }
    case YAML::NodeType::Sequence: {
        auto jsArray = jsScope.newArray();
        for (size_t i = 0; i < node.size(); i++) {
            jsArray.setValueAtIndex(i, parseSceneGlobals(jsScope, node[i]));
        }
        return jsArray;
    }
    case YAML::NodeType::Map: {
        auto jsObject = jsScope.newObject();
        for (const auto& entry : node) {
            if (!entry.first.IsScalar()) {
                continue; // Can't put non-scalar keys in JS objects.
            }
            jsObject.setValueForProperty(entry.first.Scalar(), parseSceneGlobals(jsScope, entry.second));
        }
        return jsObject;
    }
    default:
        return jsScope.newNull();
    }
}
void Property::load( const YAML::Node& yaml_node )
{
  if( yaml_node.Type() == YAML::NodeType::Scalar )
  {
    loadValue( yaml_node );
  }
  else if( yaml_node.Type() == YAML::NodeType::Map )
  {
    loadChildren( yaml_node );
  }
  else
  {
    printf( "Property::load() TODO: error handling - unexpected YAML type (Sequence) at line %d, column %d.\n",
            yaml_node.GetMark().line, yaml_node.GetMark().column );
  }
}
Exemplo n.º 5
0
void ScServer::handleRuningStateChangedMsg( const QString & data )
{
    std::stringstream stream;
    stream << data.toStdString();
    YAML::Parser parser(stream);

    bool serverRunningState;
    std::string hostName;
    int port;

    YAML::Node doc;
    while(parser.GetNextDocument(doc)) {
        assert(doc.Type() == YAML::NodeType::Sequence);

        bool success = doc[0].Read(serverRunningState);
        if (!success) return; // LATER: report error?

        success = doc[1].Read(hostName);
        if (!success) return; // LATER: report error?

        success = doc[2].Read(port);
        if (!success) return; // LATER: report error?
    }

    QString qstrHostName( hostName.c_str() );

    onRunningStateChanged( serverRunningState, qstrHostName, port );

    emit runningStateChange( serverRunningState, qstrHostName, port );
}
Exemplo n.º 6
0
void FileBundle::Import(const YAML::Node& config)
{
    switch (config.Type())
    {
    case YAML::NodeType::Scalar:
        ImportScalarNode(config);
        break;
    case YAML::NodeType::Sequence:
        for (auto i = config.begin(); i != config.end(); ++i)
        {
            const YAML::Node& node = *i;
            switch(node.Type())
            {
            case YAML::NodeType::Scalar:
                ImportScalarNode(node);
                break;
            case YAML::NodeType::Map:
                for (auto k = node.begin(); k != node.end(); ++k)
                {
                    auto file = ImportScalarNode(k->second);
                    file->name = k->first.as<string>();
                }
                break;
            }
        }
        break;
    case YAML::NodeType::Map:
        ImportCompositeBundle(config);
        break;
    }
}
Exemplo n.º 7
0
 TEST ForceInsertIntoMap()
 {
     YAML::Node node;
     node["a"] = "b";
     node.force_insert("x", "y");
     node.force_insert("a", 5);
     YAML_ASSERT(node.size() == 3);
     YAML_ASSERT(node.Type() == YAML::NodeType::Map);
     bool ab = false;
     bool a5 = false;
     bool xy = false;
     for(YAML::const_iterator it=node.begin();it!=node.end();++it) {
         if(it->first.as<std::string>() == "a") {
             if(it->second.as<std::string>() == "b")
                 ab = true;
             else if(it->second.as<std::string>() == "5")
                 a5 = true;
         } else if(it->first.as<std::string>() == "x" && it->second.as<std::string>() == "y")
             xy = true;
     }
     YAML_ASSERT(ab);
     YAML_ASSERT(a5);
     YAML_ASSERT(xy);
     return true;
 }
Exemplo n.º 8
0
bool BandwidthGui::groupFromYaml(const std::string& yaml, GroupMap* groupMap)
{
	YAML::Node grpInfo = YAML::Load(yaml);
	if (grpInfo.IsNull())
	{
		return true;
	}
	if (grpInfo.Type() != YAML::NodeType::Map)
	{
		return false;
	}
	for (const auto& pair : grpInfo)
	{
		if(pair.first.Type() != YAML::NodeType::Scalar)
		{
			return false;
		}
		std::string key = pair.first.as<std::string>();
		for (const auto& element : pair.second)
		{
			if(element.Type() != YAML::NodeType::Scalar)
			{
				return false;
			}
			(*groupMap)[key].push_back(element.as<std::string>());
		}
	}
	return true;
}
TrackExtractor *loadTEFromDisk (const char *tename) {
    try {
        ifstream is(tename);
        if (is.bad()) {
            is.close();
            cout << "is bad" << endl;
            return defaultTE();
        }

        YAML::Parser parser(is);
        YAML::Node node;
        parser.GetNextDocument(node);
     //   std::cout << "parsed\n";
        if (node.Type() != YAML::NodeType::Map) {
            cout << "failed to parse " << tename << "node is not a map " <<endl;
            return defaultTE();
        }
        if (node.FindValue("max maggot contour angle")) { //it's a maggot
            MaggotTrackExtractor *mte = new MaggotTrackExtractor;
      //      cout << "it's a maggot" << endl;
            mte->fromYAML(node);
            return mte;
        } else {
    //        cout << "basic track extractor" << endl;
            TrackExtractor *te = new TrackExtractor;
            te->fromYAML(node);
            return te;
        }

    } catch (YAML::ParserException &e) {
        std::cout << "Parser Error " << e.what() << "\n";
        return defaultTE();
    }
   
}
Exemplo n.º 10
0
 static ConfigItem parseConfigItemFromYamlNode(const YAML::Node &n) {
   ConfigItem item;
   if(n.Type() == YAML::NodeType::Scalar) {
     std::string s;
     n.GetScalar(s);
     item.setUnparsedString(s);
   }
   return item;
 }
Exemplo n.º 11
0
void StyleContext::parseSceneGlobals(const YAML::Node& node, const std::string& key, int seqIndex,
                                     duk_idx_t dukObject) {

    switch(node.Type()) {
        case YAML::NodeType::Scalar:
            if (key.size() == 0) {
                duk_push_string(m_ctx, node.Scalar().c_str());
                duk_put_prop_index(m_ctx, dukObject, seqIndex);
            } else {
                auto nodeValue = node.Scalar();
                if (nodeValue.compare(0, 8, "function") == 0) {
                    duk_push_string(m_ctx, key.c_str()); // push property key
                    duk_push_string(m_ctx, nodeValue.c_str()); // push function string
                    duk_push_string(m_ctx, "");
                    if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) { // compile function
                        duk_put_prop(m_ctx, -3); // put {key: function()}
                    } else {
                        LOGW("Compile failed in global function: %s\n%s\n---",
                             duk_safe_to_string(m_ctx, -1),
                             nodeValue.c_str());
                        duk_pop(m_ctx); //pop error
                        duk_pop(m_ctx); //pop key
                        // push property as a string
                        duk_push_string(m_ctx, nodeValue.c_str());
                        duk_put_prop_string(m_ctx, dukObject, key.c_str());
                    }
                } else {
                    duk_push_string(m_ctx, nodeValue.c_str());
                    duk_put_prop_string(m_ctx, dukObject, key.c_str());
                }
            }
            break;
        case YAML::NodeType::Sequence:
            {
                auto seqObj = duk_push_array(m_ctx);
                for (int i = 0; i < node.size(); i++) {
                    parseSceneGlobals(node[i], "", i, seqObj);
                }
                duk_put_prop_string(m_ctx, seqObj-1, key.c_str());
                break;
            }
        case YAML::NodeType::Map:
            {
                //duk_push_string(m_ctx, key.c_str());
                auto mapObj = duk_push_object(m_ctx);
                for (auto& mapNode : node) {
                    auto itemKey = mapNode.first.Scalar();
                    parseSceneGlobals(mapNode.second, itemKey, 0, mapObj);
                }
                duk_put_prop_string(m_ctx, mapObj-1, key.c_str());
            }
        default:
            break;
    }

    return;
}
Exemplo n.º 12
0
 TEST CloneMap()
 {
     YAML::Node node = YAML::Load("{foo: bar}");
     YAML::Node clone = Clone(node);
     YAML_ASSERT(!(node == clone));
     YAML_ASSERT(clone.Type() == YAML::NodeType::Map);
     YAML_ASSERT(node.size() == clone.size());
     YAML_ASSERT(node["foo"].as<std::string>() == clone["foo"].as<std::string>());
     return true;
 }
Exemplo n.º 13
0
//==============================================================================
/// Parse the conversion expression and store it in the unit.
/// 
/// \param [in] node The node with the expression.
/// \param [in] unit_p The unit to set up/
/// 
void DefinitionParser::ParseConversions( const YAML::Node& node, Unit *unit_p )
{
    if ( node.Type() == YAML::NodeType::Scalar )
    {
        double value;
        node >> value;

        unit_p->SetToBase( Conversion::ScaleFactor( value ) );
        unit_p->SetFromBase( Conversion::ScaleFactor( 1.0 / value ) );
    }
Exemplo n.º 14
0
 TEST CloneAlias()
 {
     YAML::Node node = YAML::Load("&foo [*foo]");
     YAML::Node clone = Clone(node);
     YAML_ASSERT(!(node == clone));
     YAML_ASSERT(clone.Type() == YAML::NodeType::Sequence);
     YAML_ASSERT(node.size() == clone.size());
     YAML_ASSERT(clone == clone[0]);
     return true;
 }
Exemplo n.º 15
0
 TEST CloneSeq()
 {
     YAML::Node node = YAML::Load("[1, 3, 5, 7]");
     YAML::Node clone = Clone(node);
     YAML_ASSERT(!(node == clone));
     YAML_ASSERT(clone.Type() == YAML::NodeType::Sequence);
     YAML_ASSERT(node.size() == clone.size());
     for(std::size_t i=0;i<node.size();i++)
         YAML_ASSERT(node[i].as<int>() == clone[i].as<int>());
     return true;
 }
Exemplo n.º 16
0
bool parse(const YAML::Node& doc, Data& config)
{
    if (doc.Type() != YAML::NodeType::Map)
    {
        std::cout << "Root of the config file must be a map." << std::endl;
        return false;
    }

    Writer w(config);

    return loadFromYAMLNode(doc, w);
}
Exemplo n.º 17
0
// If node is a string, loads multiple images based on a pattern
// If node is a list, calls parse_drawable on individual entries
std::vector<Drawable> parse_drawable_list(const YAML::Node& node) {
	std::vector<Drawable> drawables;

	// Error if map
	if (node.Type() == YAML::NodeType::Map) {
		throw YAML::RepresentationException(node.GetMark(),
				"Expected list (eg [\"file.png\"]), or pattern (eg \"file(0-9).png\").");
	}

	// Interpret as file pattern if string
	if (node.Type() == YAML::NodeType::Scalar) {
		std::vector<Image> images;

		if (!filepattern_to_image_list(images, parse_str(node))) {
			throw YAML::RepresentationException(node.GetMark(),
					"Expected list (eg [\"file.png\"]), or pattern (eg \"file(0-9).png\").");
		}

		images_to_drawable_list(drawables, images);
		return drawables;
	}

	// Accumulate from list
	int size = node.size();
	for (int i = 0; i < size; i++) {
		const YAML::Node& child = node[i];

		// Expand any file patterns, or image file names
		if (child.Type() == YAML::NodeType::Scalar) {
			std::vector<Image> images;
			filepattern_to_image_list(images, parse_str(child));
			images_to_drawable_list(drawables, images);
		} else {
			parse_drawable(child);
		}
	}

	return drawables;
}
Exemplo n.º 18
0
static void _toArray(const YAML::Node& node, std::shared_ptr<Array>& a) {
  NTA_CHECK(node.Type() == YAML::NodeType::Sequence);

  a->allocateBuffer(node.size());
  void *buffer = a->getBuffer();

  for (size_t i = 0; i < node.size(); i++) {
    const YAML::Node &item = node[i];
    NTA_CHECK(item.Type() == YAML::NodeType::Scalar);
    switch (a->getType()) {
    case NTA_BasicType_Byte:
      // We should have already detected this and gone down the string path
      NTA_THROW << "Internal error: attempting to convert YAML string to array "
                   "of type Byte";
      break;
    case NTA_BasicType_UInt16:
      ((UInt16*)buffer)[i] = item.as<UInt16>();
      break;
    case NTA_BasicType_Int16:
      ((Int16*)buffer)[i] = item.as<Int16>();
      break;
    case NTA_BasicType_UInt32:
     ((UInt32*)buffer)[i] = item.as<UInt32>();
      break;
    case NTA_BasicType_Int32:
     ((Int32*)buffer)[i] = item.as<Int32>();
      break;
    case NTA_BasicType_UInt64:
     ((UInt64*)buffer)[i] = item.as<UInt64>();
      break;
    case NTA_BasicType_Int64:
     ((Int64*)buffer)[i] = item.as<Int64>();
      break;
    case NTA_BasicType_Real32:
     ((Real32*)buffer)[i] = item.as<Real32>();
      break;
    case NTA_BasicType_Real64:
     ((Real64*)buffer)[i] = item.as<Real64>();
      break;
    case NTA_BasicType_Bool:
     ((bool*)buffer)[i] = item.as<bool>();
      break;
    default:
      // should not happen
      NTA_THROW << "Unknown data type " << a->getType();
    }
  }
}
Exemplo n.º 19
0
 static ConfigVector parseConfigVectorFromYamlNode(const YAML::Node &n) {
   ConfigVector vec;
   if(n.Type() == YAML::NodeType::Sequence) {
     YAML::Iterator it;
     for(it = n.begin(); it != n.end(); ++it) {
       ConfigItem item;
       if(it->Type() == YAML::NodeType::Scalar) {
         item = parseConfigItemFromYamlNode(*it);
       } else if(it->Type() == YAML::NodeType::Sequence) {
         item[""] = parseConfigVectorFromYamlNode(*it);
       } else if(it->Type() == YAML::NodeType::Map) {
         item.children = parseConfigMapFromYamlNode(*it);
       }
       vec.push_back(item);
     }
   }
   return vec;
 }
Exemplo n.º 20
0
void Property::loadChildren( const YAML::Node& yaml_node )
{
  if( yaml_node.Type() != YAML::NodeType::Map )
  {
    printf( "Property::loadChildren() TODO: error handling - unexpected YAML type.\n" );
    return;
  }

  // A special map entry named "Value" means the value of this property, not a child.
  if( const YAML::Node *value_node = yaml_node.FindValue( "Value" ))
  {
    loadValue( *value_node );
  }

  // Yaml-cpp's FindValue() and operator[] functions are order-N,
  // according to the docs, so we don't want to use those.  Instead we
  // make a hash table of the existing property children, then loop
  // over all the yaml key-value pairs, looking up their targets by
  // key (name) in the map.  This should keep this function down to
  // order-N or close, instead of order N squared.

  // First make the hash table of all child properties indexed by name.
  QHash<QString, Property*> child_map;
  int num_property_children = children_.size();
  for( int i = 0; i < num_property_children; i++ )
  {
    Property* child = children_.at( i );
    child_map[ child->getName() ] = child;
  }

  // Next loop over all yaml key/value pairs, calling load() on each
  // child whose name we find.
  for( YAML::Iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
  {
    QString key;
    it.first() >> key;
    QHash<QString, Property*>::const_iterator hash_iter = child_map.find( key );
    if( hash_iter != child_map.end() )
    {
      Property* child = hash_iter.value();
      child->load( it.second() );
    }
  }
}
Exemplo n.º 21
0
QStandardItemModel * ReferencesDialog::parse(const QString &responseData)
{
    using namespace ScLanguage;
    const Introspection & introspection = Main::scProcess()->introspection();

    if (!introspection.introspectionAvailable()) { // just required for short path name
        MainWindow::instance()->showStatusMessage("Introspection data not yet available");
        return NULL;
    }

    std::stringstream stream;
    stream << responseData.toStdString();
    YAML::Parser parser(stream);

    YAML::Node doc;
    if(!parser.GetNextDocument(doc)) {
        qWarning("no YAML document");
        return NULL;
    }

    assert (doc.Type() == YAML::NodeType::Sequence);

    QString symbol = doc[0].to<std::string>().c_str();

    QStandardItemModel * model = new QStandardItemModel(this);
    QStandardItem *parentItem = model->invisibleRootItem();

    YAML::Node const & references = doc[1];

    for (YAML::Iterator refIt = references.begin(); refIt != references.end(); ++refIt ) {
        YAML::Node const & reference = *refIt;
        QString className  = reference[0].to<std::string>().c_str();
        QString methodName = reference[1].to<std::string>().c_str();
        QString path       = reference[2].to<std::string>().c_str();
        int charPos        = reference[3].to<int>();

        QString displayPath = introspection.compactLibraryPath(path);
        QString fullName = ScLanguage::makeFullMethodName(className, methodName);

        parentItem->appendRow(makeDialogItem(fullName, displayPath, path, charPos, className, methodName, false));
    }

    return model;
}
Exemplo n.º 22
0
static void _toScalar(const YAML::Node &node, std::shared_ptr<Scalar> &s) {
  NTA_CHECK(node.Type() == YAML::NodeType::Scalar);
  switch (s->getType()) {
  case NTA_BasicType_Byte:
    // We should have already detected this and gone down the string path
    NTA_THROW << "Internal error: attempting to convert YAML string to scalar of type Byte";
    break;
  case NTA_BasicType_UInt16:
    s->value.uint16 = node.as<UInt16>();
    break;
  case NTA_BasicType_Int16:
    s->value.int16 = node.as<Int16>();
    break;
  case NTA_BasicType_UInt32:
    s->value.uint32 = node.as<UInt32>();
    break;
  case NTA_BasicType_Int32:
    s->value.int32 = node.as<Int32>();
    break;
  case NTA_BasicType_UInt64:
    s->value.uint64 = node.as<UInt64>();
    break;
  case NTA_BasicType_Int64:
    s->value.int64 = node.as<Int64>();
    break;
  case NTA_BasicType_Real32:
    s->value.real32 = node.as<Real32>();
    break;
  case NTA_BasicType_Real64:
    s->value.real64 = node.as<Real64>();
    break;
  case NTA_BasicType_Bool:
    s->value.boolean = node.as<bool>();
    break;
  case NTA_BasicType_Handle:
    NTA_THROW << "Attempt to specify a YAML value for a scalar of type Handle";
    break;
  default:
    // should not happen
    const std::string val = node.as<std::string>();
    NTA_THROW << "Unknown data type " << s->getType() << " for yaml node '" << val << "'";
  }
}
Exemplo n.º 23
0
// If node is a string, loads as image
// If node is a map, 'type:' indicates drawable type
std::vector<Image> parse_image_list(const YAML::Node& node) {
	std::vector<Image> images;

	// Interpret as file pattern if string
	if (node.Type() == YAML::NodeType::Scalar) {
		if (!filepattern_to_image_list(images, parse_str(node))) {
			throw YAML::RepresentationException(node.GetMark(),
					"Expected list (eg [\"file.png\"]), or pattern (eg \"file(0-9).png\").");
		}
		return images;
	}

	// Accumulate from list
	int size = node.size();
	for (int i = 0; i < size; i++) {
		filepattern_to_image_list(images, parse_str(node[i]));
	}

	return images;
}
Exemplo n.º 24
0
void ServerParser::setServerConfiguration(const YAML::Node& node, TagServerConfiguration& configuration) {
	if (node.Type() == YAML::NodeType::Map) {
		if (!obtenerValorScalarAlfaNumerico(node, "ip", configuration.ip)) {
			//Logger::getInstance()->writeWarning("YAML-CPP: Se toma por default (velocidad personaje).");
			configuration.ip = SERVER_IP_DEFAULT;
		}
		if (!obtenerValorScalarNumericoPositivo(node, "port", configuration.port)) {
			//Logger::getInstance()->writeWarning("YAML-CPP: Se toma por default (margen scroll).");
			configuration.port = SERVER_PORT_DEFAULT;
		}
		if (!obtenerValorScalarNumericoPositivo(node, "max_clients", configuration.max_clients)) {
			//Logger::getInstance()->writeWarning("YAML-CPP: Se toma por default (velocidad scroll).");
			configuration.max_clients = SERVER_MAX_CLIENTS_DEFAULT;
		}
	}
	else {
		//Logger::getInstance()->writeWarning("YAML-CPP:El contenido del tag de configuracion no es del tipo Map. Ubicar" + ubicarNodo(node.GetMark()));
		setServerConfigurationDefault(configuration);
	}
}
Exemplo n.º 25
0
void YamlConfigReader::readYamlNode( Config& config, const YAML::Node& yaml_node )
{
  switch( yaml_node.Type() )
  {
  case YAML::NodeType::Map:
  {
    for( YAML::Iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
    {
      std::string key;
      it.first() >> key;
      Config child = config.mapMakeChild( QString::fromStdString( key ));
      readYamlNode( child, it.second() );
    }
    break;
  }
  case YAML::NodeType::Sequence:
  {
    for( YAML::Iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
    {
      Config child = config.listAppendNew();
      readYamlNode( child, *it );
    }
    break;
  }
  case YAML::NodeType::Scalar:
  {
    std::string s;
    yaml_node >> s;
    config.setValue( QString::fromStdString( s ));
    break;
  }
  case YAML::NodeType::Null:
  default:
    break;
  }
}
Exemplo n.º 26
0
Arquivo: fncs.cpp Projeto: FNCS/fncs
static inline string nodetype(const YAML::Node &node) {
    if (node.Type() == YAML::NodeType::Scalar) { return "SCALAR"; } 
    else if (node.Type() == YAML::NodeType::Sequence) { return "SEQUENCE"; } 
    else if (node.Type() == YAML::NodeType::Map) { return "MAP"; } 
    else { return "ERROR"; }
}
Exemplo n.º 27
0
/*
 * For converting param specs for Regions and LinkPolicies
 */
ValueMap toValueMap(const char* yamlstring,
                    Collection<ParameterSpec>& parameters,
                    const std::string & nodeType,
                    const std::string & regionName)
{

    ValueMap vm;

    // yaml-cpp bug: append a space if it is only one character
    // This is very inefficient, but should be ok since it is
    // just used at construction time for short strings
    std::string paddedstring(yamlstring);
    // TODO: strip white space to determine if empty
    bool empty = (paddedstring.size() == 0);

    if (paddedstring.size() < 2)
        paddedstring = paddedstring + " ";
    std::stringstream s(paddedstring);
    // IMemStream s(yamlstring, ::strlen(yamlstring));

    // TODO: utf-8 compatible?
    YAML::Node doc;
    if (!empty)
    {
        YAML::Parser parser(s);
        bool success = parser.GetNextDocument(doc);

        if (!success)
            NTA_THROW << "Unable to find document in YAML string";

        // A ValueMap is specified as a dictionary
        if (doc.Type() != YAML::NodeType::Map)
        {
            std::string ys(yamlstring);
            if (ys.size() > 30)
            {
                ys = ys.substr(0, 30) + "...";
            }
            NTA_THROW << "YAML string '" << ys
                      << "' does not not specify a dictionary of key-value pairs. "
                      << "Region and Link parameters must be specified at a dictionary";
        }
    }

    // Grab each value out of the YAML dictionary and put into the ValueMap
    // if it is allowed by the nodespec.
    YAML::Iterator i;
    for (i = doc.begin(); i != doc.end(); i++)
    {
        const std::string key = i.first().to<std::string>();
        if (!parameters.contains(key))
        {
            std::stringstream ss;
            for (UInt j = 0; j < parameters.getCount(); j++)
            {
                ss << "   " << parameters.getByIndex(j).first << "\n";
            }

            if (nodeType == std::string(""))
            {
                NTA_THROW << "Unknown parameter '" << key << "'\n"
                          << "Valid parameters are:\n" << ss.str();
            }
            else
            {
                NTA_CHECK(regionName != std::string(""));
                NTA_THROW << "Unknown parameter '" << key << "' for region '"
                          << regionName << "' of type '" << nodeType << "'\n"
                          << "Valid parameters are:\n" << ss.str();
            }
        }
        if (vm.contains(key))
            NTA_THROW << "Parameter '" << key << "' specified more than once in YAML document";
        ParameterSpec spec = parameters.getByName(key);
        try
        {
            Value v = toValue(i.second(), spec.dataType);
            if (v.isScalar() && spec.count != 1)
            {
                throw std::runtime_error("Expected array value but got scalar value");
            }
            if (!v.isScalar() && spec.count == 1)
            {
                throw std::runtime_error("Expected scalar value but got array value");
            }
            vm.add(key, v);
        } catch (std::runtime_error& e) {
            NTA_THROW << "Unable to set parameter '" << key << "'. " << e.what();
        }
    }

    // Populate ValueMap with default values if they were not specified in the YAML dictionary.
    for (size_t i = 0; i < parameters.getCount(); i++)
    {
        std::pair<std::string, ParameterSpec>& item = parameters.getByIndex(i);
        if (!vm.contains(item.first))
        {
            ParameterSpec & ps = item.second;
            if (ps.defaultValue != "")
            {
                // TODO: This check should be uncommented after dropping NuPIC 1.x nodes (which don't comply)
                // if (ps.accessMode != ParameterSpec::CreateAccess)
                // {
                //   NTA_THROW << "Default value for non-create parameter: " << item.first;
                // }

                try {
#ifdef YAMLDEBUG
                    NTA_DEBUG << "Adding default value '" << ps.defaultValue
                              << "' to parameter " << item.first
                              << " of type " << BasicType::getName(ps.dataType)
                              << " count " << ps.count;
#endif
                    Value v = toValue(ps.defaultValue, ps.dataType);
                    vm.add(item.first, v);
                } catch (...) {
                    NTA_THROW << "Unable to set default value for item '"
                              << item.first << "' of datatype "
                              << BasicType::getName(ps.dataType)
                              <<" with value '" << ps.defaultValue << "'";
                }
            }
        }
    }

    return vm;
}
Exemplo n.º 28
0
void
YamlConfiguration::read_config_doc(const YAML::Node &doc, YamlConfigurationNode *&node)
{
  if (! node) {
    node = new YamlConfigurationNode("root");
  }

  if (doc.Type() == YAML::NodeType::Map) {
#ifdef HAVE_YAMLCPP_0_5
    for (YAML::const_iterator it = doc.begin(); it != doc.end(); ++it) {
      std::string key = it->first.as<std::string>();
#else
    for (YAML::Iterator it = doc.begin(); it != doc.end(); ++it) {
      std::string key;
      it.first() >> key;
#endif
      YamlConfigurationNode *in = node;
      if (key.find("/") != std::string::npos) {
	// we need to split and find the proper insertion node
	std::vector<std::string> pel = str_split(key);
	for (size_t i = 0; i < pel.size() - 1; ++i) {
	  YamlConfigurationNode *n = (*in)[pel[i]];
	  if (! n) {
	    n = new YamlConfigurationNode(pel[i]);
	    in->add_child(pel[i], n);
	  }
	  in = n;
	}

	key = pel.back();
      }

      YamlConfigurationNode *tmp = (*in)[key];
      if (tmp) {
#ifdef HAVE_YAMLCPP_0_5
	if (tmp->is_scalar() && it->second.Type() != YAML::NodeType::Scalar)
#else
	if (tmp->is_scalar() && it.second().Type() != YAML::NodeType::Scalar)
#endif
	{
	  throw Exception("YamlConfig: scalar %s cannot be overwritten by non-scalar",
			  tmp->name().c_str());
	}
#ifdef HAVE_YAMLCPP_0_5
	tmp->set_scalar(it->second.Scalar());
#else
	std::string s;
	if (it.second().GetScalar(s)) {
	  tmp->set_scalar(s);
	}
#endif
      } else {
#ifdef HAVE_YAMLCPP_0_5
	YamlConfigurationNode *tmp = new YamlConfigurationNode(key, it->second);
	in->add_child(key, tmp);
	read_config_doc(it->second, tmp);
#else
	YamlConfigurationNode *tmp = new YamlConfigurationNode(key, it.second());
	in->add_child(key, tmp);
	read_config_doc(it.second(), tmp);
#endif
      }
    }

  } else if (doc.Type() == YAML::NodeType::Scalar) {
    if (doc.Tag() == "tag:fawkesrobotics.org,cfg/tcp-port" ||
	doc.Tag() == "tag:fawkesrobotics.org,cfg/udp-port")
    {
      unsigned int p = 0;
      try {
	p = node->get_uint();
      } catch (Exception &e) {
	e.prepend("YamlConfig: Invalid TCP/UDP port number (not an unsigned int)");
	throw;
      }
      if (p <= 0 || p >= 65535) {
	throw Exception("YamlConfig: Invalid TCP/UDP port number "
			"(%u out of allowed range)", p);
      }
    } else if (doc.Tag() == "tag:fawkesrobotics.org,cfg/url") {
#ifdef HAVE_YAMLCPP_0_5
      std::string scalar = doc.Scalar();
#else
      std::string scalar;
      doc.GetScalar(scalar);
#endif
#ifdef USE_REGEX_CPP
      if (regex_search(scalar, __url_regex)) {
#  if 0
	// just for emacs auto-indentation
      }
#  endif
#else
      if (regexec(&__url_regex, scalar.c_str(), 0, NULL, 0) == REG_NOMATCH) {
	throw Exception("YamlConfig: %s is not a valid URL", scalar.c_str());
      }
#endif
    } else if (doc.Tag() == "tag:fawkesrobotics.org,cfg/frame") {
#ifdef HAVE_YAMLCPP_0_5
      std::string scalar = doc.Scalar();
#else
      std::string scalar;
      doc.GetScalar(scalar);
#endif
#ifdef USE_REGEX_CPP
      if (regex_search(scalar, __frame_regex)) {
#  if 0
	// just for emacs auto-indentation
      }
#  endif
#else
      if (regexec(&__frame_regex, scalar.c_str(), 0, NULL, 0) == REG_NOMATCH) {
	throw Exception("YamlConfig: %s is not a valid frame ID", scalar.c_str());
      }
#endif
    }

  }
}
Exemplo n.º 29
0
/*
 * For converting param specs for Regions and LinkPolicies
 */
ValueMap toValueMap(const char *yamlstring,
                    Collection<ParameterSpec> &parameters,
                    const std::string &nodeType,
                    const std::string &regionName) {

  ValueMap vm;

  // special value that applies to all regions.
  ParameterSpec dim_spec("Buffer dimensions for region's global dimensions. "
                    "Syntax: {dim: [2,3]}",  // description
	                  NTA_BasicType_UInt32,
							      0,                         // elementCount (an array of unknown size)
							      "",                        // constraints
							      "",                        // defaultValue
							      ParameterSpec::ReadWriteAccess);


  std::string paddedstring(yamlstring);
  // TODO: strip white space to determine if empty
  bool empty = (paddedstring.size() == 0);

  // TODO: utf-8 compatible?
  const YAML::Node doc = YAML::Load(paddedstring);
  if(!empty) {
    // A ValueMap is specified as a dictionary
    if (doc.Type() != YAML::NodeType::Map) {
      std::string ys(yamlstring);
      if (ys.size() > 30) {
        ys = ys.substr(0, 30) + "...";
      }
      NTA_THROW
          << "YAML string '" << ys
          << "' does not not specify a dictionary of key-value pairs. "
          << "Region and Link parameters must be specified as a dictionary";
    }
  }
  // Grab each value out of the YAML dictionary and put into the ValueMap
  // if it is allowed by the nodespec.
  for (auto i = doc.begin(); i != doc.end(); i++)
  {
    ParameterSpec ps;

    const auto key = i->first.as<std::string>();
    if (key == "dim")
      ps = dim_spec;
    else {
      if (!parameters.contains(key))
      {
        std::stringstream ss;
        for (UInt j = 0; j < parameters.getCount(); j++){
          ss << "   " << parameters.getByIndex(j).first << "\n";
        }

        if (nodeType == std::string("")) {
          NTA_THROW << "Unknown parameter '" << key << "'\n"
                    << "Valid parameters are:\n" << ss.str();
        } else {
          NTA_CHECK(regionName != std::string(""));
          NTA_THROW << "Unknown parameter '" << key << "' for region '"
                    << regionName << "' of type '" << nodeType << "'\n"
                    << "Valid parameters are:\n"
                    << ss.str();
        }
      }
      ps = parameters.getByName(key); // makes a copy of ParameterSpec
    }
    if (vm.contains(key))
        NTA_THROW << "Parameter '" << key << "' specified more than once in YAML document";
    try
    {
      if (ps.accessMode == ParameterSpec::ReadOnlyAccess) {
        NTA_THROW << "Parameter '" << key << "'. This is ReadOnly access. Cannot be set.";
      }
      Value v = toValue(i->second, ps.dataType);
      if (v.isScalar() && ps.count != 1)
      {
        NTA_THROW << "Parameter '" << key << "'. Bad value in runtime parameters. Expected array value but got scalar value";
      }
      if (!v.isScalar() && ps.count == 1)
      {
        NTA_THROW << "Parameter '" << key << "'. Bad value in runtime parameters. Expected scalar value but got array value";
      }
      vm.add(key, v);
    } catch (std::runtime_error &e) {
      NTA_THROW << "Unable to set parameter '" << key << "'. " << e.what();
    }
  } //end for

  // Populate ValueMap with default values if they were not specified in the YAML dictionary.
  for (size_t i = 0; i < parameters.getCount(); i++)
  {
    const std::pair<std::string, ParameterSpec>& item = parameters.getByIndex(i);
    if (!vm.contains(item.first))
    {
      const ParameterSpec & ps = item.second;
      if (ps.defaultValue != "")
      {
        // TODO: This check should be uncommented after dropping NuPIC 1.x nodes (which don't comply) //FIXME try this
        // if (ps.accessMode != ParameterSpec::CreateAccess)
        // {
        //   NTA_THROW << "Default value for non-create parameter: " << item.first;
        // }

        try {
#ifdef YAMLDEBUG
          NTA_DEBUG << "Adding default value '" << ps.defaultValue
                    << "' to parameter " << item.first << " of type "
                    << BasicType::getName(ps.dataType) << " count " << ps.count;
#endif
          // NOTE: this can handle both scalers and arrays
          //       Arrays MUST be in Yaml sequence format even if one element.
          //       i.e.  [1,2,3]
          Value v = toValue(ps.defaultValue, ps.dataType);
          if (v.isScalar() && ps.count != 1)
          {
            NTA_THROW << "Parameter '" << item.first << "'. Bad default value in spec. Expected array value but got scalar value";
          }
          if (!v.isScalar() && ps.count == 1)
          {
            NTA_THROW << "Parameter '" << item.first << "'. Bad default value in spec. Expected scalar value but got array value";
          }
          vm.add(item.first, v);
        } catch (...) {
          NTA_THROW << "Unable to set default value for item '" << item.first
                    << "' of datatype " << BasicType::getName(ps.dataType)
                    << " with value '" << ps.defaultValue << "'";
        }
      }
    }
  }

  return vm;
}
Exemplo n.º 30
0
static void yaml_traverse(struct VMGlobals* g, const YAML::Node & node, PyrObject *parent, PyrSlot *slot) {
	YAML::NodeType::value type = node.Type();
	string out;
	PyrObject *result = NULL;

	switch (type)
	{
		case YAML::NodeType::Scalar:
			node >> out;
			result = (PyrObject*)newPyrString(g->gc, out.c_str(), 0, true);
			SetObject(slot, result);
			if(parent) g->gc->GCWriteNew(parent, result); // we know result is white so we can use GCWriteNew
			break;

		case YAML::NodeType::Sequence:
			result = newPyrArray(g->gc, node.size(), 0, true);
			SetObject(slot, result);
			if(parent) g->gc->GCWriteNew(parent, result); // we know result is white so we can use GCWriteNew
			for (unsigned int i = 0; i < node.size(); i++) {
				const YAML::Node & subnode = node[i];
				result->size++;
				yaml_traverse(g, subnode, result, result->slots+i);
			}
			break;

		case YAML::NodeType::Map:
		{
			result = instantiateObject( g->gc, s_dictionary->u.classobj, 0, false, true );
			SetObject(slot, result);
			if(parent) g->gc->GCWriteNew(parent, result); // we know result is white so we can use GCWriteNew

			PyrObject *array = newPyrArray(g->gc, node.size()*2, 0, true);
			result->size = 2;
			SetObject(result->slots, array);      // array
			SetInt(result->slots+1, node.size()); // size
			g->gc->GCWriteNew(result, array); // we know array is white so we can use GCWriteNew

			int j = 0;
			for (YAML::Iterator i = node.begin(); i != node.end(); ++i) {
				const YAML::Node & key   = i.first();
				const YAML::Node & value = i.second();
				key >> out;
				PyrObject *pkey = (PyrObject*)newPyrString(g->gc, out.c_str(), 0, true);
				SetObject(array->slots+j, pkey);
				array->size++;
				g->gc->GCWriteNew(array, pkey); // we know pkey is white so we can use GCWriteNew

				array->size++;
				yaml_traverse(g, value, array, array->slots+j+1);

				j += 2;
			}
			break;
		}

		case YAML::NodeType::Null:
			SetNil(slot);
			break;

		default:
			postfl("WARNING: yaml_traverse(): unknown/unsupported node type\n");
			SetNil(slot);
	}
}