lemon/lgf_reader.h
changeset 290 f6899946c1ac
parent 236 da953e387d31
child 291 d901321d6555
equal deleted inserted replaced
26:2504176b6a66 27:ed32976bbf5b
    46     template <typename Value>
    46     template <typename Value>
    47     struct DefaultConverter {
    47     struct DefaultConverter {
    48       Value operator()(const std::string& str) {
    48       Value operator()(const std::string& str) {
    49         std::istringstream is(str);
    49         std::istringstream is(str);
    50         Value value;
    50         Value value;
    51         is >> value;
    51         if (!(is >> value)) {
       
    52           throw FormatError("Cannot read token");
       
    53         }
    52 
    54 
    53         char c;
    55         char c;
    54         if (is >> std::ws >> c) {
    56         if (is >> std::ws >> c) {
    55           throw DataFormatError("Remaining characters in token");
    57           throw FormatError("Remaining characters in token");
    56         }
    58         }
    57         return value;
    59         return value;
    58       }
    60       }
    59     };
    61     };
    60 
    62 
   164         typename std::map<std::string, Value>::const_iterator it =
   166         typename std::map<std::string, Value>::const_iterator it =
   165           _map.find(str);
   167           _map.find(str);
   166         if (it == _map.end()) {
   168         if (it == _map.end()) {
   167           std::ostringstream msg;
   169           std::ostringstream msg;
   168           msg << "Item not found: " << str;
   170           msg << "Item not found: " << str;
   169           throw DataFormatError(msg.str().c_str());
   171           throw FormatError(msg.str());
   170         }
   172         }
   171         return it->second;
   173         return it->second;
   172       }
   174       }
   173     };
   175     };
   174 
   176 
   182                                              typename Graph::Edge>& map)
   184                                              typename Graph::Edge>& map)
   183         : _graph(graph), _map(map) {}
   185         : _graph(graph), _map(map) {}
   184 
   186 
   185       typename Graph::Arc operator()(const std::string& str) {
   187       typename Graph::Arc operator()(const std::string& str) {
   186         if (str.empty() || (str[0] != '+' && str[0] != '-')) {
   188         if (str.empty() || (str[0] != '+' && str[0] != '-')) {
   187           throw DataFormatError("Item must start with '+' or '-'");
   189           throw FormatError("Item must start with '+' or '-'");
   188         }
   190         }
   189         typename std::map<std::string, typename Graph::Edge>
   191         typename std::map<std::string, typename Graph::Edge>
   190           ::const_iterator it = _map.find(str.substr(1));
   192           ::const_iterator it = _map.find(str.substr(1));
   191         if (it == _map.end()) {
   193         if (it == _map.end()) {
   192           throw DataFormatError("Item not found");
   194           throw FormatError("Item not found");
   193         }
   195         }
   194         return _graph.direct(it->second, str[0] == '+');
   196         return _graph.direct(it->second, str[0] == '+');
   195       }
   197       }
   196     };
   198     };
   197 
   199 
   233     }
   235     }
   234 
   236 
   235     inline char readEscape(std::istream& is) {
   237     inline char readEscape(std::istream& is) {
   236       char c;
   238       char c;
   237       if (!is.get(c))
   239       if (!is.get(c))
   238         throw DataFormatError("Escape format error");
   240         throw FormatError("Escape format error");
   239 
   241 
   240       switch (c) {
   242       switch (c) {
   241       case '\\':
   243       case '\\':
   242         return '\\';
   244         return '\\';
   243       case '\"':
   245       case '\"':
   262         return '\v';
   264         return '\v';
   263       case 'x':
   265       case 'x':
   264         {
   266         {
   265           int code;
   267           int code;
   266           if (!is.get(c) || !isHex(c))
   268           if (!is.get(c) || !isHex(c))
   267             throw DataFormatError("Escape format error");
   269             throw FormatError("Escape format error");
   268           else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
   270           else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
   269           else code = code * 16 + valueHex(c);
   271           else code = code * 16 + valueHex(c);
   270           return code;
   272           return code;
   271         }
   273         }
   272       default:
   274       default:
   273         {
   275         {
   274           int code;
   276           int code;
   275           if (!isOct(c))
   277           if (!isOct(c))
   276             throw DataFormatError("Escape format error");
   278             throw FormatError("Escape format error");
   277           else if (code = valueOct(c), !is.get(c) || !isOct(c))
   279           else if (code = valueOct(c), !is.get(c) || !isOct(c))
   278             is.putback(c);
   280             is.putback(c);
   279           else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
   281           else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
   280             is.putback(c);
   282             is.putback(c);
   281           else code = code * 8 + valueOct(c);
   283           else code = code * 8 + valueOct(c);
   298           if (c == '\\')
   300           if (c == '\\')
   299             c = readEscape(is);
   301             c = readEscape(is);
   300           os << c;
   302           os << c;
   301         }
   303         }
   302         if (!is)
   304         if (!is)
   303           throw DataFormatError("Quoted format error");
   305           throw FormatError("Quoted format error");
   304       } else {
   306       } else {
   305         is.putback(c);
   307         is.putback(c);
   306         while (is.get(c) && !isWhiteSpace(c)) {
   308         while (is.get(c) && !isWhiteSpace(c)) {
   307           if (c == '\\')
   309           if (c == '\\')
   308             c = readEscape(is);
   310             c = readEscape(is);
   458   private:
   460   private:
   459 
   461 
   460 
   462 
   461     std::istream* _is;
   463     std::istream* _is;
   462     bool local_is;
   464     bool local_is;
       
   465     std::string _filename;
   463 
   466 
   464     Digraph& _digraph;
   467     Digraph& _digraph;
   465 
   468 
   466     std::string _nodes_caption;
   469     std::string _nodes_caption;
   467     std::string _arcs_caption;
   470     std::string _arcs_caption;
   507     /// \brief Constructor
   510     /// \brief Constructor
   508     ///
   511     ///
   509     /// Construct a directed graph reader, which reads from the given
   512     /// Construct a directed graph reader, which reads from the given
   510     /// file.
   513     /// file.
   511     DigraphReader(const std::string& fn, Digraph& digraph)
   514     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),
   513         _use_nodes(false), _use_arcs(false),
   517         _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     }
   515 
   521 
   516     /// \brief Constructor
   522     /// \brief Constructor
   517     ///
   523     ///
   518     /// Construct a directed graph reader, which reads from the given
   524     /// Construct a directed graph reader, which reads from the given
   519     /// file.
   525     /// file.
   520     DigraphReader(const char* fn, Digraph& digraph)
   526     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),
   522         _use_nodes(false), _use_arcs(false),
   529         _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     }
   524 
   533 
   525     /// \brief Destructor
   534     /// \brief Destructor
   526     ~DigraphReader() {
   535     ~DigraphReader() {
   527       for (typename NodeMaps::iterator it = _node_maps.begin();
   536       for (typename NodeMaps::iterator it = _node_maps.begin();
   528            it != _node_maps.end(); ++it) {
   537            it != _node_maps.end(); ++it) {
   843 
   852 
   844       char c;
   853       char c;
   845       if (!readLine() || !(line >> c) || c == '@') {
   854       if (!readLine() || !(line >> c) || c == '@') {
   846         if (readSuccess() && line) line.putback(c);
   855         if (readSuccess() && line) line.putback(c);
   847         if (!_node_maps.empty())
   856         if (!_node_maps.empty())
   848           throw DataFormatError("Cannot find map names");
   857           throw FormatError("Cannot find map names");
   849         return;
   858         return;
   850       }
   859       }
   851       line.putback(c);
   860       line.putback(c);
   852 
   861 
   853       {
   862       {
   857         int index = 0;
   866         int index = 0;
   858         while (_reader_bits::readToken(line, map)) {
   867         while (_reader_bits::readToken(line, map)) {
   859           if (maps.find(map) != maps.end()) {
   868           if (maps.find(map) != maps.end()) {
   860             std::ostringstream msg;
   869             std::ostringstream msg;
   861             msg << "Multiple occurence of node map: " << map;
   870             msg << "Multiple occurence of node map: " << map;
   862             throw DataFormatError(msg.str().c_str());
   871             throw FormatError(msg.str());
   863           }
   872           }
   864           maps.insert(std::make_pair(map, index));
   873           maps.insert(std::make_pair(map, index));
   865           ++index;
   874           ++index;
   866         }
   875         }
   867 
   876 
   869           std::map<std::string, int>::iterator jt =
   878           std::map<std::string, int>::iterator jt =
   870             maps.find(_node_maps[i].first);
   879             maps.find(_node_maps[i].first);
   871           if (jt == maps.end()) {
   880           if (jt == maps.end()) {
   872             std::ostringstream msg;
   881             std::ostringstream msg;
   873             msg << "Map not found in file: " << _node_maps[i].first;
   882             msg << "Map not found in file: " << _node_maps[i].first;
   874             throw DataFormatError(msg.str().c_str());
   883             throw FormatError(msg.str());
   875           }
   884           }
   876           map_index[i] = jt->second;
   885           map_index[i] = jt->second;
   877         }
   886         }
   878 
   887 
   879         {
   888         {
   893         std::vector<std::string> tokens(map_num);
   902         std::vector<std::string> tokens(map_num);
   894         for (int i = 0; i < map_num; ++i) {
   903         for (int i = 0; i < map_num; ++i) {
   895           if (!_reader_bits::readToken(line, tokens[i])) {
   904           if (!_reader_bits::readToken(line, tokens[i])) {
   896             std::ostringstream msg;
   905             std::ostringstream msg;
   897             msg << "Column not found (" << i + 1 << ")";
   906             msg << "Column not found (" << i + 1 << ")";
   898             throw DataFormatError(msg.str().c_str());
   907             throw FormatError(msg.str());
   899           }
   908           }
   900         }
   909         }
   901         if (line >> std::ws >> c)
   910         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");
   903 
   912 
   904         Node n;
   913         Node n;
   905         if (!_use_nodes) {
   914         if (!_use_nodes) {
   906           n = _digraph.addNode();
   915           n = _digraph.addNode();
   907           if (label_index != -1)
   916           if (label_index != -1)
   908             _node_index.insert(std::make_pair(tokens[label_index], n));
   917             _node_index.insert(std::make_pair(tokens[label_index], n));
   909         } else {
   918         } else {
   910           if (label_index == -1)
   919           if (label_index == -1)
   911             throw DataFormatError("Label map not found in file");
   920             throw FormatError("Label map not found in file");
   912           typename std::map<std::string, Node>::iterator it =
   921           typename std::map<std::string, Node>::iterator it =
   913             _node_index.find(tokens[label_index]);
   922             _node_index.find(tokens[label_index]);
   914           if (it == _node_index.end()) {
   923           if (it == _node_index.end()) {
   915             std::ostringstream msg;
   924             std::ostringstream msg;
   916             msg << "Node with label not found: " << tokens[label_index];
   925             msg << "Node with label not found: " << tokens[label_index];
   917             throw DataFormatError(msg.str().c_str());
   926             throw FormatError(msg.str());
   918           }
   927           }
   919           n = it->second;
   928           n = it->second;
   920         }
   929         }
   921 
   930 
   922         for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
   931         for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
   936 
   945 
   937       char c;
   946       char c;
   938       if (!readLine() || !(line >> c) || c == '@') {
   947       if (!readLine() || !(line >> c) || c == '@') {
   939         if (readSuccess() && line) line.putback(c);
   948         if (readSuccess() && line) line.putback(c);
   940         if (!_arc_maps.empty())
   949         if (!_arc_maps.empty())
   941           throw DataFormatError("Cannot find map names");
   950           throw FormatError("Cannot find map names");
   942         return;
   951         return;
   943       }
   952       }
   944       line.putback(c);
   953       line.putback(c);
   945 
   954 
   946       {
   955       {
   950         int index = 0;
   959         int index = 0;
   951         while (_reader_bits::readToken(line, map)) {
   960         while (_reader_bits::readToken(line, map)) {
   952           if (maps.find(map) != maps.end()) {
   961           if (maps.find(map) != maps.end()) {
   953             std::ostringstream msg;
   962             std::ostringstream msg;
   954             msg << "Multiple occurence of arc map: " << map;
   963             msg << "Multiple occurence of arc map: " << map;
   955             throw DataFormatError(msg.str().c_str());
   964             throw FormatError(msg.str());
   956           }
   965           }
   957           maps.insert(std::make_pair(map, index));
   966           maps.insert(std::make_pair(map, index));
   958           ++index;
   967           ++index;
   959         }
   968         }
   960 
   969 
   962           std::map<std::string, int>::iterator jt =
   971           std::map<std::string, int>::iterator jt =
   963             maps.find(_arc_maps[i].first);
   972             maps.find(_arc_maps[i].first);
   964           if (jt == maps.end()) {
   973           if (jt == maps.end()) {
   965             std::ostringstream msg;
   974             std::ostringstream msg;
   966             msg << "Map not found in file: " << _arc_maps[i].first;
   975             msg << "Map not found in file: " << _arc_maps[i].first;
   967             throw DataFormatError(msg.str().c_str());
   976             throw FormatError(msg.str());
   968           }
   977           }
   969           map_index[i] = jt->second;
   978           map_index[i] = jt->second;
   970         }
   979         }
   971 
   980 
   972         {
   981         {
   985 
   994 
   986         std::string source_token;
   995         std::string source_token;
   987         std::string target_token;
   996         std::string target_token;
   988 
   997 
   989         if (!_reader_bits::readToken(line, source_token))
   998         if (!_reader_bits::readToken(line, source_token))
   990           throw DataFormatError("Source not found");
   999           throw FormatError("Source not found");
   991 
  1000 
   992         if (!_reader_bits::readToken(line, target_token))
  1001         if (!_reader_bits::readToken(line, target_token))
   993           throw DataFormatError("Target not found");
  1002           throw FormatError("Target not found");
   994 
  1003 
   995         std::vector<std::string> tokens(map_num);
  1004         std::vector<std::string> tokens(map_num);
   996         for (int i = 0; i < map_num; ++i) {
  1005         for (int i = 0; i < map_num; ++i) {
   997           if (!_reader_bits::readToken(line, tokens[i])) {
  1006           if (!_reader_bits::readToken(line, tokens[i])) {
   998             std::ostringstream msg;
  1007             std::ostringstream msg;
   999             msg << "Column not found (" << i + 1 << ")";
  1008             msg << "Column not found (" << i + 1 << ")";
  1000             throw DataFormatError(msg.str().c_str());
  1009             throw FormatError(msg.str());
  1001           }
  1010           }
  1002         }
  1011         }
  1003         if (line >> std::ws >> c)
  1012         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");
  1005 
  1014 
  1006         Arc a;
  1015         Arc a;
  1007         if (!_use_arcs) {
  1016         if (!_use_arcs) {
  1008 
  1017 
  1009           typename NodeIndex::iterator it;
  1018           typename NodeIndex::iterator it;
  1010 
  1019 
  1011           it = _node_index.find(source_token);
  1020           it = _node_index.find(source_token);
  1012           if (it == _node_index.end()) {
  1021           if (it == _node_index.end()) {
  1013             std::ostringstream msg;
  1022             std::ostringstream msg;
  1014             msg << "Item not found: " << source_token;
  1023             msg << "Item not found: " << source_token;
  1015             throw DataFormatError(msg.str().c_str());
  1024             throw FormatError(msg.str());
  1016           }
  1025           }
  1017           Node source = it->second;
  1026           Node source = it->second;
  1018 
  1027 
  1019           it = _node_index.find(target_token);
  1028           it = _node_index.find(target_token);
  1020           if (it == _node_index.end()) {
  1029           if (it == _node_index.end()) {
  1021             std::ostringstream msg;
  1030             std::ostringstream msg;
  1022             msg << "Item not found: " << target_token;
  1031             msg << "Item not found: " << target_token;
  1023             throw DataFormatError(msg.str().c_str());
  1032             throw FormatError(msg.str());
  1024           }
  1033           }
  1025           Node target = it->second;
  1034           Node target = it->second;
  1026 
  1035 
  1027           a = _digraph.addArc(source, target);
  1036           a = _digraph.addArc(source, target);
  1028           if (label_index != -1)
  1037           if (label_index != -1)
  1029             _arc_index.insert(std::make_pair(tokens[label_index], a));
  1038             _arc_index.insert(std::make_pair(tokens[label_index], a));
  1030         } else {
  1039         } else {
  1031           if (label_index == -1)
  1040           if (label_index == -1)
  1032             throw DataFormatError("Label map not found in file");
  1041             throw FormatError("Label map not found in file");
  1033           typename std::map<std::string, Arc>::iterator it =
  1042           typename std::map<std::string, Arc>::iterator it =
  1034             _arc_index.find(tokens[label_index]);
  1043             _arc_index.find(tokens[label_index]);
  1035           if (it == _arc_index.end()) {
  1044           if (it == _arc_index.end()) {
  1036             std::ostringstream msg;
  1045             std::ostringstream msg;
  1037             msg << "Arc with label not found: " << tokens[label_index];
  1046             msg << "Arc with label not found: " << tokens[label_index];
  1038             throw DataFormatError(msg.str().c_str());
  1047             throw FormatError(msg.str());
  1039           }
  1048           }
  1040           a = it->second;
  1049           a = it->second;
  1041         }
  1050         }
  1042 
  1051 
  1043         for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
  1052         for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
  1058       while (readLine() && line >> c && c != '@') {
  1067       while (readLine() && line >> c && c != '@') {
  1059         line.putback(c);
  1068         line.putback(c);
  1060 
  1069 
  1061         std::string attr, token;
  1070         std::string attr, token;
  1062         if (!_reader_bits::readToken(line, attr))
  1071         if (!_reader_bits::readToken(line, attr))
  1063           throw DataFormatError("Attribute name not found");
  1072           throw FormatError("Attribute name not found");
  1064         if (!_reader_bits::readToken(line, token))
  1073         if (!_reader_bits::readToken(line, token))
  1065           throw DataFormatError("Attribute value not found");
  1074           throw FormatError("Attribute value not found");
  1066         if (line >> c)
  1075         if (line >> c)
  1067           throw DataFormatError("Extra character on the end of line");
  1076           throw FormatError("Extra character on the end of line");
  1068 
  1077 
  1069         {
  1078         {
  1070           std::set<std::string>::iterator it = read_attr.find(attr);
  1079           std::set<std::string>::iterator it = read_attr.find(attr);
  1071           if (it != read_attr.end()) {
  1080           if (it != read_attr.end()) {
  1072             std::ostringstream msg;
  1081             std::ostringstream msg;
  1073             msg << "Multiple occurence of attribute " << attr;
  1082             msg << "Multiple occurence of attribute " << attr;
  1074             throw DataFormatError(msg.str().c_str());
  1083             throw FormatError(msg.str());
  1075           }
  1084           }
  1076           read_attr.insert(attr);
  1085           read_attr.insert(attr);
  1077         }
  1086         }
  1078 
  1087 
  1079         {
  1088         {
  1091       for (typename Attributes::iterator it = _attributes.begin();
  1100       for (typename Attributes::iterator it = _attributes.begin();
  1092            it != _attributes.end(); ++it) {
  1101            it != _attributes.end(); ++it) {
  1093         if (read_attr.find(it->first) == read_attr.end()) {
  1102         if (read_attr.find(it->first) == read_attr.end()) {
  1094           std::ostringstream msg;
  1103           std::ostringstream msg;
  1095           msg << "Attribute not found in file: " << it->first;
  1104           msg << "Attribute not found in file: " << it->first;
  1096           throw DataFormatError(msg.str().c_str());
  1105           throw FormatError(msg.str());
  1097         }
  1106         }
  1098       }
  1107       }
  1099     }
  1108     }
  1100 
  1109 
  1101   public:
  1110   public:
  1107     ///
  1116     ///
  1108     /// This function starts the batch processing
  1117     /// This function starts the batch processing
  1109     void run() {
  1118     void run() {
  1110       LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
  1119       LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
  1111       if (!*_is) {
  1120       if (!*_is) {
  1112         throw DataFormatError("Cannot find file");
  1121         throw FormatError("Cannot find file");
  1113       }
  1122       }
  1114 
  1123 
  1115       bool nodes_done = _skip_nodes;
  1124       bool nodes_done = _skip_nodes;
  1116       bool arcs_done = _skip_arcs;
  1125       bool arcs_done = _skip_arcs;
  1117       bool attributes_done = false;
  1126       bool attributes_done = false;
  1127           line >> c;
  1136           line >> c;
  1128           _reader_bits::readToken(line, section);
  1137           _reader_bits::readToken(line, section);
  1129           _reader_bits::readToken(line, caption);
  1138           _reader_bits::readToken(line, caption);
  1130 
  1139 
  1131           if (line >> c)
  1140           if (line >> c)
  1132             throw DataFormatError("Extra character on the end of line");
  1141             throw FormatError("Extra character on the end of line");
  1133 
  1142 
  1134           if (section == "nodes" && !nodes_done) {
  1143           if (section == "nodes" && !nodes_done) {
  1135             if (_nodes_caption.empty() || _nodes_caption == caption) {
  1144             if (_nodes_caption.empty() || _nodes_caption == caption) {
  1136               readNodes();
  1145               readNodes();
  1137               nodes_done = true;
  1146               nodes_done = true;
  1149             }
  1158             }
  1150           } else {
  1159           } else {
  1151             readLine();
  1160             readLine();
  1152             skipSection();
  1161             skipSection();
  1153           }
  1162           }
  1154         } catch (DataFormatError& error) {
  1163         } catch (FormatError& error) {
  1155           error.line(line_num);
  1164           error.line(line_num);
       
  1165           error.file(_filename);
  1156           throw;
  1166           throw;
  1157         }
  1167         }
  1158       }
  1168       }
  1159 
  1169 
  1160       if (!nodes_done) {
  1170       if (!nodes_done) {
  1161         throw DataFormatError("Section @nodes not found");
  1171         throw FormatError("Section @nodes not found");
  1162       }
  1172       }
  1163 
  1173 
  1164       if (!arcs_done) {
  1174       if (!arcs_done) {
  1165         throw DataFormatError("Section @arcs not found");
  1175         throw FormatError("Section @arcs not found");
  1166       }
  1176       }
  1167 
  1177 
  1168       if (!attributes_done && !_attributes.empty()) {
  1178       if (!attributes_done && !_attributes.empty()) {
  1169         throw DataFormatError("Section @attributes not found");
  1179         throw FormatError("Section @attributes not found");
  1170       }
  1180       }
  1171 
  1181 
  1172     }
  1182     }
  1173 
  1183 
  1174     /// @}
  1184     /// @}
  1242 
  1252 
  1243   private:
  1253   private:
  1244 
  1254 
  1245     std::istream* _is;
  1255     std::istream* _is;
  1246     bool local_is;
  1256     bool local_is;
       
  1257     std::string _filename;
  1247 
  1258 
  1248     Graph& _graph;
  1259     Graph& _graph;
  1249 
  1260 
  1250     std::string _nodes_caption;
  1261     std::string _nodes_caption;
  1251     std::string _edges_caption;
  1262     std::string _edges_caption;
  1291     /// \brief Constructor
  1302     /// \brief Constructor
  1292     ///
  1303     ///
  1293     /// Construct an undirected graph reader, which reads from the given
  1304     /// Construct an undirected graph reader, which reads from the given
  1294     /// file.
  1305     /// file.
  1295     GraphReader(const std::string& fn, Graph& graph)
  1306     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),
  1297         _use_nodes(false), _use_edges(false),
  1309         _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     }
  1299 
  1313 
  1300     /// \brief Constructor
  1314     /// \brief Constructor
  1301     ///
  1315     ///
  1302     /// Construct an undirected graph reader, which reads from the given
  1316     /// Construct an undirected graph reader, which reads from the given
  1303     /// file.
  1317     /// file.
  1304     GraphReader(const char* fn, Graph& graph)
  1318     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),
  1306         _use_nodes(false), _use_edges(false),
  1321         _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     }
  1308 
  1325 
  1309     /// \brief Destructor
  1326     /// \brief Destructor
  1310     ~GraphReader() {
  1327     ~GraphReader() {
  1311       for (typename NodeMaps::iterator it = _node_maps.begin();
  1328       for (typename NodeMaps::iterator it = _node_maps.begin();
  1312            it != _node_maps.end(); ++it) {
  1329            it != _node_maps.end(); ++it) {
  1671 
  1688 
  1672       char c;
  1689       char c;
  1673       if (!readLine() || !(line >> c) || c == '@') {
  1690       if (!readLine() || !(line >> c) || c == '@') {
  1674         if (readSuccess() && line) line.putback(c);
  1691         if (readSuccess() && line) line.putback(c);
  1675         if (!_node_maps.empty())
  1692         if (!_node_maps.empty())
  1676           throw DataFormatError("Cannot find map names");
  1693           throw FormatError("Cannot find map names");
  1677         return;
  1694         return;
  1678       }
  1695       }
  1679       line.putback(c);
  1696       line.putback(c);
  1680 
  1697 
  1681       {
  1698       {
  1685         int index = 0;
  1702         int index = 0;
  1686         while (_reader_bits::readToken(line, map)) {
  1703         while (_reader_bits::readToken(line, map)) {
  1687           if (maps.find(map) != maps.end()) {
  1704           if (maps.find(map) != maps.end()) {
  1688             std::ostringstream msg;
  1705             std::ostringstream msg;
  1689             msg << "Multiple occurence of node map: " << map;
  1706             msg << "Multiple occurence of node map: " << map;
  1690             throw DataFormatError(msg.str().c_str());
  1707             throw FormatError(msg.str());
  1691           }
  1708           }
  1692           maps.insert(std::make_pair(map, index));
  1709           maps.insert(std::make_pair(map, index));
  1693           ++index;
  1710           ++index;
  1694         }
  1711         }
  1695 
  1712 
  1697           std::map<std::string, int>::iterator jt =
  1714           std::map<std::string, int>::iterator jt =
  1698             maps.find(_node_maps[i].first);
  1715             maps.find(_node_maps[i].first);
  1699           if (jt == maps.end()) {
  1716           if (jt == maps.end()) {
  1700             std::ostringstream msg;
  1717             std::ostringstream msg;
  1701             msg << "Map not found in file: " << _node_maps[i].first;
  1718             msg << "Map not found in file: " << _node_maps[i].first;
  1702             throw DataFormatError(msg.str().c_str());
  1719             throw FormatError(msg.str());
  1703           }
  1720           }
  1704           map_index[i] = jt->second;
  1721           map_index[i] = jt->second;
  1705         }
  1722         }
  1706 
  1723 
  1707         {
  1724         {
  1721         std::vector<std::string> tokens(map_num);
  1738         std::vector<std::string> tokens(map_num);
  1722         for (int i = 0; i < map_num; ++i) {
  1739         for (int i = 0; i < map_num; ++i) {
  1723           if (!_reader_bits::readToken(line, tokens[i])) {
  1740           if (!_reader_bits::readToken(line, tokens[i])) {
  1724             std::ostringstream msg;
  1741             std::ostringstream msg;
  1725             msg << "Column not found (" << i + 1 << ")";
  1742             msg << "Column not found (" << i + 1 << ")";
  1726             throw DataFormatError(msg.str().c_str());
  1743             throw FormatError(msg.str());
  1727           }
  1744           }
  1728         }
  1745         }
  1729         if (line >> std::ws >> c)
  1746         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");
  1731 
  1748 
  1732         Node n;
  1749         Node n;
  1733         if (!_use_nodes) {
  1750         if (!_use_nodes) {
  1734           n = _graph.addNode();
  1751           n = _graph.addNode();
  1735           if (label_index != -1)
  1752           if (label_index != -1)
  1736             _node_index.insert(std::make_pair(tokens[label_index], n));
  1753             _node_index.insert(std::make_pair(tokens[label_index], n));
  1737         } else {
  1754         } else {
  1738           if (label_index == -1)
  1755           if (label_index == -1)
  1739             throw DataFormatError("Label map not found in file");
  1756             throw FormatError("Label map not found in file");
  1740           typename std::map<std::string, Node>::iterator it =
  1757           typename std::map<std::string, Node>::iterator it =
  1741             _node_index.find(tokens[label_index]);
  1758             _node_index.find(tokens[label_index]);
  1742           if (it == _node_index.end()) {
  1759           if (it == _node_index.end()) {
  1743             std::ostringstream msg;
  1760             std::ostringstream msg;
  1744             msg << "Node with label not found: " << tokens[label_index];
  1761             msg << "Node with label not found: " << tokens[label_index];
  1745             throw DataFormatError(msg.str().c_str());
  1762             throw FormatError(msg.str());
  1746           }
  1763           }
  1747           n = it->second;
  1764           n = it->second;
  1748         }
  1765         }
  1749 
  1766 
  1750         for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
  1767         for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
  1764 
  1781 
  1765       char c;
  1782       char c;
  1766       if (!readLine() || !(line >> c) || c == '@') {
  1783       if (!readLine() || !(line >> c) || c == '@') {
  1767         if (readSuccess() && line) line.putback(c);
  1784         if (readSuccess() && line) line.putback(c);
  1768         if (!_edge_maps.empty())
  1785         if (!_edge_maps.empty())
  1769           throw DataFormatError("Cannot find map names");
  1786           throw FormatError("Cannot find map names");
  1770         return;
  1787         return;
  1771       }
  1788       }
  1772       line.putback(c);
  1789       line.putback(c);
  1773 
  1790 
  1774       {
  1791       {
  1778         int index = 0;
  1795         int index = 0;
  1779         while (_reader_bits::readToken(line, map)) {
  1796         while (_reader_bits::readToken(line, map)) {
  1780           if (maps.find(map) != maps.end()) {
  1797           if (maps.find(map) != maps.end()) {
  1781             std::ostringstream msg;
  1798             std::ostringstream msg;
  1782             msg << "Multiple occurence of edge map: " << map;
  1799             msg << "Multiple occurence of edge map: " << map;
  1783             throw DataFormatError(msg.str().c_str());
  1800             throw FormatError(msg.str());
  1784           }
  1801           }
  1785           maps.insert(std::make_pair(map, index));
  1802           maps.insert(std::make_pair(map, index));
  1786           ++index;
  1803           ++index;
  1787         }
  1804         }
  1788 
  1805 
  1790           std::map<std::string, int>::iterator jt =
  1807           std::map<std::string, int>::iterator jt =
  1791             maps.find(_edge_maps[i].first);
  1808             maps.find(_edge_maps[i].first);
  1792           if (jt == maps.end()) {
  1809           if (jt == maps.end()) {
  1793             std::ostringstream msg;
  1810             std::ostringstream msg;
  1794             msg << "Map not found in file: " << _edge_maps[i].first;
  1811             msg << "Map not found in file: " << _edge_maps[i].first;
  1795             throw DataFormatError(msg.str().c_str());
  1812             throw FormatError(msg.str());
  1796           }
  1813           }
  1797           map_index[i] = jt->second;
  1814           map_index[i] = jt->second;
  1798         }
  1815         }
  1799 
  1816 
  1800         {
  1817         {
  1813 
  1830 
  1814         std::string source_token;
  1831         std::string source_token;
  1815         std::string target_token;
  1832         std::string target_token;
  1816 
  1833 
  1817         if (!_reader_bits::readToken(line, source_token))
  1834         if (!_reader_bits::readToken(line, source_token))
  1818           throw DataFormatError("Node u not found");
  1835           throw FormatError("Node u not found");
  1819 
  1836 
  1820         if (!_reader_bits::readToken(line, target_token))
  1837         if (!_reader_bits::readToken(line, target_token))
  1821           throw DataFormatError("Node v not found");
  1838           throw FormatError("Node v not found");
  1822 
  1839 
  1823         std::vector<std::string> tokens(map_num);
  1840         std::vector<std::string> tokens(map_num);
  1824         for (int i = 0; i < map_num; ++i) {
  1841         for (int i = 0; i < map_num; ++i) {
  1825           if (!_reader_bits::readToken(line, tokens[i])) {
  1842           if (!_reader_bits::readToken(line, tokens[i])) {
  1826             std::ostringstream msg;
  1843             std::ostringstream msg;
  1827             msg << "Column not found (" << i + 1 << ")";
  1844             msg << "Column not found (" << i + 1 << ")";
  1828             throw DataFormatError(msg.str().c_str());
  1845             throw FormatError(msg.str());
  1829           }
  1846           }
  1830         }
  1847         }
  1831         if (line >> std::ws >> c)
  1848         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");
  1833 
  1850 
  1834         Edge e;
  1851         Edge e;
  1835         if (!_use_edges) {
  1852         if (!_use_edges) {
  1836 
  1853 
  1837           typename NodeIndex::iterator it;
  1854           typename NodeIndex::iterator it;
  1838 
  1855 
  1839           it = _node_index.find(source_token);
  1856           it = _node_index.find(source_token);
  1840           if (it == _node_index.end()) {
  1857           if (it == _node_index.end()) {
  1841             std::ostringstream msg;
  1858             std::ostringstream msg;
  1842             msg << "Item not found: " << source_token;
  1859             msg << "Item not found: " << source_token;
  1843             throw DataFormatError(msg.str().c_str());
  1860             throw FormatError(msg.str());
  1844           }
  1861           }
  1845           Node source = it->second;
  1862           Node source = it->second;
  1846 
  1863 
  1847           it = _node_index.find(target_token);
  1864           it = _node_index.find(target_token);
  1848           if (it == _node_index.end()) {
  1865           if (it == _node_index.end()) {
  1849             std::ostringstream msg;
  1866             std::ostringstream msg;
  1850             msg << "Item not found: " << target_token;
  1867             msg << "Item not found: " << target_token;
  1851             throw DataFormatError(msg.str().c_str());
  1868             throw FormatError(msg.str());
  1852           }
  1869           }
  1853           Node target = it->second;
  1870           Node target = it->second;
  1854 
  1871 
  1855           e = _graph.addEdge(source, target);
  1872           e = _graph.addEdge(source, target);
  1856           if (label_index != -1)
  1873           if (label_index != -1)
  1857             _edge_index.insert(std::make_pair(tokens[label_index], e));
  1874             _edge_index.insert(std::make_pair(tokens[label_index], e));
  1858         } else {
  1875         } else {
  1859           if (label_index == -1)
  1876           if (label_index == -1)
  1860             throw DataFormatError("Label map not found in file");
  1877             throw FormatError("Label map not found in file");
  1861           typename std::map<std::string, Edge>::iterator it =
  1878           typename std::map<std::string, Edge>::iterator it =
  1862             _edge_index.find(tokens[label_index]);
  1879             _edge_index.find(tokens[label_index]);
  1863           if (it == _edge_index.end()) {
  1880           if (it == _edge_index.end()) {
  1864             std::ostringstream msg;
  1881             std::ostringstream msg;
  1865             msg << "Edge with label not found: " << tokens[label_index];
  1882             msg << "Edge with label not found: " << tokens[label_index];
  1866             throw DataFormatError(msg.str().c_str());
  1883             throw FormatError(msg.str());
  1867           }
  1884           }
  1868           e = it->second;
  1885           e = it->second;
  1869         }
  1886         }
  1870 
  1887 
  1871         for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
  1888         for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
  1886       while (readLine() && line >> c && c != '@') {
  1903       while (readLine() && line >> c && c != '@') {
  1887         line.putback(c);
  1904         line.putback(c);
  1888 
  1905 
  1889         std::string attr, token;
  1906         std::string attr, token;
  1890         if (!_reader_bits::readToken(line, attr))
  1907         if (!_reader_bits::readToken(line, attr))
  1891           throw DataFormatError("Attribute name not found");
  1908           throw FormatError("Attribute name not found");
  1892         if (!_reader_bits::readToken(line, token))
  1909         if (!_reader_bits::readToken(line, token))
  1893           throw DataFormatError("Attribute value not found");
  1910           throw FormatError("Attribute value not found");
  1894         if (line >> c)
  1911         if (line >> c)
  1895           throw DataFormatError("Extra character on the end of line");
  1912           throw FormatError("Extra character on the end of line");
  1896 
  1913 
  1897         {
  1914         {
  1898           std::set<std::string>::iterator it = read_attr.find(attr);
  1915           std::set<std::string>::iterator it = read_attr.find(attr);
  1899           if (it != read_attr.end()) {
  1916           if (it != read_attr.end()) {
  1900             std::ostringstream msg;
  1917             std::ostringstream msg;
  1901             msg << "Multiple occurence of attribute " << attr;
  1918             msg << "Multiple occurence of attribute " << attr;
  1902             throw DataFormatError(msg.str().c_str());
  1919             throw FormatError(msg.str());
  1903           }
  1920           }
  1904           read_attr.insert(attr);
  1921           read_attr.insert(attr);
  1905         }
  1922         }
  1906 
  1923 
  1907         {
  1924         {
  1919       for (typename Attributes::iterator it = _attributes.begin();
  1936       for (typename Attributes::iterator it = _attributes.begin();
  1920            it != _attributes.end(); ++it) {
  1937            it != _attributes.end(); ++it) {
  1921         if (read_attr.find(it->first) == read_attr.end()) {
  1938         if (read_attr.find(it->first) == read_attr.end()) {
  1922           std::ostringstream msg;
  1939           std::ostringstream msg;
  1923           msg << "Attribute not found in file: " << it->first;
  1940           msg << "Attribute not found in file: " << it->first;
  1924           throw DataFormatError(msg.str().c_str());
  1941           throw FormatError(msg.str());
  1925         }
  1942         }
  1926       }
  1943       }
  1927     }
  1944     }
  1928 
  1945 
  1929   public:
  1946   public:
  1953           line >> c;
  1970           line >> c;
  1954           _reader_bits::readToken(line, section);
  1971           _reader_bits::readToken(line, section);
  1955           _reader_bits::readToken(line, caption);
  1972           _reader_bits::readToken(line, caption);
  1956 
  1973 
  1957           if (line >> c)
  1974           if (line >> c)
  1958             throw DataFormatError("Extra character on the end of line");
  1975             throw FormatError("Extra character on the end of line");
  1959 
  1976 
  1960           if (section == "nodes" && !nodes_done) {
  1977           if (section == "nodes" && !nodes_done) {
  1961             if (_nodes_caption.empty() || _nodes_caption == caption) {
  1978             if (_nodes_caption.empty() || _nodes_caption == caption) {
  1962               readNodes();
  1979               readNodes();
  1963               nodes_done = true;
  1980               nodes_done = true;
  1975             }
  1992             }
  1976           } else {
  1993           } else {
  1977             readLine();
  1994             readLine();
  1978             skipSection();
  1995             skipSection();
  1979           }
  1996           }
  1980         } catch (DataFormatError& error) {
  1997         } catch (FormatError& error) {
  1981           error.line(line_num);
  1998           error.line(line_num);
       
  1999           error.file(_filename);
  1982           throw;
  2000           throw;
  1983         }
  2001         }
  1984       }
  2002       }
  1985 
  2003 
  1986       if (!nodes_done) {
  2004       if (!nodes_done) {
  1987         throw DataFormatError("Section @nodes not found");
  2005         throw FormatError("Section @nodes not found");
  1988       }
  2006       }
  1989 
  2007 
  1990       if (!edges_done) {
  2008       if (!edges_done) {
  1991         throw DataFormatError("Section @edges not found");
  2009         throw FormatError("Section @edges not found");
  1992       }
  2010       }
  1993 
  2011 
  1994       if (!attributes_done && !_attributes.empty()) {
  2012       if (!attributes_done && !_attributes.empty()) {
  1995         throw DataFormatError("Section @attributes not found");
  2013         throw FormatError("Section @attributes not found");
  1996       }
  2014       }
  1997 
  2015 
  1998     }
  2016     }
  1999 
  2017 
  2000     /// @}
  2018     /// @}
  2052   class SectionReader {
  2070   class SectionReader {
  2053   private:
  2071   private:
  2054 
  2072 
  2055     std::istream* _is;
  2073     std::istream* _is;
  2056     bool local_is;
  2074     bool local_is;
       
  2075     std::string _filename;
  2057 
  2076 
  2058     typedef std::map<std::string, _reader_bits::Section*> Sections;
  2077     typedef std::map<std::string, _reader_bits::Section*> Sections;
  2059     Sections _sections;
  2078     Sections _sections;
  2060 
  2079 
  2061     int line_num;
  2080     int line_num;
  2072 
  2091 
  2073     /// \brief Constructor
  2092     /// \brief Constructor
  2074     ///
  2093     ///
  2075     /// Construct a section reader, which reads from the given file.
  2094     /// Construct a section reader, which reads from the given file.
  2076     SectionReader(const std::string& fn)
  2095     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     }
  2078 
  2100 
  2079     /// \brief Constructor
  2101     /// \brief Constructor
  2080     ///
  2102     ///
  2081     /// Construct a section reader, which reads from the given file.
  2103     /// Construct a section reader, which reads from the given file.
  2082     SectionReader(const char* fn)
  2104     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     }
  2084 
  2109 
  2085     /// \brief Destructor
  2110     /// \brief Destructor
  2086     ~SectionReader() {
  2111     ~SectionReader() {
  2087       for (Sections::iterator it = _sections.begin();
  2112       for (Sections::iterator it = _sections.begin();
  2088            it != _sections.end(); ++it) {
  2113            it != _sections.end(); ++it) {
  2234           line >> c;
  2259           line >> c;
  2235           _reader_bits::readToken(line, section);
  2260           _reader_bits::readToken(line, section);
  2236           _reader_bits::readToken(line, caption);
  2261           _reader_bits::readToken(line, caption);
  2237 
  2262 
  2238           if (line >> c)
  2263           if (line >> c)
  2239             throw DataFormatError("Extra character on the end of line");
  2264             throw FormatError("Extra character on the end of line");
  2240 
  2265 
  2241           if (extra_sections.find(section) != extra_sections.end()) {
  2266           if (extra_sections.find(section) != extra_sections.end()) {
  2242             std::ostringstream msg;
  2267             std::ostringstream msg;
  2243             msg << "Multiple occurence of section " << section;
  2268             msg << "Multiple occurence of section " << section;
  2244             throw DataFormatError(msg.str().c_str());
  2269             throw FormatError(msg.str());
  2245           }
  2270           }
  2246           Sections::iterator it = _sections.find(section);
  2271           Sections::iterator it = _sections.find(section);
  2247           if (it != _sections.end()) {
  2272           if (it != _sections.end()) {
  2248             extra_sections.insert(section);
  2273             extra_sections.insert(section);
  2249             it->second->process(*_is, line_num);
  2274             it->second->process(*_is, line_num);
  2250           }
  2275           }
  2251           readLine();
  2276           readLine();
  2252           skipSection();
  2277           skipSection();
  2253         } catch (DataFormatError& error) {
  2278         } catch (FormatError& error) {
  2254           error.line(line_num);
  2279           error.line(line_num);
       
  2280           error.file(_filename);
  2255           throw;
  2281           throw;
  2256         }
  2282         }
  2257       }
  2283       }
  2258       for (Sections::iterator it = _sections.begin();
  2284       for (Sections::iterator it = _sections.begin();
  2259            it != _sections.end(); ++it) {
  2285            it != _sections.end(); ++it) {
  2260         if (extra_sections.find(it->first) == extra_sections.end()) {
  2286         if (extra_sections.find(it->first) == extra_sections.end()) {
  2261           std::ostringstream os;
  2287           std::ostringstream os;
  2262           os << "Cannot find section: " << it->first;
  2288           os << "Cannot find section: " << it->first;
  2263           throw DataFormatError(os.str().c_str());
  2289           throw FormatError(os.str());
  2264         }
  2290         }
  2265       }
  2291       }
  2266     }
  2292     }
  2267 
  2293 
  2268     /// @}
  2294     /// @}
  2358     /// \brief Constructor
  2384     /// \brief Constructor
  2359     ///
  2385     ///
  2360     /// Construct an \e LGF contents reader, which reads from the given
  2386     /// Construct an \e LGF contents reader, which reads from the given
  2361     /// file.
  2387     /// file.
  2362     LgfContents(const std::string& fn)
  2388     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     }
  2364 
  2392 
  2365     /// \brief Constructor
  2393     /// \brief Constructor
  2366     ///
  2394     ///
  2367     /// Construct an \e LGF contents reader, which reads from the given
  2395     /// Construct an \e LGF contents reader, which reads from the given
  2368     /// file.
  2396     /// file.
  2369     LgfContents(const char* fn)
  2397     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     }
  2371 
  2401 
  2372     /// \brief Destructor
  2402     /// \brief Destructor
  2373     ~LgfContents() {
  2403     ~LgfContents() {
  2374       if (local_is) delete _is;
  2404       if (local_is) delete _is;
  2375     }
  2405     }