Index: lemon/lgf_reader.h
===================================================================
--- lemon/lgf_reader.h	(revision 293)
+++ lemon/lgf_reader.h	(revision 294)
@@ -49,9 +49,11 @@
         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;
@@ -167,5 +169,5 @@
           std::ostringstream msg;
           msg << "Item not found: " << str;
-          throw DataFormatError(msg.str().c_str());
+          throw FormatError(msg.str());
         }
         return it->second;
@@ -185,10 +187,10 @@
       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<std::string, typename Graph::Edge>
           ::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] == '+');
@@ -236,5 +238,5 @@
       char c;
       if (!is.get(c))
-        throw DataFormatError("Escape format error");
+        throw FormatError("Escape format error");
 
       switch (c) {
@@ -265,5 +267,5 @@
           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);
@@ -274,5 +276,5 @@
           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);
@@ -301,5 +303,5 @@
         }
         if (!is)
-          throw DataFormatError("Quoted format error");
+          throw FormatError("Quoted format error");
       } else {
         is.putback(c);
@@ -462,4 +464,5 @@
     std::istream* _is;
     bool local_is;
+    std::string _filename;
 
     Digraph& _digraph;
@@ -511,7 +514,10 @@
     /// file.
     DigraphReader(Digraph& digraph, const std::string& fn)
-      : _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("Cannot open file", fn);
+    }
 
     /// \brief Constructor
@@ -520,7 +526,10 @@
     /// file.
     DigraphReader(Digraph& digraph, const char* fn)
-      : _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("Cannot open file", fn);
+    }
 
     /// \brief Destructor
@@ -847,5 +856,5 @@
         if (readSuccess() && line) line.putback(c);
         if (!_node_maps.empty())
-          throw DataFormatError("Cannot find map names");
+          throw FormatError("Cannot find map names");
         return;
       }
@@ -861,5 +870,5 @@
             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));
@@ -872,6 +881,6 @@
           if (jt == maps.end()) {
             std::ostringstream msg;
-            msg << "Map not found in file: " << _node_maps[i].first;
-            throw DataFormatError(msg.str().c_str());
+            msg << "Map not found: " << _node_maps[i].first;
+            throw FormatError(msg.str());
           }
           map_index[i] = jt->second;
@@ -897,9 +906,9 @@
             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 at the end of line");
 
         Node n;
@@ -910,5 +919,5 @@
         } else {
           if (label_index == -1)
-            throw DataFormatError("Label map not found in file");
+            throw FormatError("Label map not found");
           typename std::map<std::string, Node>::iterator it =
             _node_index.find(tokens[label_index]);
@@ -916,5 +925,5 @@
             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;
@@ -940,5 +949,5 @@
         if (readSuccess() && line) line.putback(c);
         if (!_arc_maps.empty())
-          throw DataFormatError("Cannot find map names");
+          throw FormatError("Cannot find map names");
         return;
       }
@@ -954,5 +963,5 @@
             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));
@@ -965,6 +974,6 @@
           if (jt == maps.end()) {
             std::ostringstream msg;
-            msg << "Map not found in file: " << _arc_maps[i].first;
-            throw DataFormatError(msg.str().c_str());
+            msg << "Map not found: " << _arc_maps[i].first;
+            throw FormatError(msg.str());
           }
           map_index[i] = jt->second;
@@ -989,8 +998,8 @@
 
         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<std::string> tokens(map_num);
@@ -999,9 +1008,9 @@
             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 at the end of line");
 
         Arc a;
@@ -1014,5 +1023,5 @@
             std::ostringstream msg;
             msg << "Item not found: " << source_token;
-            throw DataFormatError(msg.str().c_str());
+            throw FormatError(msg.str());
           }
           Node source = it->second;
@@ -1022,5 +1031,5 @@
             std::ostringstream msg;
             msg << "Item not found: " << target_token;
-            throw DataFormatError(msg.str().c_str());
+            throw FormatError(msg.str());
           }
           Node target = it->second;
@@ -1031,5 +1040,5 @@
         } else {
           if (label_index == -1)
-            throw DataFormatError("Label map not found in file");
+            throw FormatError("Label map not found");
           typename std::map<std::string, Arc>::iterator it =
             _arc_index.find(tokens[label_index]);
@@ -1037,5 +1046,5 @@
             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;
@@ -1062,9 +1071,9 @@
         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 at the end of line");
 
         {
@@ -1072,6 +1081,6 @@
           if (it != read_attr.end()) {
             std::ostringstream msg;
-            msg << "Multiple occurence of attribute " << attr;
-            throw DataFormatError(msg.str().c_str());
+            msg << "Multiple occurence of attribute: " << attr;
+            throw FormatError(msg.str());
           }
           read_attr.insert(attr);
@@ -1094,6 +1103,6 @@
         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());
+          msg << "Attribute not found: " << it->first;
+          throw FormatError(msg.str());
         }
       }
@@ -1110,7 +1119,4 @@
     void run() {
       LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
-      if (!*_is) {
-        throw DataFormatError("Cannot find file");
-      }
 
       bool nodes_done = _skip_nodes;
@@ -1131,5 +1137,5 @@
 
           if (line >> c)
-            throw DataFormatError("Extra character on the end of line");
+            throw FormatError("Extra character at the end of line");
 
           if (section == "nodes" && !nodes_done) {
@@ -1153,6 +1159,7 @@
             skipSection();
           }
-        } catch (DataFormatError& error) {
+        } catch (FormatError& error) {
           error.line(line_num);
+          error.file(_filename);
           throw;
         }
@@ -1160,13 +1167,13 @@
 
       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");
       }
 
@@ -1248,4 +1255,5 @@
     std::istream* _is;
     bool local_is;
+    std::string _filename;
 
     Graph& _graph;
