Ejemplo n.º 1
0
void Config::setList(const std::string& key, const std::vector<std::string>& list)
{
    remove(key);

    if(list.size() == 0)
        return;

    OTMLNodePtr child = OTMLNode::create(key, true);
    for(const std::string& value : list)
        child->writeIn(value);
    m_confsDoc->addChild(child);
}
Ejemplo n.º 2
0
bool luavalue_cast(int index, OTMLNodePtr& node)
{
    node = OTMLNode::create();
    node->setUnique(true);
    if(g_lua.isTable(index)) {
        g_lua.pushNil();
        while(g_lua.next(index < 0 ? index-1 : index)) {
            std::string cnodeName;
            if(!g_lua.isNumber(-2))
                cnodeName = g_lua.toString(-2);
            if(g_lua.isTable()) {
                OTMLNodePtr cnode;
                if(luavalue_cast(-1, cnode)) {
                    if(cnodeName.empty())
                        node->setUnique(false);
                    else
                        cnode->setTag(cnodeName);
                    node->addChild(cnode);
                }
            } else {
                std::string value;
                if(g_lua.isBoolean())
                    value = stdext::unsafe_cast<std::string>(g_lua.toBoolean());
                else
                    value = g_lua.toString();
                if(cnodeName.empty())
                    node->writeIn(value);
                else
                    node->writeAt(cnodeName, value);
            }
            g_lua.pop();
        }
        return true;
    }
    return false;
}
Ejemplo n.º 3
0
void OTMLParser::parseNode(const std::string& data)
{
    std::string tag;
    std::string value;
    std::size_t dotsPos = data.find_first_of(':');
    int nodeLine = currentLine;

    // node that has no tag and may have a value
    if(!data.empty() && data[0] == '-') {
        value = data.substr(1);
        stdext::trim(value);
    // node that has tag and possible a value
    } else if(dotsPos != std::string::npos) {
        tag = data.substr(0, dotsPos);
        if(data.size() > dotsPos+1)
            value = data.substr(dotsPos+1);
    // node that has only a tag
    } else {
        tag = data;
    }

    stdext::trim(tag);
    stdext::trim(value);

    // process multitine values
    if(value == "|" || value == "|-" || value == "|+") {
        // reads next lines until we can a value below the same depth
        std::string multiLineData;
        do {
            size_t lastPos = in.tellg();
            std::string line = getNextLine();
            int depth = getLineDepth(line, true);

            // depth above current depth, add the text to the multiline
            if(depth > currentDepth) {
                multiLineData += line.substr((currentDepth+1)*2);
            // it has contents below the current depth
            } else {
                // if not empty, its a node
                stdext::trim(line);
                if(!line.empty()) {
                    // rewind and break
                    in.seekg(lastPos, std::ios::beg);
                    currentLine--;
                    break;
                }
            }
            multiLineData += "\n";
        } while(!in.eof());

        /* determine how to treat new lines at the end
         * | strip all new lines at the end and add just a new one
         * |- strip all new lines at the end
         * |+ keep all the new lines at the end (the new lines until next node)
         */
        if(value == "|" || value == "|-") {
            // remove all new lines at the end
            int lastPos = multiLineData.length();
            while(multiLineData[--lastPos] == '\n')
                multiLineData.erase(lastPos, 1);

            if(value == "|")
                multiLineData.append("\n");
        } // else it's |+

        value = multiLineData;
    }

    // create the node
    OTMLNodePtr node = OTMLNode::create(tag);

    node->setUnique(dotsPos != std::string::npos);
    node->setTag(tag);
    node->setSource(doc->source() + ":" + stdext::unsafe_cast<std::string>(nodeLine));

    // ~ is considered the null value
    if(value == "~")
        node->setNull(true);
    else {
        if(stdext::starts_with(value, "[") && stdext::ends_with(value, "]")) {
            std::string tmp = value.substr(1, value.length()-2);
            boost::tokenizer<boost::escaped_list_separator<char>> tokens(tmp);
            for(std::string v : tokens) {
                stdext::trim(v);
                node->writeIn(v);
            }
        } else
            node->setValue(value);
    }

    currentParent->addChild(node);
    parentMap[node] = currentParent;
    previousNode = node;
}