c++ - How to store integer data with repeating format? -
i want utilize opengl draw lines, circles (and potentially polygons eventually, though parser extensible end). input file looks so:
l 0,0,175,55 l 22, 8, 75, 100 c 150, 200, 0 l 99, -50, 217, 77 z etc.
where z denotes end of file , c/l denote circle , line info respectively. before stack overflow help , bit of effort, have been able parse integer info input .txt file (all necessary libraries included elsewhere in .cpp file itself):
void parsefile() { ifstream lines; lines.open("c:/users/me/desktop/lines.txt"); //adjust proper absolute path of test file string currentline; while(getline(lines, currentline)) { if(currentline.empty()) //check there no garbage info in parser continue; istringstream iss(currentline); //convert line of info input stream char type; // l, c or z if(!(iss >> type)) continue; switch(type) { case 'l': //if l, store line (x0, y0, x1, y1) { char comma; int x0, y0, x1, y1; if(!(iss >> x0 >> comma >> y0 >> comma >> x1 >> comma >> y1)) { cerr << "error: failed read l: " << currentline << endl; continue; } //store cout << "(" << x0 << ", " << y0 << ", " << x1 << ", " << y1 << ")" << '\n'; break; } case 'c': //if c, store circle info (center x, center y, int radius) { char comma; int xc, yc, rad; if(!(iss >> xc >> comma >> yc >> comma >> rad)) { cerr << "error: failed read c: " << currentline << endl; continue; } break; } case 'z': //if z, end parsing { break; } default: cerr << "error: bad input " << currentline << endl; } } }
my difficulties are, though know format of every line of input, , correct, don't know how many lines there be, or whether construction l c l c, l l l l, or have you. line coordinates parsed in sets of 4 above, while circles parsed in sets of three.
thus, want store points, later, can utilize opengl functions manipulate/draw them. not sure how this. thought of using arbitrarily big array , reading in sets of 4/3 seems poor solution, since don't know whether or not 2 circles or lines occur in row, or whether 1 follow other, leaving me mismatched data. vector may more suitable, have dynamically adjustable size again, not sure how pull numbers vector in order ensure draw right primitives.
any assistance/pointing in right direction appreciated!
does work you?
the code bit messy, should able clean , hope thought of it. not sure if it's implementation seems alright use.
see results , testing at: http://ideone.com/cr1gl9 set in 1 class , made deallocate on deconstructor: http://ideone.com/2hyhqg
below code outputs:
line: 11 12 13 14 line: 21 22 23 24 circle: 31 32 33 line: 41 42 43 44 circle: 51 52 53 polygon: [0]{11, 12} [1]{21, 22} [2]{31, 32} [3]{41, 42} [4]{51, 52}
code:
//for testing #include <iostream> //required utilize vector #include <vector> //base class things force on class drawingobject { }; //holds info circle class drawingcircle : drawingobject { public: drawingcircle (int _xc, int _yc, int _rad) { xc = _xc; yc = _yc; rad = _rad; } int xc, yc, rad; }; //holds info line class drawingline : drawingobject { public: drawingline (int _x0, int _y0, int _x1, int _y1) { x0 = _x0; y0 = _y0; x1 = _x1; y1 = _y1; } int x0, y0, x1, y1; }; //holds info polygon class drawingpolygon : drawingobject { public: class point { public: point () {} point (int _x, int _y) { x = _x; y = _y; } int x, y; }; drawingpolygon (std::vector<drawingpolygon::point>& points) { point = points; } std::vector<drawingpolygon::point> point; }; //holds type of item draw class drawingdata { public: //so can tell if it's circle or line enum type { line, circle, polygon }; drawingdata (type _type, drawingobject* _data) { type = _type; info = _data; } type type; drawingobject* data; }; //pushing on object void pushobject (std::vector<drawingdata*>& vec, drawingdata::type type, drawingobject* obj) { vec.push_back(new drawingdata(type, obj)); } //pushing on line void pushline (std::vector<drawingdata*>& vec, int x0, int y0, int x1, int y1) { pushobject(vec, drawingdata::type::line, reinterpret_cast<drawingobject*> (new drawingline(x0, y0, x1, y1)) ); } //pushing on circle void pushcircle (std::vector<drawingdata*>& vec, int xc, int yc, int rad) { pushobject(vec, drawingdata::type::circle, reinterpret_cast<drawingobject*> (new drawingcircle(xc, yc, rad)) ); } //pusing on polygon void pushpolygon (std::vector<drawingdata*>& vec, std::vector<drawingpolygon::point>& points) { pushobject(vec, drawingdata::type::polygon, reinterpret_cast<drawingobject*> (new drawingpolygon(points)) ); } int main() { //vector holds drawingdata pointers std::vector<drawingdata*> myvector; //pushing on stuff pushline (myvector, 11, 12, 13, 14); pushline (myvector, 21, 22, 23, 24); pushcircle (myvector, 31, 32, 33); pushline (myvector, 41, 42, 43, 44); pushcircle (myvector, 51, 52, 53); std::vector<drawingpolygon::point> points; points.push_back(drawingpolygon::point(11, 12)); points.push_back(drawingpolygon::point(21, 22)); points.push_back(drawingpolygon::point(31, 32)); points.push_back(drawingpolygon::point(41, 42)); points.push_back(drawingpolygon::point(51, 52)); pushpolygon(myvector, points); //save space myvector.resize(myvector.size()); //loop through each element (unsigned int = 0;i < myvector.size();++i) { switch (myvector[i]->type) { case drawingdata::type::line: { //make easier access line info drawingline* currline = reinterpret_cast<drawingline*>(myvector[i]->data); //accessing elements std::cout << "line: " << currline->x0 << " " << currline->y0 << " " << currline->x1 << " " << currline->y1 << std::endl; break; } case drawingdata::type::circle: { //make easier access circle info drawingcircle* currcircle = reinterpret_cast<drawingcircle*>(myvector[i]->data); //accessing elements std::cout << "circle: " << currcircle->xc << " " << currcircle->yc << " " << currcircle->rad << std::endl; break; } case drawingdata::type::polygon: { //make easier access polygon info drawingpolygon* currpolygon = reinterpret_cast<drawingpolygon*>(myvector[i]->data); //accessing elements std::cout << "polygon: "; (unsigned int = 0;i < currpolygon->point.size();++i) { std::cout << "[" << << "]" << "{" << currpolygon->point[i].x << ", " << currpolygon->point[i].y << "} "; } std::cout << std::endl; break; } } } homecoming 0; }
also don't forget delete
classes new
'ed! otherwise nasty memory leaks. write deconstructor drawingdata
help this. inquire me if don't understand, in fact, if don't understand/want explanation on anything, inquire me. :)
c++ parsing opengl
No comments:
Post a Comment