1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include "StdAfx.h"
// SimpleParser.cpp: implementation of the SimpleParser class.
//
//////////////////////////////////////////////////////////////////////
#include <sstream>
#include "mmgr.h"
#include "SimpleParser.h"
using namespace std;
/******************************************************************************/
/** call this before using GetLine() or GetCleanLine() */
CSimpleParser::CSimpleParser(CFileHandler& fh) : file(fh)
{
lineNumber = 0;
inComment = false; // /* text */ comments are not implemented
}
/** returns the current line number */
int CSimpleParser::GetLineNumber()
{
return lineNumber;
}
/** returns next line (without newlines) */
string CSimpleParser::GetLine()
{
lineNumber++;
stringstream s;
while (!file.Eof()) {
char a = '\n'; // break if this is not overwritten
file.Read(&a, 1);
if (a == '\n') { break; }
if (a != '\r') { s << a; }
}
return s.str();
}
/** returns next non-blank line (without newlines or comments) */
string CSimpleParser::GetCleanLine()
{
string::size_type pos;
while (true) {
if (file.Eof()) {
return ""; // end of file
}
string line = GetLine();
pos = line.find_first_not_of(" \t");
if (pos == string::npos) {
continue; // blank line
}
pos = line.find("//");
if (pos != string::npos) {
line.erase(pos);
pos = line.find_first_not_of(" \t");
if (pos == string::npos) {
continue; // blank line (after removing comments)
}
}
return line;
}
}
/** splits a string based on white space */
vector<string> CSimpleParser::Tokenize(const string& line, int minWords)
{
vector<string> words;
string::size_type start;
string::size_type end = 0;
while (true) {
start = line.find_first_not_of(" \t", end);
if (start == string::npos) {
break;
}
string word;
if ((minWords > 0) && ((int)words.size() >= minWords)) {
word = line.substr(start);
// strip trailing whitespace
string::size_type pos = word.find_last_not_of(" \t");
if (pos != (word.size() - 1)) {
word.resize(pos + 1);
}
end = string::npos;
}
else {
end = line.find_first_of(" \t", start);
if (end == string::npos) {
word = line.substr(start);
} else {
word = line.substr(start, end - start);
}
}
words.push_back(word);
if (end == string::npos) {
break;
}
}
return words;
}
/******************************************************************************/
|