配置解析器打印空白

时间:2014-07-13 07:20:59

标签: c++ string map config

只是为了好玩(谁知道我以后可能会使用它),我用c ++写了一个配置文件解析器。它似乎并不困难,在我写作的时候看不出任何问题。但是,当我测试它时,配置解析器没有捕获任何东西。我已经完成了三次代码。有人能告诉我这个问题吗?

#include <istream> // for std::basic_stream<char> (aka std::string)
#include <string> // for std::basic_string<char> (aka std::string)
#include <map> // for std::map<t, t>
#include <cctype> // for character type testing, c style

std::map<std::string, std::string> configParser(std::istream &stream, const char &comment, const char &seperator) {
    std::map<std::string, std::string> options; // to hold key, value pairs
    std::string key, value; // so options[key] = value
    bool seperatorFound; // for differentiating between key and value

    while(stream) { // operate on the stream while its good
        char current = stream.get(); // current will hold the next character returned from the stream

        if(current == '\n') { // current is a newline
            options[key] = value; // add the key, value pair as an option found
            key = ""; // reset key
            value = ""; // reset value
            seperatorFound = false; // reset seperatorFound
            continue; // jump back up to the top
        }

        else if(isspace(current)) { // current is one of: \r, \t, [SPACE]
            continue; // eat the white space and jump back up to the top
        }

        else if(current == comment) { // current is a comment marker
            getline(stream, key, '\n'); // eat the rest of the line. i use key since its alreay there
                                        // no since in creating a string object to eat a line
            key = ""; // reset key
            continue; // jump back up to the top
        }

        else if(current == seperator) { // current is a seperator marker
            seperatorFound = true; // update the seperator state
            continue; // jump back up to the top
        }

        else { // current must be a symbol
            if(!seperatorFound) { // haven't found the seperator yet. as a result, must be a key
                key += current; // give key the next letter
                continue; // jump back up to the top
            }

            // otherwise, it must be a value
            value += current; // give value the next letter instead.
        }
    }

    return options;
}

#include <iostream>
#include <sstream>

int main() {
    std::map<std::string, std::string> options;
    std::string line;

    while(true) {
        getline(std::cin, line);
        std::istringstream stream(line);

        options = configParser(stream, '#', ':');

        for(auto iterator = options.begin(); iterator != options.end(); iterator++) {
            std::cout<< iterator->first <<" : "<< iterator->second << std::endl;
        }

        std::cout<< std::endl << std::endl;
    }
}

2 个答案:

答案 0 :(得分:2)

你的解析器大多数都有效,但包装器没有。 getline不会捕获'\ n',并且您的解析器不会从没有'\ n'的行中捕获结果。

将您的main()简化为:

int main() {
    std::map<std::string, std::string> options;
    std::string line;

    options = configParser(std::cin, '#', ':');

    for(auto iterator = options.begin(); iterator != options.end(); iterator++) {
        std::cout<< iterator->first <<" : "<< iterator->second << std::endl;
    }

     std::cout<< std::endl << std::endl;
}

它主要起作用。

答案 1 :(得分:1)

问题1:您的line实际上并不包含换行符,因为getline会丢弃它。可能的解决方案是:std::istringstream stream(line + '\n');

问题2: seperatorFound未初始化。它应该初始化为false。

问题3:您应该在获得角色后检查流是否正常。也就是说,你应该这样做:

while(true) {
    char current = stream.get();
    if (!stream) break;