gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Content -> contents
0 1 0
default
1 file changed with 18 insertions and 18 deletions:
↑ Collapse diff ↑
Ignore white space 384 line context
... ...
@@ -1881,529 +1881,529 @@
1881 1881
          }
1882 1882
          Node source = it->second;
1883 1883

	
1884 1884
          it = _node_index.find(target_token);
1885 1885
          if (it == _node_index.end()) {       
1886 1886
            std::ostringstream msg;            
1887 1887
            msg << "Item not found: " << target_token;
1888 1888
            throw DataFormatError(msg.str().c_str());
1889 1889
          }                                          
1890 1890
          Node target = it->second;                            
1891 1891

	
1892 1892
	  e = _graph.addEdge(source, target);
1893 1893
	  _edge_index.insert(std::make_pair(tokens[label_index], e));
1894 1894
	} else {
1895 1895
	  typename std::map<std::string, Edge>::iterator it =
1896 1896
	    _edge_index.find(tokens[label_index]);
1897 1897
	  if (it == _edge_index.end()) {
1898 1898
	    std::ostringstream msg;
1899 1899
	    msg << "Edge with label not found: " << tokens[label_index];
1900 1900
	    throw DataFormatError(msg.str().c_str());	    
1901 1901
	  }
1902 1902
	  e = it->second;
1903 1903
	}
1904 1904

	
1905 1905
	for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
1906 1906
	  _edge_maps[i].second->set(e, tokens[map_index[i]]);
1907 1907
	}
1908 1908

	
1909 1909
      }
1910 1910
      if (readSuccess()) {
1911 1911
	line.putback(c);
1912 1912
      }
1913 1913
    }
1914 1914

	
1915 1915
    void readAttributes() {
1916 1916

	
1917 1917
      std::set<std::string> read_attr;
1918 1918

	
1919 1919
      char c;
1920 1920
      while (readLine() && line >> c && c != '@') {
1921 1921
	line.putback(c);
1922 1922
	
1923 1923
	std::string attr, token;
1924 1924
	if (!_reader_bits::readToken(line, attr))
1925 1925
	  throw DataFormatError("Attribute name not found");
1926 1926
	if (!_reader_bits::readToken(line, token))
1927 1927
	  throw DataFormatError("Attribute value not found");
1928 1928
	if (line >> c)
1929 1929
	  throw DataFormatError("Extra character on the end of line");	  
1930 1930

	
1931 1931
	{
1932 1932
	  std::set<std::string>::iterator it = read_attr.find(attr);
1933 1933
	  if (it != read_attr.end()) {
1934 1934
	    std::ostringstream msg;
1935 1935
	    msg << "Multiple occurence of attribute " << attr;
1936 1936
	    throw DataFormatError(msg.str().c_str());
1937 1937
	  }
1938 1938
	  read_attr.insert(attr);
1939 1939
	}
1940 1940
	
1941 1941
	{
1942 1942
	  typename Attributes::iterator it = _attributes.lower_bound(attr);
1943 1943
	  while (it != _attributes.end() && it->first == attr) {
1944 1944
	    it->second->set(token);
1945 1945
	    ++it;
1946 1946
	  }
1947 1947
	}
1948 1948

	
1949 1949
      }
1950 1950
      if (readSuccess()) {
1951 1951
	line.putback(c);
1952 1952
      }
1953 1953
      for (typename Attributes::iterator it = _attributes.begin();
1954 1954
	   it != _attributes.end(); ++it) {
1955 1955
	if (read_attr.find(it->first) == read_attr.end()) {
1956 1956
	  std::ostringstream msg;
1957 1957
	  msg << "Attribute not found in file: " << it->first;
1958 1958
	  throw DataFormatError(msg.str().c_str());
1959 1959
	}	
1960 1960
      }
1961 1961
    }
1962 1962

	
1963 1963
  public:
1964 1964

	
1965 1965
    /// \name Execution of the reader    
