Commits

Anonymous committed 19c897f Draft

[input] fixed issues with fake delimeters

Comments (0)

Files changed (2)

input/CQGInputReader.cpp

     NotifyListeners(shared_ptr<IInputEvent>(new EofInputEvent(lastWordOrder + 1)));
 }
 
+int CQGInputReader::ReadWordsUntilDelimeterOrEof(boost::function<void (int, string)> onWordRead,
+                                                 boost::function<void (int, string)> onSpaceRead,
+                                                 string delimeter) {
+    bool isDelimeterMeet = false;
 
-int CQGInputReader::ReadWordsUntilDelimeterOrEof(boost::function<void (int, string)> onWordRead,
-                                                  boost::function<void (int, string)> onSpaceRead)
-{
-    bool isDelimeterMeet = false;
-    unsigned int order = 0;
-    while (!isDelimeterMeet && !m_inputStream.eof()) {
-        std::ostringstream word;
-        while (!m_inputStream.eof()) {
-            char letter;
-            m_inputStream >> std::noskipws >> letter;
-            // analyse letter
-            if (isalpha(letter)) { // if we read letter
-                word << letter;
-            } else { // if this is not letter
-                // are this is delimeter
-                if(letter == '=') {
-                    // if this is a delimeter
-                    char buff[3] = {0};
-                    m_inputStream.read(buff, 2);
-                    if (string(buff) == "==")
-                        isDelimeterMeet = true;
+    string space;
+    string word;
+    int wordOrder = 0;
+     while (!m_inputStream.eof() && !isDelimeterMeet) {
+        std::stringstream buffstream;
+        // try read delimeter
+        int delimeterCharsRead = 0;
+        for (; (delimeterCharsRead < delimeter.size()) && !m_inputStream.eof(); ++delimeterCharsRead) {
+            char delimeterChar;
+            m_inputStream >> std::noskipws >> delimeterChar;
+            buffstream << delimeterChar;
+            if (delimeterChar != delimeter[delimeterCharsRead])
+                break;
+        }
+        if (delimeterCharsRead == delimeter.size()) { // we have got a delimeter
+            isDelimeterMeet = true;
+            break; 
+        } else { // this is not a delimeter. deal with readed chars
+            buffstream.seekg(0, std::ios::beg);
+            // read word
+            bool fullWordRead = false;
+            while (true) {
+                char peek = buffstream.peek();
+                if (buffstream.eof()) {
+                    fullWordRead = m_inputStream.eof();
+                    break;
                 }
-                // notify about readed word
-                if (word.str() != "") { // skip empty words
-                    order++;
-                    onWordRead(order, word.str());
+                if (isalpha(peek)) {
+                    if (space.size()) {
+                        onSpaceRead(wordOrder, space);
+                        space.clear();
+                    }
+                    buffstream >> std::noskipws >> peek;                    
+                    word += peek;
+                } else {
+                    break;
                 }
-                // read space and notify about it
-                if (!isDelimeterMeet) {
-                    std::ostringstream space;
-                    space << letter;
-                    while (!m_inputStream.eof()) {
-                        char peek = m_inputStream.peek();
-                        if (isalpha(peek)) // delimeter
-                            break;
-                        if (peek == '=')    // delimeter
-                            break;
-                        m_inputStream >> std::noskipws >> letter;
-                        space << letter;
+            }
+            if (fullWordRead && word.size()) {
+                onWordRead(++wordOrder, word);
+                word.clear();
+            }
+            // read space
+            bool fullSpaceRead = false;
+            while (true) {
+                char peek = buffstream.peek();
+                if (buffstream.eof()) {
+                    fullSpaceRead = m_inputStream.eof();
+                    break;
+                }
+                if (!isalpha(peek)) {
+                    if (word.size()) {
+                        onWordRead(++wordOrder, word);
+                        word.clear();
                     }
-                    onSpaceRead(order, space.str());
+                    buffstream >> std::noskipws >> peek;                    
+                    space += peek;
+                } else {
+                    break;
                 }
-                
-                break;
+                    
+            }
+            if (fullSpaceRead && space.size()) {
+                onSpaceRead(wordOrder, space);
+                space.clear();
             }
         }
     }
-    return order + 1;
+    
+    if (word.size())
+        onWordRead(++wordOrder, word);
+    if (space.size())
+        onSpaceRead(wordOrder, space);
+
+    return wordOrder + 1;
 }
 
 

input/CQGInputReader.h

     void ReadDictionary();
     int ReadTextWords();
     int ReadWordsUntilDelimeterOrEof(boost::function<void (int, string)> onWordRead,
-                                      boost::function<void (int, string)> onSpaceRead);
+                                     boost::function<void (int, string)> onSpaceRead, 
+                                     string delimeter = "===");
 public:
     CQGInputReader(std::istream &inputStream);
     virtual void Run();