Last active
August 29, 2015 14:04
-
-
Save satoruhiga/63507cdbdec04293a0c6 to your computer and use it in GitHub Desktop.
ofxMaxCollTextParser.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
class ofxMaxCollTextParser | |
{ | |
public: | |
bool open(const string& path) | |
{ | |
if (!ofFile::doesFileExist(path)) return false; | |
ofBuffer buf = ofBufferFromFile(path); | |
data.clear(); | |
coll_indexes.clear(); | |
while (!buf.isLastLine()) | |
{ | |
string line = buf.getNextLine(); | |
if (!line.empty()) | |
parse_line(line); | |
} | |
if (data.empty()) return false; | |
coll_index = coll_indexes.front(); | |
return true; | |
} | |
const vector<int>& getCollIndexes() const { return coll_indexes; } | |
int getNumColls() const { return coll_indexes.size(); } | |
int setCollIndexAt(int index) | |
{ | |
setCollIndex(coll_indexes[index]); | |
return coll_indexes[index]; | |
} | |
void setCollIndex(int coll_index) { this->coll_index = coll_index; } | |
int getCollIndex() const { return coll_index; } | |
string getString(int index) { return data[coll_index].at(index); } | |
int getInt(int index) { return ofToInt(data[coll_index].at(index)); } | |
float getFloat(int index) { return ofToFloat(data[coll_index].at(index)); } | |
private: | |
typedef vector<string> StringArray; | |
vector<int> coll_indexes; | |
map<int, StringArray> data; | |
int coll_index; | |
void parse_line(const string& line) | |
{ | |
string chunk; | |
char last_char; | |
bool has_index = false; | |
bool text_started = false; | |
int index = 0; | |
vector<string> arr; | |
string::const_iterator it = line.begin(); | |
while (it != line.end()) | |
{ | |
char c = *it; | |
bool ignore = false; | |
if (!has_index | |
&& c == ',') | |
{ | |
has_index = true; | |
index = ofToInt(chunk); | |
chunk.clear(); | |
ignore = true; | |
} | |
else if (c == '"') | |
{ | |
if (!text_started) | |
{ | |
text_started = true; | |
ignore = true; | |
} | |
else | |
{ | |
if (last_char == '\\') | |
{ | |
chunk = chunk.substr(0, chunk.size() - 1); | |
} | |
else | |
{ | |
if (!chunk.empty()) | |
arr.push_back(chunk); | |
chunk.clear(); | |
ignore = true; | |
} | |
} | |
} | |
else if (c == ' ') | |
{ | |
// delimiter | |
if (text_started) | |
{ | |
chunk.push_back(' '); | |
} | |
else | |
{ | |
if (!chunk.empty()) arr.push_back(chunk); | |
chunk.clear(); | |
ignore = true; | |
} | |
} | |
else if (c == ';') | |
{ | |
if (!chunk.empty()) arr.push_back(chunk); | |
chunk.clear(); | |
ignore = true; | |
} | |
if (!ignore) | |
{ | |
chunk.push_back(c); | |
last_char = c; | |
} | |
it++; | |
} | |
// cout << "idx: " << index << endl; | |
if (index == 0) return; | |
data[index] = arr; | |
coll_indexes.push_back(index); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment