PermLib
group_reader.h
00001 // ---------------------------------------------------------------------------
00002 //
00003 //  This file is part of PermLib.
00004 //
00005 // Copyright (c) 2009-2012 Thomas Rehn <thomas@carmen76.de>
00006 // All rights reserved.
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions
00010 // are met:
00011 // 1. Redistributions of source code must retain the above copyright
00012 //    notice, this list of conditions and the following disclaimer.
00013 // 2. Redistributions in binary form must reproduce the above copyright
00014 //    notice, this list of conditions and the following disclaimer in the
00015 //    documentation and/or other materials provided with the distribution.
00016 // 3. The name of the author may not be used to endorse or promote products
00017 //    derived from this software without specific prior written permission.
00018 // 
00019 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00020 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00022 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00024 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00028 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // ---------------------------------------------------------------------------
00031 
00032 
00033 #include <list>
00034 #include <string>
00035 #include <fstream>
00036 
00037 #include <boost/tokenizer.hpp>
00038 #include <boost/lexical_cast.hpp>
00039 
00040 namespace permlib { namespace test {
00041 
00042 template<class PERM>
00043 class GroupReader {
00044 public:
00045         bool read(const std::string& s) {
00046                 return read(s.c_str());
00047         }
00048         
00049         bool read(const char* filename) {
00050                 m_n = 0;
00051                 m_base.clear();
00052                 m_generators.clear();
00053                 
00054                 std::ifstream file;
00055                 file.open(filename);
00056                 if (!file.is_open()) {
00057                         std::cerr << "opening " << filename << " failed" << std::endl;
00058                         return false;
00059                 }
00060                 
00061                 std::string line;
00062                 while (!file.eof()) {
00063                         std::getline(file, line);
00064                         if (line.length() < 2 || line[0] == '#')
00065                                 continue;
00066                         
00067                         if (line[0] == 'n') {
00068                                 m_n = boost::lexical_cast<unsigned int>(line.substr(1));
00069                         } else if (line[0] == 'B') {
00070                                 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00071                                 boost::char_separator<char> sepBase(",");
00072                                 std::string sub = line.substr(1);
00073                                 tokenizer tokens(sub, sepBase);
00074                                 for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
00075                                         m_base.push_back( boost::lexical_cast<dom_int>(*tok_iter) - 1 );
00076                                         BOOST_ASSERT( m_base.back() < m_n );
00077                                 }
00078                         } else {
00079                                 BOOST_ASSERT( m_n > 0 );
00080                                 typename PERM::ptr gen(new PERM(m_n, line));
00081                                 m_generators.push_back(gen);
00082                         }
00083                 }
00084                 file.close();
00085                 
00086                 return m_n > 0;
00087         }
00088         
00089         unsigned int n() const { return m_n; }
00090         const std::list<dom_int>& base() const { return m_base; }
00091         const std::list<typename PERM::ptr>& generators() const { return m_generators; }
00092 private:
00093         unsigned int m_n;
00094         std::list<dom_int> m_base;
00095         std::list<typename PERM::ptr> m_generators;
00096 };
00097 
00098 } } // end NS