[Cmap-cvs] mp2mp mp_parser.h,1.10,1.11 mp_parser.cpp,1.11,1.12
Status: Beta
Brought to you by:
dyp
From: Denis P. <dy...@us...> - 2005-10-21 08:09:39
|
Update of /cvsroot/cmap/mp2mp In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29854 Modified Files: mp_parser.h mp_parser.cpp Log Message: constify some methods. Handle [rgnxx] headers. Allow to set int attrs. Index: mp_parser.cpp =================================================================== RCS file: /cvsroot/cmap/mp2mp/mp_parser.cpp,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- mp_parser.cpp 24 Sep 2005 16:01:11 -0000 1.11 +++ mp_parser.cpp 21 Oct 2005 08:09:30 -0000 1.12 @@ -54,14 +54,14 @@ } */ -void Attr::print(FILE *f) { +void Attr::print(FILE *f) const { fprintf(f, "; %s=%s\n", name.c_str(), value.c_str()); } Rgn::Rgn() : kind(kind::Point), type(0), endLevel(0), direction(Unknown), city(Unknown) { } -void Rgn::print(FILE *f, const ObjectProp &op, bool is6Digit) { +void Rgn::print(FILE *f, const ObjectProp &op, bool is6Digit) const { fprintf(f, "; LOC=%s:%u\n", filename.c_str(), line); for (::Attrs::const_iterator it = op.attrs.begin(); it != op.attrs.end(); it++) (*it).print(f); @@ -108,13 +108,13 @@ if (kind != op.kind && op.kind == kind::Point) { unsigned int layerMax = op.layerMax > 0 ? op.layerMax : op.layerMin; for (unsigned int i = op.layerMin; i <= layerMax; i++) { - for (Elements::iterator it2 = elements.begin(); it2 != elements.end(); it2++) { + for (Elements::const_iterator it2 = elements.begin(); it2 != elements.end(); it2++) { point_t pt; pt.x = 0.0; pt.y = 0.0; double count = 0.0; - for (points_t::iterator it3 = (*it2).points.begin(); it3 != (*it2).points.end(); it3++) { + for (points_t::const_iterator it3 = (*it2).points.begin(); it3 != (*it2).points.end(); it3++) { pt.x += (*it3).x; pt.y += (*it3).y; count += 1.0; @@ -131,15 +131,15 @@ } else { unsigned int layerMax = op.layerMax > 0 ? op.layerMax : op.layerMin; for (unsigned int i = op.layerMin; i <= layerMax; i++) - for (Elements::iterator it2 = elements.begin(); it2 != elements.end(); it2++) + for (Elements::const_iterator it2 = elements.begin(); it2 != elements.end(); it2++) (*it2).print(f, op, is6Digit, i); } fprintf(f, "[END]\n"); fprintf(f, "\n"); } -void Rgn::print(FILE *f, bool is6Digit) { - for (Attrs::iterator it = attrs.begin(); it != attrs.end(); it++) +void Rgn::print(FILE *f, bool is6Digit) const { + for (Attrs::const_iterator it = attrs.begin(); it != attrs.end(); it++) (*it).print(f); switch (kind) { @@ -161,7 +161,7 @@ if (label.length() > 0) fprintf(f, "Label=%s\n", label.c_str()); - for (Elements::iterator it2 = elements.begin(); it2 != elements.end(); it2++) + for (Elements::const_iterator it2 = elements.begin(); it2 != elements.end(); it2++) (*it2).print(f, is6Digit); fprintf(f, "[END]\n"); fprintf(f, "\n"); @@ -241,6 +241,50 @@ } } +const char *Rgn::getAttr(const std::string &name) const { + for (Attrs::const_iterator it = attrs.begin(); it != attrs.end(); it++) + if (it->name == name) + return it->value.c_str(); + return NULL; +} + +int Rgn::getIntAttr(const std::string &name) const { + for (Attrs::const_iterator it = attrs.begin(); it != attrs.end(); it++) + if (it->name == name) + return ::strtol(it->value.c_str(), NULL, 10); + return 0; +} + +void Rgn::setAttr(const std::string name, const std::string value) { + for (Attrs::iterator iAttr = attrs.begin (); iAttr != attrs.end (); ++iAttr) { + Attr &attr = *iAttr; + if (attr.name == name) { + attr.value = value; + return; + } + } + Attr attr; + attr.name = name; + attr.value = value; + attrs.push_back(attr); +} + +void Rgn::setIntAttr(const std::string name, const int value) { + char s[20]; + ::sprintf(s, "%d", value); + for (Attrs::iterator iAttr = attrs.begin (); iAttr != attrs.end (); ++iAttr) { + Attr &attr = *iAttr; + if (attr.name == name) { + attr.value = s; + return; + } + } + Attr attr; + attr.name = name; + attr.value = s; + attrs.push_back(attr); +} + Element::Element(int _level) : level(_level) { } @@ -361,9 +405,9 @@ return false; } -void Element::print(FILE *f, bool is6Digit) { +void Element::print(FILE *f, bool is6Digit) const { fprintf(f, "Data%d=", level); - for (points_t::iterator it = points.begin(); it != points.end(); it++) { + for (points_t::const_iterator it = points.begin(); it != points.end(); it++) { if (it != points.begin()) fprintf(f, ","); (*it).print(f, is6Digit); @@ -371,9 +415,9 @@ fprintf(f, "\n"); } -void Element::print(FILE *f, const ObjectProp &op, bool is6Digit, unsigned int _level) { +void Element::print(FILE *f, const ObjectProp &op, bool is6Digit, unsigned int _level) const { fprintf(f, "Data%d=", _level); - for (points_t::iterator it = points.begin(); it != points.end(); it++) { + for (points_t::const_iterator it = points.begin(); it != points.end(); it++) { if (it != points.begin()) fprintf(f, ","); (*it).print(f, is6Digit); @@ -455,7 +499,7 @@ return fMaxX - fMinX; } -void point_t::print(FILE *f, bool is6Digits) { +void point_t::print(FILE *f, bool is6Digits) const { if (is6Digits) fprintf(f, "(%.6f,%.6f)", y, x); else @@ -677,14 +721,14 @@ if (ttype != '[') throw ParserException("'[' expected", t.lineno()); - if (t.nextToken() != TT_WORD || (t.sval != "polyline" && t.sval != "polygon" && t.sval != "poi")) + if (t.nextToken() != TT_WORD || (t.sval != "polyline" && t.sval != "polygon" && t.sval != "poi" && t.sval != "rgn10" && t.sval != "rgn20" && t.sval != "rgn40" && t.sval != "rgn80")) throw ParserException("'polyline', 'polygon' or 'poi' expected", t.lineno()); Kind kind; - if (t.sval == "polyline") + if (t.sval == "polyline" || t.sval == "rgn40") kind = kind::Line; - else if (t.sval == "polygon") + else if (t.sval == "polygon" || t.sval == "rgn80") kind = kind::Polygon; - else if (t.sval == "poi") + else if (t.sval == "poi" || t.sval == "rgn10" || t.sval == "rgn20") kind = kind::Point; if (t.nextToken() != ']') Index: mp_parser.h =================================================================== RCS file: /cvsroot/cmap/mp2mp/mp_parser.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- mp_parser.h 24 Sep 2005 16:01:11 -0000 1.10 +++ mp_parser.h 21 Oct 2005 08:09:30 -0000 1.11 @@ -14,7 +14,7 @@ double x; double y; - void print(FILE *f, bool is6Digits); + void print(FILE *f, bool is6Digits) const; }; typedef std::vector<point_t> points_t; @@ -30,8 +30,8 @@ int level; points_t points; - void print(FILE *f, bool is6Digit); - void print(FILE *f, const ObjectProp &op, bool is6Digit, unsigned int level); + void print(FILE *f, bool is6Digit) const; + void print(FILE *f, const ObjectProp &op, bool is6Digit, unsigned int level) const; double getArea() const; double getLength() const; double getHeight() const; @@ -46,7 +46,7 @@ std::string name; std::string value; - void print(FILE *f); + void print(FILE *f) const; }; typedef std::vector<Attr> Attrs; @@ -87,14 +87,21 @@ std::string filename; unsigned int line; - void print(FILE *f, bool is6Digit); - void print(FILE *f, const ObjectProp &op, bool is6Digit); + void print(FILE *f, bool is6Digit) const; + void print(FILE *f, const ObjectProp &op, bool is6Digit) const; double getArea() const; double getLength() const; double getHeight() const; double getWidth() const; bool isHoleOfAny(const Element &_element); void refreshInners(); + const char *getAttr(const std::string &name) const; + int getIntAttr(const std::string &name) const; + void setAttr(const std::string name, const std::string value); + void setIntAttr(const std::string name, const int value); + + point_t topright; // max + point_t bottomleft; // min }; typedef std::list<Rgn> Rgns; |