lemon/lgf_reader.h
changeset 290 f6899946c1ac
parent 236 da953e387d31
child 291 d901321d6555
     1.1 --- a/lemon/lgf_reader.h	Sat Sep 27 14:33:28 2008 +0200
     1.2 +++ b/lemon/lgf_reader.h	Tue Sep 30 20:53:18 2008 +0200
     1.3 @@ -48,11 +48,13 @@
     1.4        Value operator()(const std::string& str) {
     1.5          std::istringstream is(str);
     1.6          Value value;
     1.7 -        is >> value;
     1.8 +        if (!(is >> value)) {
     1.9 +          throw FormatError("Cannot read token");
    1.10 +        }
    1.11  
    1.12          char c;
    1.13          if (is >> std::ws >> c) {
    1.14 -          throw DataFormatError("Remaining characters in token");
    1.15 +          throw FormatError("Remaining characters in token");
    1.16          }
    1.17          return value;
    1.18        }
    1.19 @@ -166,7 +168,7 @@
    1.20          if (it == _map.end()) {
    1.21            std::ostringstream msg;
    1.22            msg << "Item not found: " << str;
    1.23 -          throw DataFormatError(msg.str().c_str());
    1.24 +          throw FormatError(msg.str());
    1.25          }
    1.26          return it->second;
    1.27        }
    1.28 @@ -184,12 +186,12 @@
    1.29  
    1.30        typename Graph::Arc operator()(const std::string& str) {
    1.31          if (str.empty() || (str[0] != '+' && str[0] != '-')) {
    1.32 -          throw DataFormatError("Item must start with '+' or '-'");
    1.33 +          throw FormatError("Item must start with '+' or '-'");
    1.34          }
    1.35          typename std::map<std::string, typename Graph::Edge>
    1.36            ::const_iterator it = _map.find(str.substr(1));
    1.37          if (it == _map.end()) {
    1.38 -          throw DataFormatError("Item not found");
    1.39 +          throw FormatError("Item not found");
    1.40          }
    1.41          return _graph.direct(it->second, str[0] == '+');
    1.42        }
    1.43 @@ -235,7 +237,7 @@
    1.44      inline char readEscape(std::istream& is) {
    1.45        char c;
    1.46        if (!is.get(c))
    1.47 -        throw DataFormatError("Escape format error");
    1.48 +        throw FormatError("Escape format error");
    1.49  
    1.50        switch (c) {
    1.51        case '\\':
    1.52 @@ -264,7 +266,7 @@
    1.53          {
    1.54            int code;
    1.55            if (!is.get(c) || !isHex(c))
    1.56 -            throw DataFormatError("Escape format error");
    1.57 +            throw FormatError("Escape format error");
    1.58            else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
    1.59            else code = code * 16 + valueHex(c);
    1.60            return code;
    1.61 @@ -273,7 +275,7 @@
    1.62          {
    1.63            int code;
    1.64            if (!isOct(c))
    1.65 -            throw DataFormatError("Escape format error");
    1.66 +            throw FormatError("Escape format error");
    1.67            else if (code = valueOct(c), !is.get(c) || !isOct(c))
    1.68              is.putback(c);
    1.69            else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
    1.70 @@ -300,7 +302,7 @@
    1.71            os << c;
    1.72          }
    1.73          if (!is)
    1.74 -          throw DataFormatError("Quoted format error");
    1.75 +          throw FormatError("Quoted format error");
    1.76        } else {
    1.77          is.putback(c);
    1.78          while (is.get(c) && !isWhiteSpace(c)) {
    1.79 @@ -460,6 +462,7 @@
    1.80  
    1.81      std::istream* _is;
    1.82      bool local_is;
    1.83 +    std::string _filename;
    1.84  
    1.85      Digraph& _digraph;
    1.86  
    1.87 @@ -509,18 +512,24 @@
    1.88      /// Construct a directed graph reader, which reads from the given
    1.89      /// file.
    1.90      DigraphReader(const std::string& fn, Digraph& digraph)
    1.91 -      : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
    1.92 +      : _is(new std::ifstream(fn.c_str())), local_is(true),
    1.93 +        _filename(fn), _digraph(digraph),
    1.94          _use_nodes(false), _use_arcs(false),
    1.95 -        _skip_nodes(false), _skip_arcs(false) {}
    1.96 +        _skip_nodes(false), _skip_arcs(false) {
    1.97 +      if (!(*_is)) throw IoError(fn, "Cannot open file");
    1.98 +    }
    1.99  
   1.100      /// \brief Constructor
   1.101      ///
   1.102      /// Construct a directed graph reader, which reads from the given
   1.103      /// file.
   1.104      DigraphReader(const char* fn, Digraph& digraph)
   1.105 -      : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
   1.106 +      : _is(new std::ifstream(fn)), local_is(true),
   1.107 +        _filename(fn), _digraph(digraph),
   1.108          _use_nodes(false), _use_arcs(false),
   1.109 -        _skip_nodes(false), _skip_arcs(false) {}
   1.110 +        _skip_nodes(false), _skip_arcs(false) {
   1.111 +      if (!(*_is)) throw IoError(fn, "Cannot open file");
   1.112 +    }
   1.113  
   1.114      /// \brief Destructor
   1.115      ~DigraphReader() {
   1.116 @@ -845,7 +854,7 @@
   1.117        if (!readLine() || !(line >> c) || c == '@') {
   1.118          if (readSuccess() && line) line.putback(c);
   1.119          if (!_node_maps.empty())
   1.120 -          throw DataFormatError("Cannot find map names");
   1.121 +          throw FormatError("Cannot find map names");
   1.122          return;
   1.123        }
   1.124        line.putback(c);
   1.125 @@ -859,7 +868,7 @@
   1.126            if (maps.find(map) != maps.end()) {
   1.127              std::ostringstream msg;
   1.128              msg << "Multiple occurence of node map: " << map;
   1.129 -            throw DataFormatError(msg.str().c_str());
   1.130 +            throw FormatError(msg.str());
   1.131            }
   1.132            maps.insert(std::make_pair(map, index));
   1.133            ++index;
   1.134 @@ -871,7 +880,7 @@
   1.135            if (jt == maps.end()) {
   1.136              std::ostringstream msg;
   1.137              msg << "Map not found in file: " << _node_maps[i].first;
   1.138 -            throw DataFormatError(msg.str().c_str());
   1.139 +            throw FormatError(msg.str());
   1.140            }
   1.141            map_index[i] = jt->second;
   1.142          }
   1.143 @@ -895,11 +904,11 @@
   1.144            if (!_reader_bits::readToken(line, tokens[i])) {
   1.145              std::ostringstream msg;
   1.146              msg << "Column not found (" << i + 1 << ")";
   1.147 -            throw DataFormatError(msg.str().c_str());
   1.148 +            throw FormatError(msg.str());
   1.149            }
   1.150          }
   1.151          if (line >> std::ws >> c)
   1.152 -          throw DataFormatError("Extra character on the end of line");
   1.153 +          throw FormatError("Extra character on the end of line");
   1.154  
   1.155          Node n;
   1.156          if (!_use_nodes) {
   1.157 @@ -908,13 +917,13 @@
   1.158              _node_index.insert(std::make_pair(tokens[label_index], n));
   1.159          } else {
   1.160            if (label_index == -1)
   1.161 -            throw DataFormatError("Label map not found in file");
   1.162 +            throw FormatError("Label map not found in file");
   1.163            typename std::map<std::string, Node>::iterator it =
   1.164              _node_index.find(tokens[label_index]);
   1.165            if (it == _node_index.end()) {
   1.166              std::ostringstream msg;
   1.167              msg << "Node with label not found: " << tokens[label_index];
   1.168 -            throw DataFormatError(msg.str().c_str());
   1.169 +            throw FormatError(msg.str());
   1.170            }
   1.171            n = it->second;
   1.172          }
   1.173 @@ -938,7 +947,7 @@
   1.174        if (!readLine() || !(line >> c) || c == '@') {
   1.175          if (readSuccess() && line) line.putback(c);
   1.176          if (!_arc_maps.empty())
   1.177 -          throw DataFormatError("Cannot find map names");
   1.178 +          throw FormatError("Cannot find map names");
   1.179          return;
   1.180        }
   1.181        line.putback(c);
   1.182 @@ -952,7 +961,7 @@
   1.183            if (maps.find(map) != maps.end()) {
   1.184              std::ostringstream msg;
   1.185              msg << "Multiple occurence of arc map: " << map;
   1.186 -            throw DataFormatError(msg.str().c_str());
   1.187 +            throw FormatError(msg.str());
   1.188            }
   1.189            maps.insert(std::make_pair(map, index));
   1.190            ++index;
   1.191 @@ -964,7 +973,7 @@
   1.192            if (jt == maps.end()) {
   1.193              std::ostringstream msg;
   1.194              msg << "Map not found in file: " << _arc_maps[i].first;
   1.195 -            throw DataFormatError(msg.str().c_str());
   1.196 +            throw FormatError(msg.str());
   1.197            }
   1.198            map_index[i] = jt->second;
   1.199          }
   1.200 @@ -987,21 +996,21 @@
   1.201          std::string target_token;
   1.202  
   1.203          if (!_reader_bits::readToken(line, source_token))
   1.204 -          throw DataFormatError("Source not found");
   1.205 +          throw FormatError("Source not found");
   1.206  
   1.207          if (!_reader_bits::readToken(line, target_token))
   1.208 -          throw DataFormatError("Target not found");
   1.209 +          throw FormatError("Target not found");
   1.210  
   1.211          std::vector<std::string> tokens(map_num);
   1.212          for (int i = 0; i < map_num; ++i) {
   1.213            if (!_reader_bits::readToken(line, tokens[i])) {
   1.214              std::ostringstream msg;
   1.215              msg << "Column not found (" << i + 1 << ")";
   1.216 -            throw DataFormatError(msg.str().c_str());
   1.217 +            throw FormatError(msg.str());
   1.218            }
   1.219          }
   1.220          if (line >> std::ws >> c)
   1.221 -          throw DataFormatError("Extra character on the end of line");
   1.222 +          throw FormatError("Extra character on the end of line");
   1.223  
   1.224          Arc a;
   1.225          if (!_use_arcs) {
   1.226 @@ -1012,7 +1021,7 @@
   1.227            if (it == _node_index.end()) {
   1.228              std::ostringstream msg;
   1.229              msg << "Item not found: " << source_token;
   1.230 -            throw DataFormatError(msg.str().c_str());
   1.231 +            throw FormatError(msg.str());
   1.232            }
   1.233            Node source = it->second;
   1.234  
   1.235 @@ -1020,7 +1029,7 @@
   1.236            if (it == _node_index.end()) {
   1.237              std::ostringstream msg;
   1.238              msg << "Item not found: " << target_token;
   1.239 -            throw DataFormatError(msg.str().c_str());
   1.240 +            throw FormatError(msg.str());
   1.241            }
   1.242            Node target = it->second;
   1.243  
   1.244 @@ -1029,13 +1038,13 @@
   1.245              _arc_index.insert(std::make_pair(tokens[label_index], a));
   1.246          } else {
   1.247            if (label_index == -1)
   1.248 -            throw DataFormatError("Label map not found in file");
   1.249 +            throw FormatError("Label map not found in file");
   1.250            typename std::map<std::string, Arc>::iterator it =
   1.251              _arc_index.find(tokens[label_index]);
   1.252            if (it == _arc_index.end()) {
   1.253              std::ostringstream msg;
   1.254              msg << "Arc with label not found: " << tokens[label_index];
   1.255 -            throw DataFormatError(msg.str().c_str());
   1.256 +            throw FormatError(msg.str());
   1.257            }
   1.258            a = it->second;
   1.259          }
   1.260 @@ -1060,18 +1069,18 @@
   1.261  
   1.262          std::string attr, token;
   1.263          if (!_reader_bits::readToken(line, attr))
   1.264 -          throw DataFormatError("Attribute name not found");
   1.265 +          throw FormatError("Attribute name not found");
   1.266          if (!_reader_bits::readToken(line, token))
   1.267 -          throw DataFormatError("Attribute value not found");
   1.268 +          throw FormatError("Attribute value not found");
   1.269          if (line >> c)
   1.270 -          throw DataFormatError("Extra character on the end of line");
   1.271 +          throw FormatError("Extra character on the end of line");
   1.272  
   1.273          {
   1.274            std::set<std::string>::iterator it = read_attr.find(attr);
   1.275            if (it != read_attr.end()) {
   1.276              std::ostringstream msg;
   1.277              msg << "Multiple occurence of attribute " << attr;
   1.278 -            throw DataFormatError(msg.str().c_str());
   1.279 +            throw FormatError(msg.str());
   1.280            }
   1.281            read_attr.insert(attr);
   1.282          }
   1.283 @@ -1093,7 +1102,7 @@
   1.284          if (read_attr.find(it->first) == read_attr.end()) {
   1.285            std::ostringstream msg;
   1.286            msg << "Attribute not found in file: " << it->first;
   1.287 -          throw DataFormatError(msg.str().c_str());
   1.288 +          throw FormatError(msg.str());
   1.289          }
   1.290        }
   1.291      }
   1.292 @@ -1109,7 +1118,7 @@
   1.293      void run() {
   1.294        LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
   1.295        if (!*_is) {
   1.296 -        throw DataFormatError("Cannot find file");
   1.297 +        throw FormatError("Cannot find file");
   1.298        }
   1.299  
   1.300        bool nodes_done = _skip_nodes;
   1.301 @@ -1129,7 +1138,7 @@
   1.302            _reader_bits::readToken(line, caption);
   1.303  
   1.304            if (line >> c)
   1.305 -            throw DataFormatError("Extra character on the end of line");
   1.306 +            throw FormatError("Extra character on the end of line");
   1.307  
   1.308            if (section == "nodes" && !nodes_done) {
   1.309              if (_nodes_caption.empty() || _nodes_caption == caption) {
   1.310 @@ -1151,22 +1160,23 @@
   1.311              readLine();
   1.312              skipSection();
   1.313            }
   1.314 -        } catch (DataFormatError& error) {
   1.315 +        } catch (FormatError& error) {
   1.316            error.line(line_num);
   1.317 +          error.file(_filename);
   1.318            throw;
   1.319          }
   1.320        }
   1.321  
   1.322        if (!nodes_done) {
   1.323 -        throw DataFormatError("Section @nodes not found");
   1.324 +        throw FormatError("Section @nodes not found");
   1.325        }
   1.326  
   1.327        if (!arcs_done) {
   1.328 -        throw DataFormatError("Section @arcs not found");
   1.329 +        throw FormatError("Section @arcs not found");
   1.330        }
   1.331  
   1.332        if (!attributes_done && !_attributes.empty()) {
   1.333 -        throw DataFormatError("Section @attributes not found");
   1.334 +        throw FormatError("Section @attributes not found");
   1.335        }
   1.336  
   1.337      }
   1.338 @@ -1244,6 +1254,7 @@
   1.339  
   1.340      std::istream* _is;
   1.341      bool local_is;
   1.342 +    std::string _filename;
   1.343  
   1.344      Graph& _graph;
   1.345  
   1.346 @@ -1293,18 +1304,24 @@
   1.347      /// Construct an undirected graph reader, which reads from the given
   1.348      /// file.
   1.349      GraphReader(const std::string& fn, Graph& graph)
   1.350 -      : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph),
   1.351 +      : _is(new std::ifstream(fn.c_str())), local_is(true),
   1.352 +        _filename(fn), _graph(graph),
   1.353          _use_nodes(false), _use_edges(false),
   1.354 -        _skip_nodes(false), _skip_edges(false) {}
   1.355 +        _skip_nodes(false), _skip_edges(false) {
   1.356 +      if (!(*_is)) throw IoError(fn, "Cannot open file");
   1.357 +    }
   1.358  
   1.359      /// \brief Constructor
   1.360      ///
   1.361      /// Construct an undirected graph reader, which reads from the given
   1.362      /// file.
   1.363      GraphReader(const char* fn, Graph& graph)
   1.364 -      : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
   1.365 +      : _is(new std::ifstream(fn)), local_is(true),
   1.366 +        _filename(fn), _graph(graph),
   1.367          _use_nodes(false), _use_edges(false),
   1.368 -        _skip_nodes(false), _skip_edges(false) {}
   1.369 +        _skip_nodes(false), _skip_edges(false) {
   1.370 +      if (!(*_is)) throw IoError(fn, "Cannot open file");
   1.371 +    }
   1.372  
   1.373      /// \brief Destructor
   1.374      ~GraphReader() {
   1.375 @@ -1673,7 +1690,7 @@
   1.376        if (!readLine() || !(line >> c) || c == '@') {
   1.377          if (readSuccess() && line) line.putback(c);
   1.378          if (!_node_maps.empty())
   1.379 -          throw DataFormatError("Cannot find map names");
   1.380 +          throw FormatError("Cannot find map names");
   1.381          return;
   1.382        }
   1.383        line.putback(c);
   1.384 @@ -1687,7 +1704,7 @@
   1.385            if (maps.find(map) != maps.end()) {
   1.386              std::ostringstream msg;
   1.387              msg << "Multiple occurence of node map: " << map;
   1.388 -            throw DataFormatError(msg.str().c_str());
   1.389 +            throw FormatError(msg.str());
   1.390            }
   1.391            maps.insert(std::make_pair(map, index));
   1.392            ++index;
   1.393 @@ -1699,7 +1716,7 @@
   1.394            if (jt == maps.end()) {
   1.395              std::ostringstream msg;
   1.396              msg << "Map not found in file: " << _node_maps[i].first;
   1.397 -            throw DataFormatError(msg.str().c_str());
   1.398 +            throw FormatError(msg.str());
   1.399            }
   1.400            map_index[i] = jt->second;
   1.401          }
   1.402 @@ -1723,11 +1740,11 @@
   1.403            if (!_reader_bits::readToken(line, tokens[i])) {
   1.404              std::ostringstream msg;
   1.405              msg << "Column not found (" << i + 1 << ")";
   1.406 -            throw DataFormatError(msg.str().c_str());
   1.407 +            throw FormatError(msg.str());
   1.408            }
   1.409          }
   1.410          if (line >> std::ws >> c)
   1.411 -          throw DataFormatError("Extra character on the end of line");
   1.412 +          throw FormatError("Extra character on the end of line");
   1.413  
   1.414          Node n;
   1.415          if (!_use_nodes) {
   1.416 @@ -1736,13 +1753,13 @@
   1.417              _node_index.insert(std::make_pair(tokens[label_index], n));
   1.418          } else {
   1.419            if (label_index == -1)
   1.420 -            throw DataFormatError("Label map not found in file");
   1.421 +            throw FormatError("Label map not found in file");
   1.422            typename std::map<std::string, Node>::iterator it =
   1.423              _node_index.find(tokens[label_index]);
   1.424            if (it == _node_index.end()) {
   1.425              std::ostringstream msg;
   1.426              msg << "Node with label not found: " << tokens[label_index];
   1.427 -            throw DataFormatError(msg.str().c_str());
   1.428 +            throw FormatError(msg.str());
   1.429            }
   1.430            n = it->second;
   1.431          }
   1.432 @@ -1766,7 +1783,7 @@
   1.433        if (!readLine() || !(line >> c) || c == '@') {
   1.434          if (readSuccess() && line) line.putback(c);
   1.435          if (!_edge_maps.empty())
   1.436 -          throw DataFormatError("Cannot find map names");
   1.437 +          throw FormatError("Cannot find map names");
   1.438          return;
   1.439        }
   1.440        line.putback(c);
   1.441 @@ -1780,7 +1797,7 @@
   1.442            if (maps.find(map) != maps.end()) {
   1.443              std::ostringstream msg;
   1.444              msg << "Multiple occurence of edge map: " << map;
   1.445 -            throw DataFormatError(msg.str().c_str());
   1.446 +            throw FormatError(msg.str());
   1.447            }
   1.448            maps.insert(std::make_pair(map, index));
   1.449            ++index;
   1.450 @@ -1792,7 +1809,7 @@
   1.451            if (jt == maps.end()) {
   1.452              std::ostringstream msg;
   1.453              msg << "Map not found in file: " << _edge_maps[i].first;
   1.454 -            throw DataFormatError(msg.str().c_str());
   1.455 +            throw FormatError(msg.str());
   1.456            }
   1.457            map_index[i] = jt->second;
   1.458          }
   1.459 @@ -1815,21 +1832,21 @@
   1.460          std::string target_token;
   1.461  
   1.462          if (!_reader_bits::readToken(line, source_token))
   1.463 -          throw DataFormatError("Node u not found");
   1.464 +          throw FormatError("Node u not found");
   1.465  
   1.466          if (!_reader_bits::readToken(line, target_token))
   1.467 -          throw DataFormatError("Node v not found");
   1.468 +          throw FormatError("Node v not found");
   1.469  
   1.470          std::vector<std::string> tokens(map_num);
   1.471          for (int i = 0; i < map_num; ++i) {
   1.472            if (!_reader_bits::readToken(line, tokens[i])) {
   1.473              std::ostringstream msg;
   1.474              msg << "Column not found (" << i + 1 << ")";
   1.475 -            throw DataFormatError(msg.str().c_str());
   1.476 +            throw FormatError(msg.str());
   1.477            }
   1.478          }
   1.479          if (line >> std::ws >> c)
   1.480 -          throw DataFormatError("Extra character on the end of line");
   1.481 +          throw FormatError("Extra character on the end of line");
   1.482  
   1.483          Edge e;
   1.484          if (!_use_edges) {
   1.485 @@ -1840,7 +1857,7 @@
   1.486            if (it == _node_index.end()) {
   1.487              std::ostringstream msg;
   1.488              msg << "Item not found: " << source_token;
   1.489 -            throw DataFormatError(msg.str().c_str());
   1.490 +            throw FormatError(msg.str());
   1.491            }
   1.492            Node source = it->second;
   1.493  
   1.494 @@ -1848,7 +1865,7 @@
   1.495            if (it == _node_index.end()) {
   1.496              std::ostringstream msg;
   1.497              msg << "Item not found: " << target_token;
   1.498 -            throw DataFormatError(msg.str().c_str());
   1.499 +            throw FormatError(msg.str());
   1.500            }
   1.501            Node target = it->second;
   1.502  
   1.503 @@ -1857,13 +1874,13 @@
   1.504              _edge_index.insert(std::make_pair(tokens[label_index], e));
   1.505          } else {
   1.506            if (label_index == -1)
   1.507 -            throw DataFormatError("Label map not found in file");
   1.508 +            throw FormatError("Label map not found in file");
   1.509            typename std::map<std::string, Edge>::iterator it =
   1.510              _edge_index.find(tokens[label_index]);
   1.511            if (it == _edge_index.end()) {
   1.512              std::ostringstream msg;
   1.513              msg << "Edge with label not found: " << tokens[label_index];
   1.514 -            throw DataFormatError(msg.str().c_str());
   1.515 +            throw FormatError(msg.str());
   1.516            }
   1.517            e = it->second;
   1.518          }
   1.519 @@ -1888,18 +1905,18 @@
   1.520  
   1.521          std::string attr, token;
   1.522          if (!_reader_bits::readToken(line, attr))
   1.523 -          throw DataFormatError("Attribute name not found");
   1.524 +          throw FormatError("Attribute name not found");
   1.525          if (!_reader_bits::readToken(line, token))
   1.526 -          throw DataFormatError("Attribute value not found");
   1.527 +          throw FormatError("Attribute value not found");
   1.528          if (line >> c)
   1.529 -          throw DataFormatError("Extra character on the end of line");
   1.530 +          throw FormatError("Extra character on the end of line");
   1.531  
   1.532          {
   1.533            std::set<std::string>::iterator it = read_attr.find(attr);
   1.534            if (it != read_attr.end()) {
   1.535              std::ostringstream msg;
   1.536              msg << "Multiple occurence of attribute " << attr;
   1.537 -            throw DataFormatError(msg.str().c_str());
   1.538 +            throw FormatError(msg.str());
   1.539            }
   1.540            read_attr.insert(attr);
   1.541          }
   1.542 @@ -1921,7 +1938,7 @@
   1.543          if (read_attr.find(it->first) == read_attr.end()) {
   1.544            std::ostringstream msg;
   1.545            msg << "Attribute not found in file: " << it->first;
   1.546 -          throw DataFormatError(msg.str().c_str());
   1.547 +          throw FormatError(msg.str());
   1.548          }
   1.549        }
   1.550      }
   1.551 @@ -1955,7 +1972,7 @@
   1.552            _reader_bits::readToken(line, caption);
   1.553  
   1.554            if (line >> c)
   1.555 -            throw DataFormatError("Extra character on the end of line");
   1.556 +            throw FormatError("Extra character on the end of line");
   1.557  
   1.558            if (section == "nodes" && !nodes_done) {
   1.559              if (_nodes_caption.empty() || _nodes_caption == caption) {
   1.560 @@ -1977,22 +1994,23 @@
   1.561              readLine();
   1.562              skipSection();
   1.563            }
   1.564 -        } catch (DataFormatError& error) {
   1.565 +        } catch (FormatError& error) {
   1.566            error.line(line_num);
   1.567 +          error.file(_filename);
   1.568            throw;
   1.569          }
   1.570        }
   1.571  
   1.572        if (!nodes_done) {
   1.573 -        throw DataFormatError("Section @nodes not found");
   1.574 +        throw FormatError("Section @nodes not found");
   1.575        }
   1.576  
   1.577        if (!edges_done) {
   1.578 -        throw DataFormatError("Section @edges not found");
   1.579 +        throw FormatError("Section @edges not found");
   1.580        }
   1.581  
   1.582        if (!attributes_done && !_attributes.empty()) {
   1.583 -        throw DataFormatError("Section @attributes not found");
   1.584 +        throw FormatError("Section @attributes not found");
   1.585        }
   1.586  
   1.587      }
   1.588 @@ -2054,6 +2072,7 @@
   1.589  
   1.590      std::istream* _is;
   1.591      bool local_is;
   1.592 +    std::string _filename;
   1.593  
   1.594      typedef std::map<std::string, _reader_bits::Section*> Sections;
   1.595      Sections _sections;
   1.596 @@ -2074,13 +2093,19 @@
   1.597      ///
   1.598      /// Construct a section reader, which reads from the given file.
   1.599      SectionReader(const std::string& fn)
   1.600 -      : _is(new std::ifstream(fn.c_str())), local_is(true) {}
   1.601 +      : _is(new std::ifstream(fn.c_str())), local_is(true),
   1.602 +        _filename(fn) {
   1.603 +      if (!(*_is)) throw IoError(fn, "Cannot open file");
   1.604 +    }
   1.605  
   1.606      /// \brief Constructor
   1.607      ///
   1.608      /// Construct a section reader, which reads from the given file.
   1.609      SectionReader(const char* fn)
   1.610 -      : _is(new std::ifstream(fn)), local_is(true) {}
   1.611 +      : _is(new std::ifstream(fn)), local_is(true),
   1.612 +        _filename(fn) {
   1.613 +      if (!(*_is)) throw IoError(fn, "Cannot open file");
   1.614 +    }
   1.615  
   1.616      /// \brief Destructor
   1.617      ~SectionReader() {
   1.618 @@ -2236,12 +2261,12 @@
   1.619            _reader_bits::readToken(line, caption);
   1.620  
   1.621            if (line >> c)
   1.622 -            throw DataFormatError("Extra character on the end of line");
   1.623 +            throw FormatError("Extra character on the end of line");
   1.624  
   1.625            if (extra_sections.find(section) != extra_sections.end()) {
   1.626              std::ostringstream msg;
   1.627              msg << "Multiple occurence of section " << section;
   1.628 -            throw DataFormatError(msg.str().c_str());
   1.629 +            throw FormatError(msg.str());
   1.630            }
   1.631            Sections::iterator it = _sections.find(section);
   1.632            if (it != _sections.end()) {
   1.633 @@ -2250,8 +2275,9 @@
   1.634            }
   1.635            readLine();
   1.636            skipSection();
   1.637 -        } catch (DataFormatError& error) {
   1.638 +        } catch (FormatError& error) {
   1.639            error.line(line_num);
   1.640 +          error.file(_filename);
   1.641            throw;
   1.642          }
   1.643        }
   1.644 @@ -2260,7 +2286,7 @@
   1.645          if (extra_sections.find(it->first) == extra_sections.end()) {
   1.646            std::ostringstream os;
   1.647            os << "Cannot find section: " << it->first;
   1.648 -          throw DataFormatError(os.str().c_str());
   1.649 +          throw FormatError(os.str());
   1.650          }
   1.651        }
   1.652      }
   1.653 @@ -2360,14 +2386,18 @@
   1.654      /// Construct an \e LGF contents reader, which reads from the given
   1.655      /// file.
   1.656      LgfContents(const std::string& fn)
   1.657 -      : _is(new std::ifstream(fn.c_str())), local_is(true) {}
   1.658 +      : _is(new std::ifstream(fn.c_str())), local_is(true) {
   1.659 +      if (!(*_is)) throw IoError(fn, "Cannot open file");
   1.660 +    }
   1.661  
   1.662      /// \brief Constructor
   1.663      ///
   1.664      /// Construct an \e LGF contents reader, which reads from the given
   1.665      /// file.
   1.666      LgfContents(const char* fn)
   1.667 -      : _is(new std::ifstream(fn)), local_is(true) {}
   1.668 +      : _is(new std::ifstream(fn)), local_is(true) {
   1.669 +      if (!(*_is)) throw IoError(fn, "Cannot open file");
   1.670 +    }
   1.671  
   1.672      /// \brief Destructor
   1.673      ~LgfContents() {