home *** CD-ROM | disk | FTP | other *** search
- /*
- * strconvt.c
- * contains: strconvert()
- *
- * Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
- */
-
- #include <stdio.h>
- #include <string.h>
- #include "gfuncts.h"
-
- /*
- * char *
- * strconvert(destination,source,option,maxoutlength)
- *
- * ARGUMENT
- * (char *) destination - Destination string
- * (char *) source - Source string
- * (int) option -
- * 1 (RABLANKS) Remove all Blanks
- * 2 (RANONALPHA) Remove all non alphabetic characters
- * 4 (RANONNUM) Remove all non numeric characters
- * 8 (RALEADBLANKS) Remove all leading blanks
- * 16 (RTRBLANKS) Remove trailing blanks
- * 32 (RAALPHA) Remove all alphabetic characters
- * 64 (RANUMERIC) Remove all numeric characters
- * 128 (RAPUNCT) Remove all punctuation
- * 256 (CMBTOSINGLE) Convert multiple blanks to single
- * 512 (CLCTOUPPER) Convert lower case characters to upper
- * 1024 (CUCTOLOWER) Convert upper case characters to lower
- * 2048 (UCWORDS) Capitalize all words
- * 4096 (NUCWORDS) Un-Capitalize all words
- *
- * (int) maxoutlength - Maximum length of output
- *
- * DESCRIPTION
- * The source string is copied to the destination string with character
- * conversions as specified in the (option) parameter.
- *
- * RETURNS
- * pointer to destination string or NULL if source or destination pointers
- * are NULL.
- *
- * AUTHOR
- * "" 14-APR-1987 09:45:03.68
- */
- char* GF_CONV strconvert(destination,source,option,maxoutlength)
- char *destination,*source;
- int option,maxoutlength;
- {
- int bumpdest,counter,nbcounter,skipcopy;
- char *savdest,*smaxout,prevchar;
-
- if(!destination||!source||!maxoutlength)
- return((char *)0);
- smaxout=source+strlen(source);
- if(option&RTRBLANKS)
- for(--smaxout;*smaxout==' '&&smaxout>=source;--smaxout)
- ;
- for(prevchar='\0',savdest=destination,bumpdest=skipcopy=nbcounter=
- counter=0;source<=smaxout&&maxoutlength&&*source;++counter,
- ++source,skipcopy=bumpdest=FALSE) {
- if(*source!=' ')
- ++nbcounter;
- if(option&RABLANKS && *source==' ')
- continue;
- if(option&RANONALPHA && (!_gisalpha(*source)&&
- !_gisspace(*source)))
- continue;
- if(option&RANONNUM && !_gisnum(*source))
- continue;
- if(option&RALEADBLANKS && !nbcounter) {
- prevchar=' ';
- continue;
- }
- if(option&RAALPHA && _gisalpha(*source))
- continue;
- if(option&RANUMERIC && _gisnum(*source))
- continue;
- if(option&RAPUNCT && _gispunct(*source))
- continue;
- if(option&CMBTOSINGLE && (*source==' '&&prevchar==' '))
- continue;
- if(option&CLCTOUPPER) {
- *destination=_gtoupper(*source);
- skipcopy=bumpdest=TRUE;
- }
- if(option&CUCTOLOWER) {
- *destination=_gtolower(*source);
- skipcopy=bumpdest=TRUE;
- }
- if(option&UCWORDS && (prevchar==' '||prevchar==','||
- prevchar=='.'||prevchar=='\0')) {
- *destination=_gtoupper(*source);
- skipcopy=bumpdest=TRUE;
- }
- if(option&NUCWORDS && (prevchar==' '||prevchar==','||
- prevchar=='.'||prevchar=='\0')) {
- *destination=_gtolower(*source);
- skipcopy=bumpdest=TRUE;
- }
- if(!skipcopy) {
- *destination++=*source; /* Default case, copy */
- prevchar=*source;
- --maxoutlength; /* character and decrement */
- } else if(bumpdest) {
- prevchar=*destination;
- ++destination;
- --maxoutlength;
- }
- }
- *destination='\0';
- return(savdest);
- }
-
- /*
- * char
- * _gtolower(c)
- *
- * ARGUMENT
- * (char) c - Character to translate
- *
- * DESCRIPTION
- * Translate an upper case character to it's lower case equivalent
- *
- * RETURNS
- * lower case if c>='A'&&c<='Z' else c
- *
- * AUTHOR
- * "" 14-APR-1987 15:45:16.54
- */
- char GF_CONV _gtolower(c)
- char c;
- {
- return((char)((c>='A'&&c<='Z')?(char)(c+' '):c));
- }
-
- /*
- * char
- * _gtoupper(c)
- *
- * ARGUMENT
- * (char) c - Character to translate
- *
- * DESCRIPTION
- * Translate a lower case character to it's upper case equivalent
- *
- * RETURNS
- * upper case if c>='a'&&c<='z' else c
- *
- * AUTHOR
- * "" 14-APR-1987 15:47:47.15
- */
- char GF_CONV _gtoupper(c)
- char c;
- {
- return((char)((c>='a'&&c<='z')?(char)(c-' '):c));
- }
-
- /*
- * int
- * _gisalpha(c)
- *
- * ARGUMENT
- * (char) c - Character to check
- *
- * DESCRIPTION
- * Test if character is an alpha character
- *
- * RETURNS
- * 1 if [a-z][A-Z] else 0
- *
- * AUTHOR
- * "" 14-APR-1987 15:49:01.45
- */
- int GF_CONV _gisalpha(c)
- char c;
- {
- return(((c>='a'&&c<='z')||(c>='A'&&c<='Z'))?TRUE:FALSE);
- }
-
-
- /*
- * int
- * _gispunct(c)
- *
- * ARGUMENT
- * (char) c - Character to check
- *
- * DESCRIPTION
- * Test if character is a punctuation character
- *
- * RETURNS
- * 1 if [.,!;:'"?] else 0
- *
- * AUTHOR
- * "" 14-APR-1987 15:49:57.71
- */
- int GF_CONV _gispunct(c)
- char c;
- {
- return((c=='.'||c==','||c=='!'||c==';'||c==':'||c=='\''||c=='"'||
- c=='?')?TRUE:FALSE);
- }
-
- /*
- * int
- * _gisspace(c)
- *
- * ARGUMENT
- * (char) c - Character to check
- *
- * DESCRIPTION
- * Test if character is a space character.
- *
- * RETURNS
- * 1 if c==' ' else 0
- *
- * AUTHOR
- * "" 14-APR-1987 15:50:35.71
- */
- int GF_CONV _gisspace(c)
- char c;
- {
- return((c==' ')?TRUE:FALSE);
- }
-
- /*
- * int
- * _gisnum(c)
- *
- * ARGUMENT
- * (char) c - Character to check
- *
- * DESCRIPTION
- * Test if character is a numeric.
- *
- * RETURNS
- * 1 if [0=9] else 0
- *
- * AUTHOR
- * "" 14-APR-1987 15:51:20.19
- */
- int GF_CONV _gisnum(c)
- char c;
- {
- return((c>='0'&&c<='9')?TRUE:FALSE);
- }
-