src/lemon/lemon_reader.h
changeset 1423 78502c63f771
parent 1421 7a21e1414c38
child 1424 c3d754f5e631
equal deleted inserted replaced
2:e15fd46c7cb5 3:01422fc87ba8
  1649 
  1649 
  1650     typedef std::map<std::string, ValueReaderBase*> Readers;
  1650     typedef std::map<std::string, ValueReaderBase*> Readers;
  1651     Readers readers;  
  1651     Readers readers;  
  1652   };
  1652   };
  1653 
  1653 
       
  1654   /// \ingroup io_group
       
  1655   /// \brief SectionReader for retrieve what is in the file.
       
  1656   ///
       
  1657   /// SectionReader for retrieve what is in the file. If you want
       
  1658   /// to know which sections, maps and items are in the file
       
  1659   /// use the next code:
       
  1660   /// \code
       
  1661   /// LemonReader reader("input.lgf");
       
  1662   /// ContentReader content(reader);
       
  1663   /// reader.run();
       
  1664   /// \endcode
       
  1665   class ContentReader : public LemonReader::SectionReader {
       
  1666     typedef LemonReader::SectionReader Parent;
       
  1667   public:
       
  1668     /// \brief Constructor.
       
  1669     ///
       
  1670     /// Constructor for
       
  1671     ContentReader(LemonReader& _reader) : Parent(_reader) {}
       
  1672 
       
  1673     /// \brief Desctructor.
       
  1674     ///
       
  1675     /// Desctructor.
       
  1676     virtual ~ContentReader() {}
       
  1677 
       
  1678     /// \brief Gives back how many nodesets are in the file.
       
  1679     ///
       
  1680     /// Gives back how many nodesets are in the file.
       
  1681     int nodeSetNum() const {
       
  1682       return nodesets.size();
       
  1683     }
       
  1684 
       
  1685     /// \brief Gives back the name of nodeset on the indiced position.
       
  1686     ///
       
  1687     /// Gives back the name of nodeset on the indiced position.
       
  1688     std::string nodeSetName(int index) const {
       
  1689       return nodesets[index].name;
       
  1690     }
       
  1691 
       
  1692     /// \brief Gives back the map names of nodeset on the indiced position.
       
  1693     ///
       
  1694     /// Gives back the map names of nodeset on the indiced position.
       
  1695     const std::vector<std::string>& nodeSetMaps(int index) const {
       
  1696       return nodesets[index].items;
       
  1697     }
       
  1698 
       
  1699     /// \brief Gives back how many edgesets are in the file.
       
  1700     ///
       
  1701     /// Gives back how many edgesets are in the file.
       
  1702     int edgeSetNum() const {
       
  1703       return edgesets.size();
       
  1704     }
       
  1705 
       
  1706     /// \brief Gives back the name of edgeset on the indiced position.
       
  1707     ///
       
  1708     /// Gives back the name of edgeset on the indiced position.
       
  1709     std::string edgeSetName(int index) const {
       
  1710       return edgesets[index].name;
       
  1711     }
       
  1712 
       
  1713     /// \brief Gives back the map names of edgeset on the indiced position.
       
  1714     ///
       
  1715     /// Gives back the map names of edgeset on the indiced position.
       
  1716     const std::vector<std::string>& edgeSetMaps(int index) const {
       
  1717       return edgesets[index].items;
       
  1718     }
       
  1719 
       
  1720     /// \brief Gives back how many undirected edgesets are in the file.
       
  1721     ///
       
  1722     /// Gives back how many undirected edgesets are in the file.
       
  1723     int undirEdgeSetNum() const {
       
  1724       return undiredgesets.size();
       
  1725     }
       
  1726 
       
  1727     /// \brief Gives back the name of undirected edgeset on the indiced 
       
  1728     /// position.
       
  1729     ///
       
  1730     /// Gives back the name of undirected edgeset on the indiced position.
       
  1731     std::string undirEdgeSetName(int index) const {
       
  1732       return undiredgesets[index].name;
       
  1733     }
       
  1734 
       
  1735     /// \brief Gives back the map names of undirected edgeset on the indiced 
       
  1736     /// position.
       
  1737     ///
       
  1738     /// Gives back the map names of undirected edgeset on the indiced position.
       
  1739     const std::vector<std::string>& undirEdgeSetMaps(int index) const {
       
  1740       return undiredgesets[index].items;
       
  1741     }
       
  1742 
       
  1743     /// \brief Gives back how many labeled nodes section are in the file.
       
  1744     ///
       
  1745     /// Gives back how many labeled nodes section are in the file.
       
  1746     int nodesNum() const {
       
  1747       return nodes.size();
       
  1748     }
       
  1749 
       
  1750     /// \brief Gives back the name of labeled nodes section on the indiced 
       
  1751     /// position.
       
  1752     ///
       
  1753     /// Gives back the name of labeled nodes section on the indiced position.
       
  1754     std::string nodesName(int index) const {
       
  1755       return nodes[index].name;
       
  1756     }
       
  1757 
       
  1758     /// \brief Gives back the names of the labeled nodes in the indiced 
       
  1759     /// section.
       
  1760     ///
       
  1761     /// Gives back the names of the labeled nodes in the indiced section.
       
  1762     const std::vector<std::string>& nodesItems(int index) const {
       
  1763       return nodes[index].items;
       
  1764     }
       
  1765 
       
  1766     /// \brief Gives back how many labeled edges section are in the file.
       
  1767     ///
       
  1768     /// Gives back how many labeled edges section are in the file.
       
  1769     int edgesNum() const {
       
  1770       return edges.size();
       
  1771     }
       
  1772 
       
  1773     /// \brief Gives back the name of labeled edges section on the indiced 
       
  1774     /// position.
       
  1775     ///
       
  1776     /// Gives back the name of labeled edges section on the indiced position.
       
  1777     std::string edgesName(int index) const {
       
  1778       return edges[index].name;
       
  1779     }
       
  1780 
       
  1781     /// \brief Gives back the names of the labeled edges in the indiced 
       
  1782     /// section.
       
  1783     ///
       
  1784     /// Gives back the names of the labeled edges in the indiced section.
       
  1785     const std::vector<std::string>& edgesItems(int index) const {
       
  1786       return edges[index].items;
       
  1787     }
       
  1788  
       
  1789     /// \brief Gives back how many labeled undirected edges section are 
       
  1790     /// in the file.
       
  1791     ///
       
  1792     /// Gives back how many labeled undirected edges section are in the file.
       
  1793     int undirEdgesNum() const {
       
  1794       return undiredges.size();
       
  1795     }
       
  1796 
       
  1797     /// \brief Gives back the name of labeled undirected edges section 
       
  1798     /// on the indiced position.
       
  1799     ///
       
  1800     /// Gives back the name of labeled undirected edges section on the 
       
  1801     /// indiced position.
       
  1802     std::string undirEdgesName(int index) const {
       
  1803       return undiredges[index].name;
       
  1804     }
       
  1805 
       
  1806     /// \brief Gives back the names of the labeled undirected edges in 
       
  1807     /// the indiced section.
       
  1808     ///
       
  1809     /// Gives back the names of the labeled undirected edges in the 
       
  1810     /// indiced section.
       
  1811     const std::vector<std::string>& undirEdgesItems(int index) const {
       
  1812       return undiredges[index].items;
       
  1813     }
       
  1814 
       
  1815  
       
  1816     /// \brief Gives back how many attributes section are in the file.
       
  1817     ///
       
  1818     /// Gives back how many attributes section are in the file.
       
  1819     int attributesNum() const {
       
  1820       return attributes.size();
       
  1821     }
       
  1822 
       
  1823     /// \brief Gives back the name of attributes section on the indiced 
       
  1824     /// position.
       
  1825     ///
       
  1826     /// Gives back the name of attributes section on the indiced position.
       
  1827     std::string attributesName(int index) const {
       
  1828       return attributes[index].name;
       
  1829     }
       
  1830 
       
  1831     /// \brief Gives back the names of the attributes in the indiced section.
       
  1832     ///
       
  1833     /// Gives back the names of the attributes in the indiced section.
       
  1834     const std::vector<std::string>& attributesItems(int index) const {
       
  1835       return attributes[index].items;
       
  1836     }
       
  1837 
       
  1838     const std::vector<std::string>& otherSections() const {
       
  1839       return sections;
       
  1840     }
       
  1841 
       
  1842   protected:
       
  1843     
       
  1844     /// \brief Gives back true when the SectionReader can process 
       
  1845     /// the section with the given header line.
       
  1846     ///
       
  1847     /// It gives back true when the section is common section.
       
  1848     bool header(const std::string& line) {
       
  1849       std::istringstream ls(line);
       
  1850       std::string command, name;
       
  1851       ls >> command >> name;
       
  1852       if (command == "@nodeset") {
       
  1853 	current = command;
       
  1854 	nodesets.push_back(SectionInfo(name));
       
  1855       } else if (command == "@edgeset") {
       
  1856 	current = command;
       
  1857 	edgesets.push_back(SectionInfo(name));
       
  1858       } else if (command == "@undiredgeset") {
       
  1859 	current = command;
       
  1860 	undiredgesets.push_back(SectionInfo(name));
       
  1861       } else if (command == "@nodes") {
       
  1862 	current = command;
       
  1863 	nodes.push_back(SectionInfo(name));
       
  1864       } else if (command == "@edges") {
       
  1865 	current = command;
       
  1866 	edges.push_back(SectionInfo(name));
       
  1867       } else if (command == "@undiredges") {
       
  1868 	current = command;
       
  1869 	undiredges.push_back(SectionInfo(name));
       
  1870       } else if (command == "@attributes") {
       
  1871 	current = command;
       
  1872 	attributes.push_back(SectionInfo(name));
       
  1873       } else {
       
  1874 	sections.push_back(line);
       
  1875 	return false;
       
  1876       }
       
  1877       return true;
       
  1878     }
       
  1879 
       
  1880     /// \brief Retrieve the items from various sections.
       
  1881     ///
       
  1882     /// Retrieve the items from various sections.
       
  1883     void read(std::istream& is) {
       
  1884       if (current == "@nodeset") {
       
  1885 	readMapNames(is, nodesets.back().items);
       
  1886       } else if (current == "@edgeset") {
       
  1887 	readMapNames(is, edgesets.back().items);
       
  1888       } else if (current == "@undiredgeset") {
       
  1889 	readMapNames(is, undiredgesets.back().items);
       
  1890       } else if (current == "@nodes") {
       
  1891 	readItemNames(is, nodes.back().items);
       
  1892       } else if (current == "@edges") {
       
  1893 	readItemNames(is, edges.back().items);
       
  1894       } else if (current == "@undiredges") {
       
  1895 	readItemNames(is, undiredges.back().items);
       
  1896       } else if (current == "@attributes") {
       
  1897 	readItemNames(is, attributes.back().items);
       
  1898       }
       
  1899     }    
       
  1900 
       
  1901   private:
       
  1902 
       
  1903     void readMapNames(std::istream& is, std::vector<std::string>& maps) {
       
  1904       std::string line, id;
       
  1905       std::getline(is, line);
       
  1906       std::istringstream ls(line);
       
  1907       while (ls >> id) {
       
  1908 	maps.push_back(id);
       
  1909       }
       
  1910       while (getline(is, line));
       
  1911     }
       
  1912 
       
  1913     void readItemNames(std::istream& is, std::vector<std::string>& maps) {
       
  1914       std::string line, id;
       
  1915       while (std::getline(is, line)) {
       
  1916 	std::istringstream ls(line);
       
  1917 	ls >> id;
       
  1918 	maps.push_back(id);
       
  1919       }
       
  1920     }
       
  1921 
       
  1922     struct SectionInfo {
       
  1923       std::string name;
       
  1924       std::vector<std::string> items;
       
  1925 
       
  1926       SectionInfo(const std::string& _name) : name(_name) {}
       
  1927     };
       
  1928 
       
  1929     std::vector<SectionInfo> nodesets;
       
  1930     std::vector<SectionInfo> edgesets;
       
  1931     std::vector<SectionInfo> undiredgesets;
       
  1932 
       
  1933     std::vector<SectionInfo> nodes;
       
  1934     std::vector<SectionInfo> edges;
       
  1935     std::vector<SectionInfo> undiredges;
       
  1936 
       
  1937     std::vector<SectionInfo> attributes;
       
  1938 
       
  1939     std::vector<std::string> sections;
       
  1940 
       
  1941     std::string current;
       
  1942 
       
  1943   };
       
  1944 
  1654 }
  1945 }
  1655 #endif
  1946 #endif