ContentReader
authordeba
Sat, 14 May 2005 20:56:53 +0000
changeset 142378502c63f771
parent 1422 469b3f628dd1
child 1424 c3d754f5e631
ContentReader

Retrieve information from the input file.
src/lemon/lemon_reader.h
     1.1 --- a/src/lemon/lemon_reader.h	Sat May 14 17:40:45 2005 +0000
     1.2 +++ b/src/lemon/lemon_reader.h	Sat May 14 20:56:53 2005 +0000
     1.3 @@ -1651,5 +1651,296 @@
     1.4      Readers readers;  
     1.5    };
     1.6  
     1.7 +  /// \ingroup io_group
     1.8 +  /// \brief SectionReader for retrieve what is in the file.
     1.9 +  ///
    1.10 +  /// SectionReader for retrieve what is in the file. If you want
    1.11 +  /// to know which sections, maps and items are in the file
    1.12 +  /// use the next code:
    1.13 +  /// \code
    1.14 +  /// LemonReader reader("input.lgf");
    1.15 +  /// ContentReader content(reader);
    1.16 +  /// reader.run();
    1.17 +  /// \endcode
    1.18 +  class ContentReader : public LemonReader::SectionReader {
    1.19 +    typedef LemonReader::SectionReader Parent;
    1.20 +  public:
    1.21 +    /// \brief Constructor.
    1.22 +    ///
    1.23 +    /// Constructor for
    1.24 +    ContentReader(LemonReader& _reader) : Parent(_reader) {}
    1.25 +
    1.26 +    /// \brief Desctructor.
    1.27 +    ///
    1.28 +    /// Desctructor.
    1.29 +    virtual ~ContentReader() {}
    1.30 +
    1.31 +    /// \brief Gives back how many nodesets are in the file.
    1.32 +    ///
    1.33 +    /// Gives back how many nodesets are in the file.
    1.34 +    int nodeSetNum() const {
    1.35 +      return nodesets.size();
    1.36 +    }
    1.37 +
    1.38 +    /// \brief Gives back the name of nodeset on the indiced position.
    1.39 +    ///
    1.40 +    /// Gives back the name of nodeset on the indiced position.
    1.41 +    std::string nodeSetName(int index) const {
    1.42 +      return nodesets[index].name;
    1.43 +    }
    1.44 +
    1.45 +    /// \brief Gives back the map names of nodeset on the indiced position.
    1.46 +    ///
    1.47 +    /// Gives back the map names of nodeset on the indiced position.
    1.48 +    const std::vector<std::string>& nodeSetMaps(int index) const {
    1.49 +      return nodesets[index].items;
    1.50 +    }
    1.51 +
    1.52 +    /// \brief Gives back how many edgesets are in the file.
    1.53 +    ///
    1.54 +    /// Gives back how many edgesets are in the file.
    1.55 +    int edgeSetNum() const {
    1.56 +      return edgesets.size();
    1.57 +    }
    1.58 +
    1.59 +    /// \brief Gives back the name of edgeset on the indiced position.
    1.60 +    ///
    1.61 +    /// Gives back the name of edgeset on the indiced position.
    1.62 +    std::string edgeSetName(int index) const {
    1.63 +      return edgesets[index].name;
    1.64 +    }
    1.65 +
    1.66 +    /// \brief Gives back the map names of edgeset on the indiced position.
    1.67 +    ///
    1.68 +    /// Gives back the map names of edgeset on the indiced position.
    1.69 +    const std::vector<std::string>& edgeSetMaps(int index) const {
    1.70 +      return edgesets[index].items;
    1.71 +    }
    1.72 +
    1.73 +    /// \brief Gives back how many undirected edgesets are in the file.
    1.74 +    ///
    1.75 +    /// Gives back how many undirected edgesets are in the file.
    1.76 +    int undirEdgeSetNum() const {
    1.77 +      return undiredgesets.size();
    1.78 +    }
    1.79 +
    1.80 +    /// \brief Gives back the name of undirected edgeset on the indiced 
    1.81 +    /// position.
    1.82 +    ///
    1.83 +    /// Gives back the name of undirected edgeset on the indiced position.
    1.84 +    std::string undirEdgeSetName(int index) const {
    1.85 +      return undiredgesets[index].name;
    1.86 +    }
    1.87 +
    1.88 +    /// \brief Gives back the map names of undirected edgeset on the indiced 
    1.89 +    /// position.
    1.90 +    ///
    1.91 +    /// Gives back the map names of undirected edgeset on the indiced position.
    1.92 +    const std::vector<std::string>& undirEdgeSetMaps(int index) const {
    1.93 +      return undiredgesets[index].items;
    1.94 +    }
    1.95 +
    1.96 +    /// \brief Gives back how many labeled nodes section are in the file.
    1.97 +    ///
    1.98 +    /// Gives back how many labeled nodes section are in the file.
    1.99 +    int nodesNum() const {
   1.100 +      return nodes.size();
   1.101 +    }
   1.102 +
   1.103 +    /// \brief Gives back the name of labeled nodes section on the indiced 
   1.104 +    /// position.
   1.105 +    ///
   1.106 +    /// Gives back the name of labeled nodes section on the indiced position.
   1.107 +    std::string nodesName(int index) const {
   1.108 +      return nodes[index].name;
   1.109 +    }
   1.110 +
   1.111 +    /// \brief Gives back the names of the labeled nodes in the indiced 
   1.112 +    /// section.
   1.113 +    ///
   1.114 +    /// Gives back the names of the labeled nodes in the indiced section.
   1.115 +    const std::vector<std::string>& nodesItems(int index) const {
   1.116 +      return nodes[index].items;
   1.117 +    }
   1.118 +
   1.119 +    /// \brief Gives back how many labeled edges section are in the file.
   1.120 +    ///
   1.121 +    /// Gives back how many labeled edges section are in the file.
   1.122 +    int edgesNum() const {
   1.123 +      return edges.size();
   1.124 +    }
   1.125 +
   1.126 +    /// \brief Gives back the name of labeled edges section on the indiced 
   1.127 +    /// position.
   1.128 +    ///
   1.129 +    /// Gives back the name of labeled edges section on the indiced position.
   1.130 +    std::string edgesName(int index) const {
   1.131 +      return edges[index].name;
   1.132 +    }
   1.133 +
   1.134 +    /// \brief Gives back the names of the labeled edges in the indiced 
   1.135 +    /// section.
   1.136 +    ///
   1.137 +    /// Gives back the names of the labeled edges in the indiced section.
   1.138 +    const std::vector<std::string>& edgesItems(int index) const {
   1.139 +      return edges[index].items;
   1.140 +    }
   1.141 + 
   1.142 +    /// \brief Gives back how many labeled undirected edges section are 
   1.143 +    /// in the file.
   1.144 +    ///
   1.145 +    /// Gives back how many labeled undirected edges section are in the file.
   1.146 +    int undirEdgesNum() const {
   1.147 +      return undiredges.size();
   1.148 +    }
   1.149 +
   1.150 +    /// \brief Gives back the name of labeled undirected edges section 
   1.151 +    /// on the indiced position.
   1.152 +    ///
   1.153 +    /// Gives back the name of labeled undirected edges section on the 
   1.154 +    /// indiced position.
   1.155 +    std::string undirEdgesName(int index) const {
   1.156 +      return undiredges[index].name;
   1.157 +    }
   1.158 +
   1.159 +    /// \brief Gives back the names of the labeled undirected edges in 
   1.160 +    /// the indiced section.
   1.161 +    ///
   1.162 +    /// Gives back the names of the labeled undirected edges in the 
   1.163 +    /// indiced section.
   1.164 +    const std::vector<std::string>& undirEdgesItems(int index) const {
   1.165 +      return undiredges[index].items;
   1.166 +    }
   1.167 +
   1.168 + 
   1.169 +    /// \brief Gives back how many attributes section are in the file.
   1.170 +    ///
   1.171 +    /// Gives back how many attributes section are in the file.
   1.172 +    int attributesNum() const {
   1.173 +      return attributes.size();
   1.174 +    }
   1.175 +
   1.176 +    /// \brief Gives back the name of attributes section on the indiced 
   1.177 +    /// position.
   1.178 +    ///
   1.179 +    /// Gives back the name of attributes section on the indiced position.
   1.180 +    std::string attributesName(int index) const {
   1.181 +      return attributes[index].name;
   1.182 +    }
   1.183 +
   1.184 +    /// \brief Gives back the names of the attributes in the indiced section.
   1.185 +    ///
   1.186 +    /// Gives back the names of the attributes in the indiced section.
   1.187 +    const std::vector<std::string>& attributesItems(int index) const {
   1.188 +      return attributes[index].items;
   1.189 +    }
   1.190 +
   1.191 +    const std::vector<std::string>& otherSections() const {
   1.192 +      return sections;
   1.193 +    }
   1.194 +
   1.195 +  protected:
   1.196 +    
   1.197 +    /// \brief Gives back true when the SectionReader can process 
   1.198 +    /// the section with the given header line.
   1.199 +    ///
   1.200 +    /// It gives back true when the section is common section.
   1.201 +    bool header(const std::string& line) {
   1.202 +      std::istringstream ls(line);
   1.203 +      std::string command, name;
   1.204 +      ls >> command >> name;
   1.205 +      if (command == "@nodeset") {
   1.206 +	current = command;
   1.207 +	nodesets.push_back(SectionInfo(name));
   1.208 +      } else if (command == "@edgeset") {
   1.209 +	current = command;
   1.210 +	edgesets.push_back(SectionInfo(name));
   1.211 +      } else if (command == "@undiredgeset") {
   1.212 +	current = command;
   1.213 +	undiredgesets.push_back(SectionInfo(name));
   1.214 +      } else if (command == "@nodes") {
   1.215 +	current = command;
   1.216 +	nodes.push_back(SectionInfo(name));
   1.217 +      } else if (command == "@edges") {
   1.218 +	current = command;
   1.219 +	edges.push_back(SectionInfo(name));
   1.220 +      } else if (command == "@undiredges") {
   1.221 +	current = command;
   1.222 +	undiredges.push_back(SectionInfo(name));
   1.223 +      } else if (command == "@attributes") {
   1.224 +	current = command;
   1.225 +	attributes.push_back(SectionInfo(name));
   1.226 +      } else {
   1.227 +	sections.push_back(line);
   1.228 +	return false;
   1.229 +      }
   1.230 +      return true;
   1.231 +    }
   1.232 +
   1.233 +    /// \brief Retrieve the items from various sections.
   1.234 +    ///
   1.235 +    /// Retrieve the items from various sections.
   1.236 +    void read(std::istream& is) {
   1.237 +      if (current == "@nodeset") {
   1.238 +	readMapNames(is, nodesets.back().items);
   1.239 +      } else if (current == "@edgeset") {
   1.240 +	readMapNames(is, edgesets.back().items);
   1.241 +      } else if (current == "@undiredgeset") {
   1.242 +	readMapNames(is, undiredgesets.back().items);
   1.243 +      } else if (current == "@nodes") {
   1.244 +	readItemNames(is, nodes.back().items);
   1.245 +      } else if (current == "@edges") {
   1.246 +	readItemNames(is, edges.back().items);
   1.247 +      } else if (current == "@undiredges") {
   1.248 +	readItemNames(is, undiredges.back().items);
   1.249 +      } else if (current == "@attributes") {
   1.250 +	readItemNames(is, attributes.back().items);
   1.251 +      }
   1.252 +    }    
   1.253 +
   1.254 +  private:
   1.255 +
   1.256 +    void readMapNames(std::istream& is, std::vector<std::string>& maps) {
   1.257 +      std::string line, id;
   1.258 +      std::getline(is, line);
   1.259 +      std::istringstream ls(line);
   1.260 +      while (ls >> id) {
   1.261 +	maps.push_back(id);
   1.262 +      }
   1.263 +      while (getline(is, line));
   1.264 +    }
   1.265 +
   1.266 +    void readItemNames(std::istream& is, std::vector<std::string>& maps) {
   1.267 +      std::string line, id;
   1.268 +      while (std::getline(is, line)) {
   1.269 +	std::istringstream ls(line);
   1.270 +	ls >> id;
   1.271 +	maps.push_back(id);
   1.272 +      }
   1.273 +    }
   1.274 +
   1.275 +    struct SectionInfo {
   1.276 +      std::string name;
   1.277 +      std::vector<std::string> items;
   1.278 +
   1.279 +      SectionInfo(const std::string& _name) : name(_name) {}
   1.280 +    };
   1.281 +
   1.282 +    std::vector<SectionInfo> nodesets;
   1.283 +    std::vector<SectionInfo> edgesets;
   1.284 +    std::vector<SectionInfo> undiredgesets;
   1.285 +
   1.286 +    std::vector<SectionInfo> nodes;
   1.287 +    std::vector<SectionInfo> edges;
   1.288 +    std::vector<SectionInfo> undiredges;
   1.289 +
   1.290 +    std::vector<SectionInfo> attributes;
   1.291 +
   1.292 +    std::vector<std::string> sections;
   1.293 +
   1.294 +    std::string current;
   1.295 +
   1.296 +  };
   1.297 +
   1.298  }
   1.299  #endif