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

  1. /*
  2.     fndecp.c     11/18/86
  3.  
  4.     % strdecp, strnodecp
  5.  
  6.     Routines for using fixed decimal points 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.      6/01/89 gam    added ocountry stuff
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21.  
  22. #include "cscape.h"
  23. #include "fnfunc.h"            /* for field functions */
  24.  
  25. char *strdecp(s, pos)
  26.     char *s;
  27.     int     pos;
  28. /*
  29.     Place a fixed decimal point at pos.
  30.     Removes leading zeroes (unless there is no other number left of the dec pt).
  31.  
  32.     (pos = 2)        [   -00000342]
  33.      becomes:         [       -3.42]
  34. */
  35. {
  36.     int        count, minus;
  37.     char    first;
  38.     char     *p, *q;
  39.  
  40.     /* remember first character */
  41.     first = *s;
  42.  
  43.     /* go through the string thrice */
  44.  
  45.     /* first add leading zeroes, (so that short strings work correctly) */
  46.     /* see if this is a negative number */
  47.     minus = FALSE;
  48.     for (p = s;; p++) {
  49.         if (*p == ' ') {
  50.             *p = '0';
  51.         }
  52.         else if (*p == '-') {
  53.             *p = '0';
  54.             minus = TRUE;
  55.         }
  56.         else {
  57.             break;
  58.         }
  59.     }
  60.  
  61.     /* second, add the decimal point (removing old decimal points) */
  62.     p = s + strlen(s) - 1;
  63.     count = 0;
  64.     while(p >= s) {
  65.         if ((p > s) && isdigit(*p)) {
  66.             count++;
  67.             if ((count == pos) && (*(p-1) != ocountry.dec_char)) {
  68.                 /* slide over front of string (left), insert a point. */
  69.                 p--;    /* move to decp's slot */
  70.                 for (q = s; q < p; q++) {
  71.                     *q = *(q+1);
  72.                 }
  73.                 *q = ocountry.dec_char;
  74.                 /* increment count so that all future decp's are bad */
  75.                 count++;
  76.             }
  77.             p--;
  78.         }
  79.         else if (*p == ocountry.dec_char) {
  80.             if (count == pos) {
  81.                 /* decp belongs */
  82.                 p--;
  83.                 /* increment count so that all future decp's are bad */
  84.                 count++;
  85.             }
  86.             else {
  87.                 /* remove old decp */
  88.                 /* slide over front of string (right), insert a '0' at the beginning */
  89.                 for (q = p; q > s; q--) {
  90.                     *q = *(q-1);
  91.                 }
  92.                 *s = '0';
  93.             }
  94.         }
  95.         else {
  96.             p--;
  97.         }
  98.     }
  99.  
  100.     /* now get rid of leading zeroes (assume no leading spaces) */
  101.     for(p = s, q = s; *p != '\0'; p++) {
  102.         if (*p != '0' || *(p+1) == ocountry.dec_char || *(p+1) == '\0') {
  103.             break;
  104.         }
  105.         else {
  106.             *p = ' ';
  107.             q++;
  108.         }
  109.     }
  110.  
  111.     /* restore minus sign */
  112.     q--;
  113.     if (minus && (q >= s)) {
  114.         *q = '-';
  115.     }
  116.  
  117.     /* restore first char if possible */
  118.     if (*s == ' ' && first != '0') {
  119.         *s = first;
  120.     }
  121.  
  122.     return(s);
  123. }
  124.  
  125. char *strnodecp(s)
  126.     char     *s;
  127. /*
  128.     Removes the decimal point from a string.
  129.     Contracts strings towards the right.
  130.  
  131.                     [ 1234567.89]
  132.      becomes:        [  123456789]
  133. */
  134. {
  135.     char     *p, *q;
  136.  
  137.     p = s + strlen(s) - 1;
  138.  
  139.     /* move through the string removing commas */
  140.     while(p >= s) {
  141.         if (*p == ocountry.dec_char) {
  142.             /* slide over front of string, insert a space at the beginning */
  143.             for (q = p; q > s; q--) {
  144.                 *q = *(q-1);
  145.             }
  146.             *s = ' ';
  147.         }
  148.         else {
  149.             p--;
  150.         }
  151.     }
  152.  
  153.     return(s);
  154. }
  155.