check parenthesis
[calc.git] / format.c
1 #include <malloc.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "format.h"
6
7 /* global variables */
8
9 #define DEFAULT_FORMAT "=> %.6g\n"
10 char *format = NULL;
11 char *minform = NULL;
12
13 /* print function */
14
15 void set_format (char *prompt, int precision)
16 {
17 char buffer[128] = {0};
18 free_format ();
19 sprintf (buffer, "%s%%.%dg\n", prompt, precision);
20 format = strdup (buffer);
21 sprintf (buffer, "%%.%dg", precision);
22 minform = strdup (buffer);
23 }
24
25 void free_format ()
26 {
27 if (format) {
28 free (format);
29 format = NULL;
30 }
31 if (minform) {
32 free (minform);
33 minform = NULL;
34 }
35 }
36
37 double print (double value)
38 {
39 fprintf (stdout, format ? format : DEFAULT_FORMAT, value);
40 fflush (stdout);
41 return value;
42 }
43
44 /* vim: set ts=4 sw=4 et: */