Changeset 294:cbe3ec2d59d2 in lemon-main
- Timestamp:
- 10/01/08 13:56:40 (16 years ago)
- Branch:
- default
- Children:
- 295:7c796c1cf1b0, 296:9768e60aa4e1
- Parents:
- 293:47fbc814aa31 (diff), 292:e7af73f1805e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Phase:
- public
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
demo/lgf_demo.cc
r290 r294 45 45 46 46 try { 47 digraphReader( "digraph.lgf", g). // read the directed graph into g47 digraphReader(g, "digraph.lgf"). // read the directed graph into g 48 48 arcMap("capacity", cap). // read the 'capacity' arc map into cap 49 49 node("source", s). // read 'source' node to s … … 61 61 std::cout << "We can write it to the standard output:" << std::endl; 62 62 63 digraphWriter( std::cout, g).// write g to the standard output63 digraphWriter(g). // write g to the standard output 64 64 arcMap("capacity", cap). // write cap into 'capacity' 65 65 node("source", s). // write s to 'source' -
demo/lgf_demo.cc
r293 r294 50 50 node("target", t). // read 'target' node to t 51 51 run(); 52 } catch ( DataFormatError& error) { // check if there was any error52 } catch (Exception& error) { // check if there was any error 53 53 std::cerr << "Error: " << error.what() << std::endl; 54 54 return -1; -
lemon/lgf_reader.h
r291 r294 393 393 394 394 template <typename Digraph> 395 DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph); 395 DigraphReader<Digraph> digraphReader(Digraph& digraph, 396 std::istream& is = std::cin); 396 397 397 398 template <typename Digraph> 398 DigraphReader<Digraph> digraphReader( const std::string& fn, Digraph& digraph);399 DigraphReader<Digraph> digraphReader(Digraph& digraph, const std::string& fn); 399 400 400 401 template <typename Digraph> 401 DigraphReader<Digraph> digraphReader( const char *fn, Digraph& digraph);402 DigraphReader<Digraph> digraphReader(Digraph& digraph, const char *fn); 402 403 403 404 /// \ingroup lemon_io … … 422 423 /// 423 424 ///\code 424 /// DigraphReader<Digraph>( std::cin, digraph).425 /// DigraphReader<Digraph>(digraph, std::cin). 425 426 /// nodeMap("coordinates", coord_map). 426 427 /// arcMap("capacity", cap_map). … … 503 504 /// Construct a directed graph reader, which reads from the given 504 505 /// input stream. 505 DigraphReader( std::istream& is, Digraph& digraph)506 DigraphReader(Digraph& digraph, std::istream& is = std::cin) 506 507 : _is(&is), local_is(false), _digraph(digraph), 507 508 _use_nodes(false), _use_arcs(false), … … 512 513 /// Construct a directed graph reader, which reads from the given 513 514 /// file. 514 DigraphReader( const std::string& fn, Digraph& digraph)515 DigraphReader(Digraph& digraph, const std::string& fn) 515 516 : _is(new std::ifstream(fn.c_str())), local_is(true), 516 517 _filename(fn), _digraph(digraph), … … 524 525 /// Construct a directed graph reader, which reads from the given 525 526 /// file. 526 DigraphReader( const char* fn, Digraph& digraph)527 DigraphReader(Digraph& digraph, const char* fn) 527 528 : _is(new std::ifstream(fn)), local_is(true), 528 529 _filename(fn), _digraph(digraph), … … 557 558 private: 558 559 559 friend DigraphReader<Digraph> digraphReader<>( std::istream& is,560 Digraph& digraph);561 friend DigraphReader<Digraph> digraphReader<>( const std::string& fn,562 Digraph& digraph);563 friend DigraphReader<Digraph> digraphReader<>( const char *fn,564 Digraph& digraph);560 friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, 561 std::istream& is); 562 friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, 563 const std::string& fn); 564 friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, 565 const char *fn); 565 566 566 567 DigraphReader(DigraphReader& other) … … 1188 1189 /// \relates DigraphReader 1189 1190 template <typename Digraph> 1190 DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) { 1191 DigraphReader<Digraph> tmp(is, digraph); 1191 DigraphReader<Digraph> digraphReader(Digraph& digraph, 1192 std::istream& is = std::cin) { 1193 DigraphReader<Digraph> tmp(digraph, is); 1192 1194 return tmp; 1193 1195 } … … 1198 1200 /// \relates DigraphReader 1199 1201 template <typename Digraph> 1200 DigraphReader<Digraph> digraphReader( const std::string& fn,1201 Digraph& digraph) {1202 DigraphReader<Digraph> tmp( fn, digraph);1202 DigraphReader<Digraph> digraphReader(Digraph& digraph, 1203 const std::string& fn) { 1204 DigraphReader<Digraph> tmp(digraph, fn); 1203 1205 return tmp; 1204 1206 } … … 1209 1211 /// \relates DigraphReader 1210 1212 template <typename Digraph> 1211 DigraphReader<Digraph> digraphReader( const char* fn, Digraph& digraph) {1212 DigraphReader<Digraph> tmp( fn, digraph);1213 DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) { 1214 DigraphReader<Digraph> tmp(digraph, fn); 1213 1215 return tmp; 1214 1216 } … … 1218 1220 1219 1221 template <typename Graph> 1220 GraphReader<Graph> graphReader(std::istream& is, Graph& graph); 1222 GraphReader<Graph> graphReader(Graph& graph, 1223 std::istream& is = std::cin); 1221 1224 1222 1225 template <typename Graph> 1223 GraphReader<Graph> graphReader( const std::string& fn, Graph& graph);1226 GraphReader<Graph> graphReader(Graph& graph, const std::string& fn); 1224 1227 1225 1228 template <typename Graph> 1226 GraphReader<Graph> graphReader( const char *fn, Graph& graph);1229 GraphReader<Graph> graphReader(Graph& graph, const char *fn); 1227 1230 1228 1231 /// \ingroup lemon_io … … 1292 1295 /// Construct an undirected graph reader, which reads from the given 1293 1296 /// input stream. 1294 GraphReader( std::istream& is, Graph& graph)1297 GraphReader(Graph& graph, std::istream& is = std::cin) 1295 1298 : _is(&is), local_is(false), _graph(graph), 1296 1299 _use_nodes(false), _use_edges(false), … … 1301 1304 /// Construct an undirected graph reader, which reads from the given 1302 1305 /// file. 1303 GraphReader( const std::string& fn, Graph& graph)1306 GraphReader(Graph& graph, const std::string& fn) 1304 1307 : _is(new std::ifstream(fn.c_str())), local_is(true), 1305 1308 _filename(fn), _graph(graph), … … 1313 1316 /// Construct an undirected graph reader, which reads from the given 1314 1317 /// file. 1315 GraphReader( const char* fn, Graph& graph)1318 GraphReader(Graph& graph, const char* fn) 1316 1319 : _is(new std::ifstream(fn)), local_is(true), 1317 1320 _filename(fn), _graph(graph), … … 1345 1348 1346 1349 private: 1347 friend GraphReader<Graph> graphReader<>( std::istream& is, Graph& graph);1348 friend GraphReader<Graph> graphReader<>( const std::string& fn,1349 Graph& graph);1350 friend GraphReader<Graph> graphReader<>( const char *fn, Graph& graph);1350 friend GraphReader<Graph> graphReader<>(Graph& graph, std::istream& is); 1351 friend GraphReader<Graph> graphReader<>(Graph& graph, 1352 const std::string& fn); 1353 friend GraphReader<Graph> graphReader<>(Graph& graph, const char *fn); 1351 1354 1352 1355 GraphReader(GraphReader& other) … … 2022 2025 /// \relates GraphReader 2023 2026 template <typename Graph> 2024 GraphReader<Graph> graphReader( std::istream& is, Graph& graph) {2025 GraphReader<Graph> tmp( is, graph);2027 GraphReader<Graph> graphReader(Graph& graph, std::istream& is = std::cin) { 2028 GraphReader<Graph> tmp(graph, is); 2026 2029 return tmp; 2027 2030 } … … 2032 2035 /// \relates GraphReader 2033 2036 template <typename Graph> 2034 GraphReader<Graph> graphReader(const std::string& fn, 2035 Graph& graph) { 2036 GraphReader<Graph> tmp(fn, graph); 2037 GraphReader<Graph> graphReader(Graph& graph, const std::string& fn) { 2038 GraphReader<Graph> tmp(graph, fn); 2037 2039 return tmp; 2038 2040 } … … 2043 2045 /// \relates GraphReader 2044 2046 template <typename Graph> 2045 GraphReader<Graph> graphReader( const char* fn, Graph& graph) {2046 GraphReader<Graph> tmp( fn, graph);2047 GraphReader<Graph> graphReader(Graph& graph, const char* fn) { 2048 GraphReader<Graph> tmp(graph, fn); 2047 2049 return tmp; 2048 2050 } -
lemon/lgf_reader.h
r293 r294 49 49 std::istringstream is(str); 50 50 Value value; 51 is >> value; 51 if (!(is >> value)) { 52 throw FormatError("Cannot read token"); 53 } 52 54 53 55 char c; 54 56 if (is >> std::ws >> c) { 55 throw DataFormatError("Remaining characters in token");57 throw FormatError("Remaining characters in token"); 56 58 } 57 59 return value; … … 167 169 std::ostringstream msg; 168 170 msg << "Item not found: " << str; 169 throw DataFormatError(msg.str().c_str());171 throw FormatError(msg.str()); 170 172 } 171 173 return it->second; … … 185 187 typename Graph::Arc operator()(const std::string& str) { 186 188 if (str.empty() || (str[0] != '+' && str[0] != '-')) { 187 throw DataFormatError("Item must start with '+' or '-'");189 throw FormatError("Item must start with '+' or '-'"); 188 190 } 189 191 typename std::map<std::string, typename Graph::Edge> 190 192 ::const_iterator it = _map.find(str.substr(1)); 191 193 if (it == _map.end()) { 192 throw DataFormatError("Item not found");194 throw FormatError("Item not found"); 193 195 } 194 196 return _graph.direct(it->second, str[0] == '+'); … … 236 238 char c; 237 239 if (!is.get(c)) 238 throw DataFormatError("Escape format error");240 throw FormatError("Escape format error"); 239 241 240 242 switch (c) { … … 265 267 int code; 266 268 if (!is.get(c) || !isHex(c)) 267 throw DataFormatError("Escape format error");269 throw FormatError("Escape format error"); 268 270 else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); 269 271 else code = code * 16 + valueHex(c); … … 274 276 int code; 275 277 if (!isOct(c)) 276 throw DataFormatError("Escape format error");278 throw FormatError("Escape format error"); 277 279 else if (code = valueOct(c), !is.get(c) || !isOct(c)) 278 280 is.putback(c); … … 301 303 } 302 304 if (!is) 303 throw DataFormatError("Quoted format error");305 throw FormatError("Quoted format error"); 304 306 } else { 305 307 is.putback(c); … … 462 464 std::istream* _is; 463 465 bool local_is; 466 std::string _filename; 464 467 465 468 Digraph& _digraph; … … 511 514 /// file. 512 515 DigraphReader(Digraph& digraph, const std::string& fn) 513 : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph), 516 : _is(new std::ifstream(fn.c_str())), local_is(true), 517 _filename(fn), _digraph(digraph), 514 518 _use_nodes(false), _use_arcs(false), 515 _skip_nodes(false), _skip_arcs(false) {} 519 _skip_nodes(false), _skip_arcs(false) { 520 if (!(*_is)) throw IoError("Cannot open file", fn); 521 } 516 522 517 523 /// \brief Constructor … … 520 526 /// file. 521 527 DigraphReader(Digraph& digraph, const char* fn) 522 : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph), 528 : _is(new std::ifstream(fn)), local_is(true), 529 _filename(fn), _digraph(digraph), 523 530 _use_nodes(false), _use_arcs(false), 524 _skip_nodes(false), _skip_arcs(false) {} 531 _skip_nodes(false), _skip_arcs(false) { 532 if (!(*_is)) throw IoError("Cannot open file", fn); 533 } 525 534 526 535 /// \brief Destructor … … 847 856 if (readSuccess() && line) line.putback(c); 848 857 if (!_node_maps.empty()) 849 throw DataFormatError("Cannot find map names");858 throw FormatError("Cannot find map names"); 850 859 return; 851 860 } … … 861 870 std::ostringstream msg; 862 871 msg << "Multiple occurence of node map: " << map; 863 throw DataFormatError(msg.str().c_str());872 throw FormatError(msg.str()); 864 873 } 865 874 maps.insert(std::make_pair(map, index)); … … 872 881 if (jt == maps.end()) { 873 882 std::ostringstream msg; 874 msg << "Map not found in file: " << _node_maps[i].first;875 throw DataFormatError(msg.str().c_str());883 msg << "Map not found: " << _node_maps[i].first; 884 throw FormatError(msg.str()); 876 885 } 877 886 map_index[i] = jt->second; … … 897 906 std::ostringstream msg; 898 907 msg << "Column not found (" << i + 1 << ")"; 899 throw DataFormatError(msg.str().c_str());908 throw FormatError(msg.str()); 900 909 } 901 910 } 902 911 if (line >> std::ws >> c) 903 throw DataFormatError("Extra character onthe end of line");912 throw FormatError("Extra character at the end of line"); 904 913 905 914 Node n; … … 910 919 } else { 911 920 if (label_index == -1) 912 throw DataFormatError("Label map not found in file");921 throw FormatError("Label map not found"); 913 922 typename std::map<std::string, Node>::iterator it = 914 923 _node_index.find(tokens[label_index]); … … 916 925 std::ostringstream msg; 917 926 msg << "Node with label not found: " << tokens[label_index]; 918 throw DataFormatError(msg.str().c_str());927 throw FormatError(msg.str()); 919 928 } 920 929 n = it->second; … … 940 949 if (readSuccess() && line) line.putback(c); 941 950 if (!_arc_maps.empty()) 942 throw DataFormatError("Cannot find map names");951 throw FormatError("Cannot find map names"); 943 952 return; 944 953 } … … 954 963 std::ostringstream msg; 955 964 msg << "Multiple occurence of arc map: " << map; 956 throw DataFormatError(msg.str().c_str());965 throw FormatError(msg.str()); 957 966 } 958 967 maps.insert(std::make_pair(map, index)); … … 965 974 if (jt == maps.end()) { 966 975 std::ostringstream msg; 967 msg << "Map not found in file: " << _arc_maps[i].first;968 throw DataFormatError(msg.str().c_str());976 msg << "Map not found: " << _arc_maps[i].first; 977 throw FormatError(msg.str()); 969 978 } 970 979 map_index[i] = jt->second; … … 989 998 990 999 if (!_reader_bits::readToken(line, source_token)) 991 throw DataFormatError("Source not found");1000 throw FormatError("Source not found"); 992 1001 993 1002 if (!_reader_bits::readToken(line, target_token)) 994 throw DataFormatError("Target not found");1003 throw FormatError("Target not found"); 995 1004 996 1005 std::vector<std::string> tokens(map_num); … … 999 1008 std::ostringstream msg; 1000 1009 msg << "Column not found (" << i + 1 << ")"; 1001 throw DataFormatError(msg.str().c_str());1010 throw FormatError(msg.str()); 1002 1011 } 1003 1012 } 1004 1013 if (line >> std::ws >> c) 1005 throw DataFormatError("Extra character onthe end of line");1014 throw FormatError("Extra character at the end of line"); 1006 1015 1007 1016 Arc a; … … 1014 1023 std::ostringstream msg; 1015 1024 msg << "Item not found: " << source_token; 1016 throw DataFormatError(msg.str().c_str());1025 throw FormatError(msg.str()); 1017 1026 } 1018 1027 Node source = it->second; … … 1022 1031 std::ostringstream msg; 1023 1032 msg << "Item not found: " << target_token; 1024 throw DataFormatError(msg.str().c_str());1033 throw FormatError(msg.str()); 1025 1034 } 1026 1035 Node target = it->second; … … 1031 1040 } else { 1032 1041 if (label_index == -1) 1033 throw DataFormatError("Label map not found in file");1042 throw FormatError("Label map not found"); 1034 1043 typename std::map<std::string, Arc>::iterator it = 1035 1044 _arc_index.find(tokens[label_index]); … … 1037 1046 std::ostringstream msg; 1038 1047 msg << "Arc with label not found: " << tokens[label_index]; 1039 throw DataFormatError(msg.str().c_str());1048 throw FormatError(msg.str()); 1040 1049 } 1041 1050 a = it->second; … … 1062 1071 std::string attr, token; 1063 1072 if (!_reader_bits::readToken(line, attr)) 1064 throw DataFormatError("Attribute name not found");1073 throw FormatError("Attribute name not found"); 1065 1074 if (!_reader_bits::readToken(line, token)) 1066 throw DataFormatError("Attribute value not found");1075 throw FormatError("Attribute value not found"); 1067 1076 if (line >> c) 1068 throw DataFormatError("Extra character onthe end of line");1077 throw FormatError("Extra character at the end of line"); 1069 1078 1070 1079 { … … 1072 1081 if (it != read_attr.end()) { 1073 1082 std::ostringstream msg; 1074 msg << "Multiple occurence of attribute " << attr;1075 throw DataFormatError(msg.str().c_str());1083 msg << "Multiple occurence of attribute: " << attr; 1084 throw FormatError(msg.str()); 1076 1085 } 1077 1086 read_attr.insert(attr); … … 1094 1103 if (read_attr.find(it->first) == read_attr.end()) { 1095 1104 std::ostringstream msg; 1096 msg << "Attribute not found in file: " << it->first;1097 throw DataFormatError(msg.str().c_str());1105 msg << "Attribute not found: " << it->first; 1106 throw FormatError(msg.str()); 1098 1107 } 1099 1108 } … … 1110 1119 void run() { 1111 1120 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); 1112 if (!*_is) {1113 throw DataFormatError("Cannot find file");1114 }1115 1121 1116 1122 bool nodes_done = _skip_nodes; … … 1131 1137 1132 1138 if (line >> c) 1133 throw DataFormatError("Extra character onthe end of line");1139 throw FormatError("Extra character at the end of line"); 1134 1140 1135 1141 if (section == "nodes" && !nodes_done) { … … 1153 1159 skipSection(); 1154 1160 } 1155 } catch ( DataFormatError& error) {1161 } catch (FormatError& error) { 1156 1162 error.line(line_num); 1163 error.file(_filename); 1157 1164 throw; 1158 1165 } … … 1160 1167 1161 1168 if (!nodes_done) { 1162 throw DataFormatError("Section @nodes not found");1169 throw FormatError("Section @nodes not found"); 1163 1170 } 1164 1171 1165 1172 if (!arcs_done) { 1166 throw DataFormatError("Section @arcs not found");1173 throw FormatError("Section @arcs not found"); 1167 1174 } 1168 1175 1169 1176 if (!attributes_done && !_attributes.empty()) { 1170 throw DataFormatError("Section @attributes not found");1177 throw FormatError("Section @attributes not found"); 1171 1178 } 1172 1179 … … 1248 1255 std::istream* _is; 1249 1256 bool local_is; 1257 std::string _filename; 1250 1258 1251 1259 Graph& _graph; … … 1297 1305 /// file. 1298 1306 GraphReader(Graph& graph, const std::string& fn) 1299 : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph), 1307 : _is(new std::ifstream(fn.c_str())), local_is(true), 1308 _filename(fn), _graph(graph), 1300 1309 _use_nodes(false), _use_edges(false), 1301 _skip_nodes(false), _skip_edges(false) {} 1310 _skip_nodes(false), _skip_edges(false) { 1311 if (!(*_is)) throw IoError("Cannot open file", fn); 1312 } 1302 1313 1303 1314 /// \brief Constructor … … 1306 1317 /// file. 1307 1318 GraphReader(Graph& graph, const char* fn) 1308 : _is(new std::ifstream(fn)), local_is(true), _graph(graph), 1319 : _is(new std::ifstream(fn)), local_is(true), 1320 _filename(fn), _graph(graph), 1309 1321 _use_nodes(false), _use_edges(false), 1310 _skip_nodes(false), _skip_edges(false) {} 1322 _skip_nodes(false), _skip_edges(false) { 1323 if (!(*_is)) throw IoError("Cannot open file", fn); 1324 } 1311 1325 1312 1326 /// \brief Destructor … … 1677 1691 if (readSuccess() && line) line.putback(c); 1678 1692 if (!_node_maps.empty()) 1679 throw DataFormatError("Cannot find map names");1693 throw FormatError("Cannot find map names"); 1680 1694 return; 1681 1695 } … … 1691 1705 std::ostringstream msg; 1692 1706 msg << "Multiple occurence of node map: " << map; 1693 throw DataFormatError(msg.str().c_str());1707 throw FormatError(msg.str()); 1694 1708 } 1695 1709 maps.insert(std::make_pair(map, index)); … … 1702 1716 if (jt == maps.end()) { 1703 1717 std::ostringstream msg; 1704 msg << "Map not found in file: " << _node_maps[i].first;1705 throw DataFormatError(msg.str().c_str());1718 msg << "Map not found: " << _node_maps[i].first; 1719 throw FormatError(msg.str()); 1706 1720 } 1707 1721 map_index[i] = jt->second; … … 1727 1741 std::ostringstream msg; 1728 1742 msg << "Column not found (" << i + 1 << ")"; 1729 throw DataFormatError(msg.str().c_str());1743 throw FormatError(msg.str()); 1730 1744 } 1731 1745 } 1732 1746 if (line >> std::ws >> c) 1733 throw DataFormatError("Extra character onthe end of line");1747 throw FormatError("Extra character at the end of line"); 1734 1748 1735 1749 Node n; … … 1740 1754 } else { 1741 1755 if (label_index == -1) 1742 throw DataFormatError("Label map not found in file");1756 throw FormatError("Label map not found"); 1743 1757 typename std::map<std::string, Node>::iterator it = 1744 1758 _node_index.find(tokens[label_index]); … … 1746 1760 std::ostringstream msg; 1747 1761 msg << "Node with label not found: " << tokens[label_index]; 1748 throw DataFormatError(msg.str().c_str());1762 throw FormatError(msg.str()); 1749 1763 } 1750 1764 n = it->second; … … 1770 1784 if (readSuccess() && line) line.putback(c); 1771 1785 if (!_edge_maps.empty()) 1772 throw DataFormatError("Cannot find map names");1786 throw FormatError("Cannot find map names"); 1773 1787 return; 1774 1788 } … … 1784 1798 std::ostringstream msg; 1785 1799 msg << "Multiple occurence of edge map: " << map; 1786 throw DataFormatError(msg.str().c_str());1800 throw FormatError(msg.str()); 1787 1801 } 1788 1802 maps.insert(std::make_pair(map, index)); … … 1795 1809 if (jt == maps.end()) { 1796 1810 std::ostringstream msg; 1797 msg << "Map not found in file: " << _edge_maps[i].first;1798 throw DataFormatError(msg.str().c_str());1811 msg << "Map not found: " << _edge_maps[i].first; 1812 throw FormatError(msg.str()); 1799 1813 } 1800 1814 map_index[i] = jt->second; … … 1819 1833 1820 1834 if (!_reader_bits::readToken(line, source_token)) 1821 throw DataFormatError("Node u not found");1835 throw FormatError("Node u not found"); 1822 1836 1823 1837 if (!_reader_bits::readToken(line, target_token)) 1824 throw DataFormatError("Node v not found");1838 throw FormatError("Node v not found"); 1825 1839 1826 1840 std::vector<std::string> tokens(map_num); … … 1829 1843 std::ostringstream msg; 1830 1844 msg << "Column not found (" << i + 1 << ")"; 1831 throw DataFormatError(msg.str().c_str());1845 throw FormatError(msg.str()); 1832 1846 } 1833 1847 } 1834 1848 if (line >> std::ws >> c) 1835 throw DataFormatError("Extra character onthe end of line");1849 throw FormatError("Extra character at the end of line"); 1836 1850 1837 1851 Edge e; … … 1844 1858 std::ostringstream msg; 1845 1859 msg << "Item not found: " << source_token; 1846 throw DataFormatError(msg.str().c_str());1860 throw FormatError(msg.str()); 1847 1861 } 1848 1862 Node source = it->second; … … 1852 1866 std::ostringstream msg; 1853 1867 msg << "Item not found: " << target_token; 1854 throw DataFormatError(msg.str().c_str());1868 throw FormatError(msg.str()); 1855 1869 } 1856 1870 Node target = it->second; … … 1861 1875 } else { 1862 1876 if (label_index == -1) 1863 throw DataFormatError("Label map not found in file");1877 throw FormatError("Label map not found"); 1864 1878 typename std::map<std::string, Edge>::iterator it = 1865 1879 _edge_index.find(tokens[label_index]); … … 1867 1881 std::ostringstream msg; 1868 1882 msg << "Edge with label not found: " << tokens[label_index]; 1869 throw DataFormatError(msg.str().c_str());1883 throw FormatError(msg.str()); 1870 1884 } 1871 1885 e = it->second; … … 1892 1906 std::string attr, token; 1893 1907 if (!_reader_bits::readToken(line, attr)) 1894 throw DataFormatError("Attribute name not found");1908 throw FormatError("Attribute name not found"); 1895 1909 if (!_reader_bits::readToken(line, token)) 1896 throw DataFormatError("Attribute value not found");1910 throw FormatError("Attribute value not found"); 1897 1911 if (line >> c) 1898 throw DataFormatError("Extra character onthe end of line");1912 throw FormatError("Extra character at the end of line"); 1899 1913 1900 1914 { … … 1902 1916 if (it != read_attr.end()) { 1903 1917 std::ostringstream msg; 1904 msg << "Multiple occurence of attribute " << attr;1905 throw DataFormatError(msg.str().c_str());1918 msg << "Multiple occurence of attribute: " << attr; 1919 throw FormatError(msg.str()); 1906 1920 } 1907 1921 read_attr.insert(attr); … … 1924 1938 if (read_attr.find(it->first) == read_attr.end()) { 1925 1939 std::ostringstream msg; 1926 msg << "Attribute not found in file: " << it->first;1927 throw DataFormatError(msg.str().c_str());1940 msg << "Attribute not found: " << it->first; 1941 throw FormatError(msg.str()); 1928 1942 } 1929 1943 } … … 1959 1973 1960 1974 if (line >> c) 1961 throw DataFormatError("Extra character onthe end of line");1975 throw FormatError("Extra character at the end of line"); 1962 1976 1963 1977 if (section == "nodes" && !nodes_done) { … … 1981 1995 skipSection(); 1982 1996 } 1983 } catch ( DataFormatError& error) {1997 } catch (FormatError& error) { 1984 1998 error.line(line_num); 1999 error.file(_filename); 1985 2000 throw; 1986 2001 } … … 1988 2003 1989 2004 if (!nodes_done) { 1990 throw DataFormatError("Section @nodes not found");2005 throw FormatError("Section @nodes not found"); 1991 2006 } 1992 2007 1993 2008 if (!edges_done) { 1994 throw DataFormatError("Section @edges not found");2009 throw FormatError("Section @edges not found"); 1995 2010 } 1996 2011 1997 2012 if (!attributes_done && !_attributes.empty()) { 1998 throw DataFormatError("Section @attributes not found");2013 throw FormatError("Section @attributes not found"); 1999 2014 } 2000 2015 … … 2057 2072 std::istream* _is; 2058 2073 bool local_is; 2074 std::string _filename; 2059 2075 2060 2076 typedef std::map<std::string, _reader_bits::Section*> Sections; … … 2077 2093 /// Construct a section reader, which reads from the given file. 2078 2094 SectionReader(const std::string& fn) 2079 : _is(new std::ifstream(fn.c_str())), local_is(true) {} 2095 : _is(new std::ifstream(fn.c_str())), local_is(true), 2096 _filename(fn) { 2097 if (!(*_is)) throw IoError("Cannot open file", fn); 2098 } 2080 2099 2081 2100 /// \brief Constructor … … 2083 2102 /// Construct a section reader, which reads from the given file. 2084 2103 SectionReader(const char* fn) 2085 : _is(new std::ifstream(fn)), local_is(true) {} 2104 : _is(new std::ifstream(fn)), local_is(true), 2105 _filename(fn) { 2106 if (!(*_is)) throw IoError("Cannot open file", fn); 2107 } 2086 2108 2087 2109 /// \brief Destructor … … 2239 2261 2240 2262 if (line >> c) 2241 throw DataFormatError("Extra character onthe end of line");2263 throw FormatError("Extra character at the end of line"); 2242 2264 2243 2265 if (extra_sections.find(section) != extra_sections.end()) { 2244 2266 std::ostringstream msg; 2245 msg << "Multiple occurence of section " << section;2246 throw DataFormatError(msg.str().c_str());2267 msg << "Multiple occurence of section: " << section; 2268 throw FormatError(msg.str()); 2247 2269 } 2248 2270 Sections::iterator it = _sections.find(section); … … 2253 2275 readLine(); 2254 2276 skipSection(); 2255 } catch ( DataFormatError& error) {2277 } catch (FormatError& error) { 2256 2278 error.line(line_num); 2279 error.file(_filename); 2257 2280 throw; 2258 2281 } … … 2263 2286 std::ostringstream os; 2264 2287 os << "Cannot find section: " << it->first; 2265 throw DataFormatError(os.str().c_str());2288 throw FormatError(os.str()); 2266 2289 } 2267 2290 } … … 2363 2386 /// file. 2364 2387 LgfContents(const std::string& fn) 2365 : _is(new std::ifstream(fn.c_str())), local_is(true) {} 2388 : _is(new std::ifstream(fn.c_str())), local_is(true) { 2389 if (!(*_is)) throw IoError("Cannot open file", fn); 2390 } 2366 2391 2367 2392 /// \brief Constructor … … 2370 2395 /// file. 2371 2396 LgfContents(const char* fn) 2372 : _is(new std::ifstream(fn)), local_is(true) {} 2397 : _is(new std::ifstream(fn)), local_is(true) { 2398 if (!(*_is)) throw IoError("Cannot open file", fn); 2399 } 2373 2400 2374 2401 /// \brief Destructor -
lemon/lgf_writer.h
r291 r294 353 353 354 354 template <typename Digraph> 355 DigraphWriter<Digraph> digraphWriter( std::ostream& os,356 const Digraph& digraph);355 DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, 356 std::ostream& os = std::cout); 357 357 358 358 template <typename Digraph> 359 DigraphWriter<Digraph> digraphWriter(const std::string& fn,360 const Digraph& digraph);359 DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, 360 const std::string& fn); 361 361 362 362 template <typename Digraph> 363 DigraphWriter<Digraph> digraphWriter(const char *fn,364 const Digraph& digraph);363 DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, 364 const char *fn); 365 365 366 366 /// \ingroup lemon_io … … 383 383 /// 384 384 ///\code 385 /// DigraphWriter<Digraph>( std::cout, digraph).385 /// DigraphWriter<Digraph>(digraph, std::cout). 386 386 /// nodeMap("coordinates", coord_map). 387 387 /// nodeMap("size", size). … … 453 453 /// Construct a directed graph writer, which writes to the given 454 454 /// output stream. 455 DigraphWriter( std::ostream& is, const Digraph& digraph)456 : _os(& is), local_os(false), _digraph(digraph),455 DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout) 456 : _os(&os), local_os(false), _digraph(digraph), 457 457 _skip_nodes(false), _skip_arcs(false) {} 458 458 … … 461 461 /// Construct a directed graph writer, which writes to the given 462 462 /// output file. 463 DigraphWriter(const std::string& fn, const Digraph& digraph)463 DigraphWriter(const Digraph& digraph, const std::string& fn) 464 464 : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), 465 465 _skip_nodes(false), _skip_arcs(false) { … … 471 471 /// Construct a directed graph writer, which writes to the given 472 472 /// output file. 473 DigraphWriter(const char* fn, const Digraph& digraph)473 DigraphWriter(const Digraph& digraph, const char* fn) 474 474 : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), 475 475 _skip_nodes(false), _skip_arcs(false) { … … 501 501 private: 502 502 503 friend DigraphWriter<Digraph> digraphWriter<>( std::ostream& os,504 const Digraph& digraph);505 friend DigraphWriter<Digraph> digraphWriter<>(const std::string& fn,506 const Digraph& digraph);507 friend DigraphWriter<Digraph> digraphWriter<>(const char *fn,508 const Digraph& digraph);503 friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, 504 std::ostream& os); 505 friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, 506 const std::string& fn); 507 friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, 508 const char *fn); 509 509 510 510 DigraphWriter(DigraphWriter& other) … … 913 913 /// \relates DigraphWriter 914 914 template <typename Digraph> 915 DigraphWriter<Digraph> digraphWriter( std::ostream& os,916 const Digraph& digraph) {917 DigraphWriter<Digraph> tmp( os, digraph);915 DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, 916 std::ostream& os = std::cout) { 917 DigraphWriter<Digraph> tmp(digraph, os); 918 918 return tmp; 919 919 } … … 924 924 /// \relates DigraphWriter 925 925 template <typename Digraph> 926 DigraphWriter<Digraph> digraphWriter(const std::string& fn,927 const Digraph& digraph) {928 DigraphWriter<Digraph> tmp( fn, digraph);926 DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, 927 const std::string& fn) { 928 DigraphWriter<Digraph> tmp(digraph, fn); 929 929 return tmp; 930 930 } … … 935 935 /// \relates DigraphWriter 936 936 template <typename Digraph> 937 DigraphWriter<Digraph> digraphWriter(const char* fn,938 const Digraph& digraph) {939 DigraphWriter<Digraph> tmp( fn, digraph);937 DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, 938 const char* fn) { 939 DigraphWriter<Digraph> tmp(digraph, fn); 940 940 return tmp; 941 941 } … … 945 945 946 946 template <typename Graph> 947 GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph); 947 GraphWriter<Graph> graphWriter(const Graph& graph, 948 std::ostream& os = std::cout); 948 949 949 950 template <typename Graph> 950 GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph);951 GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn); 951 952 952 953 template <typename Graph> 953 GraphWriter<Graph> graphWriter(const char *fn, const Graph& graph);954 GraphWriter<Graph> graphWriter(const Graph& graph, const char *fn); 954 955 955 956 /// \ingroup lemon_io … … 1013 1014 /// Construct a directed graph writer, which writes to the given 1014 1015 /// output stream. 1015 GraphWriter( std::ostream& is, const Graph& graph)1016 : _os(& is), local_os(false), _graph(graph),1016 GraphWriter(const Graph& graph, std::ostream& os = std::cout) 1017 : _os(&os), local_os(false), _graph(graph), 1017 1018 _skip_nodes(false), _skip_edges(false) {} 1018 1019 … … 1021 1022 /// Construct a directed graph writer, which writes to the given 1022 1023 /// output file. 1023 GraphWriter(const std::string& fn, const Graph& graph)1024 GraphWriter(const Graph& graph, const std::string& fn) 1024 1025 : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), 1025 1026 _skip_nodes(false), _skip_edges(false) { … … 1031 1032 /// Construct a directed graph writer, which writes to the given 1032 1033 /// output file. 1033 GraphWriter(const char* fn, const Graph& graph)1034 GraphWriter(const Graph& graph, const char* fn) 1034 1035 : _os(new std::ofstream(fn)), local_os(true), _graph(graph), 1035 1036 _skip_nodes(false), _skip_edges(false) { … … 1061 1062 private: 1062 1063 1063 friend GraphWriter<Graph> graphWriter<>( std::ostream& os,1064 const Graph& graph);1065 friend GraphWriter<Graph> graphWriter<>(const std::string& fn,1066 const Graph& graph);1067 friend GraphWriter<Graph> graphWriter<>(const char *fn,1068 const Graph& graph);1064 friend GraphWriter<Graph> graphWriter<>(const Graph& graph, 1065 std::ostream& os); 1066 friend GraphWriter<Graph> graphWriter<>(const Graph& graph, 1067 const std::string& fn); 1068 friend GraphWriter<Graph> graphWriter<>(const Graph& graph, 1069 const char *fn); 1069 1070 1070 1071 GraphWriter(GraphWriter& other) … … 1519 1520 /// \relates GraphWriter 1520 1521 template <typename Graph> 1521 GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph) { 1522 GraphWriter<Graph> tmp(os, graph); 1522 GraphWriter<Graph> graphWriter(const Graph& graph, 1523 std::ostream& os = std::cout) { 1524 GraphWriter<Graph> tmp(graph, os); 1523 1525 return tmp; 1524 1526 } … … 1529 1531 /// \relates GraphWriter 1530 1532 template <typename Graph> 1531 GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph) {1532 GraphWriter<Graph> tmp( fn, graph);1533 GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) { 1534 GraphWriter<Graph> tmp(graph, fn); 1533 1535 return tmp; 1534 1536 } … … 1539 1541 /// \relates GraphWriter 1540 1542 template <typename Graph> 1541 GraphWriter<Graph> graphWriter(const char* fn, const Graph& graph) {1542 GraphWriter<Graph> tmp( fn, graph);1543 GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) { 1544 GraphWriter<Graph> tmp(graph, fn); 1543 1545 return tmp; 1544 1546 } -
lemon/lgf_writer.h
r293 r294 56 56 template <typename T> 57 57 bool operator<(const T&, const T&) { 58 throw DataFormatError("Label map is not comparable");58 throw FormatError("Label map is not comparable"); 59 59 } 60 60 … … 204 204 _map.find(str); 205 205 if (it == _map.end()) { 206 throw DataFormatError("Item not found");206 throw FormatError("Item not found"); 207 207 } 208 208 return it->second; … … 224 224 ::const_iterator it = _map.find(val); 225 225 if (it == _map.end()) { 226 throw DataFormatError("Item not found");226 throw FormatError("Item not found"); 227 227 } 228 228 return (_graph.direction(val) ? '+' : '-') + it->second; … … 463 463 DigraphWriter(const Digraph& digraph, const std::string& fn) 464 464 : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), 465 _skip_nodes(false), _skip_arcs(false) {} 465 _skip_nodes(false), _skip_arcs(false) { 466 if (!(*_os)) throw IoError("Cannot write file", fn); 467 } 466 468 467 469 /// \brief Constructor … … 471 473 DigraphWriter(const Digraph& digraph, const char* fn) 472 474 : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), 473 _skip_nodes(false), _skip_arcs(false) {} 475 _skip_nodes(false), _skip_arcs(false) { 476 if (!(*_os)) throw IoError("Cannot write file", fn); 477 } 474 478 475 479 /// \brief Destructor … … 1020 1024 GraphWriter(const Graph& graph, const std::string& fn) 1021 1025 : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), 1022 _skip_nodes(false), _skip_edges(false) {} 1026 _skip_nodes(false), _skip_edges(false) { 1027 if (!(*_os)) throw IoError("Cannot write file", fn); 1028 } 1023 1029 1024 1030 /// \brief Constructor … … 1028 1034 GraphWriter(const Graph& graph, const char* fn) 1029 1035 : _os(new std::ofstream(fn)), local_os(true), _graph(graph), 1030 _skip_nodes(false), _skip_edges(false) {} 1036 _skip_nodes(false), _skip_edges(false) { 1037 if (!(*_os)) throw IoError("Cannot write file", fn); 1038 } 1031 1039 1032 1040 /// \brief Destructor … … 1579 1587 /// Construct a section writer, which writes into the given file. 1580 1588 SectionWriter(const std::string& fn) 1581 : _os(new std::ofstream(fn.c_str())), local_os(true) {} 1589 : _os(new std::ofstream(fn.c_str())), local_os(true) { 1590 if (!(*_os)) throw IoError("Cannot write file", fn); 1591 } 1582 1592 1583 1593 /// \brief Constructor … … 1585 1595 /// Construct a section writer, which writes into the given file. 1586 1596 SectionWriter(const char* fn) 1587 : _os(new std::ofstream(fn)), local_os(true) {} 1597 : _os(new std::ofstream(fn)), local_os(true) { 1598 if (!(*_os)) throw IoError("Cannot write file", fn); 1599 } 1588 1600 1589 1601 /// \brief Destructor
Note: See TracChangeset
for help on using the changeset viewer.