Changeset 1208:f486d30e4e7b in lemon-0.x
- Timestamp:
- 03/09/05 15:15:22 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1627
- Location:
- src/lemon
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/lemon/graph_reader.h
r1164 r1208 36 36 // Exceptions 37 37 38 class IOException {39 public:40 virtual ~IOException() {}41 virtual string what() const = 0;42 };43 44 class DataFormatException : public IOException {45 std::string message;46 public:47 DataFormatException(const std::string& _message)48 : message(_message) {}49 std::string what() const {50 return "DataFormatException: " + message;51 }52 };53 54 template <typename _Exception>55 class StreamException : public _Exception {56 public:57 typedef _Exception Exception;58 StreamException(int _line, Exception _exception)59 : Exception(_exception), line_num(_line) {}60 virtual int line() const {61 return line_num;62 }63 64 virtual ~StreamException() {}65 66 virtual std::string what() const {67 ostringstream os;68 os << Exception::what() << " in line " << line();69 return os.str();70 }71 private:72 int line_num;73 };74 75 38 76 39 /// \brief Standard ReaderTraits for the GraphReader class. … … 92 55 void read(std::istream& is, Value& value) { 93 56 if (!(is >> value)) 94 throw DataFormatE xception("Default Reader format exception");57 throw DataFormatError("Default reader format exception"); 95 58 } 96 59 }; … … 123 86 is >> ws; 124 87 if (!is.get(c) || c != '\"') 125 throw DataFormatE xception("Quoted string format");88 throw DataFormatError("Quoted string format error"); 126 89 while (is.get(c) && c != '\"') { 127 90 if (escaped && c == '\\') { … … 131 94 } 132 95 } 133 if (!is) throw DataFormatE xception("Quoted string format");96 if (!is) throw DataFormatError("Quoted string format error"); 134 97 } 135 98 … … 165 128 int code; 166 129 if (!is.get(c) || !isHex(c)) 167 throw DataFormatE xception("Escape format exception");130 throw DataFormatError("Escape format error"); 168 131 else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); 169 132 else code = code * 16 + valueHex(c); … … 174 137 int code; 175 138 if (!isOct(c)) 176 throw DataFormatE xception("Escape format exception");139 throw DataFormatError("Escape format error"); 177 140 else if (code = valueOct(c), !is.get(c) || !isOct(c)) 178 141 is.putback(c); … … 226 189 /// \brief Construct a new GraphReader. 227 190 /// 228 /// Construct a new GraphReader. It reads from the given map, 229 /// it constructs the given map and it use the given reader as the 230 /// default skipper. 191 /// Construct a new GraphReader. It reads into the given graph 192 /// and it use the given reader as the default skipper. 231 193 GraphReader(std::istream& _is, Graph& _graph, 232 194 const DefaultReader& _reader = DefaultReader()) … … 266 228 const Reader& reader = Reader()) { 267 229 if (node_map_readers.find(name) != node_map_readers.end()) { 268 throw Exception() << "Multiple read rule for node map: " << name; 230 ErrorMessage msg; 231 msg << "Multiple read rule for node map: " << name; 232 throw IOLogicError(msg.message()); 269 233 } 270 234 node_map_readers.insert( … … 280 244 const Reader& reader = Reader()) { 281 245 if (node_map_readers.find(name) != node_map_readers.end()) { 282 throw Exception() << "Multiple read rule for node map: " << name; 246 ErrorMessage msg; 247 msg << "Multiple read rule for node map: " << name; 248 throw IOLogicError(msg.message()); 283 249 } 284 250 node_map_readers.insert( … … 304 270 const Reader& reader = Reader()) { 305 271 if (edge_map_readers.find(name) != edge_map_readers.end()) { 306 throw Exception() << "Multiple read rule for edge map: " << name; 272 ErrorMessage msg; 273 msg << "Multiple read rule for edge map: " << name; 274 throw IOLogicError(msg.message()); 307 275 } 308 276 edge_map_readers.insert( … … 318 286 const Reader& reader = Reader()) { 319 287 if (edge_map_readers.find(name) != edge_map_readers.end()) { 320 throw Exception() << "Multiple read rule for edge map: " << name; 288 ErrorMessage msg; 289 msg << "Multiple read rule for edge map: " << name; 290 throw IOLogicError(msg.message()); 321 291 } 322 292 edge_map_readers.insert( … … 330 300 GraphReader& addNode(std::string name, Node& node) { 331 301 if (node_readers.find(name) != node_readers.end()) { 332 throw Exception() << "Multiple read rule for node"; 302 ErrorMessage msg; 303 msg << "Multiple read rule for node: " << name; 304 throw IOLogicError(msg.message()); 333 305 } 334 306 node_readers.insert(make_pair(name, &node)); … … 341 313 GraphReader& addEdge(std::string name, Edge& edge) { 342 314 if (edge_readers.find(name) != edge_readers.end()) { 343 throw Exception() << "Multiple read rule for edge"; 315 ErrorMessage msg; 316 msg << "Multiple read rule for edge: " << name; 317 throw IOLogicError(msg.message()); 344 318 } 345 319 edge_readers.insert(make_pair(name, &edge)); … … 369 343 } 370 344 if (line.find("@end") != 0) { 371 throw DataFormatException("Invalid control sequence: " + line); 372 } 373 } catch (DataFormatException e) { 374 throw StreamException<DataFormatException>(line_num, e); 345 throw DataFormatError("Invalid control sequence error"); 346 } 347 } catch (DataFormatError e) { 348 e.line(line_num); 349 throw e; 375 350 } 376 351 } … … 400 375 401 376 if (index.size() == 0) { 402 throw DataFormatE xception("No node map found");377 throw DataFormatError("Cannot find node id map"); 403 378 } 404 379 … … 437 412 438 413 if (index.size() == 0) { 439 throw DataFormatE xception("No edge map found");414 throw DataFormatError("Cannot find edge id map"); 440 415 } 441 416 … … 493 468 } 494 469 } 495 throw DataFormatE xception("End of stream");470 throw DataFormatError("End of stream error"); 496 471 } 497 472 … … 533 508 inverse.insert(make_pair(value, item)); 534 509 } else { 535 throw DataFormatE xception("Multiple ID occurence");510 throw DataFormatError("Multiple ID occurence"); 536 511 } 537 512 } … … 544 519 return it->second; 545 520 } else { 546 throw DataFormatE xception("Invalid ID");521 throw DataFormatError("Invalid ID error"); 547 522 } 548 523 } … … 571 546 inverse.insert(make_pair(value, item)); 572 547 } else { 573 throw DataFormatE xception("Multiple ID occurence");548 throw DataFormatError("Multiple ID occurence error"); 574 549 } 575 550 } … … 582 557 return it->second; 583 558 } else { 584 throw DataFormatE xception("Invalid ID");559 throw DataFormatError("Invalid ID error"); 585 560 } 586 561 } … … 673 648 }; 674 649 650 /// Ready to use reader function. 651 template<typename Graph, typename CapacityMap, typename CostMap> 652 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 653 typename Graph::Node &s, typename Graph::Node &t, 654 CostMap& cost) { 655 GraphReader<Graph> reader(is, g); 656 reader.addEdgeMap("capacity", capacity); 657 reader.addEdgeMap("cost", cost); 658 reader.addNode("source", s); 659 reader.addNode("target", t); 660 reader.run(); 661 } 662 663 template<typename Graph, typename CapacityMap> 664 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 665 typename Graph::Node &s, typename Graph::Node &t) { 666 GraphReader<Graph> reader(is, g); 667 reader.addEdgeMap("capacity", capacity); 668 reader.addNode("source", s); 669 reader.addNode("target", t); 670 reader.run(); 671 } 672 673 template<typename Graph, typename CapacityMap> 674 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 675 typename Graph::Node &s) { 676 GraphReader<Graph> reader(is, g); 677 reader.addEdgeMap("capacity", capacity); 678 reader.addNode("source", s); 679 reader.run(); 680 } 681 682 template<typename Graph, typename CapacityMap> 683 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) { 684 GraphReader<Graph> reader(is, g); 685 reader.addEdgeMap("capacity", capacity); 686 reader.run(); 687 } 688 689 template<typename Graph> 690 void readGraph(std::istream& is, Graph &g) { 691 GraphReader<Graph> reader(is, g); 692 reader.run(); 693 } 694 675 695 } -
src/lemon/graph_writer.h
r1188 r1208 28 28 #include <memory> 29 29 30 #include <lemon/map_utils.h> 30 31 #include <lemon/invalid.h> 31 32 #include <lemon/error.h> … … 163 164 /// it constructs the given map and it use the given writer as the 164 165 /// default skipper. 165 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {} 166 GraphWriter(std::ostream& _os, const Graph& _graph) 167 : os(_os), graph(_graph) {} 166 168 167 169 … … 368 370 369 371 std::ostream& os; 370 Graph& graph;372 const Graph& graph; 371 373 372 374 }; 373 375 376 /// Ready to use writer function. 377 template<typename Graph, typename CapacityMap, typename CostMap> 378 void writeGraph(std::ostream& os, const Graph &g, 379 const CapacityMap& capacity, const typename Graph::Node &s, 380 const typename Graph::Node &t, const CostMap& cost) { 381 GraphWriter<Graph> reader(os, g); 382 IdMap<Graph, typename Graph::Node> nodeIdMap(g); 383 reader.addNodeMap("id", nodeIdMap); 384 IdMap<Graph, typename Graph::Edge> edgeIdMap(g); 385 reader.addEdgeMap("id", edgeIdMap); 386 reader.addEdgeMap("capacity", capacity); 387 reader.addEdgeMap("cost", cost); 388 reader.addNode("source", s); 389 reader.addNode("target", t); 390 reader.run(); 391 } 392 393 /// Ready to use writer function. 394 template<typename Graph, typename CapacityMap, typename CostMap> 395 void writeGraph(std::ostream& os, const Graph &g, 396 const CapacityMap& capacity, const typename Graph::Node &s, 397 const typename Graph::Node &t) { 398 GraphWriter<Graph> reader(os, g); 399 IdMap<Graph, typename Graph::Node> nodeIdMap(g); 400 reader.addNodeMap("id", nodeIdMap); 401 IdMap<Graph, typename Graph::Edge> edgeIdMap(g); 402 reader.addEdgeMap("id", edgeIdMap); 403 reader.addEdgeMap("capacity", capacity); 404 reader.addNode("source", s); 405 reader.addNode("target", t); 406 reader.run(); 407 } 408 409 /// Ready to use writer function. 410 template<typename Graph, typename CapacityMap> 411 void writeGraph(std::ostream& os, const Graph &g, 412 const CapacityMap& capacity, const typename Graph::Node &s) { 413 GraphWriter<Graph> reader(os, g); 414 IdMap<Graph, typename Graph::Node> nodeIdMap(g); 415 reader.addNodeMap("id", nodeIdMap); 416 IdMap<Graph, typename Graph::Edge> edgeIdMap(g); 417 reader.addEdgeMap("id", edgeIdMap); 418 reader.addEdgeMap("capacity", capacity); 419 reader.addNode("source", s); 420 reader.run(); 421 } 422 /// Ready to use writer function. 423 template<typename Graph, typename CapacityMap> 424 void writeGraph(std::ostream& os, const Graph &g, 425 const CapacityMap& capacity) { 426 GraphWriter<Graph> reader(os, g); 427 IdMap<Graph, typename Graph::Node> nodeIdMap(g); 428 reader.addNodeMap("id", nodeIdMap); 429 IdMap<Graph, typename Graph::Edge> edgeIdMap(g); 430 reader.addEdgeMap("id", edgeIdMap); 431 reader.addEdgeMap("capacity", capacity); 432 reader.run(); 433 } 434 /// Ready to use writer function. 435 template<typename Graph> 436 void writeGraph(std::ostream& os, const Graph &g) { 437 GraphWriter<Graph> reader(os, g); 438 IdMap<Graph, typename Graph::Node> nodeIdMap(g); 439 reader.addNodeMap("id", nodeIdMap); 440 IdMap<Graph, typename Graph::Edge> edgeIdMap(g); 441 reader.addEdgeMap("id", edgeIdMap); 442 reader.run(); 443 } 444 374 445 375 446 }
Note: See TracChangeset
for help on using the changeset viewer.