[gcalctool] Add a command-line version of gcalctool 'gcalccmd' (Rich Burridge, Bug #590767)



commit 03e0fdbf4e83a6a3ff5de73c340d49b221b23cc5
Author: Rich Burridge <richb src gnome org>
Date:   Tue Nov 3 10:25:29 2009 +1100

    Add a command-line version of gcalctool 'gcalccmd' (Rich Burridge, Bug #590767)

 ChangeLog       |    4 ++
 configure.in    |    6 +++-
 src/Makefile.am |   20 +++++++++--
 src/gcalccmd.c  |   99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 125 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9d1ba7e..977a7cb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@
 gcalctool change history.
 =========================
 
+2009-11-03 Robert Ancell <robert ancell gmail com>
+
+    * Add a command-line version of gcalctool 'gcalccmd' (Rich Burridge, Bug #590767)
+
 2009-10-28 Robert Ancell <robert ancell gmail com>
 
     * Support arbitrary varible names, e.g. set with "name=55"
diff --git a/configure.in b/configure.in
index 97fee88..9cdd032 100644
--- a/configure.in
+++ b/configure.in
@@ -21,7 +21,7 @@ dnl ###########################################################################
 GTK_REQUIRED=2.17.5
 GCONF_REQUIRED=1.1.9
 
-PKG_CHECK_MODULES(PACKAGE, [
+PKG_CHECK_MODULES(GCALCTOOL, [
     gtk+-2.0 >= $GTK_REQUIRED
     gconf-2.0 >= $GCONF_REQUIRED
     libxml-2.0
@@ -29,6 +29,10 @@ PKG_CHECK_MODULES(PACKAGE, [
     gmodule-export-2.0
 ])
 
+PKG_CHECK_MODULES(GCALCCMD, [
+    glib-2.0
+])
+
 dnl ###########################################################################
 dnl Determine if a usable lex is available on this system
 dnl ###########################################################################
diff --git a/src/Makefile.am b/src/Makefile.am
index d4a07e4..7f16efc 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -8,13 +8,13 @@ INCLUDES = \
 	-DUI_DIR=\""$(datadir)/gcalctool"\" \
 	-DLOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\" \
 	-DPIXMAP_DIR=\""$(prefix)/$(DATADIRNAME)/pixmaps"\" \
-	$(PACKAGE_CFLAGS) \
+	$(GCALCTOOL_CFLAGS) \
 	$(GNOME_UTILS_CFLAGS) \
 	$(GCONF_CFLAGS) \
 	$(LIBXML_CFLAGS) \
 	$(LIBSOUP_CFLAGS)
 
-bin_PROGRAMS = gcalctool
+bin_PROGRAMS = gcalctool gcalccmd
 
 gcalctool_SOURCES = \
 	calctool.c \
@@ -47,6 +47,16 @@ gcalctool_SOURCES = \
 	unittest.c \
 	unittest.h
 
+gcalccmd_SOURCES = \
+	gcalccmd.c \
+	mp.c \
+	mp-convert.c \
+	mp-binary.c \
+	mp-trigonometric.c \
+	mp-equation.c \
+	mp-equation-parser.c \
+	mp-equation-lexer.c
+
 CLEANFILES = \
 	mp-equation-parser.h \
 	mp-equation-parser.c \
@@ -55,13 +65,17 @@ CLEANFILES = \
 	libparser.a
 
 gcalctool_LDADD = \
-	$(PACKAGE_LIBS) \
+	$(GCALCTOOL_LIBS) \
 	$(GNOME_UTILS_LIBS) \
 	$(GCONF_LIBS) \
 	$(LIBXML_LIBS) \
 	$(LIBSOUP_LIBS) \
 	libparser.a
 
+gcalccmd_LDADD = \
+	$(GCALCCMD_LIBS) \
+	-lm
+
 libparser.a: \
 	mp-equation-lexer.o\
 	mp-equation-parser.o\
diff --git a/src/gcalccmd.c b/src/gcalccmd.c
new file mode 100644
index 0000000..d2d5d8b
--- /dev/null
+++ b/src/gcalccmd.c
@@ -0,0 +1,99 @@
+/*  $Header$
+ *
+ *  Copyright (c) 2009 Rich Burridge
+ *           
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *           
+ *  This program is distributed in the hope that it will be useful, but 
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
+ *  General Public License for more details.
+ *           
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ *  02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <time.h>
+
+#include "mp-equation.h"
+
+#define MAXLINE 1024
+
+static void
+solve(const char *equation)
+{
+    int ret;
+    MPEquationOptions options;
+    MPNumber z;
+    char result_str[MAXLINE];
+    
+    memset(&options, 0, sizeof(options));
+    options.wordlen = 32;
+    options.angle_units = MP_DEGREES;
+    
+    ret = mp_equation_parse(equation, &options, &z, NULL);
+
+    if (ret == PARSER_ERR_MP)
+        fprintf(stderr, "Error %s\n", mp_get_error());
+    else if (ret)        
+        fprintf(stderr, "Error %d\n", ret);
+    else {
+        mp_cast_to_string(&z, 10, 9, 1, result_str, MAXLINE);
+        printf("%s\n", result_str);
+    }
+}
+
+
+/* Adjust user input equation string before solving it. */
+void
+str_adjust(char *str)
+{
+    int i, j = 0;
+
+    str[strlen(str)-1] = '\0';        /* Remove newline at end of string. */
+    for (i = 0; str[i] != '\0'; i++) {        /* Remove whitespace. */
+        if (str[i] != ' ' && str[i] != '\t')
+            str[j++] = str[i];
+    }
+    str[j] = '\0';
+    if (j > 0 && str[j-1] == '=')     /* Remove trailing '=' (if present). */
+        str[j-1] = '\0';
+}
+
+int
+main(int argc, char **argv)
+{
+    char *equation;
+    int bytes_read;
+    size_t nbytes = MAXLINE;
+
+    /* Seed random number generator. */
+    srand48((long) time((time_t *) 0));
+
+    while (1) {
+        printf("> ");
+        equation = (char *) malloc(MAXLINE * sizeof(char));
+        bytes_read = getline(&equation, &nbytes, stdin);
+
+        if (bytes_read != -1) {
+            str_adjust(equation);
+            if (!strcmp(equation, "exit") || !strcmp(equation, "quit") ||
+                strlen(equation) == 0) {
+                printf("\n");
+                exit(1);
+            } else {
+                solve(equation);
+                free(equation);
+            }
+        }
+    }
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]