X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=format.c;h=152875022ad8a10f826378c7d611a5abd5d6edd4;hb=e2a309f921968691af1566000dd694f191eca857;hp=48cc56f57951d5d528d08eb726bd6388ed728277;hpb=a24bd5195f9990159a974c98751f1473f29b9fa4;p=calc.git diff --git a/format.c b/format.c index 48cc56f..1528750 100644 --- a/format.c +++ b/format.c @@ -7,36 +7,78 @@ /* global variables */ #define DEFAULT_FORMAT "=> %.6g\n" -char *format = NULL; -char *minform = NULL; +char *_format = NULL; +#define DEFAULT_MINFORM "%.6g" +char *_minform = NULL; + +int _precision = 6; + +#define DEFAULT_PROMPT "=> " +char *_prompt = NULL; /* print function */ -void set_format (char *prompt, int precision) +void set_precision (int precision) +{ + _precision = precision; +} + +int get_precision () +{ + return _precision; +} + +void set_prompt (char *prompt) +{ + if (_prompt) { + free (_prompt); + } + _prompt = strdup (prompt); +} + +void set_format () { char buffer[128] = {0}; - free_format (); - sprintf (buffer, "%s%%.%dg\n", prompt, precision); - format = strdup (buffer); - sprintf (buffer, "%%.%dg", precision); - minform = strdup (buffer); + sprintf (buffer, "%s%%.%dg\n", _prompt ? _prompt : DEFAULT_PROMPT, _precision); + if (_format) { + free (_format); + } + _format = strdup (buffer); + sprintf (buffer, "%%.%dg", _precision); + if (_minform) { + free (_minform); + } + _minform = strdup (buffer); } void free_format () { - if (format) { - free (format); - format = NULL; + if (_format) { + free (_format); + _format = NULL; } - if (minform) { - free (minform); - minform = NULL; + if (_minform) { + free (_minform); + _minform = NULL; + } + if (_prompt) { + free (_prompt); + _prompt = NULL; } } double print (double value) { - fprintf (stdout, format ? format : DEFAULT_FORMAT, value); + fprintf (stdout, _format ? _format : DEFAULT_FORMAT, value); + fflush (stdout); + return value; +} + +double printl (double value) +{ + fprintf (stdout, _minform ? _minform : DEFAULT_MINFORM, value); fflush (stdout); return value; } + +/* vim: set ts=4 sw=4 et: */