Change 'undo' and 'clear' buttons to use symbolic icons
[gcalctool.git] / src / gcalccmd.c
blobe65ac5a8e421943a7638827338243b201df095ac
1 /*
2 * Copyright (C) 2009 Rich Burridge
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, either version 2 of the License, or (at your option) any later
7 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
8 * license.
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <time.h>
16 #include <locale.h>
18 #include "mp-equation.h"
19 #include "mp-serializer.h"
21 #define MAXLINE 1024
23 static MpSerializer *result_serializer;
25 static void
26 solve(const char *equation)
28 int ret;
29 MPEquationOptions options;
30 MPNumber z;
31 gchar *result_str = NULL;
33 memset(&options, 0, sizeof(options));
34 options.base = 10;
35 options.wordlen = 32;
36 options.angle_units = MP_DEGREES;
38 ret = mp_equation_parse(equation, &options, &z, NULL);
40 if (ret == PARSER_ERR_MP)
41 fprintf(stderr, "Error %s\n", mp_get_error());
42 else if (ret)
43 fprintf(stderr, "Error %d\n", ret);
44 else {
45 result_str = mp_serializer_to_string(result_serializer, &z);
46 printf("%s\n", result_str);
48 g_free(result_str);
52 /* Adjust user input equation string before solving it. */
53 static void
54 str_adjust(char *str)
56 int i, j = 0;
58 str[strlen(str)-1] = '\0'; /* Remove newline at end of string. */
59 for (i = 0; str[i] != '\0'; i++) { /* Remove whitespace. */
60 if (str[i] != ' ' && str[i] != '\t')
61 str[j++] = str[i];
63 str[j] = '\0';
64 if (j > 0 && str[j-1] == '=') /* Remove trailing '=' (if present). */
65 str[j-1] = '\0';
68 int
69 main(int argc, char **argv)
71 char *equation, *line;
72 size_t nbytes = MAXLINE;
74 /* Seed random number generator. */
75 srand48((long) time((time_t *) 0));
77 g_type_init ();
78 setlocale(LC_ALL, "");
80 result_serializer = mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, 10, 9);
82 equation = (char *) malloc(MAXLINE * sizeof(char));
83 while (1) {
84 printf("> ");
85 line = fgets(equation, nbytes, stdin);
87 if (line != NULL)
88 str_adjust(equation);
90 if (line == NULL || strcmp(equation, "exit") == 0 || strcmp(equation, "quit") == 0 || strlen(equation) == 0)
91 break;
93 solve(equation);
95 free(equation);
97 return 0;