LGF content reader class
authorBalazs Dezso <deba@inf.elte.hu>
Mon, 16 Jun 2008 18:23:11 +0200
changeset 173b026e9779b28
parent 171 02f4d5d9bfd7
child 174 2ec3c1bbc687
LGF content reader class
lemon/lgf_reader.h
     1.1 --- a/lemon/lgf_reader.h	Sun Jun 15 22:05:23 2008 +0200
     1.2 +++ b/lemon/lgf_reader.h	Mon Jun 16 18:23:11 2008 +0200
     1.3 @@ -2071,6 +2071,343 @@
     1.4      GraphReader<Graph> tmp(fn, graph);
     1.5      return tmp;
     1.6    }
     1.7 +
     1.8 +  /// \ingroup lemon_io
     1.9 +  ///
    1.10 +  /// \brief Reader for the content of the \ref lgf-format "LGF" file 
    1.11 +  ///
    1.12 +  /// This class can be used to read the sections, the map names and
    1.13 +  /// the attributes from a file. Usually, the Lemon programs know
    1.14 +  /// that, which type of graph, which maps and which attributes
    1.15 +  /// should be read from a file, but in general tools (like glemon)
    1.16 +  /// the content of an LGF file should be guessed somehow. This class
    1.17 +  /// reads the graph and stores the appropriate information for
    1.18 +  /// reading the graph.
    1.19 +  ///
    1.20 +  ///\code LgfContent content("graph.lgf"); 
    1.21 +  /// content.run();
    1.22 +  ///
    1.23 +  /// // does it contain any node section and arc section
    1.24 +  /// if (content.nodeSectionNum() == 0 || content.arcSectionNum()) {
    1.25 +  ///   std::cerr << "Failure, cannot find graph" << std::endl;
    1.26 +  ///   return -1;
    1.27 +  /// }
    1.28 +  /// std::cout << "The name of the default node section : " 
    1.29 +  ///           << content.nodeSection(0) << std::endl;
    1.30 +  /// std::cout << "The number of the arc maps : " 
    1.31 +  ///           << content.arcMaps(0).size() << std::endl;
    1.32 +  /// std::cout << "The name of second arc map : " 
    1.33 +  ///           << content.arcMaps(0)[1] << std::endl;
    1.34 +  ///\endcode
    1.35 +  class LgfContent {    
    1.36 +  private:
    1.37 +
    1.38 +    std::istream* _is;
    1.39 +    bool local_is;
    1.40 +
    1.41 +    std::vector<std::string> _node_sections;
    1.42 +    std::vector<std::string> _edge_sections;
    1.43 +    std::vector<std::string> _attribute_sections;
    1.44 +    std::vector<std::string> _extra_sections;
    1.45 +
    1.46 +    std::vector<bool> _arc_sections;
    1.47 +
    1.48 +    std::vector<std::vector<std::string> > _node_maps;
    1.49 +    std::vector<std::vector<std::string> > _edge_maps;
    1.50 +
    1.51 +    std::vector<std::vector<std::string> > _attributes;
    1.52 +
    1.53 +
    1.54 +    int line_num;
    1.55 +    std::istringstream line;
    1.56 +    
    1.57 +  public:
    1.58 +
    1.59 +    /// \brief Constructor
    1.60 +    ///
    1.61 +    /// Construct an \e LGF content reader, which reads from the given
    1.62 +    /// input stream.
    1.63 +    LgfContent(std::istream& is) 
    1.64 +      : _is(&is), local_is(false) {}
    1.65 +
    1.66 +    /// \brief Constructor
    1.67 +    ///
    1.68 +    /// Construct an \e LGF content reader, which reads from the given
    1.69 +    /// file.
    1.70 +    LgfContent(const std::string& fn) 
    1.71 +      : _is(new std::ifstream(fn.c_str())), local_is(true) {}
    1.72 +
    1.73 +    /// \brief Constructor
    1.74 +    ///
    1.75 +    /// Construct an \e LGF content reader, which reads from the given
    1.76 +    /// file.
    1.77 +    LgfContent(const char* fn)
    1.78 +      : _is(new std::ifstream(fn)), local_is(true) {}
    1.79 +
    1.80 +    /// \brief Copy constructor
    1.81 +    ///
    1.82 +    /// The copy constructor transfers all data from the other reader,
    1.83 +    /// therefore the copied reader will not be usable more. 
    1.84 +    LgfContent(LgfContent& other)
    1.85 +      : _is(other._is), local_is(other.local_is) {
    1.86 +      
    1.87 +      other._is = 0;
    1.88 +      other.local_is = false;
    1.89 +      
    1.90 +      _node_sections.swap(other._node_sections);
    1.91 +      _edge_sections.swap(other._edge_sections);
    1.92 +      _attribute_sections.swap(other._attribute_sections);
    1.93 +      _extra_sections.swap(other._extra_sections);
    1.94 +
    1.95 +      _arc_sections.swap(other._arc_sections);
    1.96 +
    1.97 +      _node_maps.swap(other._node_maps);
    1.98 +      _edge_maps.swap(other._edge_maps);
    1.99 +      _attributes.swap(other._attributes);
   1.100 +    }
   1.101 +    
   1.102 +    /// \brief Destructor
   1.103 +    ~LgfContent() {
   1.104 +      if (local_is) delete _is;
   1.105 +    }
   1.106 +
   1.107 +
   1.108 +    /// \name Node sections
   1.109 +    /// @{
   1.110 +
   1.111 +    /// \brief Gives back the number of node sections in the file.
   1.112 +    ///
   1.113 +    /// Gives back the number of node sections in the file.
   1.114 +    int nodeSectionNum() const {
   1.115 +      return _node_sections.size();
   1.116 +    }
   1.117 +
   1.118 +    /// \brief Returns the section name at the given position. 
   1.119 +    ///
   1.120 +    /// Returns the section name at the given position. 
   1.121 +    const std::string& nodeSection(int i) const {
   1.122 +      return _node_sections[i];
   1.123 +    }
   1.124 +
   1.125 +    /// \brief Gives back the node maps for the given section.
   1.126 +    ///
   1.127 +    /// Gives back the node maps for the given section.
   1.128 +    const std::vector<std::string>& nodeMaps(int i) const {
   1.129 +      return _node_maps[i];
   1.130 +    }
   1.131 +
   1.132 +    /// @}
   1.133 +
   1.134 +    /// \name Arc sections 
   1.135 +    /// @{
   1.136 +
   1.137 +    /// \brief Gives back the number of arc sections in the file.
   1.138 +    ///
   1.139 +    /// Gives back the number of arc sections in the file.
   1.140 +    /// \note It is synonim of \c edgeSectionNum().
   1.141 +    int arcSectionNum() const {
   1.142 +      return _edge_sections.size();
   1.143 +    }
   1.144 +
   1.145 +    /// \brief Returns the section name at the given position. 
   1.146 +    ///
   1.147 +    /// Returns the section name at the given position. 
   1.148 +    /// \note It is synonim of \c edgeSection().
   1.149 +    const std::string& arcSection(int i) const {
   1.150 +      return _edge_sections[i];
   1.151 +    }
   1.152 +
   1.153 +    /// \brief Gives back the arc maps for the given section.
   1.154 +    ///
   1.155 +    /// Gives back the arc maps for the given section.
   1.156 +    /// \note It is synonim of \c edgeMaps().
   1.157 +    const std::vector<std::string>& arcMaps(int i) const {
   1.158 +      return _edge_maps[i];
   1.159 +    }
   1.160 +
   1.161 +    /// \brief Returns true when the section type is \c "@arcs".
   1.162 +    ///
   1.163 +    /// Returns true when the section type is \c "@arcs", and not "@edges".
   1.164 +    bool isArcSection(int i) const {
   1.165 +      return _arc_sections[i];
   1.166 +    }
   1.167 +
   1.168 +    /// @}
   1.169 +
   1.170 +    /// \name Edge sections   
   1.171 +    /// @{
   1.172 +
   1.173 +    /// \brief Gives back the number of edge sections in the file.
   1.174 +    ///
   1.175 +    /// Gives back the number of edge sections in the file.
   1.176 +    int edgeSectionNum() const {
   1.177 +      return _edge_sections.size();
   1.178 +    }
   1.179 +
   1.180 +    /// \brief Returns the section name at the given position. 
   1.181 +    ///
   1.182 +    /// Returns the section name at the given position. 
   1.183 +    const std::string& edgeSection(int i) const {
   1.184 +      return _edge_sections[i];
   1.185 +    }
   1.186 +
   1.187 +    /// \brief Gives back the edge maps for the given section.
   1.188 +    ///
   1.189 +    /// Gives back the edge maps for the given section.
   1.190 +    const std::vector<std::string>& edgeMaps(int i) const {
   1.191 +      return _edge_maps[i];
   1.192 +    }
   1.193 +
   1.194 +    /// \brief Returns true when the section type is \c "@edges".
   1.195 +    ///
   1.196 +    /// Returns true when the section type is \c "@edges", and not "@arcs".
   1.197 +    bool isEdgeSection(int i) const {
   1.198 +      return !_arc_sections[i];
   1.199 +    }
   1.200 +
   1.201 +    /// @}
   1.202 +
   1.203 +    /// \name Attribute sections   
   1.204 +    /// @{
   1.205 +
   1.206 +    /// \brief Gives back the number of attribute sections in the file.
   1.207 +    ///
   1.208 +    /// Gives back the number of attribute sections in the file.
   1.209 +    int attributeSectionNum() const {
   1.210 +      return _attribute_sections.size();
   1.211 +    }
   1.212 +
   1.213 +    /// \brief Returns the section name at the given position. 
   1.214 +    ///
   1.215 +    /// Returns the section name at the given position. 
   1.216 +    const std::string& attributeSection(int i) const {
   1.217 +      return _attribute_sections[i];
   1.218 +    }
   1.219 +
   1.220 +    /// \brief Gives back the attributes for the given section.
   1.221 +    ///
   1.222 +    /// Gives back the attributes for the given section.
   1.223 +    const std::vector<std::string>& attributes(int i) const {
   1.224 +      return _attributes[i];
   1.225 +    }
   1.226 +
   1.227 +    /// @}
   1.228 +
   1.229 +    /// \name Extra sections   
   1.230 +    /// @{
   1.231 +
   1.232 +    /// \brief Gives back the number of extra sections in the file.
   1.233 +    ///
   1.234 +    /// Gives back the number of extra sections in the file.
   1.235 +    int extraSectionNum() const {
   1.236 +      return _extra_sections.size();
   1.237 +    }
   1.238 +
   1.239 +    /// \brief Returns the extra section type at the given position. 
   1.240 +    ///
   1.241 +    /// Returns the section type at the given position. 
   1.242 +    const std::string& extraSection(int i) const {
   1.243 +      return _extra_sections[i];
   1.244 +    }
   1.245 +
   1.246 +    /// @}
   1.247 +
   1.248 +  private:
   1.249 +
   1.250 +    bool readLine() {
   1.251 +      std::string str;
   1.252 +      while(++line_num, std::getline(*_is, str)) {
   1.253 +	line.clear(); line.str(str);
   1.254 +	char c;
   1.255 +	if (line >> std::ws >> c && c != '#') {
   1.256 +	  line.putback(c);
   1.257 +	  return true;
   1.258 +	}
   1.259 +      }
   1.260 +      return false;
   1.261 +    }
   1.262 +
   1.263 +    bool readSuccess() {
   1.264 +      return static_cast<bool>(*_is);
   1.265 +    }
   1.266 +
   1.267 +    void skipSection() {
   1.268 +      char c;
   1.269 +      while (readSuccess() && line >> c && c != '@') {
   1.270 +	readLine();
   1.271 +      }
   1.272 +      line.putback(c);
   1.273 +    }
   1.274 +
   1.275 +    void readMaps(std::vector<std::string>& maps) {
   1.276 +      if (!readLine()) 
   1.277 +	throw DataFormatError("Cannot find map captions");
   1.278 +      std::string map;
   1.279 +      while (_reader_bits::readToken(line, map)) {
   1.280 +	maps.push_back(map);
   1.281 +      }
   1.282 +    }
   1.283 +
   1.284 +    void readAttributes(std::vector<std::string>& attrs) {
   1.285 +      readLine();
   1.286 +      char c;
   1.287 +      while (readSuccess() && line >> c && c != '@') {
   1.288 +	line.putback(c);
   1.289 +	std::string attr;
   1.290 +	_reader_bits::readToken(line, attr);
   1.291 +	attrs.push_back(attr);
   1.292 +	readLine();
   1.293 +      }
   1.294 +      line.putback(c);
   1.295 +    }
   1.296 +
   1.297 +  public:
   1.298 +
   1.299 +    /// \name Execution of the content reader    
   1.300 +    /// @{
   1.301 +
   1.302 +    /// \brief Start the reading
   1.303 +    ///
   1.304 +    /// This function starts the reading
   1.305 +    void run() {
   1.306 +
   1.307 +      readLine();
   1.308 +      skipSection();
   1.309 +
   1.310 +      while (readSuccess()) {
   1.311 +
   1.312 +	char c;
   1.313 +	line >> c;
   1.314 +
   1.315 +	std::string section, caption;
   1.316 +	_reader_bits::readToken(line, section);
   1.317 +	_reader_bits::readToken(line, caption);
   1.318 +
   1.319 +	if (section == "nodes") {
   1.320 +	  _node_sections.push_back(caption);
   1.321 +	  _node_maps.push_back(std::vector<std::string>());
   1.322 +	  readMaps(_node_maps.back());
   1.323 +	  readLine(); skipSection();
   1.324 +	} else if (section == "arcs" || section == "edges") {
   1.325 +	  _edge_sections.push_back(caption);
   1.326 +	  _arc_sections.push_back(section == "arcs");
   1.327 +	  _edge_maps.push_back(std::vector<std::string>());
   1.328 +	  readMaps(_edge_maps.back());
   1.329 +	  readLine(); skipSection();
   1.330 +	} else if (section == "attributes") {
   1.331 +	  _attribute_sections.push_back(caption);
   1.332 +	  _attributes.push_back(std::vector<std::string>());
   1.333 +	  readAttributes(_attributes.back());
   1.334 +	} else {
   1.335 +	  _extra_sections.push_back(section);
   1.336 +	  readLine(); skipSection();
   1.337 +	}
   1.338 +      }
   1.339 +    }
   1.340 +
   1.341 +    /// @}
   1.342 +    
   1.343 +  };
   1.344  }
   1.345  
   1.346  #endif