2014-07-13 56 views
1

只是爲了好玩(誰知道我以後可能會用到它),我用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; 
    } 
} 
+0

科林發現了錯誤,我想;但通常只需要進行一些調試即可找到錯誤。即使簡單的printf調試在這裏也會有所幫助。 –

回答

1

問題1:您的line實際上並不包含換行符,因爲getline丟棄它。一種可能的解決方案是:std::istringstream stream(line + '\n');

問題2:seperatorFound未初始化。它應該被初始化爲false。

問題3:您應該檢查該流是否良好你得到一個角色。也就是說,你應該這樣做:

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

好的,謝謝你......所以我只是在愚蠢......然後,爲什麼我還要檢查?如果它不好,然後我嘗試提取一個字符?它不會是無效的,因爲我沒有測試它? – DTSCode

+0

是的,的確,它在這裏應該沒有什麼不同,但這樣做很好,因爲有些情況很重要。 – d0c

+0

小心擴大?不要試圖變得困難。我只是沒有做過很多真實世界的編程,所以我很好奇當有人可能這樣做 – DTSCode

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; 
} 

,它主要是工作。

+0

爲什麼不'for(auto const&p:options)std :: cout << p.first etc'? – Nawaz

+0

嗯,看看我有,但不會解析器永遠繼續? – DTSCode