diff --git a/lemon/lgf_reader.h b/lemon/lgf_reader.h --- a/lemon/lgf_reader.h +++ b/lemon/lgf_reader.h @@ -48,11 +48,13 @@ Value operator()(const std::string& str) { std::istringstream is(str); Value value; - is >> value; + if (!(is >> value)) { + throw FormatError("Cannot read token"); + } char c; if (is >> std::ws >> c) { - throw DataFormatError("Remaining characters in token"); + throw FormatError("Remaining characters in token"); } return value; } @@ -166,7 +168,7 @@ if (it == _map.end()) { std::ostringstream msg; msg << "Item not found: " << str; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } return it->second; } @@ -184,12 +186,12 @@ typename Graph::Arc operator()(const std::string& str) { if (str.empty() || (str[0] != '+' && str[0] != '-')) { - throw DataFormatError("Item must start with '+' or '-'"); + throw FormatError("Item must start with '+' or '-'"); } typename std::map ::const_iterator it = _map.find(str.substr(1)); if (it == _map.end()) { - throw DataFormatError("Item not found"); + throw FormatError("Item not found"); } return _graph.direct(it->second, str[0] == '+'); } @@ -235,7 +237,7 @@ inline char readEscape(std::istream& is) { char c; if (!is.get(c)) - throw DataFormatError("Escape format error"); + throw FormatError("Escape format error"); switch (c) { case '\\': @@ -264,7 +266,7 @@ { int code; if (!is.get(c) || !isHex(c)) - throw DataFormatError("Escape format error"); + throw FormatError("Escape format error"); else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); else code = code * 16 + valueHex(c); return code; @@ -273,7 +275,7 @@ { int code; if (!isOct(c)) - throw DataFormatError("Escape format error"); + throw FormatError("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)) @@ -300,7 +302,7 @@ os << c; } if (!is) - throw DataFormatError("Quoted format error"); + throw FormatError("Quoted format error"); } else { is.putback(c); while (is.get(c) && !isWhiteSpace(c)) { @@ -460,6 +462,7 @@ std::istream* _is; bool local_is; + std::string _filename; Digraph& _digraph; @@ -509,18 +512,24 @@ /// Construct a directed graph reader, which reads from the given /// file. DigraphReader(const std::string& fn, Digraph& digraph) - : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph), + : _is(new std::ifstream(fn.c_str())), local_is(true), + _filename(fn), _digraph(digraph), _use_nodes(false), _use_arcs(false), - _skip_nodes(false), _skip_arcs(false) {} + _skip_nodes(false), _skip_arcs(false) { + if (!(*_is)) throw IoError(fn, "Cannot open file"); + } /// \brief Constructor /// /// Construct a directed graph reader, which reads from the given /// file. DigraphReader(const char* fn, Digraph& digraph) - : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph), + : _is(new std::ifstream(fn)), local_is(true), + _filename(fn), _digraph(digraph), _use_nodes(false), _use_arcs(false), - _skip_nodes(false), _skip_arcs(false) {} + _skip_nodes(false), _skip_arcs(false) { + if (!(*_is)) throw IoError(fn, "Cannot open file"); + } /// \brief Destructor ~DigraphReader() { @@ -845,7 +854,7 @@ if (!readLine() || !(line >> c) || c == '@') { if (readSuccess() && line) line.putback(c); if (!_node_maps.empty()) - throw DataFormatError("Cannot find map names"); + throw FormatError("Cannot find map names"); return; } line.putback(c); @@ -859,7 +868,7 @@ if (maps.find(map) != maps.end()) { std::ostringstream msg; msg << "Multiple occurence of node map: " << map; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } maps.insert(std::make_pair(map, index)); ++index; @@ -871,7 +880,7 @@ if (jt == maps.end()) { std::ostringstream msg; msg << "Map not found in file: " << _node_maps[i].first; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } map_index[i] = jt->second; } @@ -895,11 +904,11 @@ if (!_reader_bits::readToken(line, tokens[i])) { std::ostringstream msg; msg << "Column not found (" << i + 1 << ")"; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } } if (line >> std::ws >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); Node n; if (!_use_nodes) { @@ -908,13 +917,13 @@ _node_index.insert(std::make_pair(tokens[label_index], n)); } else { if (label_index == -1) - throw DataFormatError("Label map not found in file"); + throw FormatError("Label map not found in file"); typename std::map::iterator it = _node_index.find(tokens[label_index]); if (it == _node_index.end()) { std::ostringstream msg; msg << "Node with label not found: " << tokens[label_index]; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } n = it->second; } @@ -938,7 +947,7 @@ if (!readLine() || !(line >> c) || c == '@') { if (readSuccess() && line) line.putback(c); if (!_arc_maps.empty()) - throw DataFormatError("Cannot find map names"); + throw FormatError("Cannot find map names"); return; } line.putback(c); @@ -952,7 +961,7 @@ if (maps.find(map) != maps.end()) { std::ostringstream msg; msg << "Multiple occurence of arc map: " << map; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } maps.insert(std::make_pair(map, index)); ++index; @@ -964,7 +973,7 @@ if (jt == maps.end()) { std::ostringstream msg; msg << "Map not found in file: " << _arc_maps[i].first; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } map_index[i] = jt->second; } @@ -987,21 +996,21 @@ std::string target_token; if (!_reader_bits::readToken(line, source_token)) - throw DataFormatError("Source not found"); + throw FormatError("Source not found"); if (!_reader_bits::readToken(line, target_token)) - throw DataFormatError("Target not found"); + throw FormatError("Target not found"); std::vector tokens(map_num); for (int i = 0; i < map_num; ++i) { if (!_reader_bits::readToken(line, tokens[i])) { std::ostringstream msg; msg << "Column not found (" << i + 1 << ")"; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } } if (line >> std::ws >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); Arc a; if (!_use_arcs) { @@ -1012,7 +1021,7 @@ if (it == _node_index.end()) { std::ostringstream msg; msg << "Item not found: " << source_token; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } Node source = it->second; @@ -1020,7 +1029,7 @@ if (it == _node_index.end()) { std::ostringstream msg; msg << "Item not found: " << target_token; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } Node target = it->second; @@ -1029,13 +1038,13 @@ _arc_index.insert(std::make_pair(tokens[label_index], a)); } else { if (label_index == -1) - throw DataFormatError("Label map not found in file"); + throw FormatError("Label map not found in file"); typename std::map::iterator it = _arc_index.find(tokens[label_index]); if (it == _arc_index.end()) { std::ostringstream msg; msg << "Arc with label not found: " << tokens[label_index]; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } a = it->second; } @@ -1060,18 +1069,18 @@ std::string attr, token; if (!_reader_bits::readToken(line, attr)) - throw DataFormatError("Attribute name not found"); + throw FormatError("Attribute name not found"); if (!_reader_bits::readToken(line, token)) - throw DataFormatError("Attribute value not found"); + throw FormatError("Attribute value not found"); if (line >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); { std::set::iterator it = read_attr.find(attr); if (it != read_attr.end()) { std::ostringstream msg; msg << "Multiple occurence of attribute " << attr; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } read_attr.insert(attr); } @@ -1093,7 +1102,7 @@ if (read_attr.find(it->first) == read_attr.end()) { std::ostringstream msg; msg << "Attribute not found in file: " << it->first; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } } } @@ -1109,7 +1118,7 @@ void run() { LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); if (!*_is) { - throw DataFormatError("Cannot find file"); + throw FormatError("Cannot find file"); } bool nodes_done = _skip_nodes; @@ -1129,7 +1138,7 @@ _reader_bits::readToken(line, caption); if (line >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); if (section == "nodes" && !nodes_done) { if (_nodes_caption.empty() || _nodes_caption == caption) { @@ -1151,22 +1160,23 @@ readLine(); skipSection(); } - } catch (DataFormatError& error) { + } catch (FormatError& error) { error.line(line_num); + error.file(_filename); throw; } } if (!nodes_done) { - throw DataFormatError("Section @nodes not found"); + throw FormatError("Section @nodes not found"); } if (!arcs_done) { - throw DataFormatError("Section @arcs not found"); + throw FormatError("Section @arcs not found"); } if (!attributes_done && !_attributes.empty()) { - throw DataFormatError("Section @attributes not found"); + throw FormatError("Section @attributes not found"); } } @@ -1244,6 +1254,7 @@ std::istream* _is; bool local_is; + std::string _filename; Graph& _graph; @@ -1293,18 +1304,24 @@ /// Construct an undirected graph reader, which reads from the given /// file. GraphReader(const std::string& fn, Graph& graph) - : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph), + : _is(new std::ifstream(fn.c_str())), local_is(true), + _filename(fn), _graph(graph), _use_nodes(false), _use_edges(false), - _skip_nodes(false), _skip_edges(false) {} + _skip_nodes(false), _skip_edges(false) { + if (!(*_is)) throw IoError(fn, "Cannot open file"); + } /// \brief Constructor /// /// Construct an undirected graph reader, which reads from the given /// file. GraphReader(const char* fn, Graph& graph) - : _is(new std::ifstream(fn)), local_is(true), _graph(graph), + : _is(new std::ifstream(fn)), local_is(true), + _filename(fn), _graph(graph), _use_nodes(false), _use_edges(false), - _skip_nodes(false), _skip_edges(false) {} + _skip_nodes(false), _skip_edges(false) { + if (!(*_is)) throw IoError(fn, "Cannot open file"); + } /// \brief Destructor ~GraphReader() { @@ -1673,7 +1690,7 @@ if (!readLine() || !(line >> c) || c == '@') { if (readSuccess() && line) line.putback(c); if (!_node_maps.empty()) - throw DataFormatError("Cannot find map names"); + throw FormatError("Cannot find map names"); return; } line.putback(c); @@ -1687,7 +1704,7 @@ if (maps.find(map) != maps.end()) { std::ostringstream msg; msg << "Multiple occurence of node map: " << map; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } maps.insert(std::make_pair(map, index)); ++index; @@ -1699,7 +1716,7 @@ if (jt == maps.end()) { std::ostringstream msg; msg << "Map not found in file: " << _node_maps[i].first; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } map_index[i] = jt->second; } @@ -1723,11 +1740,11 @@ if (!_reader_bits::readToken(line, tokens[i])) { std::ostringstream msg; msg << "Column not found (" << i + 1 << ")"; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } } if (line >> std::ws >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); Node n; if (!_use_nodes) { @@ -1736,13 +1753,13 @@ _node_index.insert(std::make_pair(tokens[label_index], n)); } else { if (label_index == -1) - throw DataFormatError("Label map not found in file"); + throw FormatError("Label map not found in file"); typename std::map::iterator it = _node_index.find(tokens[label_index]); if (it == _node_index.end()) { std::ostringstream msg; msg << "Node with label not found: " << tokens[label_index]; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } n = it->second; } @@ -1766,7 +1783,7 @@ if (!readLine() || !(line >> c) || c == '@') { if (readSuccess() && line) line.putback(c); if (!_edge_maps.empty()) - throw DataFormatError("Cannot find map names"); + throw FormatError("Cannot find map names"); return; } line.putback(c); @@ -1780,7 +1797,7 @@ if (maps.find(map) != maps.end()) { std::ostringstream msg; msg << "Multiple occurence of edge map: " << map; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } maps.insert(std::make_pair(map, index)); ++index; @@ -1792,7 +1809,7 @@ if (jt == maps.end()) { std::ostringstream msg; msg << "Map not found in file: " << _edge_maps[i].first; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } map_index[i] = jt->second; } @@ -1815,21 +1832,21 @@ std::string target_token; if (!_reader_bits::readToken(line, source_token)) - throw DataFormatError("Node u not found"); + throw FormatError("Node u not found"); if (!_reader_bits::readToken(line, target_token)) - throw DataFormatError("Node v not found"); + throw FormatError("Node v not found"); std::vector tokens(map_num); for (int i = 0; i < map_num; ++i) { if (!_reader_bits::readToken(line, tokens[i])) { std::ostringstream msg; msg << "Column not found (" << i + 1 << ")"; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } } if (line >> std::ws >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); Edge e; if (!_use_edges) { @@ -1840,7 +1857,7 @@ if (it == _node_index.end()) { std::ostringstream msg; msg << "Item not found: " << source_token; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } Node source = it->second; @@ -1848,7 +1865,7 @@ if (it == _node_index.end()) { std::ostringstream msg; msg << "Item not found: " << target_token; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } Node target = it->second; @@ -1857,13 +1874,13 @@ _edge_index.insert(std::make_pair(tokens[label_index], e)); } else { if (label_index == -1) - throw DataFormatError("Label map not found in file"); + throw FormatError("Label map not found in file"); typename std::map::iterator it = _edge_index.find(tokens[label_index]); if (it == _edge_index.end()) { std::ostringstream msg; msg << "Edge with label not found: " << tokens[label_index]; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } e = it->second; } @@ -1888,18 +1905,18 @@ std::string attr, token; if (!_reader_bits::readToken(line, attr)) - throw DataFormatError("Attribute name not found"); + throw FormatError("Attribute name not found"); if (!_reader_bits::readToken(line, token)) - throw DataFormatError("Attribute value not found"); + throw FormatError("Attribute value not found"); if (line >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); { std::set::iterator it = read_attr.find(attr); if (it != read_attr.end()) { std::ostringstream msg; msg << "Multiple occurence of attribute " << attr; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } read_attr.insert(attr); } @@ -1921,7 +1938,7 @@ if (read_attr.find(it->first) == read_attr.end()) { std::ostringstream msg; msg << "Attribute not found in file: " << it->first; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } } } @@ -1955,7 +1972,7 @@ _reader_bits::readToken(line, caption); if (line >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); if (section == "nodes" && !nodes_done) { if (_nodes_caption.empty() || _nodes_caption == caption) { @@ -1977,22 +1994,23 @@ readLine(); skipSection(); } - } catch (DataFormatError& error) { + } catch (FormatError& error) { error.line(line_num); + error.file(_filename); throw; } } if (!nodes_done) { - throw DataFormatError("Section @nodes not found"); + throw FormatError("Section @nodes not found"); } if (!edges_done) { - throw DataFormatError("Section @edges not found"); + throw FormatError("Section @edges not found"); } if (!attributes_done && !_attributes.empty()) { - throw DataFormatError("Section @attributes not found"); + throw FormatError("Section @attributes not found"); } } @@ -2054,6 +2072,7 @@ std::istream* _is; bool local_is; + std::string _filename; typedef std::map Sections; Sections _sections; @@ -2074,13 +2093,19 @@ /// /// Construct a section reader, which reads from the given file. SectionReader(const std::string& fn) - : _is(new std::ifstream(fn.c_str())), local_is(true) {} + : _is(new std::ifstream(fn.c_str())), local_is(true), + _filename(fn) { + if (!(*_is)) throw IoError(fn, "Cannot open file"); + } /// \brief Constructor /// /// Construct a section reader, which reads from the given file. SectionReader(const char* fn) - : _is(new std::ifstream(fn)), local_is(true) {} + : _is(new std::ifstream(fn)), local_is(true), + _filename(fn) { + if (!(*_is)) throw IoError(fn, "Cannot open file"); + } /// \brief Destructor ~SectionReader() { @@ -2236,12 +2261,12 @@ _reader_bits::readToken(line, caption); if (line >> c) - throw DataFormatError("Extra character on the end of line"); + throw FormatError("Extra character on the end of line"); if (extra_sections.find(section) != extra_sections.end()) { std::ostringstream msg; msg << "Multiple occurence of section " << section; - throw DataFormatError(msg.str().c_str()); + throw FormatError(msg.str()); } Sections::iterator it = _sections.find(section); if (it != _sections.end()) { @@ -2250,8 +2275,9 @@ } readLine(); skipSection(); - } catch (DataFormatError& error) { + } catch (FormatError& error) { error.line(line_num); + error.file(_filename); throw; } } @@ -2260,7 +2286,7 @@ if (extra_sections.find(it->first) == extra_sections.end()) { std::ostringstream os; os << "Cannot find section: " << it->first; - throw DataFormatError(os.str().c_str()); + throw FormatError(os.str()); } } } @@ -2360,14 +2386,18 @@ /// Construct an \e LGF contents reader, which reads from the given /// file. LgfContents(const std::string& fn) - : _is(new std::ifstream(fn.c_str())), local_is(true) {} + : _is(new std::ifstream(fn.c_str())), local_is(true) { + if (!(*_is)) throw IoError(fn, "Cannot open file"); + } /// \brief Constructor /// /// Construct an \e LGF contents reader, which reads from the given /// file. LgfContents(const char* fn) - : _is(new std::ifstream(fn)), local_is(true) {} + : _is(new std::ifstream(fn)), local_is(true) { + if (!(*_is)) throw IoError(fn, "Cannot open file"); + } /// \brief Destructor ~LgfContents() {