Changes in lemon/lgf_reader.h [236:da953e387d31:291:d901321d6555] in lemon-1.0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/lgf_reader.h
r236 r291 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); … … 461 463 std::istream* _is; 462 464 bool local_is; 465 std::string _filename; 463 466 464 467 Digraph& _digraph; … … 510 513 /// file. 511 514 DigraphReader(const std::string& fn, Digraph& digraph) 512 : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph), 515 : _is(new std::ifstream(fn.c_str())), local_is(true), 516 _filename(fn), _digraph(digraph), 513 517 _use_nodes(false), _use_arcs(false), 514 _skip_nodes(false), _skip_arcs(false) {} 518 _skip_nodes(false), _skip_arcs(false) { 519 if (!(*_is)) throw IoError("Cannot open file", fn); 520 } 515 521 516 522 /// \brief Constructor … … 519 525 /// file. 520 526 DigraphReader(const char* fn, Digraph& digraph) 521 : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph), 527 : _is(new std::ifstream(fn)), local_is(true), 528 _filename(fn), _digraph(digraph), 522 529 _use_nodes(false), _use_arcs(false), 523 _skip_nodes(false), _skip_arcs(false) {} 530 _skip_nodes(false), _skip_arcs(false) { 531 if (!(*_is)) throw IoError("Cannot open file", fn); 532 } 524 533 525 534 /// \brief Destructor … … 846 855 if (readSuccess() && line) line.putback(c); 847 856 if (!_node_maps.empty()) 848 throw DataFormatError("Cannot find map names");857 throw FormatError("Cannot find map names"); 849 858 return; 850 859 } … … 860 869 std::ostringstream msg; 861 870 msg << "Multiple occurence of node map: " << map; 862 throw DataFormatError(msg.str().c_str());871 throw FormatError(msg.str()); 863 872 } 864 873 maps.insert(std::make_pair(map, index)); … … 871 880 if (jt == maps.end()) { 872 881 std::ostringstream msg; 873 msg << "Map not found in file: " << _node_maps[i].first;874 throw DataFormatError(msg.str().c_str());882 msg << "Map not found: " << _node_maps[i].first; 883 throw FormatError(msg.str()); 875 884 } 876 885 map_index[i] = jt->second; … … 896 905 std::ostringstream msg; 897 906 msg << "Column not found (" << i + 1 << ")"; 898 throw DataFormatError(msg.str().c_str());907 throw FormatError(msg.str()); 899 908 } 900 909 } 901 910 if (line >> std::ws >> c) 902 throw DataFormatError("Extra character onthe end of line");911 throw FormatError("Extra character at the end of line"); 903 912 904 913 Node n; … … 909 918 } else { 910 919 if (label_index == -1) 911 throw DataFormatError("Label map not found in file");920 throw FormatError("Label map not found"); 912 921 typename std::map<std::string, Node>::iterator it = 913 922 _node_index.find(tokens[label_index]); … … 915 924 std::ostringstream msg; 916 925 msg << "Node with label not found: " << tokens[label_index]; 917 throw DataFormatError(msg.str().c_str());926 throw FormatError(msg.str()); 918 927 } 919 928 n = it->second; … … 939 948 if (readSuccess() && line) line.putback(c); 940 949 if (!_arc_maps.empty()) 941 throw DataFormatError("Cannot find map names");950 throw FormatError("Cannot find map names"); 942 951 return; 943 952 } … … 953 962 std::ostringstream msg; 954 963 msg << "Multiple occurence of arc map: " << map; 955 throw DataFormatError(msg.str().c_str());964 throw FormatError(msg.str()); 956 965 } 957 966 maps.insert(std::make_pair(map, index)); … … 964 973 if (jt == maps.end()) { 965 974 std::ostringstream msg; 966 msg << "Map not found in file: " << _arc_maps[i].first;967 throw DataFormatError(msg.str().c_str());975 msg << "Map not found: " << _arc_maps[i].first; 976 throw FormatError(msg.str()); 968 977 } 969 978 map_index[i] = jt->second; … … 988 997 989 998 if (!_reader_bits::readToken(line, source_token)) 990 throw DataFormatError("Source not found");999 throw FormatError("Source not found"); 991 1000 992 1001 if (!_reader_bits::readToken(line, target_token)) 993 throw DataFormatError("Target not found");1002 throw FormatError("Target not found"); 994 1003 995 1004 std::vector<std::string> tokens(map_num); … … 998 1007 std::ostringstream msg; 999 1008 msg << "Column not found (" << i + 1 << ")"; 1000 throw DataFormatError(msg.str().c_str());1009 throw FormatError(msg.str()); 1001 1010 } 1002 1011 } 1003 1012 if (line >> std::ws >> c) 1004 throw DataFormatError("Extra character onthe end of line");1013 throw FormatError("Extra character at the end of line"); 1005 1014 1006 1015 Arc a; … … 1013 1022 std::ostringstream msg; 1014 1023 msg << "Item not found: " << source_token; 1015 throw DataFormatError(msg.str().c_str());1024 throw FormatError(msg.str()); 1016 1025 } 1017 1026 Node source = it->second; … … 1021 1030 std::ostringstream msg; 1022 1031 msg << "Item not found: " << target_token; 1023 throw DataFormatError(msg.str().c_str());1032 throw FormatError(msg.str()); 1024 1033 } 1025 1034 Node target = it->second; … … 1030 1039 } else { 1031 1040 if (label_index == -1) 1032 throw DataFormatError("Label map not found in file");1041 throw FormatError("Label map not found"); 1033 1042 typename std::map<std::string, Arc>::iterator it = 1034 1043 _arc_index.find(tokens[label_index]); … … 1036 1045 std::ostringstream msg; 1037 1046 msg << "Arc with label not found: " << tokens[label_index]; 1038 throw DataFormatError(msg.str().c_str());1047 throw FormatError(msg.str()); 1039 1048 } 1040 1049 a = it->second; … … 1061 1070 std::string attr, token; 1062 1071 if (!_reader_bits::readToken(line, attr)) 1063 throw DataFormatError("Attribute name not found");1072 throw FormatError("Attribute name not found"); 1064 1073 if (!_reader_bits::readToken(line, token)) 1065 throw DataFormatError("Attribute value not found");1074 throw FormatError("Attribute value not found"); 1066 1075 if (line >> c) 1067 throw DataFormatError("Extra character onthe end of line");1076 throw FormatError("Extra character at the end of line"); 1068 1077 1069 1078 { … … 1071 1080 if (it != read_attr.end()) { 1072 1081 std::ostringstream msg; 1073 msg << "Multiple occurence of attribute " << attr;1074 throw DataFormatError(msg.str().c_str());1082 msg << "Multiple occurence of attribute: " << attr; 1083 throw FormatError(msg.str()); 1075 1084 } 1076 1085 read_attr.insert(attr); … … 1093 1102 if (read_attr.find(it->first) == read_attr.end()) { 1094 1103 std::ostringstream msg; 1095 msg << "Attribute not found in file: " << it->first;1096 throw DataFormatError(msg.str().c_str());1104 msg << "Attribute not found: " << it->first; 1105 throw FormatError(msg.str()); 1097 1106 } 1098 1107 } … … 1109 1118 void run() { 1110 1119 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); 1111 if (!*_is) {1112 throw DataFormatError("Cannot find file");1113 }1114 1120 1115 1121 bool nodes_done = _skip_nodes; … … 1130 1136 1131 1137 if (line >> c) 1132 throw DataFormatError("Extra character onthe end of line");1138 throw FormatError("Extra character at the end of line"); 1133 1139 1134 1140 if (section == "nodes" && !nodes_done) { … … 1152 1158 skipSection(); 1153 1159 } 1154 } catch ( DataFormatError& error) {1160 } catch (FormatError& error) { 1155 1161 error.line(line_num); 1162 error.file(_filename); 1156 1163 throw; 1157 1164 } … … 1159 1166 1160 1167 if (!nodes_done) { 1161 throw DataFormatError("Section @nodes not found");1168 throw FormatError("Section @nodes not found"); 1162 1169 } 1163 1170 1164 1171 if (!arcs_done) { 1165 throw DataFormatError("Section @arcs not found");1172 throw FormatError("Section @arcs not found"); 1166 1173 } 1167 1174 1168 1175 if (!attributes_done && !_attributes.empty()) { 1169 throw DataFormatError("Section @attributes not found");1176 throw FormatError("Section @attributes not found"); 1170 1177 } 1171 1178 … … 1245 1252 std::istream* _is; 1246 1253 bool local_is; 1254 std::string _filename; 1247 1255 1248 1256 Graph& _graph; … … 1294 1302 /// file. 1295 1303 GraphReader(const std::string& fn, Graph& graph) 1296 : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph), 1304 : _is(new std::ifstream(fn.c_str())), local_is(true), 1305 _filename(fn), _graph(graph), 1297 1306 _use_nodes(false), _use_edges(false), 1298 _skip_nodes(false), _skip_edges(false) {} 1307 _skip_nodes(false), _skip_edges(false) { 1308 if (!(*_is)) throw IoError("Cannot open file", fn); 1309 } 1299 1310 1300 1311 /// \brief Constructor … … 1303 1314 /// file. 1304 1315 GraphReader(const char* fn, Graph& graph) 1305 : _is(new std::ifstream(fn)), local_is(true), _graph(graph), 1316 : _is(new std::ifstream(fn)), local_is(true), 1317 _filename(fn), _graph(graph), 1306 1318 _use_nodes(false), _use_edges(false), 1307 _skip_nodes(false), _skip_edges(false) {} 1319 _skip_nodes(false), _skip_edges(false) { 1320 if (!(*_is)) throw IoError("Cannot open file", fn); 1321 } 1308 1322 1309 1323 /// \brief Destructor … … 1674 1688 if (readSuccess() && line) line.putback(c); 1675 1689 if (!_node_maps.empty()) 1676 throw DataFormatError("Cannot find map names");1690 throw FormatError("Cannot find map names"); 1677 1691 return; 1678 1692 } … … 1688 1702 std::ostringstream msg; 1689 1703 msg << "Multiple occurence of node map: " << map; 1690 throw DataFormatError(msg.str().c_str());1704 throw FormatError(msg.str()); 1691 1705 } 1692 1706 maps.insert(std::make_pair(map, index)); … … 1699 1713 if (jt == maps.end()) { 1700 1714 std::ostringstream msg; 1701 msg << "Map not found in file: " << _node_maps[i].first;1702 throw DataFormatError(msg.str().c_str());1715 msg << "Map not found: " << _node_maps[i].first; 1716 throw FormatError(msg.str()); 1703 1717 } 1704 1718 map_index[i] = jt->second; … … 1724 1738 std::ostringstream msg; 1725 1739 msg << "Column not found (" << i + 1 << ")"; 1726 throw DataFormatError(msg.str().c_str());1740 throw FormatError(msg.str()); 1727 1741 } 1728 1742 } 1729 1743 if (line >> std::ws >> c) 1730 throw DataFormatError("Extra character onthe end of line");1744 throw FormatError("Extra character at the end of line"); 1731 1745 1732 1746 Node n; … … 1737 1751 } else { 1738 1752 if (label_index == -1) 1739 throw DataFormatError("Label map not found in file");1753 throw FormatError("Label map not found"); 1740 1754 typename std::map<std::string, Node>::iterator it = 1741 1755 _node_index.find(tokens[label_index]); … … 1743 1757 std::ostringstream msg; 1744 1758 msg << "Node with label not found: " << tokens[label_index]; 1745 throw DataFormatError(msg.str().c_str());1759 throw FormatError(msg.str()); 1746 1760 } 1747 1761 n = it->second; … … 1767 1781 if (readSuccess() && line) line.putback(c); 1768 1782 if (!_edge_maps.empty()) 1769 throw DataFormatError("Cannot find map names");1783 throw FormatError("Cannot find map names"); 1770 1784 return; 1771 1785 } … … 1781 1795 std::ostringstream msg; 1782 1796 msg << "Multiple occurence of edge map: " << map; 1783 throw DataFormatError(msg.str().c_str());1797 throw FormatError(msg.str()); 1784 1798 } 1785 1799 maps.insert(std::make_pair(map, index)); … … 1792 1806 if (jt == maps.end()) { 1793 1807 std::ostringstream msg; 1794 msg << "Map not found in file: " << _edge_maps[i].first;1795 throw DataFormatError(msg.str().c_str());1808 msg << "Map not found: " << _edge_maps[i].first; 1809 throw FormatError(msg.str()); 1796 1810 } 1797 1811 map_index[i] = jt->second; … … 1816 1830 1817 1831 if (!_reader_bits::readToken(line, source_token)) 1818 throw DataFormatError("Node u not found");1832 throw FormatError("Node u not found"); 1819 1833 1820 1834 if (!_reader_bits::readToken(line, target_token)) 1821 throw DataFormatError("Node v not found");1835 throw FormatError("Node v not found"); 1822 1836 1823 1837 std::vector<std::string> tokens(map_num); … … 1826 1840 std::ostringstream msg; 1827 1841 msg << "Column not found (" << i + 1 << ")"; 1828 throw DataFormatError(msg.str().c_str());1842 throw FormatError(msg.str()); 1829 1843 } 1830 1844 } 1831 1845 if (line >> std::ws >> c) 1832 throw DataFormatError("Extra character onthe end of line");1846 throw FormatError("Extra character at the end of line"); 1833 1847 1834 1848 Edge e; … … 1841 1855 std::ostringstream msg; 1842 1856 msg << "Item not found: " << source_token; 1843 throw DataFormatError(msg.str().c_str());1857 throw FormatError(msg.str()); 1844 1858 } 1845 1859 Node source = it->second; … … 1849 1863 std::ostringstream msg; 1850 1864 msg << "Item not found: " << target_token; 1851 throw DataFormatError(msg.str().c_str());1865 throw FormatError(msg.str()); 1852 1866 } 1853 1867 Node target = it->second; … … 1858 1872 } else { 1859 1873 if (label_index == -1) 1860 throw DataFormatError("Label map not found in file");1874 throw FormatError("Label map not found"); 1861 1875 typename std::map<std::string, Edge>::iterator it = 1862 1876 _edge_index.find(tokens[label_index]); … … 1864 1878 std::ostringstream msg; 1865 1879 msg << "Edge with label not found: " << tokens[label_index]; 1866 throw DataFormatError(msg.str().c_str());1880 throw FormatError(msg.str()); 1867 1881 } 1868 1882 e = it->second; … … 1889 1903 std::string attr, token; 1890 1904 if (!_reader_bits::readToken(line, attr)) 1891 throw DataFormatError("Attribute name not found");1905 throw FormatError("Attribute name not found"); 1892 1906 if (!_reader_bits::readToken(line, token)) 1893 throw DataFormatError("Attribute value not found");1907 throw FormatError("Attribute value not found"); 1894 1908 if (line >> c) 1895 throw DataFormatError("Extra character onthe end of line");1909 throw FormatError("Extra character at the end of line"); 1896 1910 1897 1911 { … … 1899 1913 if (it != read_attr.end()) { 1900 1914 std::ostringstream msg; 1901 msg << "Multiple occurence of attribute " << attr;1902 throw DataFormatError(msg.str().c_str());1915 msg << "Multiple occurence of attribute: " << attr; 1916 throw FormatError(msg.str()); 1903 1917 } 1904 1918 read_attr.insert(attr); … … 1921 1935 if (read_attr.find(it->first) == read_attr.end()) { 1922 1936 std::ostringstream msg; 1923 msg << "Attribute not found in file: " << it->first;1924 throw DataFormatError(msg.str().c_str());1937 msg << "Attribute not found: " << it->first; 1938 throw FormatError(msg.str()); 1925 1939 } 1926 1940 } … … 1956 1970 1957 1971 if (line >> c) 1958 throw DataFormatError("Extra character onthe end of line");1972 throw FormatError("Extra character at the end of line"); 1959 1973 1960 1974 if (section == "nodes" && !nodes_done) { … … 1978 1992 skipSection(); 1979 1993 } 1980 } catch ( DataFormatError& error) {1994 } catch (FormatError& error) { 1981 1995 error.line(line_num); 1996 error.file(_filename); 1982 1997 throw; 1983 1998 } … … 1985 2000 1986 2001 if (!nodes_done) { 1987 throw DataFormatError("Section @nodes not found");2002 throw FormatError("Section @nodes not found"); 1988 2003 } 1989 2004 1990 2005 if (!edges_done) { 1991 throw DataFormatError("Section @edges not found");2006 throw FormatError("Section @edges not found"); 1992 2007 } 1993 2008 1994 2009 if (!attributes_done && !_attributes.empty()) { 1995 throw DataFormatError("Section @attributes not found");2010 throw FormatError("Section @attributes not found"); 1996 2011 } 1997 2012 … … 2055 2070 std::istream* _is; 2056 2071 bool local_is; 2072 std::string _filename; 2057 2073 2058 2074 typedef std::map<std::string, _reader_bits::Section*> Sections; … … 2075 2091 /// Construct a section reader, which reads from the given file. 2076 2092 SectionReader(const std::string& fn) 2077 : _is(new std::ifstream(fn.c_str())), local_is(true) {} 2093 : _is(new std::ifstream(fn.c_str())), local_is(true), 2094 _filename(fn) { 2095 if (!(*_is)) throw IoError("Cannot open file", fn); 2096 } 2078 2097 2079 2098 /// \brief Constructor … … 2081 2100 /// Construct a section reader, which reads from the given file. 2082 2101 SectionReader(const char* fn) 2083 : _is(new std::ifstream(fn)), local_is(true) {} 2102 : _is(new std::ifstream(fn)), local_is(true), 2103 _filename(fn) { 2104 if (!(*_is)) throw IoError("Cannot open file", fn); 2105 } 2084 2106 2085 2107 /// \brief Destructor … … 2237 2259 2238 2260 if (line >> c) 2239 throw DataFormatError("Extra character onthe end of line");2261 throw FormatError("Extra character at the end of line"); 2240 2262 2241 2263 if (extra_sections.find(section) != extra_sections.end()) { 2242 2264 std::ostringstream msg; 2243 msg << "Multiple occurence of section " << section;2244 throw DataFormatError(msg.str().c_str());2265 msg << "Multiple occurence of section: " << section; 2266 throw FormatError(msg.str()); 2245 2267 } 2246 2268 Sections::iterator it = _sections.find(section); … … 2251 2273 readLine(); 2252 2274 skipSection(); 2253 } catch ( DataFormatError& error) {2275 } catch (FormatError& error) { 2254 2276 error.line(line_num); 2277 error.file(_filename); 2255 2278 throw; 2256 2279 } … … 2261 2284 std::ostringstream os; 2262 2285 os << "Cannot find section: " << it->first; 2263 throw DataFormatError(os.str().c_str());2286 throw FormatError(os.str()); 2264 2287 } 2265 2288 } … … 2361 2384 /// file. 2362 2385 LgfContents(const std::string& fn) 2363 : _is(new std::ifstream(fn.c_str())), local_is(true) {} 2386 : _is(new std::ifstream(fn.c_str())), local_is(true) { 2387 if (!(*_is)) throw IoError("Cannot open file", fn); 2388 } 2364 2389 2365 2390 /// \brief Constructor … … 2368 2393 /// file. 2369 2394 LgfContents(const char* fn) 2370 : _is(new std::ifstream(fn)), local_is(true) {} 2395 : _is(new std::ifstream(fn)), local_is(true) { 2396 if (!(*_is)) throw IoError("Cannot open file", fn); 2397 } 2371 2398 2372 2399 /// \brief Destructor
Note: See TracChangeset
for help on using the changeset viewer.