@@ -1297,7 +1305,10 @@
     /// file.
     GraphReader(Graph& graph, const std::string& fn)
-      : _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("Cannot open file", fn);
+    }
 
     /// \brief Constructor
@@ -1306,7 +1317,10 @@
     /// file.
     GraphReader(Graph& graph, const char* fn)
-      : _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("Cannot open file", fn);
+    }
 
     /// \brief Destructor
@@ -1677,5 +1691,5 @@
         if (readSuccess() && line) line.putback(c);
         if (!_node_maps.empty())
-          throw DataFormatError("Cannot find map names");
+          throw FormatError("Cannot find map names");
         return;
       }
@@ -1691,5 +1705,5 @@
             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));
@@ -1702,6 +1716,6 @@
           if (jt == maps.end()) {
             std::ostringstream msg;
-            msg << "Map not found in file: " << _node_maps[i].first;
-            throw DataFormatError(msg.str().c_str());
+            msg << "Map not found: " << _node_maps[i].first;
+            throw FormatError(msg.str());
           }
           map_index[i] = jt->second;
@@ -1727,9 +1741,9 @@
             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 at the end of line");
 
         Node n;
@@ -1740,5 +1754,5 @@
         } else {
           if (label_index == -1)
-            throw DataFormatError("Label map not found in file");
+            throw FormatError("Label map not found");
           typename std::map<std::string, Node>::iterator it =
             _node_index.find(tokens[label_index]);
@@ -1746,5 +1760,5 @@
             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;
@@ -1770,5 +1784,5 @@
         if (readSuccess() && line) line.putback(c);
         if (!_edge_maps.empty())
-          throw DataFormatError("Cannot find map names");
+          throw FormatError("Cannot find map names");
         return;
       }
@@ -1784,5 +1798,5 @@
             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));
@@ -1795,6 +1809,6 @@
           if (jt == maps.end()) {
             std::ostringstream msg;
-            msg << "Map not found in file: " << _edge_maps[i].first;
-            throw DataFormatError(msg.str().c_str());
+            msg << "Map not found: " << _edge_maps[i].first;
+            throw FormatError(msg.str());
           }
           map_index[i] = jt->second;
@@ -1819,8 +1833,8 @@
 
         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<std::string> tokens(map_num);
@@ -1829,9 +1843,9 @@
             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 at the end of line");
 
         Edge e;
@@ -1844,5 +1858,5 @@
             std::ostringstream msg;
             msg << "Item not found: " << source_token;
-            throw DataFormatError(msg.str().c_str());
+            throw FormatError(msg.str());
           }
           Node source = it->second;
@@ -1852,5 +1866,5 @@
             std::ostringstream msg;
             msg << "Item not found: " << target_token;
-            throw DataFormatError(msg.str().c_str());
+            throw FormatError(msg.str());
           }
           Node target = it->second;
@@ -1861,5 +1875,5 @@
         } else {
           if (label_index == -1)
-            throw DataFormatError("Label map not found in file");
+            throw FormatError("Label map not found");
           typename std::map<std::string, Edge>::iterator it =
             _edge_index.find(tokens[label_index]);
@@ -1867,5 +1881,5 @@
             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;
@@ -1892,9 +1906,9 @@
         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 at the end of line");
 
         {
@@ -1902,6 +1916,6 @@
           if (it != read_attr.end()) {
             std::ostringstream msg;
-            msg << "Multiple occurence of attribute " << attr;
-            throw DataFormatError(msg.str().c_str());
+            msg << "Multiple occurence of attribute: " << attr;
+            throw FormatError(msg.str());
           }
           read_attr.insert(attr);
@@ -1924,6 +1938,6 @@
         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());
+          msg << "Attribute not found: " << it->first;
+          throw FormatError(msg.str());
         }
       }
@@ -1959,5 +1973,5 @@
 
           if (line >> c)
-            throw DataFormatError("Extra character on the end of line");
+            throw FormatError("Extra character at the end of line");
 
           if (section == "nodes" && !nodes_done) {
@@ -1981,6 +1995,7 @@
             skipSection();
           }
-        } catch (DataFormatError& error) {
+        } catch (FormatError& error) {
           error.line(line_num);
+          error.file(_filename);
           throw;
         }
@@ -1988,13 +2003,13 @@
 
       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");
       }
 
@@ -2057,4 +2072,5 @@
     std::istream* _is;
     bool local_is;
+    std::string _filename;
 
     typedef std::map<std::string, _reader_bits::Section*> Sections;
@@ -2077,5 +2093,8 @@
     /// 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("Cannot open file", fn);
+    }
 
     /// \brief Constructor
@@ -2083,5 +2102,8 @@
     /// 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("Cannot open file", fn);
+    }
 
     /// \brief Destructor
@@ -2239,10 +2261,10 @@
 
           if (line >> c)
-            throw DataFormatError("Extra character on the end of line");
+            throw FormatError("Extra character at 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());
+            msg << "Multiple occurence of section: " << section;
+            throw FormatError(msg.str());
           }
           Sections::iterator it = _sections.find(section);
@@ -2253,6 +2275,7 @@
           readLine();
           skipSection();
-        } catch (DataFormatError& error) {
+        } catch (FormatError& error) {
           error.line(line_num);
+          error.file(_filename);
           throw;
         }
@@ -2263,5 +2286,5 @@
           std::ostringstream os;
           os << "Cannot find section: " << it->first;
-          throw DataFormatError(os.str().c_str());
+          throw FormatError(os.str());
         }
       }
@@ -2363,5 +2386,7 @@
     /// 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("Cannot open file", fn);
+    }
 
     /// \brief Constructor
@@ -2370,5 +2395,7 @@
     /// 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("Cannot open file", fn);
+    }
 
     /// \brief Destructor
