diff -r 8117169c9049 -r f486d30e4e7b src/lemon/graph_reader.h --- a/src/lemon/graph_reader.h Wed Mar 09 14:13:01 2005 +0000 +++ b/src/lemon/graph_reader.h Wed Mar 09 14:15:22 2005 +0000 @@ -35,43 +35,6 @@ // Exceptions - class IOException { - public: - virtual ~IOException() {} - virtual string what() const = 0; - }; - - class DataFormatException : public IOException { - std::string message; - public: - DataFormatException(const std::string& _message) - : message(_message) {} - std::string what() const { - return "DataFormatException: " + message; - } - }; - - template - class StreamException : public _Exception { - public: - typedef _Exception Exception; - StreamException(int _line, Exception _exception) - : Exception(_exception), line_num(_line) {} - virtual int line() const { - return line_num; - } - - virtual ~StreamException() {} - - virtual std::string what() const { - ostringstream os; - os << Exception::what() << " in line " << line(); - return os.str(); - } - private: - int line_num; - }; - /// \brief Standard ReaderTraits for the GraphReader class. /// @@ -91,7 +54,7 @@ /// Reads a value from the given stream. void read(std::istream& is, Value& value) { if (!(is >> value)) - throw DataFormatException("Default Reader format exception"); + throw DataFormatError("Default reader format exception"); } }; @@ -122,7 +85,7 @@ value.clear(); is >> ws; if (!is.get(c) || c != '\"') - throw DataFormatException("Quoted string format"); + throw DataFormatError("Quoted string format error"); while (is.get(c) && c != '\"') { if (escaped && c == '\\') { value += readEscape(is); @@ -130,7 +93,7 @@ value += c; } } - if (!is) throw DataFormatException("Quoted string format"); + if (!is) throw DataFormatError("Quoted string format error"); } private: @@ -164,7 +127,7 @@ { int code; if (!is.get(c) || !isHex(c)) - throw DataFormatException("Escape format exception"); + throw DataFormatError("Escape format error"); else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); else code = code * 16 + valueHex(c); return code; @@ -173,7 +136,7 @@ { int code; if (!isOct(c)) - throw DataFormatException("Escape format exception"); + throw DataFormatError("Escape format error"); else if (code = valueOct(c), !is.get(c) || !isOct(c)) is.putback(c); else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) @@ -225,9 +188,8 @@ /// \brief Construct a new GraphReader. /// - /// Construct a new GraphReader. It reads from the given map, - /// it constructs the given map and it use the given reader as the - /// default skipper. + /// Construct a new GraphReader. It reads into the given graph + /// and it use the given reader as the default skipper. GraphReader(std::istream& _is, Graph& _graph, const DefaultReader& _reader = DefaultReader()) : is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {} @@ -265,7 +227,9 @@ GraphReader& addNodeMap(std::string name, Map& map, const Reader& reader = Reader()) { if (node_map_readers.find(name) != node_map_readers.end()) { - throw Exception() << "Multiple read rule for node map: " << name; + ErrorMessage msg; + msg << "Multiple read rule for node map: " << name; + throw IOLogicError(msg.message()); } node_map_readers.insert( make_pair(name, new MapReader(map, reader))); @@ -279,7 +243,9 @@ GraphReader& skipNodeMap(std::string name, const Reader& reader = Reader()) { if (node_map_readers.find(name) != node_map_readers.end()) { - throw Exception() << "Multiple read rule for node map: " << name; + ErrorMessage msg; + msg << "Multiple read rule for node map: " << name; + throw IOLogicError(msg.message()); } node_map_readers.insert( make_pair(name, new SkipReader(reader))); @@ -303,7 +269,9 @@ GraphReader& addEdgeMap(std::string name, Map& map, const Reader& reader = Reader()) { if (edge_map_readers.find(name) != edge_map_readers.end()) { - throw Exception() << "Multiple read rule for edge map: " << name; + ErrorMessage msg; + msg << "Multiple read rule for edge map: " << name; + throw IOLogicError(msg.message()); } edge_map_readers.insert( make_pair(name, new MapReader(map, reader))); @@ -317,7 +285,9 @@ GraphReader& skipEdgeMap(std::string name, const Reader& reader = Reader()) { if (edge_map_readers.find(name) != edge_map_readers.end()) { - throw Exception() << "Multiple read rule for edge map: " << name; + ErrorMessage msg; + msg << "Multiple read rule for edge map: " << name; + throw IOLogicError(msg.message()); } edge_map_readers.insert( make_pair(name, new SkipReader(reader))); @@ -329,7 +299,9 @@ /// Add a new labeled node reader for the reader. GraphReader& addNode(std::string name, Node& node) { if (node_readers.find(name) != node_readers.end()) { - throw Exception() << "Multiple read rule for node"; + ErrorMessage msg; + msg << "Multiple read rule for node: " << name; + throw IOLogicError(msg.message()); } node_readers.insert(make_pair(name, &node)); return *this; @@ -340,7 +312,9 @@ /// Add a new labeled edge reader for the reader. GraphReader& addEdge(std::string name, Edge& edge) { if (edge_readers.find(name) != edge_readers.end()) { - throw Exception() << "Multiple read rule for edge"; + ErrorMessage msg; + msg << "Multiple read rule for edge: " << name; + throw IOLogicError(msg.message()); } edge_readers.insert(make_pair(name, &edge)); return *this; @@ -368,10 +342,11 @@ line = readEdges(line_num, edgeInverter); } if (line.find("@end") != 0) { - throw DataFormatException("Invalid control sequence: " + line); + throw DataFormatError("Invalid control sequence error"); } - } catch (DataFormatException e) { - throw StreamException(line_num, e); + } catch (DataFormatError e) { + e.line(line_num); + throw e; } } @@ -399,7 +374,7 @@ } if (index.size() == 0) { - throw DataFormatException("No node map found"); + throw DataFormatError("Cannot find node id map"); } nodeInverter = auto_ptr >(index[0]->getInverter()); @@ -436,7 +411,7 @@ } if (index.size() == 0) { - throw DataFormatException("No edge map found"); + throw DataFormatError("Cannot find edge id map"); } edgeInverter = auto_ptr >(index[0]->getInverter()); @@ -492,7 +467,7 @@ return line.substr(vi); } } - throw DataFormatException("End of stream"); + throw DataFormatError("End of stream error"); } // Inverters store and give back the Item from the id, @@ -532,7 +507,7 @@ if (it == inverse.end()) { inverse.insert(make_pair(value, item)); } else { - throw DataFormatException("Multiple ID occurence"); + throw DataFormatError("Multiple ID occurence"); } } @@ -543,7 +518,7 @@ if (it != inverse.end()) { return it->second; } else { - throw DataFormatException("Invalid ID"); + throw DataFormatError("Invalid ID error"); } } }; @@ -570,7 +545,7 @@ if (it == inverse.end()) { inverse.insert(make_pair(value, item)); } else { - throw DataFormatException("Multiple ID occurence"); + throw DataFormatError("Multiple ID occurence error"); } } @@ -581,7 +556,7 @@ if (it != inverse.end()) { return it->second; } else { - throw DataFormatException("Invalid ID"); + throw DataFormatError("Invalid ID error"); } } private: @@ -672,4 +647,49 @@ }; + /// Ready to use reader function. + template + void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, + typename Graph::Node &s, typename Graph::Node &t, + CostMap& cost) { + GraphReader reader(is, g); + reader.addEdgeMap("capacity", capacity); + reader.addEdgeMap("cost", cost); + reader.addNode("source", s); + reader.addNode("target", t); + reader.run(); + } + + template + void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, + typename Graph::Node &s, typename Graph::Node &t) { + GraphReader reader(is, g); + reader.addEdgeMap("capacity", capacity); + reader.addNode("source", s); + reader.addNode("target", t); + reader.run(); + } + + template + void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, + typename Graph::Node &s) { + GraphReader reader(is, g); + reader.addEdgeMap("capacity", capacity); + reader.addNode("source", s); + reader.run(); + } + + template + void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) { + GraphReader reader(is, g); + reader.addEdgeMap("capacity", capacity); + reader.run(); + } + + template + void readGraph(std::istream& is, Graph &g) { + GraphReader reader(is, g); + reader.run(); + } + }