COIN-OR::LEMON - Graph Library

Changeset 290:f6899946c1ac in lemon-main for lemon/lgf_reader.h


Ignore:
Timestamp:
09/30/08 20:53:18 (16 years ago)
Author:
Balazs Dezso <deba@…>
Branch:
default
Phase:
public
Message:

Simplifying exceptions

  • Using asserts instead of exceptions for unitialized parameters
  • Only the IO exceptions are used in the lemon
  • DataFormatError? is renamed to FormatError?
  • The IoError? is used for file access errors
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/lgf_reader.h

    r236 r290  
    4949        std::istringstream is(str);
    5050        Value value;
    51         is >> value;
     51        if (!(is >> value)) {
     52          throw FormatError("Cannot read token");
     53        }
    5254
    5355        char c;
    5456        if (is >> std::ws >> c) {
    55           throw DataFormatError("Remaining characters in token");
     57          throw FormatError("Remaining characters in token");
    5658        }
    5759        return value;
     
    167169          std::ostringstream msg;
    168170          msg << "Item not found: " << str;
    169           throw DataFormatError(msg.str().c_str());
     171          throw FormatError(msg.str());
    170172        }
    171173        return it->second;
     
    185187      typename Graph::Arc operator()(const std::string& str) {
    186188        if (str.empty() || (str[0] != '+' && str[0] != '-')) {
    187           throw DataFormatError("Item must start with '+' or '-'");
     189          throw FormatError("Item must start with '+' or '-'");
    188190        }
    189191        typename std::map<std::string, typename Graph::Edge>
    190192          ::const_iterator it = _map.find(str.substr(1));
    191193        if (it == _map.end()) {
    192           throw DataFormatError("Item not found");
     194          throw FormatError("Item not found");
    193195        }
    194196        return _graph.direct(it->second, str[0] == '+');
     
    236238      char c;
    237239      if (!is.get(c))
    238         throw DataFormatError("Escape format error");
     240        throw FormatError("Escape format error");
    239241
    240242      switch (c) {
     
    265267          int code;
    266268          if (!is.get(c) || !isHex(c))
    267             throw DataFormatError("Escape format error");
     269            throw FormatError("Escape format error");
    268270          else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
    269271          else code = code * 16 + valueHex(c);
     
    274276          int code;
    275277          if (!isOct(c))
    276             throw DataFormatError("Escape format error");
     278            throw FormatError("Escape format error");
    277279          else if (code = valueOct(c), !is.get(c) || !isOct(c))
    278280            is.putback(c);
     
    301303        }
    302304        if (!is)
    303           throw DataFormatError("Quoted format error");
     305          throw FormatError("Quoted format error");
    304306      } else {
    305307        is.putback(c);
     
    461463    std::istream* _is;
    462464    bool local_is;
     465    std::string _filename;
    463466
    464467    Digraph& _digraph;
     
    510513    /// file.
    511514    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),
    513517        _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(fn, "Cannot open file");
     520    }
    515521
    516522    /// \brief Constructor
     
    519525    /// file.
    520526    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),
    522529        _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(fn, "Cannot open file");
     532    }
    524533
    525534    /// \brief Destructor
     
    846855        if (readSuccess() && line) line.putback(c);
    847856        if (!_node_maps.empty())
    848           throw DataFormatError("Cannot find map names");
     857          throw FormatError("Cannot find map names");
    849858        return;
    850859      }
     
    860869            std::ostringstream msg;
    861870            msg << "Multiple occurence of node map: " << map;
    862             throw DataFormatError(msg.str().c_str());
     871            throw FormatError(msg.str());
    863872          }
    864873          maps.insert(std::make_pair(map, index));
     
    872881            std::ostringstream msg;
    873882            msg << "Map not found in file: " << _node_maps[i].first;
    874             throw DataFormatError(msg.str().c_str());
     883            throw FormatError(msg.str());
    875884          }
    876885          map_index[i] = jt->second;
     
    896905            std::ostringstream msg;
    897906            msg << "Column not found (" << i + 1 << ")";
    898             throw DataFormatError(msg.str().c_str());
     907            throw FormatError(msg.str());
    899908          }
    900909        }
    901910        if (line >> std::ws >> c)
    902           throw DataFormatError("Extra character on the end of line");
     911          throw FormatError("Extra character on the end of line");
    903912
    904913        Node n;
     
    909918        } else {
    910919          if (label_index == -1)
    911             throw DataFormatError("Label map not found in file");
     920            throw FormatError("Label map not found in file");
    912921          typename std::map<std::string, Node>::iterator it =
    913922            _node_index.find(tokens[label_index]);
     
    915924            std::ostringstream msg;
    916925            msg << "Node with label not found: " << tokens[label_index];
    917             throw DataFormatError(msg.str().c_str());
     926            throw FormatError(msg.str());
    918927          }
    919928          n = it->second;
     
    939948        if (readSuccess() && line) line.putback(c);
    940949        if (!_arc_maps.empty())
    941           throw DataFormatError("Cannot find map names");
     950          throw FormatError("Cannot find map names");
    942951        return;
    943952      }
     
    953962            std::ostringstream msg;
    954963            msg << "Multiple occurence of arc map: " << map;
    955             throw DataFormatError(msg.str().c_str());
     964            throw FormatError(msg.str());
    956965          }
    957966          maps.insert(std::make_pair(map, index));
     
    965974            std::ostringstream msg;
    966975            msg << "Map not found in file: " << _arc_maps[i].first;
    967             throw DataFormatError(msg.str().c_str());
     976            throw FormatError(msg.str());
    968977          }
    969978          map_index[i] = jt->second;
     
    988997
    989998        if (!_reader_bits::readToken(line, source_token))
    990           throw DataFormatError("Source not found");
     999          throw FormatError("Source not found");
    9911000
    9921001        if (!_reader_bits::readToken(line, target_token))
    993           throw DataFormatError("Target not found");
     1002          throw FormatError("Target not found");
    9941003
    9951004        std::vector<std::string> tokens(map_num);
     
    9981007            std::ostringstream msg;
    9991008            msg << "Column not found (" << i + 1 << ")";
    1000             throw DataFormatError(msg.str().c_str());
     1009            throw FormatError(msg.str());
    10011010          }
    10021011        }
    10031012        if (line >> std::ws >> c)
    1004           throw DataFormatError("Extra character on the end of line");
     1013          throw FormatError("Extra character on the end of line");
    10051014
    10061015        Arc a;
     
    10131022            std::ostringstream msg;
    10141023            msg << "Item not found: " << source_token;
    1015             throw DataFormatError(msg.str().c_str());
     1024            throw FormatError(msg.str());
    10161025          }
    10171026          Node source = it->second;
     
    10211030            std::ostringstream msg;
    10221031            msg << "Item not found: " << target_token;
    1023             throw DataFormatError(msg.str().c_str());
     1032            throw FormatError(msg.str());
    10241033          }
    10251034          Node target = it->second;
     
    10301039        } else {
    10311040          if (label_index == -1)
    1032             throw DataFormatError("Label map not found in file");
     1041            throw FormatError("Label map not found in file");
    10331042          typename std::map<std::string, Arc>::iterator it =
    10341043            _arc_index.find(tokens[label_index]);
     
    10361045            std::ostringstream msg;
    10371046            msg << "Arc with label not found: " << tokens[label_index];
    1038             throw DataFormatError(msg.str().c_str());
     1047            throw FormatError(msg.str());
    10391048          }
    10401049          a = it->second;
     
    10611070        std::string attr, token;
    10621071        if (!_reader_bits::readToken(line, attr))
    1063           throw DataFormatError("Attribute name not found");
     1072          throw FormatError("Attribute name not found");
    10641073        if (!_reader_bits::readToken(line, token))
    1065           throw DataFormatError("Attribute value not found");
     1074          throw FormatError("Attribute value not found");
    10661075        if (line >> c)
    1067           throw DataFormatError("Extra character on the end of line");
     1076          throw FormatError("Extra character on the end of line");
    10681077
    10691078        {
     
    10721081            std::ostringstream msg;
    10731082            msg << "Multiple occurence of attribute " << attr;
    1074             throw DataFormatError(msg.str().c_str());
     1083            throw FormatError(msg.str());
    10751084          }
    10761085          read_attr.insert(attr);
     
    10941103          std::ostringstream msg;
    10951104          msg << "Attribute not found in file: " << it->first;
    1096           throw DataFormatError(msg.str().c_str());
     1105          throw FormatError(msg.str());
    10971106        }
    10981107      }
     
    11101119      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
    11111120      if (!*_is) {
    1112         throw DataFormatError("Cannot find file");
     1121        throw FormatError("Cannot find file");
    11131122      }
    11141123
     
    11301139
    11311140          if (line >> c)
    1132             throw DataFormatError("Extra character on the end of line");
     1141            throw FormatError("Extra character on the end of line");
    11331142
    11341143          if (section == "nodes" && !nodes_done) {
     
    11521161            skipSection();
    11531162          }
    1154         } catch (DataFormatError& error) {
     1163        } catch (FormatError& error) {
    11551164          error.line(line_num);
     1165          error.file(_filename);
    11561166          throw;
    11571167        }
     
    11591169
    11601170      if (!nodes_done) {
    1161         throw DataFormatError("Section @nodes not found");
     1171        throw FormatError("Section @nodes not found");
    11621172      }
    11631173
    11641174      if (!arcs_done) {
    1165         throw DataFormatError("Section @arcs not found");
     1175        throw FormatError("Section @arcs not found");
    11661176      }
    11671177
    11681178      if (!attributes_done && !_attributes.empty()) {
    1169         throw DataFormatError("Section @attributes not found");
     1179        throw FormatError("Section @attributes not found");
    11701180      }
    11711181
     
    12451255    std::istream* _is;
    12461256    bool local_is;
     1257    std::string _filename;
    12471258
    12481259    Graph& _graph;
     
    12941305    /// file.
    12951306    GraphReader(const std::string& fn, Graph& graph)
    1296       : _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),
    12971309        _use_nodes(false), _use_edges(false),
    1298         _skip_nodes(false), _skip_edges(false) {}
     1310        _skip_nodes(false), _skip_edges(false) {
     1311      if (!(*_is)) throw IoError(fn, "Cannot open file");
     1312    }
    12991313
    13001314    /// \brief Constructor
     
    13031317    /// file.
    13041318    GraphReader(const char* fn, Graph& graph)
    1305       : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
     1319      : _is(new std::ifstream(fn)), local_is(true),
     1320        _filename(fn), _graph(graph),
    13061321        _use_nodes(false), _use_edges(false),
    1307         _skip_nodes(false), _skip_edges(false) {}
     1322        _skip_nodes(false), _skip_edges(false) {
     1323      if (!(*_is)) throw IoError(fn, "Cannot open file");
     1324    }
    13081325
    13091326    /// \brief Destructor
     
    16741691        if (readSuccess() && line) line.putback(c);
    16751692        if (!_node_maps.empty())
    1676           throw DataFormatError("Cannot find map names");
     1693          throw FormatError("Cannot find map names");
    16771694        return;
    16781695      }
     
    16881705            std::ostringstream msg;
    16891706            msg << "Multiple occurence of node map: " << map;
    1690             throw DataFormatError(msg.str().c_str());
     1707            throw FormatError(msg.str());
    16911708          }
    16921709          maps.insert(std::make_pair(map, index));
     
    17001717            std::ostringstream msg;
    17011718            msg << "Map not found in file: " << _node_maps[i].first;
    1702             throw DataFormatError(msg.str().c_str());
     1719            throw FormatError(msg.str());
    17031720          }
    17041721          map_index[i] = jt->second;
     
    17241741            std::ostringstream msg;
    17251742            msg << "Column not found (" << i + 1 << ")";
    1726             throw DataFormatError(msg.str().c_str());
     1743            throw FormatError(msg.str());
    17271744          }
    17281745        }
    17291746        if (line >> std::ws >> c)
    1730           throw DataFormatError("Extra character on the end of line");
     1747          throw FormatError("Extra character on the end of line");
    17311748
    17321749        Node n;
     
    17371754        } else {
    17381755          if (label_index == -1)
    1739             throw DataFormatError("Label map not found in file");
     1756            throw FormatError("Label map not found in file");
    17401757          typename std::map<std::string, Node>::iterator it =
    17411758            _node_index.find(tokens[label_index]);
     
    17431760            std::ostringstream msg;
    17441761            msg << "Node with label not found: " << tokens[label_index];
    1745             throw DataFormatError(msg.str().c_str());
     1762            throw FormatError(msg.str());
    17461763          }
    17471764          n = it->second;
     
    17671784        if (readSuccess() && line) line.putback(c);
    17681785        if (!_edge_maps.empty())
    1769           throw DataFormatError("Cannot find map names");
     1786          throw FormatError("Cannot find map names");
    17701787        return;
    17711788      }
     
    17811798            std::ostringstream msg;
    17821799            msg << "Multiple occurence of edge map: " << map;
    1783             throw DataFormatError(msg.str().c_str());
     1800            throw FormatError(msg.str());
    17841801          }
    17851802          maps.insert(std::make_pair(map, index));
     
    17931810            std::ostringstream msg;
    17941811            msg << "Map not found in file: " << _edge_maps[i].first;
    1795             throw DataFormatError(msg.str().c_str());
     1812            throw FormatError(msg.str());
    17961813          }
    17971814          map_index[i] = jt->second;
     
    18161833
    18171834        if (!_reader_bits::readToken(line, source_token))
    1818           throw DataFormatError("Node u not found");
     1835          throw FormatError("Node u not found");
    18191836
    18201837        if (!_reader_bits::readToken(line, target_token))
    1821           throw DataFormatError("Node v not found");
     1838          throw FormatError("Node v not found");
    18221839
    18231840        std::vector<std::string> tokens(map_num);
     
    18261843            std::ostringstream msg;
    18271844            msg << "Column not found (" << i + 1 << ")";
    1828             throw DataFormatError(msg.str().c_str());
     1845            throw FormatError(msg.str());
    18291846          }
    18301847        }
    18311848        if (line >> std::ws >> c)
    1832           throw DataFormatError("Extra character on the end of line");
     1849          throw FormatError("Extra character on the end of line");
    18331850
    18341851        Edge e;
     
    18411858            std::ostringstream msg;
    18421859            msg << "Item not found: " << source_token;
    1843             throw DataFormatError(msg.str().c_str());
     1860            throw FormatError(msg.str());
    18441861          }
    18451862          Node source = it->second;
     
    18491866            std::ostringstream msg;
    18501867            msg << "Item not found: " << target_token;
    1851             throw DataFormatError(msg.str().c_str());
     1868            throw FormatError(msg.str());
    18521869          }
    18531870          Node target = it->second;
     
    18581875        } else {
    18591876          if (label_index == -1)
    1860             throw DataFormatError("Label map not found in file");
     1877            throw FormatError("Label map not found in file");
    18611878          typename std::map<std::string, Edge>::iterator it =
    18621879            _edge_index.find(tokens[label_index]);
     
    18641881            std::ostringstream msg;
    18651882            msg << "Edge with label not found: " << tokens[label_index];
    1866             throw DataFormatError(msg.str().c_str());
     1883            throw FormatError(msg.str());
    18671884          }
    18681885          e = it->second;
     
    18891906        std::string attr, token;
    18901907        if (!_reader_bits::readToken(line, attr))
    1891           throw DataFormatError("Attribute name not found");
     1908          throw FormatError("Attribute name not found");
    18921909        if (!_reader_bits::readToken(line, token))
    1893           throw DataFormatError("Attribute value not found");
     1910          throw FormatError("Attribute value not found");
    18941911        if (line >> c)
    1895           throw DataFormatError("Extra character on the end of line");
     1912          throw FormatError("Extra character on the end of line");
    18961913
    18971914        {
     
    19001917            std::ostringstream msg;
    19011918            msg << "Multiple occurence of attribute " << attr;
    1902             throw DataFormatError(msg.str().c_str());
     1919            throw FormatError(msg.str());
    19031920          }
    19041921          read_attr.insert(attr);
     
    19221939          std::ostringstream msg;
    19231940          msg << "Attribute not found in file: " << it->first;
    1924           throw DataFormatError(msg.str().c_str());
     1941          throw FormatError(msg.str());
    19251942        }
    19261943      }
     
    19561973
    19571974          if (line >> c)
    1958             throw DataFormatError("Extra character on the end of line");
     1975            throw FormatError("Extra character on the end of line");
    19591976
    19601977          if (section == "nodes" && !nodes_done) {
     
    19781995            skipSection();
    19791996          }
    1980         } catch (DataFormatError& error) {
     1997        } catch (FormatError& error) {
    19811998          error.line(line_num);
     1999          error.file(_filename);
    19822000          throw;
    19832001        }
     
    19852003
    19862004      if (!nodes_done) {
    1987         throw DataFormatError("Section @nodes not found");
     2005        throw FormatError("Section @nodes not found");
    19882006      }
    19892007
    19902008      if (!edges_done) {
    1991         throw DataFormatError("Section @edges not found");
     2009        throw FormatError("Section @edges not found");
    19922010      }
    19932011
    19942012      if (!attributes_done && !_attributes.empty()) {
    1995         throw DataFormatError("Section @attributes not found");
     2013        throw FormatError("Section @attributes not found");
    19962014      }
    19972015
     
    20552073    std::istream* _is;
    20562074    bool local_is;
     2075    std::string _filename;
    20572076
    20582077    typedef std::map<std::string, _reader_bits::Section*> Sections;
     
    20752094    /// Construct a section reader, which reads from the given file.
    20762095    SectionReader(const std::string& fn)
    2077       : _is(new std::ifstream(fn.c_str())), local_is(true) {}
     2096      : _is(new std::ifstream(fn.c_str())), local_is(true),
     2097        _filename(fn) {
     2098      if (!(*_is)) throw IoError(fn, "Cannot open file");
     2099    }
    20782100
    20792101    /// \brief Constructor
     
    20812103    /// Construct a section reader, which reads from the given file.
    20822104    SectionReader(const char* fn)
    2083       : _is(new std::ifstream(fn)), local_is(true) {}
     2105      : _is(new std::ifstream(fn)), local_is(true),
     2106        _filename(fn) {
     2107      if (!(*_is)) throw IoError(fn, "Cannot open file");
     2108    }
    20842109
    20852110    /// \brief Destructor
     
    22372262
    22382263          if (line >> c)
    2239             throw DataFormatError("Extra character on the end of line");
     2264            throw FormatError("Extra character on the end of line");
    22402265
    22412266          if (extra_sections.find(section) != extra_sections.end()) {
    22422267            std::ostringstream msg;
    22432268            msg << "Multiple occurence of section " << section;
    2244             throw DataFormatError(msg.str().c_str());
     2269            throw FormatError(msg.str());
    22452270          }
    22462271          Sections::iterator it = _sections.find(section);
     
    22512276          readLine();
    22522277          skipSection();
    2253         } catch (DataFormatError& error) {
     2278        } catch (FormatError& error) {
    22542279          error.line(line_num);
     2280          error.file(_filename);
    22552281          throw;
    22562282        }
     
    22612287          std::ostringstream os;
    22622288          os << "Cannot find section: " << it->first;
    2263           throw DataFormatError(os.str().c_str());
     2289          throw FormatError(os.str());
    22642290        }
    22652291      }
     
    23612387    /// file.
    23622388    LgfContents(const std::string& fn)
    2363       : _is(new std::ifstream(fn.c_str())), local_is(true) {}
     2389      : _is(new std::ifstream(fn.c_str())), local_is(true) {
     2390      if (!(*_is)) throw IoError(fn, "Cannot open file");
     2391    }
    23642392
    23652393    /// \brief Constructor
     
    23682396    /// file.
    23692397    LgfContents(const char* fn)
    2370       : _is(new std::ifstream(fn)), local_is(true) {}
     2398      : _is(new std::ifstream(fn)), local_is(true) {
     2399      if (!(*_is)) throw IoError(fn, "Cannot open file");
     2400    }
    23712401
    23722402    /// \brief Destructor
Note: See TracChangeset for help on using the changeset viewer.