aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/other/picture.c
diff options
context:
space:
mode:
authorWilliam Harrington <kb0iic@berzerkula.org>2025-01-14 16:06:02 -0600
committerWilliam Harrington <kb0iic@berzerkula.org>2025-01-14 16:06:02 -0600
commit0cc9b20c15460213e488bf5e70963b941482f628 (patch)
treebb0143245583ec846630f39bfa2258dba640ccd7 /sdk/other/picture.c
parent0e084ade5069756d487b5c948c48b777e37c00c9 (diff)
Add source.
Diffstat (limited to 'sdk/other/picture.c')
-rw-r--r--sdk/other/picture.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/sdk/other/picture.c b/sdk/other/picture.c
new file mode 100644
index 0000000..3ff2dba
--- /dev/null
+++ b/sdk/other/picture.c
@@ -0,0 +1,112 @@
+/*
+ * String formatting of numeric data.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <std/string.h>
+
+char *picture(char *buf, const char *pict, long value)
+{
+ char *bp = buf, *tp;
+ long shift = 0;
+ int sign = 0;
+ int zfill = 0;
+ int digit;
+ int currency = 0;
+ int digits = 0;
+
+ if(value < 0)
+ {
+ ++sign;
+ value = -value;
+ }
+
+ while(*pict)
+ {
+ switch(*pict)
+ {
+ case '$':
+ ++currency;
+ *(bp++) = ' ';
+ ++pict;
+ break;
+ case '+':
+ if(sign)
+ *(bp++) = ' ';
+ else
+ *(bp++) = '+';
+ ++pict;
+ break;
+ case '-':
+ if(sign)
+ *(bp++) = '-';
+ else
+ *(bp++) = ' ';
+ ++pict;
+ break;
+ case '9':
+ case '0':
+ shift *= 10;
+ if(!shift)
+ shift = 1;
+ default:
+ *(bp++) = *(pict++);
+ }
+ }
+ if(value >= shift * 10)
+ {
+ bp = buf;
+ while(*bp)
+ {
+ if(isdigit(*bp))
+ *bp = '#';
+ ++bp;
+ }
+ return buf;
+ }
+ bp = tp = buf;
+ while(*bp)
+ {
+ switch(*bp)
+ {
+ case ',':
+ if(*(bp - 1) == '#')
+ {
+ *bp = '#';
+ break;
+ }
+ if(!zfill)
+ *bp = ' ';
+ break;
+ case '#':
+ if(!digits)
+ if(shift > value)
+ break;
+ case '0':
+ ++zfill;
+ case '9':
+ ++digits;
+ digit = (int)(value / shift);
+ if((digit > 0) || (shift == 1))
+ ++zfill;
+ if(!zfill && !digit)
+ digit = (int)(' ' - '0');
+ *bp = (char)('0' + digit);
+ if(isdigit(*bp) && currency)
+ {
+ currency = 0;
+ *(tp - 1) = '$';
+ }
+ value %= shift;
+ shift /= 10;
+ }
+ if(*bp != '#')
+ *(tp++) = *bp;
+ ++bp;
+ }
+ *tp = 0;
+ return buf;
+}
+