COIN-OR::LEMON - Graph Library

Changeset 1208:f486d30e4e7b in lemon-0.x for src/lemon/graph_reader.h


Ignore:
Timestamp:
03/09/05 15:15:22 (19 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1627
Message:

Easy input-output function for common graphs.
Modified Exception handling in graph_reader.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/lemon/graph_reader.h

    r1164 r1208  
    3636  // Exceptions
    3737
    38   class IOException {
    39   public:
    40     virtual ~IOException() {}
    41     virtual string what() const = 0;
    42   };
    43 
    44   class DataFormatException : public IOException {
    45     std::string message;
    46   public:
    47     DataFormatException(const std::string& _message)
    48       : message(_message) {}
    49     std::string what() const {
    50       return "DataFormatException: " + message;
    51     }
    52   };
    53 
    54   template <typename _Exception>
    55   class StreamException : public _Exception {
    56   public:
    57     typedef _Exception Exception;
    58     StreamException(int _line, Exception _exception)
    59       : Exception(_exception), line_num(_line) {}
    60     virtual int line() const {
    61       return line_num;
    62     }
    63 
    64     virtual ~StreamException() {}
    65 
    66     virtual std::string what() const {
    67       ostringstream os;
    68       os << Exception::what() << " in line " << line();
    69       return os.str();
    70     }
    71   private:
    72     int line_num;
    73   }; 
    74 
    7538
    7639  /// \brief Standard ReaderTraits for the GraphReader class.
     
    9255      void read(std::istream& is, Value& value) {
    9356        if (!(is >> value))
    94           throw DataFormatException("Default Reader format exception");
     57          throw DataFormatError("Default reader format exception");
    9558      }
    9659    };
     
    12386      is >> ws;
    12487      if (!is.get(c) || c != '\"')
    125         throw DataFormatException("Quoted string format");
     88        throw DataFormatError("Quoted string format error");
    12689      while (is.get(c) && c != '\"') {
    12790        if (escaped && c == '\\') {
     
    13194        }
    13295      }
    133       if (!is) throw DataFormatException("Quoted string format");
     96      if (!is) throw DataFormatError("Quoted string format error");
    13497    }
    13598
     
    165128          int code;
    166129          if (!is.get(c) || !isHex(c))
    167             throw DataFormatException("Escape format exception");
     130            throw DataFormatError("Escape format error");
    168131          else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
    169132          else code = code * 16 + valueHex(c);
     
    174137          int code;
    175138          if (!isOct(c))
    176             throw DataFormatException("Escape format exception");
     139            throw DataFormatError("Escape format error");
    177140          else if (code = valueOct(c), !is.get(c) || !isOct(c))
    178141            is.putback(c);
     
    226189    /// \brief Construct a new GraphReader.
    227190    ///
    228     /// Construct a new GraphReader. It reads from the given map,
    229     /// it constructs the given map and it use the given reader as the
    230     /// default skipper.
     191    /// Construct a new GraphReader. It reads into the given graph
     192    /// and it use the given reader as the default skipper.
    231193    GraphReader(std::istream& _is, Graph& _graph,
    232194                const DefaultReader& _reader = DefaultReader())
     
    266228                             const Reader& reader = Reader()) {
    267229      if (node_map_readers.find(name) != node_map_readers.end()) {
    268         throw Exception() << "Multiple read rule for node map: " << name;
     230        ErrorMessage msg;
     231        msg << "Multiple read rule for node map: " << name;
     232        throw IOLogicError(msg.message());
    269233      }
    270234      node_map_readers.insert(
     
    280244                             const Reader& reader = Reader()) {
    281245      if (node_map_readers.find(name) != node_map_readers.end()) {
    282         throw Exception() << "Multiple read rule for node map: " << name;
     246        ErrorMessage msg;
     247        msg << "Multiple read rule for node map: " << name;
     248        throw IOLogicError(msg.message());
    283249      }
    284250      node_map_readers.insert(
     
    304270                             const Reader& reader = Reader()) {
    305271      if (edge_map_readers.find(name) != edge_map_readers.end()) {
    306         throw Exception() << "Multiple read rule for edge map: " << name;
     272        ErrorMessage msg;
     273        msg << "Multiple read rule for edge map: " << name;
     274        throw IOLogicError(msg.message());
    307275      }
    308276      edge_map_readers.insert(
     
    318286                             const Reader& reader = Reader()) {
    319287      if (edge_map_readers.find(name) != edge_map_readers.end()) {
    320         throw Exception() << "Multiple read rule for edge map: " << name;
     288        ErrorMessage msg;
     289        msg << "Multiple read rule for edge map: " << name;
     290        throw IOLogicError(msg.message());
    321291      }
    322292      edge_map_readers.insert(
     
    330300    GraphReader& addNode(std::string name, Node& node) {
    331301      if (node_readers.find(name) != node_readers.end()) {
    332         throw Exception() << "Multiple read rule for node";
     302        ErrorMessage msg;
     303        msg << "Multiple read rule for node: " << name;
     304        throw IOLogicError(msg.message());
    333305      }
    334306      node_readers.insert(make_pair(name, &node));
     
    341313    GraphReader& addEdge(std::string name, Edge& edge) {
    342314      if (edge_readers.find(name) != edge_readers.end()) {
    343         throw Exception() << "Multiple read rule for edge";
     315        ErrorMessage msg;
     316        msg << "Multiple read rule for edge: " << name;
     317        throw IOLogicError(msg.message());
    344318      }
    345319      edge_readers.insert(make_pair(name, &edge));
     
    369343        }
    370344        if (line.find("@end") != 0) {
    371           throw DataFormatException("Invalid control sequence: " + line);
    372         }
    373       } catch (DataFormatException e) {
    374         throw StreamException<DataFormatException>(line_num, e);
     345          throw DataFormatError("Invalid control sequence error");
     346        }
     347      } catch (DataFormatError e) {
     348        e.line(line_num);
     349        throw e;
    375350      }
    376351    }
     
    400375
    401376      if (index.size() == 0) {
    402         throw DataFormatException("No node map found");
     377        throw DataFormatError("Cannot find node id map");
    403378      }
    404379
     
    437412
    438413      if (index.size() == 0) {
    439         throw DataFormatException("No edge map found");
     414        throw DataFormatError("Cannot find edge id map");
    440415      }
    441416
     
    493468        }
    494469      }
    495       throw DataFormatException("End of stream");
     470      throw DataFormatError("End of stream error");
    496471    }
    497472   
     
    533508          inverse.insert(make_pair(value, item));
    534509        } else {
    535           throw DataFormatException("Multiple ID occurence");
     510          throw DataFormatError("Multiple ID occurence");
    536511        }
    537512      }
     
    544519          return it->second;
    545520        } else {
    546           throw DataFormatException("Invalid ID");
     521          throw DataFormatError("Invalid ID error");
    547522        }
    548523      }     
     
    571546          inverse.insert(make_pair(value, item));
    572547        } else {
    573           throw DataFormatException("Multiple ID occurence");
     548          throw DataFormatError("Multiple ID occurence error");
    574549        }
    575550      }
     
    582557          return it->second;
    583558        } else {
    584           throw DataFormatException("Invalid ID");
     559          throw DataFormatError("Invalid ID error");
    585560        }
    586561      }     
     
    673648  };
    674649
     650  /// Ready to use reader function. 
     651  template<typename Graph, typename CapacityMap, typename CostMap>
     652  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
     653                  typename Graph::Node &s, typename Graph::Node &t,
     654                  CostMap& cost) {
     655    GraphReader<Graph> reader(is, g);
     656    reader.addEdgeMap("capacity", capacity);
     657    reader.addEdgeMap("cost", cost);
     658    reader.addNode("source", s);
     659    reader.addNode("target", t);
     660    reader.run();
     661  }
     662
     663  template<typename Graph, typename CapacityMap>
     664  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
     665                  typename Graph::Node &s, typename Graph::Node &t) {
     666    GraphReader<Graph> reader(is, g);
     667    reader.addEdgeMap("capacity", capacity);
     668    reader.addNode("source", s);
     669    reader.addNode("target", t);
     670    reader.run();
     671  }
     672
     673  template<typename Graph, typename CapacityMap>
     674  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
     675                  typename Graph::Node &s) {
     676    GraphReader<Graph> reader(is, g);
     677    reader.addEdgeMap("capacity", capacity);
     678    reader.addNode("source", s);
     679    reader.run();
     680  }
     681
     682  template<typename Graph, typename CapacityMap>
     683  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
     684    GraphReader<Graph> reader(is, g);
     685    reader.addEdgeMap("capacity", capacity);
     686    reader.run();
     687  }
     688
     689  template<typename Graph>
     690  void readGraph(std::istream& is, Graph &g) {
     691    GraphReader<Graph> reader(is, g);
     692    reader.run();
     693  }
     694
    675695}
Note: See TracChangeset for help on using the changeset viewer.