Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ===========================
- // VMF::getToken()
- // Gets next token in file
- // ===========================
- string VMF::getToken(int& bracketsDeep, bool& error)
- {
- bool withinQuotes = false;
- string token;
- while (true)
- {
- // Get next character.
- char ch = file.get();
- // If end-of-file has been found, act accordingly and return token.
- if (file.eof())
- {
- if (bracketsDeep != 0) { error = true; }
- else if (withinQuotes) { error = true; }
- return token;
- }
- // If \" is found, end parsing.
- if (ch == '\\' && file.peek() == '\"') { error = true; break; }
- // If token is enclosed in quotes, look for characters and act accordingly.
- if (withinQuotes == true)
- {
- if (ch == '\"') { return token; }
- else if (ch == '\n') { error = true; return token; }
- }
- // If token isn't enclosed in quotes, look for characters and act accordingly.
- else if (withinQuotes == false)
- {
- if (ch == '\"')
- {
- if (token.length() == 0) { withinQuotes = true; continue; }
- else { file.unget(); return token; }
- }
- else if (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t')
- {
- if (token.length() == 0) { continue; }
- else { return token; }
- }
- else if (ch == '{' || ch == '}')
- {
- if (token.length() == 0) { token = ch; return token; }
- else { file.unget(); return token; }
- }
- else if (ch == '/' && file.peek() == '/')
- {
- while (file.get() != '\n' && !file.eof()) {}
- if (token.length() == 0) { continue; }
- else { return token; }
- }
- else if (ch == '\\') { error = true; break; }
- }
- token.push_back(ch);
- }
- }
- // ========================
- // VMF::parse()
- // Parses the VMF file.
- // ========================
- bool VMF::parse()
- {
- int bracketsDeep = 0;
- bool isOdd = false;
- bool error = false;
- while (true)
- {
- // Get first token. If bracket, act accordingly. Check for validity.
- string thisToken = getToken(bracketsDeep, error);
- if (file.eof())
- {
- if (isOdd == false) { break; }
- else { error = true; break; }
- }
- if (thisToken == "}") { --bracketsDeep; isOdd = false; continue; }
- isOdd = true;
- if (thisToken == "{") { error = true; }
- if (error) { break; }
- // Get second token. If bracket, act accordingly. Check for validity.
- string nextToken = getToken(bracketsDeep, error);
- cout << thisToken << " " << nextToken << endl;
- if (file.eof())
- {
- if (isOdd == false) { break; }
- else { error = true; break; }
- }
- if (nextToken == "{") { ++bracketsDeep; isOdd = false; continue; }
- isOdd = false;
- if (nextToken == "}") { error = true; }
- if (error) { break; }
- }
- return error;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement