| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2009 Alberto Scarpa * |
| 3 |
* 2009 Enrico Ros <enrico.ros@email.it> * |
| 4 |
* 2009 Alberto Scarpa <skaal.sl@gmail.com> * |
| 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 |
|
| 20 |
#include "Scrambler.h" |
| 21 |
|
| 22 |
#include <QFile> |
| 23 |
#include <QTextStream> |
| 24 |
|
| 25 |
#include <QDebug> |
| 26 |
|
| 27 |
#define notImplemented() qDebug("%s %s not implemented", __FILE__, __FUNCTION__); |
| 28 |
|
| 29 |
QStringList permute( const QString & str, int l ); |
| 30 |
|
| 31 |
Scrambler::Scrambler() |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
Scrambler::~Scrambler() |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
void Scrambler::addDictionary( const QString & filename) |
| 40 |
{ |
| 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 |
} |
| 53 |
} |
| 54 |
|
| 55 |
QStringList Scrambler::words( const QString & base, int min, int max ) |
| 56 |
{ |
| 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; |
| 67 |
} |
| 68 |
|
| 69 |
QStringList Scrambler::allWords( const QString & base, int min, int max ) |
| 70 |
{ |
| 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; |
| 80 |
} |
| 81 |
|
| 82 |
void Scrambler::setOk( const QString & /*word*/ ) |
| 83 |
{ |
| 84 |
notImplemented(); |
| 85 |
} |
| 86 |
|
| 87 |
void Scrambler::setFail( const QString & /*word*/ ) |
| 88 |
{ |
| 89 |
notImplemented(); |
| 90 |
} |
| 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 |
} |