1966 1966
    /// @{
1967 1967

	
1968 1968
    /// \brief Start the batch processing
1969 1969
    ///
1970 1970
    /// This function starts the batch processing
1971 1971
    void run() {
1972 1972
      
1973 1973
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
1974 1974
      
1975 1975
      bool nodes_done = false;
1976 1976
      bool edges_done = false;
1977 1977
      bool attributes_done = false;
1978 1978
      std::set<std::string> extra_sections;
1979 1979

	
1980 1980
      line_num = 0;      
1981 1981
      readLine();
1982 1982
      skipSection();
1983 1983

	
1984 1984
      while (readSuccess()) {
1985 1985
	try {
1986 1986
	  char c;
1987 1987
	  std::string section, caption;
1988 1988
	  line >> c;
1989 1989
	  _reader_bits::readToken(line, section);
1990 1990
	  _reader_bits::readToken(line, caption);
1991 1991

	
1992 1992
	  if (line >> c) 
1993 1993
	    throw DataFormatError("Extra character on the end of line");
1994 1994

	
1995 1995
	  if (section == "nodes" && !nodes_done) {
1996 1996
	    if (_nodes_caption.empty() || _nodes_caption == caption) {
1997 1997
	      readNodes();
1998 1998
	      nodes_done = true;
1999 1999
	    }
2000 2000
	  } else if ((section == "edges" || section == "arcs") && 
2001 2001
		     !edges_done) {
2002 2002
	    if (_edges_caption.empty() || _edges_caption == caption) {
2003 2003
	      readEdges();
2004 2004
	      edges_done = true;
2005 2005
	    }
2006 2006
	  } else if (section == "attributes" && !attributes_done) {
2007 2007
	    if (_attributes_caption.empty() || _attributes_caption == caption) {
2008 2008
	      readAttributes();
2009 2009
	      attributes_done = true;
2010 2010
	    }
2011 2011
	  } else {
2012 2012
	    if (extra_sections.find(section) != extra_sections.end()) {
2013 2013
	      std::ostringstream msg;
2014 2014
	      msg << "Multiple occurence of section " << section;
2015 2015
	      throw DataFormatError(msg.str().c_str());
2016 2016
	    }
2017 2017
	    Sections::iterator it = _sections.find(section);
2018 2018
	    if (it != _sections.end()) {
2019 2019
	      extra_sections.insert(section);
2020 2020
	      it->second->process(*_is, line_num);
2021 2021
	    }
2022 2022
	    readLine();
2023 2023
	    skipSection();
2024 2024
	  }
2025 2025
	} catch (DataFormatError& error) {
2026 2026
	  error.line(line_num);
2027 2027
	  throw;
2028 2028
	}	
2029 2029
      }
2030 2030

	
2031 2031
      if (!nodes_done) {
2032 2032
	throw DataFormatError("Section @nodes not found");
2033 2033
      }
2034 2034

	
2035 2035
      if (!edges_done) {
2036 2036
	throw DataFormatError("Section @edges not found");
2037 2037
      }
2038 2038

	
2039 2039
      if (!attributes_done && !_attributes.empty()) {
2040 2040
	throw DataFormatError("Section @attributes not found");
2041 2041
      }
2042 2042

	
2043 2043
    }
2044 2044

	
2045 2045
    /// @}
2046 2046
    
2047 2047
  };
2048 2048

	
2049 2049
  /// \relates GraphReader
2050 2050
  template <typename Graph>
2051 2051
  GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
2052 2052
    GraphReader<Graph> tmp(is, graph);
2053 2053
    return tmp;
2054 2054
  }
2055 2055

	
2056 2056
  /// \relates GraphReader
2057 2057
  template <typename Graph>
2058 2058
  GraphReader<Graph> graphReader(const std::string& fn, 
2059 2059
				       Graph& graph) {
2060 2060
    GraphReader<Graph> tmp(fn, graph);
2061 2061
    return tmp;
2062 2062
  }
