diff options
author | Mike Buland <mike@xagasoft.com> | 2013-05-10 08:36:15 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2013-05-10 08:36:15 -0600 |
commit | dc0139c1880d1500b3e275b09781116613862f5f (patch) | |
tree | 479c22d0003d14a227040aa939de622067278c55 /src/options.cpp | |
parent | 0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1 (diff) | |
download | clic-dc0139c1880d1500b3e275b09781116613862f5f.tar.gz clic-dc0139c1880d1500b3e275b09781116613862f5f.tar.bz2 clic-dc0139c1880d1500b3e275b09781116613862f5f.tar.xz clic-dc0139c1880d1500b3e275b09781116613862f5f.zip |
Added more checking & unit-tests.
Diffstat (limited to 'src/options.cpp')
-rw-r--r-- | src/options.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/options.cpp b/src/options.cpp index 59dd2b4..9ade56a 100644 --- a/src/options.cpp +++ b/src/options.cpp | |||
@@ -21,6 +21,10 @@ Options::Options( int argc, char *argv[] ) : | |||
21 | addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime", | 21 | addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime", |
22 | "Tests every parameter after to see if it is prime then prints out " | 22 | "Tests every parameter after to see if it is prime then prints out " |
23 | "the ones that are prime. Set radix first."); | 23 | "the ones that are prime. Set radix first."); |
24 | addOption( Bu::slot(this, &Options::convert), 'c', "convert", | ||
25 | "Convert the provided number to the given radix in the format " | ||
26 | "number:to-radix or from-radix:number:to-radix. The radix should " | ||
27 | "be provided in base-10"); | ||
24 | addHelpOption('h', "help", "This help"); | 28 | addHelpOption('h', "help", "This help"); |
25 | 29 | ||
26 | parse( argc, argv ); | 30 | parse( argc, argv ); |
@@ -118,3 +122,32 @@ int Options::isPrime( Bu::StringArray aArgs ) | |||
118 | return aArgs.getSize(); | 122 | return aArgs.getSize(); |
119 | } | 123 | } |
120 | 124 | ||
125 | int Options::convert( Bu::StringArray aArgs ) | ||
126 | { | ||
127 | for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) | ||
128 | { | ||
129 | Bu::StringList lBits = (*i).split(':'); | ||
130 | if( lBits.getSize() < 2 || lBits.getSize() > 3 ) | ||
131 | throw Bu::ExceptionBase("Invalid format"); | ||
132 | |||
133 | int iFromRadix = iRadix; | ||
134 | int iToRadix; | ||
135 | Number n; | ||
136 | if( lBits.getSize() == 2 ) | ||
137 | { | ||
138 | iToRadix = strtol( lBits.last().getStr(), 0, 10 ); | ||
139 | n = Number( lBits.first(), 0, iFromRadix ); | ||
140 | } | ||
141 | else if( lBits.getSize() == 3 ) | ||
142 | { | ||
143 | iFromRadix = strtol( lBits.first().getStr(), 0, 10 ); | ||
144 | iToRadix = strtol( lBits.last().getStr(), 0, 10 ); | ||
145 | n = Number( *(lBits.begin()+1), 0, iFromRadix ); | ||
146 | } | ||
147 | Bu::println("%1").arg( n.toRadix( iToRadix ).toString() ); | ||
148 | } | ||
149 | |||
150 | exit( 0 ); | ||
151 | return 0; | ||
152 | } | ||
153 | |||