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

  1. /*
  2.       fnvalfmt.c            9/21/88
  3.  
  4.     % valid_Format
  5.  
  6.     Field formatter
  7.  
  8.     C-scape 3.1
  9.     Copyright (c) 1988, by Oakland Group, Inc.
  10.     ALL RIGHTS RESERVED.
  11.  
  12.     Revision History:
  13.     -----------------
  14.     12/16/88 jmd    Added test for empty format string
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. #include "cscape.h"
  22. #include "strdecl.h"        /* for C-scape string functions */
  23. #include "fnfunc.h"            /* For the rest of the str functions */
  24.  
  25. enum not_flag          { NOT_OFF, NOT_PENDING, NOT_ON };
  26. enum case_style      { CASE_NOCHANGE, CASE_UPPER, CASE_LOWER, CASE_PROPER, CASE_IMPROPER };
  27. enum justify_style      { JUST_NOCHANGE, JUST_LEFT, JUST_RIGHT, JUST_CENTER };
  28. enum comma_style      { COMMA_NOCHANGE, COMMA_ON, COMMA_OFF };
  29. enum pad_style           { PAD_NOCHANGE, PAD_ON, PAD_OFF };
  30.  
  31. void valid_Format(sed, fieldno, fmt)
  32.     sed_type sed;
  33.     int fieldno;
  34.     char *fmt;
  35. /*
  36.     Performs formatting commands upon a field record.
  37.  
  38.     Supports the following commands:
  39.  
  40.     Not:
  41.     ----
  42.     !    Negate next command character
  43.  
  44.  
  45.     Formats:
  46.     --------
  47.     U    UPPER CASE                !U    lower case
  48.     l    lower case                !l    UPPER CASE
  49.     P    Proper Case                !P    iMPROPER cASE
  50.     _    Pad with spaces            !_    clip trailing spaces
  51.     c    Compact: remove spaces    !c    Compact
  52.  
  53.     Jusification:
  54.     -------------
  55.     ^    Center                    !^    Center
  56.     <    left justify            !<    right justify
  57.     >   right justify            !>    left justify
  58.  
  59.     Numeric:
  60.     --------
  61.     0 - 9    Number of fixed decimal places.        0      removes decpt
  62.     ,        Add commas to numerics                !,     remove commas
  63.  
  64.     fmt contains format command string.
  65.  
  66.     Modifies contents of field record.
  67.     Use scratch pad.
  68. */
  69. {
  70.     char   *p, *spad, *q;
  71.     int     rlen;
  72.     boolean    first;
  73.     boolean    compact = FALSE;
  74.  
  75.     enum not_flag        fnot   = NOT_OFF;                /* "fnot for you..." */
  76.     enum case_style       fcase  = CASE_NOCHANGE;
  77.     enum justify_style fjust  = JUST_NOCHANGE;
  78.     enum comma_style   fcomma = COMMA_NOCHANGE;
  79.     enum pad_style     fpad   = PAD_NOCHANGE;
  80.  
  81.     int        decp = -1;    /* don't touch decimal point unless asked */
  82.  
  83.     if (fmt == NULL || fmt[0] == '\0') {
  84.         return;
  85.     }
  86.  
  87.     /* put record into scratch pad, get lengths */
  88.     spad = sed_GetScratchPad(sed);
  89.     strcpy(spad, sed_GetRecord(sed, fieldno));
  90.     rlen = sed_GetRecordLen(sed, fieldno);
  91.  
  92.     /* set formatting switches */
  93.     for (p = fmt; *p != '\0'; p++) {
  94.         /* deal with not flag (turn on if pending, off otherwise) */
  95.         fnot = (fnot != NOT_PENDING) ? NOT_OFF : NOT_ON;
  96.  
  97.         switch(*p) {
  98.         case '!':
  99.             /* Not flag (set to pending, next loop through will turn it off) */
  100.             fnot = NOT_PENDING;
  101.             break;
  102.         case 'U':
  103.             /* UPPER CASE */
  104.             fcase = (fnot == NOT_ON) ? CASE_LOWER : CASE_UPPER;
  105.             break;
  106.         case 'l':
  107.             /* lower case */
  108.             fcase = (fnot == NOT_ON) ? CASE_UPPER : CASE_LOWER;
  109.             break;
  110.         case 'P':
  111.             /* Proper Case */
  112.             fcase = (fnot == NOT_ON) ? CASE_IMPROPER : CASE_PROPER;
  113.             break;
  114.         case 'c':
  115.             /* Compact string (remove all spaces) */
  116.             compact = TRUE;
  117.             break;
  118.         case '_':
  119.             /* Pad with spaces */
  120.             fpad = (fnot == NOT_ON) ? PAD_OFF : PAD_ON;
  121.             break;
  122.         case '^':
  123.             /* Center */
  124.             fjust = JUST_CENTER;
  125.             break;
  126.         case '<':
  127.             /* Left justify */
  128.             fjust = (fnot == NOT_ON) ? JUST_RIGHT : JUST_LEFT;
  129.             break;
  130.         case '>':
  131.             /* Right justify */
  132.             fjust = (fnot == NOT_ON) ? JUST_LEFT : JUST_RIGHT;
  133.             break;
  134.         case ',':
  135.             /* Comma'd numeric */
  136.             fcomma = (fnot == NOT_ON) ? COMMA_OFF : COMMA_ON;
  137.             break;
  138.  
  139.         case '0':
  140.         case '1':
  141.         case '2':
  142.         case '3':
  143.         case '4':
  144.         case '5':
  145.         case '6':
  146.         case '7':
  147.         case '8':
  148.         case '9':
  149.             /* Fixed decimal numeric */
  150.             decp = *p - '0';
  151.             break;
  152.         }
  153.     }
  154.  
  155.     /* Compact string */
  156.     if (compact) {
  157.         strcompact(spad);
  158.     }
  159.  
  160.     /* Perform numeric formatting */
  161.     if (decp > 0) {
  162.         strright(spad, rlen);    /* strdecp assumes left justified strings */
  163.         strdecp(spad, decp);
  164.     }
  165.     else if (decp == 0) {
  166.         strnodecp(spad);
  167.     }
  168.  
  169.     switch (fcomma) {
  170.     case COMMA_ON:
  171.         strright(spad, rlen);    /* strcomma assumes left justified strings */
  172.         strcomma(spad);
  173.         break;
  174.     case COMMA_OFF:
  175.         strnocomma(spad);
  176.         break;
  177.     }
  178.  
  179.     /* Perform string formatting */
  180.     switch (fcase) {
  181.     case CASE_UPPER:
  182.     case CASE_LOWER:
  183.         for (q = spad; *q != '\0'; q++) {
  184.             *q = (fcase == CASE_UPPER) ? toupper(*q) : tolower(*q);
  185.         }
  186.         break;
  187.     case CASE_PROPER:
  188.     case CASE_IMPROPER:
  189.         first = TRUE;
  190.         for (q = spad; *q != '\0'; q++) {
  191.             if (first) {
  192.                 *q = (fcase == CASE_PROPER) ? toupper(*q) : tolower(*q);
  193.                 first = FALSE;
  194.             }
  195.             else {
  196.                 *q = (fcase == CASE_PROPER) ? tolower(*q) : toupper(*q);
  197.             }
  198.             first = (*q == ' ');
  199.         }
  200.         break;
  201.     }
  202.  
  203.     switch (fjust) {
  204.     case JUST_LEFT:
  205.         strleft(spad, rlen);
  206.         break;
  207.     case JUST_RIGHT:
  208.         strright(spad, rlen);
  209.         break;
  210.     case JUST_CENTER:
  211.         strcenter(spad, rlen);
  212.         break;
  213.     }
  214.  
  215.     switch (fpad) {
  216.     case PAD_ON:
  217.         strpad(spad, rlen);
  218.         break;
  219.     case PAD_OFF:
  220.         strclip(spad);
  221.         break;
  222.     }
  223.  
  224.     /* copy scratch pad back into record */
  225.     sed_SetRecord(sed, spad, fieldno);
  226. }
  227.