home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 1.ddi / FUNCS.EXE / CSCAPE / SOURCE / FNCOMMA.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  2.3 KB  |  121 lines

  1. /*
  2.     fncomma.c      11/18/86
  3.  
  4.     % strcomma, strnocomma
  5.  
  6.     Routines for using commas in numeric fields.
  7.  
  8.     C-scape 3.1
  9.     Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
  10.     ALL RIGHTS RESERVED.
  11.  
  12.     Revision History:
  13.     -----------------
  14.      5/13/88 jmd     removed length restrictions
  15.     12/13/88 jmd     removed nasty strchr
  16.      6/01/89 gam    added ocountry stuff
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22.  
  23. #include "cscape.h"
  24. #include "fnfunc.h"            /* for field functions */
  25.  
  26. char *strcomma(s)
  27.     char *s;
  28. /*
  29.     Adds appropriate commas to a numeric string.
  30.     Everything to right of the (first) decimal point is ignored.
  31.  
  32.                     [   123456789.3333]
  33.      becomes:         [ 123,456,789.3333]
  34. */
  35. {
  36.     int        count;
  37.     char    first;
  38.     char     *p, *q;
  39.  
  40.     /* remember first character */
  41.     first = *s;
  42.  
  43.     /* find end of string or first decimal point */
  44.     for (p = s; *p != ocountry.dec_char && *p != '\0'; p++) {
  45.         ;
  46.     }
  47.  
  48.     /* move through the string adding commas (removing old ones as we go) */
  49.     count = 0;
  50.     while(p >= s) {
  51.         if ((p > s) && isdigit(*p)) {
  52.             count++;
  53.             if ((count % 3) == 0 && isdigit(*(p-1))) {
  54.                 /* slide over front of string (left), insert a comma. */
  55.                 p--;    /* move to comma's slot */
  56.                 for (q = s; q < p; q++) {
  57.                     *q = *(q+1);
  58.                 }
  59.                 *q = ocountry.sep_char;
  60.             }
  61.             p--;
  62.         }
  63.         else if (*p == ocountry.sep_char) {
  64.             if ((p > s) && (count > 0) && (count % 3) == 0 && isdigit(*(p-1))) {
  65.                 /* comma belongs */
  66.                 p--;
  67.             }
  68.             else {
  69.                 /* remove old comma */
  70.                 /* slide over front of string (right), insert a space at the beginning */
  71.                 for (q = p; q > s; q--) {
  72.                     *q = *(q-1);
  73.                 }
  74.                 *s = ' ';
  75.             }
  76.         }
  77.         else {
  78.             p--;
  79.         }
  80.     }
  81.  
  82.     /* restore first char if possible */
  83.     if (*s == ' ') {
  84.         *s = first;
  85.     }
  86.  
  87.     return(s);
  88. }
  89.  
  90. char *strnocomma(s)
  91.     char     *s;
  92. /*
  93.     Removes commas from a string.
  94.     Contracts strings towards the right.
  95.  
  96.                     [ 123,456,789]
  97.      becomes:        [   123456789]
  98. */
  99. {
  100.     char     *p, *q;
  101.  
  102.     p = s + strlen(s) - 1;
  103.  
  104.     /* move through the string removing commas */
  105.     while(p >= s) {
  106.         if (*p == ocountry.sep_char) {
  107.             /* slide over front of string, insert a space at the beginning */
  108.             for (q = p; q > s; q--) {
  109.                 *q = *(q-1);
  110.             }
  111.             *s = ' ';
  112.         }
  113.         else {
  114.             p--;
  115.         }
  116.     }
  117.  
  118.     return(s);
  119. }
  120.  
  121.