| a8ebb9d by Enrico Ros at 2009-02-20 |
1 |
/*************************************************************************** |
| e45848b by Enrico Ros at 2009-05-31 |
2 |
* Copyright (c) 2009 Alberto Scarpa * |
| 0e5745d by Enrico Ros at 2009-05-31 |
3 |
* 2009 Enrico Ros <enrico.ros@email.it> * |
|
4 |
* 2009 Alberto Scarpa <skaal.sl@gmail.com> * |
| a8ebb9d by Enrico Ros at 2009-02-20 |
5 |
* * |
|
6 |
* Permission is hereby granted, free of charge, to any person * |
|
7 |
* obtaining a copy of this software and associated documentation * |
|
8 |
* files (the "Software"), to deal in the Software without * |
|
9 |
* restriction, including without limitation the rights to use, * |
|
10 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell * |
|
11 |
* copies of the Software, and to permit persons to whom the * |
|
12 |
* Software is furnished to do so, subject to the following * |
|
13 |
* conditions: * |
|
14 |
* * |
|
15 |
* The above copyright notice and this permission notice shall be * |
|
16 |
* included in all copies or substantial portions of the Software. * |
|
17 |
* * |
|
18 |
***************************************************************************/ |
|
19 |
|
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
20 |
#include "Scrambler.h" |
|
21 |
|
| 023fc63 by Alberto Scarpa at 2009-02-21 |
22 |
#include <QFile> |
|
23 |
#include <QTextStream> |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
24 |
|
| 023fc63 by Alberto Scarpa at 2009-02-21 |
25 |
#include <QDebug> |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
26 |
|
| 023fc63 by Alberto Scarpa at 2009-02-21 |
27 |
#define notImplemented() qDebug("%s %s not implemented", __FILE__, __FUNCTION__); |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
28 |
|
| 023fc63 by Alberto Scarpa at 2009-02-21 |
29 |
QStringList permute( const QString & str, int l ); |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
30 |
|
|
31 |
Scrambler::Scrambler() |
|
32 |
{ |
|
33 |
} |
|
34 |
|
|
35 |
Scrambler::~Scrambler() |
|
36 |
{ |
|
37 |
} |
|
38 |
|
|
39 |
void Scrambler::addDictionary( const QString & filename) |
|
40 |
{ |
| 023fc63 by Alberto Scarpa at 2009-02-21 |
41 |
QFile f( filename ); |
|
42 |
if( !f.open( QIODevice::ReadOnly ) ) { |
|
43 |
qWarning( "Scrambler::addDictionary can not open %s", qPrintable(filename) ); |
|
44 |
} |
|
45 |
|
|
46 |
QTextStream s( &f ); |
|
47 |
while( !s.atEnd() ) { |
|
48 |
QString w = s.readLine(); |
|
49 |
QChar c = w.at( 0 ); |
|
50 |
QStringList & sl = m_dictionaries[ c ]; |
|
51 |
sl.append( w ); |
|
52 |
} |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
53 |
} |
|
54 |
|
|
55 |
QStringList Scrambler::words( const QString & base, int min, int max ) |
|
56 |
{ |
| 023fc63 by Alberto Scarpa at 2009-02-21 |
57 |
QStringList res; |
|
58 |
QStringList all = allWords( base, min, max ); |
|
59 |
|
|
60 |
foreach( const QString & s, all ) { |
|
61 |
if ( m_dictionaries.value( s.at( 0 ) ).contains( s ) ) { |
|
62 |
if ( !res.contains( s ) ) |
|
63 |
res.append( s ); |
|
64 |
} |
|
65 |
} |
|
66 |
return res; |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
67 |
} |
|
68 |
|
|
69 |
QStringList Scrambler::allWords( const QString & base, int min, int max ) |
|
70 |
{ |
| d9bfc5d by Alberto Scarpa at 2009-02-21 |
71 |
min = qMax( 1, min ); |
|
72 |
min = qMin( min, base.size() ); |
|
73 |
max = qMax( min, max ); |
|
74 |
|
|
75 |
QStringList r; |
|
76 |
for ( int i = min; i <= max; ++i ) |
|
77 |
r.append( permute( base, i ) ); |
|
78 |
|
|
79 |
return r; |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
80 |
} |
|
81 |
|
| 0e5745d by Enrico Ros at 2009-05-31 |
82 |
void Scrambler::setOk( const QString & /*word*/ ) |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
83 |
{ |
|
84 |
notImplemented(); |
|
85 |
} |
|
86 |
|
| 0e5745d by Enrico Ros at 2009-05-31 |
87 |
void Scrambler::setFail( const QString & /*word*/ ) |
| f3da3f3 by Alberto Scarpa at 2009-02-20 |
88 |
{ |
|
89 |
notImplemented(); |
|
90 |
} |
| 023fc63 by Alberto Scarpa at 2009-02-21 |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
QStringList permute( const QString & str, int l ) |
|
96 |
{ |
|
97 |
QStringList r; |
|
98 |
|
|
99 |
if ( l == 1 ) { |
|
100 |
for( int i = 0; i < str.size() ; ++i ) { |
|
101 |
r.append( QString(str.at( i )) ); |
|
102 |
} |
|
103 |
return r; |
|
104 |
} |
|
105 |
|
|
106 |
for( int i = 0; i < str.size() ; ++i ) { |
|
107 |
QString sub = str; |
|
108 |
sub.replace(i,1,QString("") ); |
|
109 |
QStringList r1 = permute( sub, l-1 ); |
|
110 |
foreach( const QString &s, r1 ) { |
|
111 |
QString a = s; |
|
112 |
a.prepend( str.at(i) ); |
|
113 |
r.append( a ); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
return r; |
|
118 |
} |