home *** CD-ROM | disk | FTP | other *** search
- /*
- fndecp.c 11/18/86
-
- % strdecp, strnodecp
-
- Routines for using fixed decimal points 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
- 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 *strdecp(s, pos)
- char *s;
- int pos;
- /*
- Place a fixed decimal point at pos.
- Removes leading zeroes (unless there is no other number left of the dec pt).
-
- (pos = 2) [ -00000342]
- becomes: [ -3.42]
- */
- {
- int count, minus;
- char first;
- char *p, *q;
-
- /* remember first character */
- first = *s;
-
- /* go through the string thrice */
-
- /* first add leading zeroes, (so that short strings work correctly) */
- /* see if this is a negative number */
- minus = FALSE;
- for (p = s;; p++) {
- if (*p == ' ') {
- *p = '0';
- }
- else if (*p == '-') {
- *p = '0';
- minus = TRUE;
- }
- else {
- break;
- }
- }
-
- /* second, add the decimal point (removing old decimal points) */
- p = s + strlen(s) - 1;
- count = 0;
- while(p >= s) {
- if ((p > s) && isdigit(*p)) {
- count++;
- if ((count == pos) && (*(p-1) != ocountry.dec_char)) {
- /* slide over front of string (left), insert a point. */
- p--; /* move to decp's slot */
- for (q = s; q < p; q++) {
- *q = *(q+1);
- }
- *q = ocountry.dec_char;
- /* increment count so that all future decp's are bad */
- count++;
- }
- p--;
- }
- else if (*p == ocountry.dec_char) {
- if (count == pos) {
- /* decp belongs */
- p--;
- /* increment count so that all future decp's are bad */
- count++;
- }
- else {
- /* remove old decp */
- /* slide over front of string (right), insert a '0' at the beginning */
- for (q = p; q > s; q--) {
- *q = *(q-1);
- }
- *s = '0';
- }
- }
- else {
- p--;
- }
- }
-
- /* now get rid of leading zeroes (assume no leading spaces) */
- for(p = s, q = s; *p != '\0'; p++) {
- if (*p != '0' || *(p+1) == ocountry.dec_char || *(p+1) == '\0') {
- break;
- }
- else {
- *p = ' ';
- q++;
- }
- }
-
- /* restore minus sign */
- q--;
- if (minus && (q >= s)) {
- *q = '-';
- }
-
- /* restore first char if possible */
- if (*s == ' ' && first != '0') {
- *s = first;
- }
-
- return(s);
- }
-
- char *strnodecp(s)
- char *s;
- /*
- Removes the decimal point from a string.
- Contracts strings towards the right.
-
- [ 1234567.89]
- becomes: [ 123456789]
- */
- {
- char *p, *q;
-
- p = s + strlen(s) - 1;
-
- /* move through the string removing commas */
- while(p >= s) {
- if (*p == ocountry.dec_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);
- }
-