1 // --------------------------------------------------------------------------- 2 // 3 // This file is part of PermLib. 4 // 5 // Copyright (c) 2009-2012 Thomas Rehn <thomas@carmen76.de> 6 // All rights reserved. 7 // 8 // Redistribution and use in source and binary forms, with or without 9 // modification, are permitted provided that the following conditions 10 // are met: 11 // 1. Redistributions of source code must retain the above copyright 12 // notice, this list of conditions and the following disclaimer. 13 // 2. Redistributions in binary form must reproduce the above copyright 14 // notice, this list of conditions and the following disclaimer in the 15 // documentation and/or other materials provided with the distribution. 16 // 3. The name of the author may not be used to endorse or promote products 17 // derived from this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // --------------------------------------------------------------------------- 31 32 33 #include <list> 34 #include <string> 35 #include <fstream> 36 37 #include <boost/tokenizer.hpp> 38 #include <boost/lexical_cast.hpp> 39 40 namespace permlib { namespace test { 41 42 template<class PERM> 43 class GroupReader { 44 public: read(const std::string & s)45 bool read(const std::string& s) { 46 return read(s.c_str()); 47 } 48 read(const char * filename)49 bool read(const char* filename) { 50 m_n = 0; 51 m_base.clear(); 52 m_generators.clear(); 53 54 std::ifstream file; 55 file.open(filename); 56 if (!file.is_open()) { 57 std::cerr << "opening " << filename << " failed" << std::endl; 58 return false; 59 } 60 61 std::string line; 62 while (!file.eof()) { 63 std::getline(file, line); 64 if (line.length() < 2 || line[0] == '#') 65 continue; 66 67 if (line[0] == 'n') { 68 m_n = boost::lexical_cast<unsigned int>(line.substr(1)); 69 } else if (line[0] == 'B') { 70 typedef boost::tokenizer<boost::char_separator<char> > tokenizer; 71 boost::char_separator<char> sepBase(","); 72 std::string sub = line.substr(1); 73 tokenizer tokens(sub, sepBase); 74 for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) { 75 m_base.push_back( boost::lexical_cast<dom_int>(*tok_iter) - 1 ); 76 BOOST_ASSERT( m_base.back() < m_n ); 77 } 78 } else { 79 BOOST_ASSERT( m_n > 0 ); 80 typename PERM::ptr gen(new PERM(m_n, line)); 81 m_generators.push_back(gen); 82 } 83 } 84 file.close(); 85 86 return m_n > 0; 87 } 88 n()89 unsigned int n() const { return m_n; } base()90 const std::list<dom_int>& base() const { return m_base; } generators()91 const std::list<typename PERM::ptr>& generators() const { return m_generators; } 92 private: 93 unsigned int m_n; 94 std::list<dom_int> m_base; 95 std::list<typename PERM::ptr> m_generators; 96 }; 97 98 } } // end NS 99