home *** CD-ROM | disk | FTP | other *** search
- /*
- fnvalfmt.c 9/21/88
-
- % valid_Format
-
- Field formatter
-
- C-scape 3.2
- Copyright (c) 1988, by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 12/16/88 jmd Added test for empty format string
-
- 11/04/89 jdc changed toupper & tolower to otoupper & otolower
- 3/19/90 jmd converted into a boolean
- 3/28/90 jmd ansi-fied
- 6/10/90 mla fixed fixed decimal formating
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
-
- #include "cscape.h"
- #include "strdecl.h" /* for C-scape string functions */
- #include "fnfunc.h" /* For the rest of the str functions */
-
- enum not_flag { NOT_OFF, NOT_PENDING, NOT_ON };
- enum case_style { CASE_NOCHANGE, CASE_UPPER, CASE_LOWER, CASE_PROPER, CASE_IMPROPER };
- enum justify_style { JUST_NOCHANGE, JUST_LEFT, JUST_RIGHT, JUST_CENTER };
- enum comma_style { COMMA_NOCHANGE, COMMA_ON, COMMA_OFF };
- enum pad_style { PAD_NOCHANGE, PAD_ON, PAD_OFF };
-
- boolean valid_Format(sed_type sed, int fieldno, char *fmt)
- /*
- Performs formatting commands upon a field record.
- Supports the following commands:
- Not:
- ----
- ! Negate next command character
- Formats:
- --------
- U UPPER CASE !U lower case
- l lower case !l UPPER CASE
- P Proper Case !P iMPROPER cASE
- _ Pad with spaces !_ clip trailing spaces
- c Compact: remove spaces !c Compact
- Jusification:
- -------------
- ^ Center !^ Center
- < left justify !< right justify
- > right justify !> left justify
- Numeric:
- --------
- 0 - 9 Number of fixed decimal places. 0 removes decpt
- , Add commas to numerics !, remove commas
- fmt contains format command string.
- Modifies contents of field record.
- Use scratch pad.
- Returns TRUE if the record changed, FALSE otherwise
- */
- {
- char *p, *spad, *q;
- int rlen;
- boolean first;
- boolean compact = FALSE;
-
- enum not_flag fnot = NOT_OFF; /* "fnot for you..." */
- enum case_style fcase = CASE_NOCHANGE;
- enum justify_style fjust = JUST_NOCHANGE;
- enum comma_style fcomma = COMMA_NOCHANGE;
- enum pad_style fpad = PAD_NOCHANGE;
-
- int decp = -1; /* don't touch decimal point unless asked */
-
- if (fmt == NULL || fmt[0] == '\0') {
- return(FALSE);
- }
-
- /* put record into scratch pad, get lengths */
- spad = sed_GetScratchPad(sed);
- strcpy(spad, sed_GetRecord(sed, fieldno));
- rlen = sed_GetRecordLen(sed, fieldno);
-
- /* set formatting switches */
- for (p = fmt; *p != '\0'; p++) {
- /* deal with not flag (turn on if pending, off otherwise) */
- fnot = (fnot != NOT_PENDING) ? NOT_OFF : NOT_ON;
-
- switch(*p) {
- case '!':
- /* Not flag (set to pending, next loop through will turn it off) */
- fnot = NOT_PENDING;
- break;
- case 'U':
- /* UPPER CASE */
- fcase = (fnot == NOT_ON) ? CASE_LOWER : CASE_UPPER;
- break;
- case 'l':
- /* lower case */
- fcase = (fnot == NOT_ON) ? CASE_UPPER : CASE_LOWER;
- break;
- case 'P':
- /* Proper Case */
- fcase = (fnot == NOT_ON) ? CASE_IMPROPER : CASE_PROPER;
- break;
- case 'c':
- /* Compact string (remove all spaces) */
- compact = TRUE;
- break;
- case '_':
- /* Pad with spaces */
- fpad = (fnot == NOT_ON) ? PAD_OFF : PAD_ON;
- break;
- case '^':
- /* Center */
- fjust = JUST_CENTER;
- break;
- case '<':
- /* Left justify */
- fjust = (fnot == NOT_ON) ? JUST_RIGHT : JUST_LEFT;
- break;
- case '>':
- /* Right justify */
- fjust = (fnot == NOT_ON) ? JUST_LEFT : JUST_RIGHT;
- break;
- case ',':
- /* Comma'd numeric */
- fcomma = (fnot == NOT_ON) ? COMMA_OFF : COMMA_ON;
- break;
-
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- /* Fixed decimal numeric */
- decp = *p - '0';
- break;
- }
- }
-
- /* Compact string */
- if (compact) {
- strcompact(spad);
- }
-
- /* Perform numeric formatting */
- if (decp >= 0) {
- strcompact(spad);
- strfixdp(spad, decp, rlen);
- strright(spad, rlen);
- }
-
- switch (fcomma) {
- case COMMA_ON:
- strright(spad, rlen); /* strcomma assumes left justified strings */
- strcomma(spad);
- break;
- case COMMA_OFF:
- strnocomma(spad);
- break;
- }
-
- /* Perform string formatting */
- switch (fcase) {
- case CASE_UPPER:
- case CASE_LOWER:
- for (q = spad; *q != '\0'; q++) {
- *q = (fcase == CASE_UPPER) ? otoupper(*q) : otolower(*q);
- }
- break;
- case CASE_PROPER:
- case CASE_IMPROPER:
- first = TRUE;
- for (q = spad; *q != '\0'; q++) {
- if (first) {
- *q = (char)((fcase == CASE_PROPER) ? otoupper(*q) : otolower(*q));
- first = FALSE;
- }
- else {
- *q = (char)((fcase == CASE_PROPER) ? otolower(*q) : otoupper(*q));
- }
- first = (*q == ' ');
- }
- break;
- }
-
- switch (fjust) {
- case JUST_LEFT:
- strleft(spad, rlen);
- break;
- case JUST_RIGHT:
- strright(spad, rlen);
- break;
- case JUST_CENTER:
- strcenter(spad, rlen);
- break;
- }
-
- switch (fpad) {
- case PAD_ON:
- strpad(spad, rlen);
- break;
- case PAD_OFF:
- strclip(spad);
- break;
- }
-
- /* test if the record changed */
- if (strcmp(sed_GetRecord(sed, fieldno), spad) == 0) {
- /* no change */
- return(FALSE);
- }
- else {
- /* copy scratch pad back into record */
- sed_SetRecord(sed, spad, fieldno);
- return(TRUE);
- }
- }
-