[Cmap-cvs] mp2mp mp2mp.cpp,1.2,1.3 mp_parser.cpp,1.2,1.3 mp_parser.h,1.2,1.3 parserImpl.cpp,1.2,1.3
Status: Beta
Brought to you by:
dyp
From: Denis P. <dy...@us...> - 2005-02-23 11:19:30
|
Update of /cvsroot/cmap/mp2mp In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21650 Modified Files: mp2mp.cpp mp_parser.cpp mp_parser.h parserImpl.cpp Log Message: Comments are handled on tokenizer level. All exceptions contains line number. Index: parserImpl.cpp =================================================================== RCS file: /cvsroot/cmap/mp2mp/parserImpl.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- parserImpl.cpp 14 Nov 2004 15:00:57 -0000 1.2 +++ parserImpl.cpp 23 Feb 2005 11:19:22 -0000 1.3 @@ -32,7 +32,7 @@ return var; if (varName == "attr") { if (t.nextToken() != TT_STRING) - throw ParserException("Attribute name expected"); + throw ParserException("Attribute name expected", t.lineno()); return new AttrVar(t.sval); } return NULL; @@ -45,14 +45,14 @@ case TT_WORD: break; default: - throw ParserException("Expected 'file'"); + throw ParserException("Expected 'file'", t.lineno()); } if (t.sval != "file") - throw ParserException("Expected 'file'"); + throw ParserException("Expected 'file'", t.lineno()); if (t.nextToken() != TT_STRING) - throw ParserException("Expected file name"); + throw ParserException("Expected file name", t.lineno()); ObjectConfImpl *obj = new ObjectConfImpl(t.sval.c_str()); Index: mp2mp.cpp =================================================================== RCS file: /cvsroot/cmap/mp2mp/mp2mp.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- mp2mp.cpp 14 Nov 2004 15:00:57 -0000 1.2 +++ mp2mp.cpp 23 Feb 2005 11:19:21 -0000 1.3 @@ -9,6 +9,10 @@ #include "object.h" #include "mp_parser.h" +std::string headerFileName; +std::string mapID; +std::string mapName; + class Converter { public: Converter() { @@ -20,14 +24,19 @@ void Converter::process(FILE *f) { if (f != NULL) { - FILE *inHead = fopen("header.txt", "r"); + const char *fileName = headerFileName.empty() ? "header.txt" : headerFileName.c_str(); + const char *realMapID = mapID.empty() ? "11111111" : mapID.c_str(); + const char *realMapName = mapName.empty() ? "M" : mapName.c_str(); + FILE *inHead = fopen(fileName, "r"); int buf_header; if (!inHead) { - fprintf(stderr, "header.txt: File not found.\n"); + fprintf(stderr, "%s: File not found.\n", fileName); } else { while ((buf_header = fgetc(inHead)) != EOF) if (buf_header == '%') - fprintf(f, "%s", "11111111"); + fprintf(f, "%s", realMapID); + else if (buf_header == '$') + fprintf(f, "%s", realMapName); else fprintf (f, "%c", (unsigned char)buf_header); fprintf (f, "\n\n"); @@ -58,6 +67,7 @@ while (p.parseObject()) ; } catch (ParserException &) { + fprintf(stderr, "Error in file: '%s'\n", filename.c_str()); t.printToken(); fclose(f); throw; @@ -108,13 +118,47 @@ fclose(f); } +static void printUsage(const char *name) { + fprintf(stderr, "Usage: %s [-h header_file] <config> [<output>]\n", name); +} + +static std::string convertMapID(const char *s) { + std::string id = "111"; + id += (s[0] - 'A') / 10 + '0'; + id += (s[0] - 'A') % 10 + '0'; + id += s[2]; + id += s[3]; + id += s[5]; + + return id; +} + int main(int argc, char* argv[]) { bool doSplit = false; int firstArg = 1; + if (argc >= 2 && strcmp(argv[firstArg], "-h") == 0) { + if (argc < 3) { + printUsage(argv[0]); + return 1; + } + headerFileName = argv[firstArg + 1]; + firstArg += 2; + } + + if (argc >= firstArg + 1 && strcmp(argv[firstArg], "-m") == 0) { + if (argc < firstArg + 2) { + printUsage(argv[0]); + return 1; + } + mapName = argv[firstArg + 1]; + mapID = convertMapID(argv[firstArg + 1]); + firstArg += 2; + } + if (argc < firstArg + 1) { - fprintf(stderr, "Usage: %s <config> [<output>]\n", argv[0]); + printUsage(argv[0]); return 1; } const char *configFile = argv[firstArg]; Index: mp_parser.cpp =================================================================== RCS file: /cvsroot/cmap/mp2mp/mp_parser.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- mp_parser.cpp 14 Nov 2004 15:00:57 -0000 1.2 +++ mp_parser.cpp 23 Feb 2005 11:19:22 -0000 1.3 @@ -85,7 +85,7 @@ } void point_t::print(FILE *f) { - fprintf(f, "(%.5f, %.5f)", y, x); + fprintf(f, "(%.5f,%.5f)", y, x); } MPParser::MPParser(Tokenizer &_t) : t(_t) { @@ -100,9 +100,11 @@ t.whitespaceChar('\n'); t.whitespaceChar('\r'); t.whitespaceChar('\t'); + t.produceComments(false); + t.commentChar(';'); } -static bool parseBool(const std::string &sval) { +bool MPParser::parseBool(const std::string &sval) { if (sval == "T") return true; if (sval == "F") @@ -112,56 +114,56 @@ if (sval == "N") return false; else - throw ParserException("Unknown boolean value"); + throw ParserException("Unknown boolean value", t.lineno()); } -static int parseInt(Tokenizer &t) { +int MPParser::parseInt() { if (t.nextToken() != TT_NUMBER) throw ParserException("Number expected", t.lineno()); return t.nval; } -static int parseInt(const std::string &sval) { +int MPParser::parseInt(const std::string &sval) { char *err = NULL; long res = strtol(sval.c_str(), &err, 10); if (err != NULL && *err != 0) - throw ParserException("Invalid number"); + throw ParserException("Invalid number", t.lineno()); return res; } -static float parseFloat(const std::string &sval) { +float MPParser::parseFloat(const std::string &sval) { char *err = NULL; float res = strtod(sval.c_str(), &err); if (err != NULL && *err != 0) - throw ParserException("Invalid float number"); + throw ParserException("Invalid float number", t.lineno()); return res; } void MPParser::parseHeader(Map &map) { if (t.nextToken() != '[') - throw ParserException("'[' expected"); + throw ParserException("'[' expected", t.lineno()); if (t.nextToken() != TT_WORD || t.sval != "img") - throw ParserException("'img' expected"); + throw ParserException("'img' expected", t.lineno()); if (t.nextToken() != TT_WORD || t.sval != "id") - throw ParserException("'id' expected"); + throw ParserException("'id' expected", t.lineno()); if (t.nextToken() != ']') - throw ParserException("']' expected"); + throw ParserException("']' expected", t.lineno()); for (;;) { int ttype = t.nextToken(); if (ttype == '[') { if (t.nextToken() != TT_WORD || t.sval != "end-img") - throw ParserException("'end-img' expected"); + throw ParserException("'end-img' expected", t.lineno()); if (t.nextToken() != TT_WORD || t.sval != "id") - throw ParserException("'id' expected"); + throw ParserException("'id' expected", t.lineno()); if (t.nextToken() != ']') - throw ParserException("']' expected"); + throw ParserException("']' expected", t.lineno()); break; } std::string varName = t.sval; if (ttype != TT_WORD) - throw ParserException("word expected"); + throw ParserException("word expected", t.lineno()); if (t.nextToken() != '=') throw ParserException("'=' expected", t.lineno()); t.nextToEOL(true); @@ -178,7 +180,7 @@ else if (t.sval == "F") map.elevation = Map::Feets; else - throw ParserException("Unknown elevation type"); + throw ParserException("Unknown elevation type", t.lineno()); } else if (varName == "preprocess") map.preprocess = parseBool(t.sval); else if (varName == "codepage") @@ -196,7 +198,7 @@ else if (varName.substr(0, 5) == "level") { } else if (varName.substr(0, 4) == "zoom") { } else - throw ParserException(("Unknown variable: " + varName).c_str()); + throw ParserException(("Unknown variable: " + varName).c_str(), t.lineno()); } } @@ -242,21 +244,23 @@ // Process comments for (;;) { ttype = t.nextToken(); - if (ttype != ';') + if (ttype != TT_COMMENT) break; - t.nextToEOL(true); size_t p; if ((p = t.sval.find('=')) == std::string::npos) continue; if (p == 0) continue; + size_t ns = 0; + while (ns < t.sval.length() && t.sval[ns] == ' ') + ns++; size_t np = p - 1; while (np > 0 && t.sval[np] == ' ') np--; if (np == 0) continue; Attr a; - a.name = t.sval.substr(0, np + 1); + a.name = t.sval.substr(ns, np + 1 - ns); size_t ep = p + 1; while (ep < t.sval.length() && t.sval[ep] == ' ') ep++; @@ -265,14 +269,14 @@ size_t ee = t.sval.length() - 1; while (ee > 0 && t.sval[ee] == ' ') ee--; - a.value = t.sval.substr(ep, ee); + a.value = t.sval.substr(ep, ee - ep + 1); rgn.attrs.push_back(a); } // Process region if (ttype != '[') - throw ParserException("'[' expected"); + throw ParserException("'[' expected", t.lineno()); if (t.nextToken() != TT_WORD || (t.sval != "polyline" && t.sval != "polygon" && t.sval != "poi")) - throw ParserException("'polyline', 'polygon' or 'poi' expected"); + throw ParserException("'polyline', 'polygon' or 'poi' expected", t.lineno()); enum Rgn::RgnType rgnType; if (t.sval == "polyline") rgnType = Rgn::Polyline; @@ -282,37 +286,37 @@ rgnType = Rgn::POI; if (t.nextToken() != ']') - throw ParserException("']' expected"); + throw ParserException("']' expected", t.lineno()); rgn.rgnType = rgnType; for (;;) { ttype = t.nextToken(); if (ttype == '[') { if (t.nextToken() != TT_WORD || t.sval != "end") - throw ParserException("'end' expected"); + throw ParserException("'end' expected", t.lineno()); if (t.nextToken() != ']') - throw ParserException("']' expected"); + throw ParserException("']' expected", t.lineno()); break; } std::string varName = t.sval; if (ttype != TT_WORD) - throw ParserException("word expected"); + throw ParserException("word expected", t.lineno()); if (t.nextToken() != '=') throw ParserException("'=' expected", t.lineno()); if (varName == "label") { t.nextToEOL(true); rgn.label = t.sval; } else if (varName == "endlevel") - rgn.endLevel = parseInt(t); + rgn.endLevel = parseInt(); else if (varName == "type") { - rgn.type = parseInt(t); + rgn.type = parseInt(); } else if (varName.substr(0, 4) == "data") { Element e(parseInt(varName.substr(4))); parseElement(e); rgn.elements.push_back(e); } else - throw ParserException(("Unknown variable: " + varName).c_str()); + throw ParserException(("Unknown variable: " + varName).c_str(), t.lineno()); } map.rgns.push_back(rgn); @@ -324,6 +328,7 @@ bool MPParser::parseObject() { parseHeader(map); + t.produceComments(true); while (parseRgn(map)) ; // while (t.nextToken() != TT_EOF) Index: mp_parser.h =================================================================== RCS file: /cvsroot/cmap/mp2mp/mp_parser.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- mp_parser.h 14 Nov 2004 15:00:57 -0000 1.2 +++ mp_parser.h 23 Feb 2005 11:19:22 -0000 1.3 @@ -88,8 +88,13 @@ Map map; private: + bool parseBool(const std::string &sval); + int parseInt(); + int parseInt(const std::string &sval); + float parseFloat(const std::string &sval); + std::string filename; - Tokenizer t; + Tokenizer &t; }; #endif |