summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2014-12-01 07:53:29 -0700
committerMike Buland <eichlan@xagasoft.com>2014-12-01 07:53:29 -0700
commit4d0ab515cc4142b22247e26f1b57b5cd0a6b31d5 (patch)
tree5ec74681e91d6c37c25b5e04926f4e193f48ed7c
parent9f7cbe624a91008a4cbe47c7b369c5e3f96a4eff (diff)
downloadclic-4d0ab515cc4142b22247e26f1b57b5cd0a6b31d5.tar.gz
clic-4d0ab515cc4142b22247e26f1b57b5cd0a6b31d5.tar.bz2
clic-4d0ab515cc4142b22247e26f1b57b5cd0a6b31d5.tar.xz
clic-4d0ab515cc4142b22247e26f1b57b5cd0a6b31d5.zip
Added "--sum" command line option.
Reads in a list of numbers on standard in, one per line, and sums them.
-rw-r--r--src/options.cpp19
-rw-r--r--src/options.h1
2 files changed, 20 insertions, 0 deletions
diff --git a/src/options.cpp b/src/options.cpp
index d9d34fa..d88df3b 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -8,6 +8,7 @@
8#include "lexer.h" 8#include "lexer.h"
9#include "parser.h" 9#include "parser.h"
10 10
11#include <bu/streamstack.h>
11#include <bu/sio.h> 12#include <bu/sio.h>
12#include <stdlib.h> 13#include <stdlib.h>
13 14
@@ -36,6 +37,8 @@ Options::Options( int argc, char *argv[] ) :
36 "use all remaining parameters as the equation (you may wish to quote " 37 "use all remaining parameters as the equation (you may wish to quote "
37 "your equation to prevent special characters from being interpreted by " 38 "your equation to prevent special characters from being interpreted by "
38 "your shell)."); 39 "your shell).");
40 addOption( Bu::slot(this, &Options::sum), "sum",
41 "Read numbers from standard input and sum them, output the result.");
39 addOption( Bu::slot(this, &Options::version), 'v', "version", 42 addOption( Bu::slot(this, &Options::version), 'v', "version",
40 "Show the version string ('" CLIC_VERSION_STR "')"); 43 "Show the version string ('" CLIC_VERSION_STR "')");
41 addHelpOption('h', "help", "This help"); 44 addHelpOption('h', "help", "This help");
@@ -198,6 +201,22 @@ int Options::execute( Bu::StringArray aArgs )
198 return aArgs.getSize(); 201 return aArgs.getSize();
199} 202}
200 203
204int Options::sum( Bu::StringArray aArgs )
205{
206 Number sum( iScale, iRadix );
207 while( !Bu::sioRaw.isEos() )
208 {
209 Bu::String s = Bu::sioRaw.readLine();
210 if( s.isEmpty() )
211 break;
212 Number n( s, iScale, iRadix );
213 sum = sum + n;
214 }
215 Bu::println("%1").arg( sum );
216 exit( 0 );
217 return 0;
218}
219
201int Options::version( Bu::StringArray aArgs ) 220int Options::version( Bu::StringArray aArgs )
202{ 221{
203 Bu::println( CLIC_VERSION_STR ); 222 Bu::println( CLIC_VERSION_STR );
diff --git a/src/options.h b/src/options.h
index 9afa0ac..f3f59a6 100644
--- a/src/options.h
+++ b/src/options.h
@@ -18,6 +18,7 @@ private:
18 int isPrime( Bu::StringArray aArgs ); 18 int isPrime( Bu::StringArray aArgs );
19 int convert( Bu::StringArray aArgs ); 19 int convert( Bu::StringArray aArgs );
20 int execute( Bu::StringArray aArgs ); 20 int execute( Bu::StringArray aArgs );
21 int sum( Bu::StringArray aArgs );
21 int version( Bu::StringArray aArgs ); 22 int version( Bu::StringArray aArgs );
22 23
23 int iScale; 24 int iScale;