home *** CD-ROM | disk | FTP | other *** search
- /*
- fncomma.c 11/18/86
-
- % strcomma, strnocomma
-
- Routines for using commas in numeric fields.
-
- C-scape 3.1
- Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 5/13/88 jmd removed length restrictions
- 12/13/88 jmd removed nasty strchr
- 6/01/89 gam added ocountry stuff
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
-
- #include "cscape.h"
- #include "fnfunc.h" /* for field functions */
-
- char *strcomma(s)
- char *s;
- /*
- Adds appropriate commas to a numeric string.
- Everything to right of the (first) decimal point is ignored.
-
- [ 123456789.3333]
- becomes: [ 123,456,789.3333]
- */
- {
- int count;
- char first;
- char *p, *q;
-
- /* remember first character */
- first = *s;
-
- /* find end of string or first decimal point */
- for (p = s; *p != ocountry.dec_char && *p != '\0'; p++) {
- ;
- }
-
- /* move through the string adding commas (removing old ones as we go) */
- count = 0;
- while(p >= s) {
- if ((p > s) && isdigit(*p)) {
- count++;
- if ((count % 3) == 0 && isdigit(*(p-1))) {
- /* slide over front of string (left), insert a comma. */
- p--; /* move to comma's slot */
- for (q = s; q < p; q++) {
- *q = *(q+1);
- }
- *q = ocountry.sep_char;
- }
- p--;
- }
- else if (*p == ocountry.sep_char) {
- if ((p > s) && (count > 0) && (count % 3) == 0 && isdigit(*(p-1))) {
- /* comma belongs */
- p--;
- }
- else {
- /* remove old comma */
- /* slide over front of string (right), insert a space at the beginning */
- for (q = p; q > s; q--) {
- *q = *(q-1);
- }
- *s = ' ';
- }
- }
- else {
- p--;
- }
- }
-
- /* restore first char if possible */
- if (*s == ' ') {
- *s = first;
- }
-
- return(s);
- }
-
- char *strnocomma(s)
- char *s;
- /*
- Removes commas from a string.
- Contracts strings towards the right.
-
- [ 123,456,789]
- becomes: [ 123456789]
- */
- {
- char *p, *q;
-
- p = s + strlen(s) - 1;
-
- /* move through the string removing commas */
- while(p >= s) {
- if (*p == ocountry.sep_char) {
- /* slide over front of string, insert a space at the beginning */
- for (q = p; q > s; q--) {
- *q = *(q-1);
- }
- *s = ' ';
- }
- else {
- p--;
- }
- }
-
- return(s);
- }
-
-