1 /***************************************************************************
2     Copyright (C) 2007 Giuseppe "denever" Martino
3     email                : denever@users.sourceforge.net
4  ***************************************************************************/
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *  This program is distributed in the hope that it will be useful,        *
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
15  *  GNU General Public License for more details.                           *
16  *                                                                         *
17  *  You should have received a copy of the GNU General Public License      *
18  *  along with this program; if not, write to the Free Software            *
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             *
20  *  MA 02110-1301 USA                                                      *
21  *                                                                         *
22  ***************************************************************************/
23 
24 #include "blocks.hh"
25 #include "random.hh"
26 
27 using namespace libexercises;
28 
29 // give a random string from input
randstring(std::string input,unsigned int num)30 std::string Blocks::randstring(std::string input, unsigned int num)
31 {
32     unsigned int size = input.size();
33 
34     librandom::Random RND;
35 
36     std::string output;
37     output.clear();
38 
39     while(num != 0)
40     {
41 	unsigned int pos = RND.integer(0, size-1);
42 
43 	output += input[pos];
44 	num--;
45     }
46 
47     return output;
48 }
49 
Blocks()50 Blocks::Blocks(): m_num_chars(0), m_num_strings(0)
51 {
52     m_strings.clear();
53 }
54 
Blocks(unsigned int num,std::string symbols,unsigned int numch)55 Blocks::Blocks(unsigned int num, std::string symbols, unsigned int numch): m_num_chars(numch), m_num_strings(num)
56 {
57     m_strings = randstring(symbols, m_num_chars * m_num_strings);
58 }
59 
Blocks(const Blocks & cpy)60 Blocks::Blocks(const Blocks& cpy)
61 {
62     m_strings = cpy.m_strings;
63 }
64 
~Blocks()65 Blocks::~Blocks()
66 {
67     m_strings.clear();
68 }
69 
stringtok()70 std::list< std::string > Blocks::stringtok()
71 {
72     typedef std::string::const_iterator c_str;
73     unsigned int i = 0;
74 
75     std::list< std::string > tmplst;
76     std::string tmp;
77 
78     for(c_str it = m_strings.begin(); it != m_strings.end(); it++)
79     {
80 	if(i == m_num_chars)
81 	{
82 	    tmplst.push_back(tmp);
83 	    tmp.clear();
84 	    i = 0;
85 	}
86 	tmp.push_back(*it);
87 	i++;
88     }
89 
90     tmplst.push_back(tmp);
91     tmp.clear();
92 
93     return tmplst;
94 }
95 
operator <<(libkeyer::Keyer & out,const Blocks & exc)96 libkeyer::Keyer& libexercises::operator<<(libkeyer::Keyer& out, const Blocks& exc)
97 {
98     unsigned int strlen = exc.string_len();
99 
100     for(unsigned int i=0; i< exc.len(); i++)
101 	out << exc.get_string().substr(i* strlen, strlen);
102 
103     return out;
104 }
105