Easy input-output function for common graphs.
Modified Exception handling in graph_reader.
1.1 --- a/src/lemon/graph_reader.h Wed Mar 09 14:13:01 2005 +0000
1.2 +++ b/src/lemon/graph_reader.h Wed Mar 09 14:15:22 2005 +0000
1.3 @@ -35,43 +35,6 @@
1.4
1.5 // Exceptions
1.6
1.7 - class IOException {
1.8 - public:
1.9 - virtual ~IOException() {}
1.10 - virtual string what() const = 0;
1.11 - };
1.12 -
1.13 - class DataFormatException : public IOException {
1.14 - std::string message;
1.15 - public:
1.16 - DataFormatException(const std::string& _message)
1.17 - : message(_message) {}
1.18 - std::string what() const {
1.19 - return "DataFormatException: " + message;
1.20 - }
1.21 - };
1.22 -
1.23 - template <typename _Exception>
1.24 - class StreamException : public _Exception {
1.25 - public:
1.26 - typedef _Exception Exception;
1.27 - StreamException(int _line, Exception _exception)
1.28 - : Exception(_exception), line_num(_line) {}
1.29 - virtual int line() const {
1.30 - return line_num;
1.31 - }
1.32 -
1.33 - virtual ~StreamException() {}
1.34 -
1.35 - virtual std::string what() const {
1.36 - ostringstream os;
1.37 - os << Exception::what() << " in line " << line();
1.38 - return os.str();
1.39 - }
1.40 - private:
1.41 - int line_num;
1.42 - };
1.43 -
1.44
1.45 /// \brief Standard ReaderTraits for the GraphReader class.
1.46 ///
1.47 @@ -91,7 +54,7 @@
1.48 /// Reads a value from the given stream.
1.49 void read(std::istream& is, Value& value) {
1.50 if (!(is >> value))
1.51 - throw DataFormatException("Default Reader format exception");
1.52 + throw DataFormatError("Default reader format exception");
1.53 }
1.54 };
1.55
1.56 @@ -122,7 +85,7 @@
1.57 value.clear();
1.58 is >> ws;
1.59 if (!is.get(c) || c != '\"')
1.60 - throw DataFormatException("Quoted string format");
1.61 + throw DataFormatError("Quoted string format error");
1.62 while (is.get(c) && c != '\"') {
1.63 if (escaped && c == '\\') {
1.64 value += readEscape(is);
1.65 @@ -130,7 +93,7 @@
1.66 value += c;
1.67 }
1.68 }
1.69 - if (!is) throw DataFormatException("Quoted string format");
1.70 + if (!is) throw DataFormatError("Quoted string format error");
1.71 }
1.72
1.73 private:
1.74 @@ -164,7 +127,7 @@
1.75 {
1.76 int code;
1.77 if (!is.get(c) || !isHex(c))
1.78 - throw DataFormatException("Escape format exception");
1.79 + throw DataFormatError("Escape format error");
1.80 else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
1.81 else code = code * 16 + valueHex(c);
1.82 return code;
1.83 @@ -173,7 +136,7 @@
1.84 {
1.85 int code;
1.86 if (!isOct(c))
1.87 - throw DataFormatException("Escape format exception");
1.88 + throw DataFormatError("Escape format error");
1.89 else if (code = valueOct(c), !is.get(c) || !isOct(c))
1.90 is.putback(c);
1.91 else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
1.92 @@ -225,9 +188,8 @@
1.93
1.94 /// \brief Construct a new GraphReader.
1.95 ///
1.96 - /// Construct a new GraphReader. It reads from the given map,
1.97 - /// it constructs the given map and it use the given reader as the
1.98 - /// default skipper.
1.99 + /// Construct a new GraphReader. It reads into the given graph
1.100 + /// and it use the given reader as the default skipper.
1.101 GraphReader(std::istream& _is, Graph& _graph,
1.102 const DefaultReader& _reader = DefaultReader())
1.103 : is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {}
1.104 @@ -265,7 +227,9 @@
1.105 GraphReader& addNodeMap(std::string name, Map& map,
1.106 const Reader& reader = Reader()) {
1.107 if (node_map_readers.find(name) != node_map_readers.end()) {
1.108 - throw Exception() << "Multiple read rule for node map: " << name;
1.109 + ErrorMessage msg;
1.110 + msg << "Multiple read rule for node map: " << name;
1.111 + throw IOLogicError(msg.message());
1.112 }
1.113 node_map_readers.insert(
1.114 make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
1.115 @@ -279,7 +243,9 @@
1.116 GraphReader& skipNodeMap(std::string name,
1.117 const Reader& reader = Reader()) {
1.118 if (node_map_readers.find(name) != node_map_readers.end()) {
1.119 - throw Exception() << "Multiple read rule for node map: " << name;
1.120 + ErrorMessage msg;
1.121 + msg << "Multiple read rule for node map: " << name;
1.122 + throw IOLogicError(msg.message());
1.123 }
1.124 node_map_readers.insert(
1.125 make_pair(name, new SkipReader<Node, Reader>(reader)));
1.126 @@ -303,7 +269,9 @@
1.127 GraphReader& addEdgeMap(std::string name, Map& map,
1.128 const Reader& reader = Reader()) {
1.129 if (edge_map_readers.find(name) != edge_map_readers.end()) {
1.130 - throw Exception() << "Multiple read rule for edge map: " << name;
1.131 + ErrorMessage msg;
1.132 + msg << "Multiple read rule for edge map: " << name;
1.133 + throw IOLogicError(msg.message());
1.134 }
1.135 edge_map_readers.insert(
1.136 make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
1.137 @@ -317,7 +285,9 @@
1.138 GraphReader& skipEdgeMap(std::string name,
1.139 const Reader& reader = Reader()) {
1.140 if (edge_map_readers.find(name) != edge_map_readers.end()) {
1.141 - throw Exception() << "Multiple read rule for edge map: " << name;
1.142 + ErrorMessage msg;
1.143 + msg << "Multiple read rule for edge map: " << name;
1.144 + throw IOLogicError(msg.message());
1.145 }
1.146 edge_map_readers.insert(
1.147 make_pair(name, new SkipReader<Edge, Reader>(reader)));
1.148 @@ -329,7 +299,9 @@
1.149 /// Add a new labeled node reader for the reader.
1.150 GraphReader& addNode(std::string name, Node& node) {
1.151 if (node_readers.find(name) != node_readers.end()) {
1.152 - throw Exception() << "Multiple read rule for node";
1.153 + ErrorMessage msg;
1.154 + msg << "Multiple read rule for node: " << name;
1.155 + throw IOLogicError(msg.message());
1.156 }
1.157 node_readers.insert(make_pair(name, &node));
1.158 return *this;
1.159 @@ -340,7 +312,9 @@
1.160 /// Add a new labeled edge reader for the reader.
1.161 GraphReader& addEdge(std::string name, Edge& edge) {
1.162 if (edge_readers.find(name) != edge_readers.end()) {
1.163 - throw Exception() << "Multiple read rule for edge";
1.164 + ErrorMessage msg;
1.165 + msg << "Multiple read rule for edge: " << name;
1.166 + throw IOLogicError(msg.message());
1.167 }
1.168 edge_readers.insert(make_pair(name, &edge));
1.169 return *this;
1.170 @@ -368,10 +342,11 @@
1.171 line = readEdges(line_num, edgeInverter);
1.172 }
1.173 if (line.find("@end") != 0) {
1.174 - throw DataFormatException("Invalid control sequence: " + line);
1.175 + throw DataFormatError("Invalid control sequence error");
1.176 }
1.177 - } catch (DataFormatException e) {
1.178 - throw StreamException<DataFormatException>(line_num, e);
1.179 + } catch (DataFormatError e) {
1.180 + e.line(line_num);
1.181 + throw e;
1.182 }
1.183 }
1.184
1.185 @@ -399,7 +374,7 @@
1.186 }
1.187
1.188 if (index.size() == 0) {
1.189 - throw DataFormatException("No node map found");
1.190 + throw DataFormatError("Cannot find node id map");
1.191 }
1.192
1.193 nodeInverter = auto_ptr<InverterBase<Node> >(index[0]->getInverter());
1.194 @@ -436,7 +411,7 @@
1.195 }
1.196
1.197 if (index.size() == 0) {
1.198 - throw DataFormatException("No edge map found");
1.199 + throw DataFormatError("Cannot find edge id map");
1.200 }
1.201
1.202 edgeInverter = auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
1.203 @@ -492,7 +467,7 @@
1.204 return line.substr(vi);
1.205 }
1.206 }
1.207 - throw DataFormatException("End of stream");
1.208 + throw DataFormatError("End of stream error");
1.209 }
1.210
1.211 // Inverters store and give back the Item from the id,
1.212 @@ -532,7 +507,7 @@
1.213 if (it == inverse.end()) {
1.214 inverse.insert(make_pair(value, item));
1.215 } else {
1.216 - throw DataFormatException("Multiple ID occurence");
1.217 + throw DataFormatError("Multiple ID occurence");
1.218 }
1.219 }
1.220
1.221 @@ -543,7 +518,7 @@
1.222 if (it != inverse.end()) {
1.223 return it->second;
1.224 } else {
1.225 - throw DataFormatException("Invalid ID");
1.226 + throw DataFormatError("Invalid ID error");
1.227 }
1.228 }
1.229 };
1.230 @@ -570,7 +545,7 @@
1.231 if (it == inverse.end()) {
1.232 inverse.insert(make_pair(value, item));
1.233 } else {
1.234 - throw DataFormatException("Multiple ID occurence");
1.235 + throw DataFormatError("Multiple ID occurence error");
1.236 }
1.237 }
1.238
1.239 @@ -581,7 +556,7 @@
1.240 if (it != inverse.end()) {
1.241 return it->second;
1.242 } else {
1.243 - throw DataFormatException("Invalid ID");
1.244 + throw DataFormatError("Invalid ID error");
1.245 }
1.246 }
1.247 private:
1.248 @@ -672,4 +647,49 @@
1.249
1.250 };
1.251
1.252 + /// Ready to use reader function.
1.253 + template<typename Graph, typename CapacityMap, typename CostMap>
1.254 + void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
1.255 + typename Graph::Node &s, typename Graph::Node &t,
1.256 + CostMap& cost) {
1.257 + GraphReader<Graph> reader(is, g);
1.258 + reader.addEdgeMap("capacity", capacity);
1.259 + reader.addEdgeMap("cost", cost);
1.260 + reader.addNode("source", s);
1.261 + reader.addNode("target", t);
1.262 + reader.run();
1.263 + }
1.264 +
1.265 + template<typename Graph, typename CapacityMap>
1.266 + void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
1.267 + typename Graph::Node &s, typename Graph::Node &t) {
1.268 + GraphReader<Graph> reader(is, g);
1.269 + reader.addEdgeMap("capacity", capacity);
1.270 + reader.addNode("source", s);
1.271 + reader.addNode("target", t);
1.272 + reader.run();
1.273 + }
1.274 +
1.275 + template<typename Graph, typename CapacityMap>
1.276 + void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
1.277 + typename Graph::Node &s) {
1.278 + GraphReader<Graph> reader(is, g);
1.279 + reader.addEdgeMap("capacity", capacity);
1.280 + reader.addNode("source", s);
1.281 + reader.run();
1.282 + }
1.283 +
1.284 + template<typename Graph, typename CapacityMap>
1.285 + void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
1.286 + GraphReader<Graph> reader(is, g);
1.287 + reader.addEdgeMap("capacity", capacity);
1.288 + reader.run();
1.289 + }
1.290 +
1.291 + template<typename Graph>
1.292 + void readGraph(std::istream& is, Graph &g) {
1.293 + GraphReader<Graph> reader(is, g);
1.294 + reader.run();
1.295 + }
1.296 +
1.297 }
2.1 --- a/src/lemon/graph_writer.h Wed Mar 09 14:13:01 2005 +0000
2.2 +++ b/src/lemon/graph_writer.h Wed Mar 09 14:15:22 2005 +0000
2.3 @@ -27,6 +27,7 @@
2.4
2.5 #include <memory>
2.6
2.7 +#include <lemon/map_utils.h>
2.8 #include <lemon/invalid.h>
2.9 #include <lemon/error.h>
2.10
2.11 @@ -162,7 +163,8 @@
2.12 /// Construct a new GraphWriter. It writes from the given map,
2.13 /// it constructs the given map and it use the given writer as the
2.14 /// default skipper.
2.15 - GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
2.16 + GraphWriter(std::ostream& _os, const Graph& _graph)
2.17 + : os(_os), graph(_graph) {}
2.18
2.19
2.20 /// \brief Destruct the graph writer.
2.21 @@ -367,9 +369,78 @@
2.22 EdgeWriters edge_writers;
2.23
2.24 std::ostream& os;
2.25 - Graph& graph;
2.26 + const Graph& graph;
2.27
2.28 };
2.29
2.30 + /// Ready to use writer function.
2.31 + template<typename Graph, typename CapacityMap, typename CostMap>
2.32 + void writeGraph(std::ostream& os, const Graph &g,
2.33 + const CapacityMap& capacity, const typename Graph::Node &s,
2.34 + const typename Graph::Node &t, const CostMap& cost) {
2.35 + GraphWriter<Graph> reader(os, g);
2.36 + IdMap<Graph, typename Graph::Node> nodeIdMap(g);
2.37 + reader.addNodeMap("id", nodeIdMap);
2.38 + IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
2.39 + reader.addEdgeMap("id", edgeIdMap);
2.40 + reader.addEdgeMap("capacity", capacity);
2.41 + reader.addEdgeMap("cost", cost);
2.42 + reader.addNode("source", s);
2.43 + reader.addNode("target", t);
2.44 + reader.run();
2.45 + }
2.46 +
2.47 + /// Ready to use writer function.
2.48 + template<typename Graph, typename CapacityMap, typename CostMap>
2.49 + void writeGraph(std::ostream& os, const Graph &g,
2.50 + const CapacityMap& capacity, const typename Graph::Node &s,
2.51 + const typename Graph::Node &t) {
2.52 + GraphWriter<Graph> reader(os, g);
2.53 + IdMap<Graph, typename Graph::Node> nodeIdMap(g);
2.54 + reader.addNodeMap("id", nodeIdMap);
2.55 + IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
2.56 + reader.addEdgeMap("id", edgeIdMap);
2.57 + reader.addEdgeMap("capacity", capacity);
2.58 + reader.addNode("source", s);
2.59 + reader.addNode("target", t);
2.60 + reader.run();
2.61 + }
2.62 +
2.63 + /// Ready to use writer function.
2.64 + template<typename Graph, typename CapacityMap>
2.65 + void writeGraph(std::ostream& os, const Graph &g,
2.66 + const CapacityMap& capacity, const typename Graph::Node &s) {
2.67 + GraphWriter<Graph> reader(os, g);
2.68 + IdMap<Graph, typename Graph::Node> nodeIdMap(g);
2.69 + reader.addNodeMap("id", nodeIdMap);
2.70 + IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
2.71 + reader.addEdgeMap("id", edgeIdMap);
2.72 + reader.addEdgeMap("capacity", capacity);
2.73 + reader.addNode("source", s);
2.74 + reader.run();
2.75 + }
2.76 + /// Ready to use writer function.
2.77 + template<typename Graph, typename CapacityMap>
2.78 + void writeGraph(std::ostream& os, const Graph &g,
2.79 + const CapacityMap& capacity) {
2.80 + GraphWriter<Graph> reader(os, g);
2.81 + IdMap<Graph, typename Graph::Node> nodeIdMap(g);
2.82 + reader.addNodeMap("id", nodeIdMap);
2.83 + IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
2.84 + reader.addEdgeMap("id", edgeIdMap);
2.85 + reader.addEdgeMap("capacity", capacity);
2.86 + reader.run();
2.87 + }
2.88 + /// Ready to use writer function.
2.89 + template<typename Graph>
2.90 + void writeGraph(std::ostream& os, const Graph &g) {
2.91 + GraphWriter<Graph> reader(os, g);
2.92 + IdMap<Graph, typename Graph::Node> nodeIdMap(g);
2.93 + reader.addNodeMap("id", nodeIdMap);
2.94 + IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
2.95 + reader.addEdgeMap("id", edgeIdMap);
2.96 + reader.run();
2.97 + }
2.98 +
2.99
2.100 }