[gcalctool] Add a --solve command-line argument
- From: Robert Ancell <rancell src gnome org>
- To: svn-commits-list gnome org
- Subject: [gcalctool] Add a --solve command-line argument
- Date: Wed, 6 May 2009 22:28:15 -0400 (EDT)
commit 64ea668b0d5532ed6abcdb6e20ba8bb15a3e5557
Author: Robert Ancell <robert ancell gmail com>
Date: Thu May 7 11:49:48 2009 +1000
Add a --solve command-line argument
Mark command-line strings for translation
Made program return 0 on success, 1 on failure
---
ChangeLog | 4 ++
gcalctool/calctool.c | 93 +++++++++++++++++++++++++++++++++++---------------
gcalctool/unittest.c | 9 +++--
3 files changed, 75 insertions(+), 31 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 8869e6b..08faab5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@
gcalctool change history.
=========================
+2009-05-07 Robert Ancell <robert ancell gmail com>
+
+ * Add a --solve command-line argument (Robert Ancell)
+
2009-05-04 Robert Ancell <robert ancell gmail com>
* Make GConf schemas translatable (Robert Ancell, Bug #571948)
diff --git a/gcalctool/calctool.c b/gcalctool/calctool.c
index f9f365f..16d622f 100644
--- a/gcalctool/calctool.c
+++ b/gcalctool/calctool.c
@@ -124,51 +124,71 @@ matherr(struct exception *exc)
static void
version()
{
- /* FIXME: Mark for translation post 2.26 */
+ /* NOTE: Is not translated so can be easily parsed */
fprintf(stderr, "%1$s %2$s\n", v->progname, VERSION);
- exit(1);
+}
+
+static void
+solve(const char *equation)
+{
+ int error;
+ MPNumber result;
+ char result_str[MAXLINE];
+
+ error = ce_parse(equation, &result);
+ if(error != 0) {
+ fprintf(stderr, "Error %d\n", error);
+ exit(1);
+ }
+ else {
+ mp_cast_to_string(result_str, MAXLINE, &result, basevals[v->base], 9);
+ printf("%s\n", result_str);
+ exit(0);
+ }
}
static void
usage(int show_gtk)
{
- /* FIXME: Mark for translation post 2.26 */
+ /* Translators: Description on how to use gcalctool displayed on command-line */
fprintf(stderr,
- "Usage:\n"
- " %s - Perform mathematical calculations\n", v->progname);
+ _("Usage:\n"
+ " %s - Perform mathematical calculations"), v->progname);
fprintf(stderr,
- "\n");
+ "\n\n");
+ /* Translators: Description on gcalctool command-line help options displayed on command-line */
fprintf(stderr,
- "Help Options:\n"
- " -v, --version Show release version\n"
- " -h, -?, --help Show help options\n"
- " --help-all Show all help options\n"
- " --help-gtk Show GTK+ options\n");
+ _("Help Options:\n"
+ " -v, --version Show release version\n"
+ " -h, -?, --help Show help options\n"
+ " --help-all Show all help options\n"
+ " --help-gtk Show GTK+ options"));
fprintf(stderr,
- "\n");
+ "\n\n");
if (show_gtk) {
- fprintf(stderr,
- "GTK+ Options\n"
- " --class=CLASS Program class as used by the window manager\n"
- " --name=NAME Program name as used by the window manager\n"
- " --screen=SCREEN X screen to use\n"
- " --sync Make X calls synchronous\n"
- " --gtk-module=MODULES Load additional GTK+ modules\n"
- " --g-fatal-warnings Make all warnings fatal\n");
+ /* Translators: Description on gcalctool command-line GTK+ options displayed on command-line */
+ fprintf(stderr,
+ _("GTK+ Options:\n"
+ " --class=CLASS Program class as used by the window manager\n"
+ " --name=NAME Program name as used by the window manager\n"
+ " --screen=SCREEN X screen to use\n"
+ " --sync Make X calls synchronous\n"
+ " --gtk-module=MODULES Load additional GTK+ modules\n"
+ " --g-fatal-warnings Make all warnings fatal"));
fprintf(stderr,
- "\n");
+ "\n\n");
}
+ /* Translators: Description on gcalctool application options displayed on command-line */
fprintf(stderr,
- "Application Options:\n"
- " -u, --unittest Perform unittests\n");
+ _("Application Options:\n"
+ " -u, --unittest Perform unittests\n"
+ " -s, --solve <equation> Solve the given equation"));
fprintf(stderr,
- "\n");
-
- exit(1);
+ "\n\n");
}
void
@@ -184,22 +204,39 @@ get_options(int argc, char *argv[])
strcmp(arg, "--version") == 0 ||
strcmp(arg, "-?") == 0) {
version();
+ exit(0);
}
else if (strcmp(arg, "-h") == 0 ||
strcmp(arg, "--help") == 0) {
usage(FALSE);
+ exit(0);
}
else if (strcmp(arg, "--help-all") == 0) {
usage(TRUE);
+ exit(0);
+ }
+ else if (strcmp(arg, "-s") == 0 ||
+ strcmp(arg, "--solve") == 0) {
+ i++;
+ if (i >= argc) {
+ /* Translators: Error printed to stderr when user uses --solve argument without an equation */
+ fprintf(stderr, _("Argument --solve requires an equation to solve"));
+ fprintf(stderr, "\n");
+ exit(1);
+ }
+ else
+ solve(argv[i]);
}
else if (strcmp(arg, "-u") == 0 ||
strcmp(arg, "--unittest") == 0) {
unittest();
}
else {
- /* FIXME: Mark for translation post 2.26 */
- fprintf(stderr, "Unknown argument '%s'\n", arg);
+ /* Translators: Error printed to stderr when user provides an unknown command-line argument */
+ fprintf(stderr, _("Unknown argument '%s'"), arg);
+ fprintf(stderr, "\n");
usage(FALSE);
+ exit(1);
}
}
}
diff --git a/gcalctool/unittest.c b/gcalctool/unittest.c
index 6d1d5aa..d204d0d 100644
--- a/gcalctool/unittest.c
+++ b/gcalctool/unittest.c
@@ -26,6 +26,8 @@
#include "calctool.h"
#include "ce_parser.h"
+static int fails = 0;
+
static void
test(char *expression, char *expected, int expected_error)
{
@@ -44,8 +46,10 @@ test(char *expression, char *expected, int expected_error)
}
mp_cast_to_string(result_str, MAXLINE, &result, basevals[v->base], 9);
- if(strcmp(result_str, expected) != 0)
+ if(strcmp(result_str, expected) != 0) {
+ fails++;
printf("FAIL: '%s' -> '%s', expected '%s'\n", expression, result_str, expected);
+ }
else
printf("SUCCESS: '%s' -> '%s'\n", expression, result_str);
}
@@ -201,6 +205,5 @@ void
unittest()
{
test_parser();
-
- exit(1);
+ exit(fails > 0 ? 1 : 0);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]