2063 2063

	
2064 2064
  /// \relates GraphReader
2065 2065
  template <typename Graph>
2066 2066
  GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
2067 2067
    GraphReader<Graph> tmp(fn, graph);
2068 2068
    return tmp;
2069 2069
  }
2070 2070

	
2071 2071
  /// \ingroup lemon_io
2072 2072
  ///
2073
  /// \brief Reader for the content of the \ref lgf-format "LGF" file 
2073
  /// \brief Reader for the contents of the \ref lgf-format "LGF" file 
2074 2074
  ///
2075 2075
  /// This class can be used to read the sections, the map names and
2076 2076
  /// the attributes from a file. Usually, the Lemon programs know
2077 2077
  /// that, which type of graph, which maps and which attributes
2078 2078
  /// should be read from a file, but in general tools (like glemon)
2079
  /// the content of an LGF file should be guessed somehow. This class
2079
  /// the contents of an LGF file should be guessed somehow. This class
2080 2080
  /// reads the graph and stores the appropriate information for
2081 2081
  /// reading the graph.
2082 2082
  ///
2083
  ///\code LgfContent content("graph.lgf"); 
2084
  /// content.run();
2083
  ///\code LgfContents contents("graph.lgf"); 
2084
  /// contents.run();
2085 2085
  ///
2086 2086
  /// // does it contain any node section and arc section
2087
  /// if (content.nodeSectionNum() == 0 || content.arcSectionNum()) {
2087
  /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
2088 2088
  ///   std::cerr << "Failure, cannot find graph" << std::endl;
2089 2089
  ///   return -1;
2090 2090
  /// }
2091 2091
  /// std::cout << "The name of the default node section : " 
2092
  ///           << content.nodeSection(0) << std::endl;
2092
  ///           << contents.nodeSection(0) << std::endl;
2093 2093
  /// std::cout << "The number of the arc maps : " 
2094
  ///           << content.arcMaps(0).size() << std::endl;
2094
  ///           << contents.arcMaps(0).size() << std::endl;
2095 2095
  /// std::cout << "The name of second arc map : " 
2096
  ///           << content.arcMaps(0)[1] << std::endl;
2096
  ///           << contents.arcMaps(0)[1] << std::endl;
2097 2097
  ///\endcode
2098
  class LgfContent {    
2098
  class LgfContents {    
2099 2099
  private:
2100 2100

	
2101 2101
    std::istream* _is;
2102 2102
    bool local_is;
2103 2103

	
2104 2104
    std::vector<std::string> _node_sections;
2105 2105
    std::vector<std::string> _edge_sections;
2106 2106
    std::vector<std::string> _attribute_sections;
2107 2107
    std::vector<std::string> _extra_sections;
2108 2108

	
2109 2109
    std::vector<bool> _arc_sections;
2110 2110

	
2111 2111
    std::vector<std::vector<std::string> > _node_maps;
2112 2112
    std::vector<std::vector<std::string> > _edge_maps;
2113 2113

	
2114 2114
    std::vector<std::vector<std::string> > _attributes;
2115 2115

	
2116 2116

	
2117 2117
    int line_num;
2118 2118
    std::istringstream line;
2119 2119
    
2120 2120
  public:
2121 2121

	
2122 2122
    /// \brief Constructor
2123 2123
    ///
2124
    /// Construct an \e LGF content reader, which reads from the given
2124
    /// Construct an \e LGF contents reader, which reads from the given
2125 2125
    /// input stream.
2126
    LgfContent(std::istream& is) 
2126
    LgfContents(std::istream& is) 
2127 2127
      : _is(&is), local_is(false) {}
2128 2128

	
2129 2129
    /// \brief Constructor
2130 2130
    ///
2131
    /// Construct an \e LGF content reader, which reads from the given
2131
    /// Construct an \e LGF contents reader, which reads from the given
2132 2132
    /// file.
2133
    LgfContent(const std::string& fn) 
2133
    LgfContents(const std::string& fn) 
2134 2134
      : _is(new std::ifstream(fn.c_str())), local_is(true) {}
2135 2135

	
2136 2136
    /// \brief Constructor
2137 2137
    ///
2138
    /// Construct an \e LGF content reader, which reads from the given
2138
    /// Construct an \e LGF contents reader, which reads from the given
2139 2139
    /// file.
2140
    LgfContent(const char* fn)
2140
    LgfContents(const char* fn)
2141 2141
      : _is(new std::ifstream(fn)), local_is(true) {}
2142 2142

	
2143 2143
    /// \brief Copy constructor
2144 2144
    ///
2145 2145
    /// The copy constructor transfers all data from the other reader,
2146 2146
    /// therefore the copied reader will not be usable more. 
2147
    LgfContent(LgfContent& other)
2147
    LgfContents(LgfContents& other)
2148 2148
      : _is(other._is), local_is(other.local_is) {
2149 2149
      
2150 2150
      other._is = 0;
2151 2151
      other.local_is = false;
2152 2152
      
2153 2153
      _node_sections.swap(other._node_sections);
2154 2154
      _edge_sections.swap(other._edge_sections);
2155 2155
      _attribute_sections.swap(other._attribute_sections);
2156 2156
      _extra_sections.swap(other._extra_sections);
2157 2157

	
2158 2158
      _arc_sections.swap(other._arc_sections);
2159 2159

	
2160 2160
      _node_maps.swap(other._node_maps);
2161 2161
      _edge_maps.swap(other._edge_maps);
2162 2162
      _attributes.swap(other._attributes);
2163 2163
    }
2164 2164
    
2165 2165
    /// \brief Destructor
2166
    ~LgfContent() {
2166
    ~LgfContents() {
2167 2167
      if (local_is) delete _is;
2168 2168
    }
2169 2169

	
2170 2170

	
2171 2171
    /// \name Node sections
2172 2172
    /// @{
2173 2173

	
2174 2174
    /// \brief Gives back the number of node sections in the file.
2175 2175
    ///
2176 2176
    /// Gives back the number of node sections in the file.
2177 2177
    int nodeSectionNum() const {
2178 2178
      return _node_sections.size();
2179 2179
    }
2180 2180

	
2181 2181
    /// \brief Returns the section name at the given position. 
2182 2182
    ///
2183 2183
    /// Returns the section name at the given position. 
2184 2184
    const std::string& nodeSection(int i) const {
2185 2185
      return _node_sections[i];
2186 2186
    }
2187 2187

	
2188 2188
    /// \brief Gives back the node maps for the given section.
2189 2189
    ///
2190 2190
    /// Gives back the node maps for the given section.
2191 2191
    const std::vector<std::string>& nodeMaps(int i) const {
2192 2192
      return _node_maps[i];
2193 2193
    }
2194 2194

	
2195 2195
    /// @}
2196 2196

	
2197 2197
    /// \name Arc sections 
2198 2198
    /// @{
2199 2199

	
2200 2200
    /// \brief Gives back the number of arc sections in the file.
2201 2201
    ///
2202 2202
    /// Gives back the number of arc sections in the file.
2203 2203
    /// \note It is synonim of \c edgeSectionNum().
2204 2204
    int arcSectionNum() const {
2205 2205
      return _edge_sections.size();
2206 2206
    }
2207 2207

	
2208 2208
    /// \brief Returns the section name at the given position. 
2209 2209
    ///
2210 2210
    /// Returns the section name at the given position. 
2211 2211
    /// \note It is synonim of \c edgeSection().
2212 2212
    const std::string& arcSection(int i) const {
2213 2213
      return _edge_sections[i];
2214 2214
    }
2215 2215

	
2216 2216
    /// \brief Gives back the arc maps for the given section.
2217 2217
    ///
2218 2218
    /// Gives back the arc maps for the given section.
2219 2219
    /// \note It is synonim of \c edgeMaps().
2220 2220
    const std::vector<std::string>& arcMaps(int i) const {
2221 2221
      return _edge_maps[i];
2222 2222
    }
2223 2223

	
2224 2224
    /// \brief Returns true when the section type is \c "@arcs".
2225 2225
    ///
2226 2226
    /// Returns true when the section type is \c "@arcs", and not "@edges".
2227 2227
    bool isArcSection(int i) const {
2228 2228
      return _arc_sections[i];
2229 2229
    }
2230 2230

	
2231 2231
    /// @}
2232 2232

	
2233 2233
    /// \name Edge sections   
2234 2234
    /// @{
2235 2235

	
2236 2236
    /// \brief Gives back the number of edge sections in the file.
2237 2237
    ///
2238 2238
    /// Gives back the number of edge sections in the file.
2239 2239
    int edgeSectionNum() const {
2240 2240
      return _edge_sections.size();
2241 2241
    }
2242 2242

	
2243 2243
    /// \brief Returns the section name at the given position. 
2244 2244
    ///
2245 2245
    /// Returns the section name at the given position. 
2246 2246
    const std::string& edgeSection(int i) const {
2247 2247
      return _edge_sections[i];
2248 2248
    }
2249 2249

	
2250 2250
    /// \brief Gives back the edge maps for the given section.
2251 2251
    ///
2252 2252
    /// Gives back the edge maps for the given section.
2253 2253
    const std::vector<std::string>& edgeMaps(int i) const {
2254 2254
      return _edge_maps[i];
2255 2255
    }
2256 2256

	
2257 2257
    /// \brief Returns true when the section type is \c "@edges".
2258 2258
    ///
2259 2259
    /// Returns true when the section type is \c "@edges", and not "@arcs".
2260 2260
    bool isEdgeSection(int i) const {
2261 2261
      return !_arc_sections[i];
2262 2262
    }
2263 2263

	
2264 2264
    /// @}
2265 2265

	
2266 2266
    /// \name Attribute sections   
2267 2267
    /// @{
2268 2268

	
2269 2269
    /// \brief Gives back the number of attribute sections in the file.
2270 2270
    ///
2271 2271
    /// Gives back the number of attribute sections in the file.
2272 2272
    int attributeSectionNum() const {
2273 2273
      return _attribute_sections.size();
2274 2274
    }
2275 2275

	
2276 2276
    /// \brief Returns the section name at the given position. 
2277 2277
    ///
2278 2278
    /// Returns the section name at the given position. 
2279 2279
    const std::string& attributeSection(int i) const {
2280 2280
      return _attribute_sections[i];
2281 2281
    }
2282 2282

	
2283 2283
    /// \brief Gives back the attributes for the given section.
2284 2284
    ///
2285 2285
    /// Gives back the attributes for the given section.
2286 2286
    const std::vector<std::string>& attributes(int i) const {
2287 2287
      return _attributes[i];
2288 2288
    }
2289 2289

	
2290 2290
    /// @}
2291 2291

	
2292 2292
    /// \name Extra sections   
2293 2293
    /// @{
2294 2294

	
2295 2295
    /// \brief Gives back the number of extra sections in the file.
2296 2296
    ///
2297 2297
    /// Gives back the number of extra sections in the file.
2298 2298
    int extraSectionNum() const {
2299 2299
      return _extra_sections.size();
2300 2300
    }
2301 2301

	
2302 2302
    /// \brief Returns the extra section type at the given position. 
2303 2303
    ///
2304 2304
    /// Returns the section type at the given position. 
2305 2305
    const std::string& extraSection(int i) const {
2306 2306
      return _extra_sections[i];
2307 2307
    }
2308 2308

	
2309 2309
    /// @}
2310 2310

	
2311 2311
  private:
2312 2312

	
2313 2313
    bool readLine() {
2314 2314
      std::string str;
2315 2315
      while(++line_num, std::getline(*_is, str)) {
2316 2316
	line.clear(); line.str(str);
2317 2317
	char c;
2318 2318
	if (line >> std::ws >> c && c != '#') {
2319 2319
	  line.putback(c);
2320 2320
	  return true;
2321 2321
	}
2322 2322
      }
2323 2323
      return false;
2324 2324
    }
2325 2325

	
2326 2326
    bool readSuccess() {
2327 2327
      return static_cast<bool>(*_is);
2328 2328
    }
2329 2329

	
2330 2330
    void skipSection() {
2331 2331
      char c;
2332 2332
      while (readSuccess() && line >> c && c != '@') {
2333 2333
	readLine();
2334 2334
      }
2335 2335
      line.putback(c);
2336 2336
    }
2337 2337

	
2338 2338
    void readMaps(std::vector<std::string>& maps) {
2339 2339
      if (!readLine()) 
2340 2340
	throw DataFormatError("Cannot find map captions");
2341 2341
      std::string map;
2342 2342
      while (_reader_bits::readToken(line, map)) {
2343 2343
	maps.push_back(map);
2344 2344
      }
2345 2345
    }
2346 2346

	
2347 2347
    void readAttributes(std::vector<std::string>& attrs) {
2348 2348
      readLine();
2349 2349
      char c;
2350 2350
      while (readSuccess() && line >> c && c != '@') {
2351 2351
	line.putback(c);
2352 2352
	std::string attr;
2353 2353
	_reader_bits::readToken(line, attr);
2354 2354
	attrs.push_back(attr);
2355 2355
	readLine();
2356 2356
      }
2357 2357
      line.putback(c);
2358 2358
    }
2359 2359

	
2360 2360
  public:
2361 2361

	
2362
    /// \name Execution of the content reader    
2362
    /// \name Execution of the contents reader    
2363 2363
    /// @{
2364 2364

	
2365 2365
    /// \brief Start the reading
2366 2366
    ///
2367 2367
    /// This function starts the reading
2368 2368
    void run() {
2369 2369

	
2370 2370
      readLine();
2371 2371
      skipSection();
2372 2372

	
2373 2373
      while (readSuccess()) {
2374 2374

	
2375 2375
	char c;
2376 2376
	line >> c;
2377 2377

	
2378 2378
	std::string section, caption;
2379 2379
	_reader_bits::readToken(line, section);
2380 2380
	_reader_bits::readToken(line, caption);
2381 2381

	
2382 2382
	if (section == "nodes") {
2383 2383
	  _node_sections.push_back(caption);
2384 2384
	  _node_maps.push_back(std::vector<std::string>());
2385 2385
	  readMaps(_node_maps.back());
2386 2386
	  readLine(); skipSection();
2387 2387
	} else if (section == "arcs" || section == "edges") {
2388 2388
	  _edge_sections.push_back(caption);
2389 2389
	  _arc_sections.push_back(section == "arcs");
2390 2390
	  _edge_maps.push_back(std::vector<std::string>());
2391 2391
	  readMaps(_edge_maps.back());
2392 2392
	  readLine(); skipSection();
2393 2393
	} else if (section == "attributes") {
2394 2394
	  _attribute_sections.push_back(caption);
2395 2395
	  _attributes.push_back(std::vector<std::string>());
2396 2396
	  readAttributes(_attributes.back());
2397 2397
	} else {
2398 2398
	  _extra_sections.push_back(section);
2399 2399
	  readLine(); skipSection();
2400 2400
	}
2401 2401
      }
2402 2402
    }
2403 2403

	
2404 2404
    /// @}
2405 2405
    
2406 2406
  };
2407 2407
}
2408 2408

	
2409 2409
#endif
0 comments (0 inline)