home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-2.ZIP / GFUNC / STRCONVT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  5.4 KB  |  249 lines

  1. /*
  2.  * strconvt.c
  3.  * contains: strconvert()
  4.  *
  5.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "gfuncts.h"
  11.  
  12. /*
  13.  *  char *
  14.  * strconvert(destination,source,option,maxoutlength)
  15.  *
  16.  * ARGUMENT
  17.  *  (char *)    destination    -    Destination string
  18.  *  (char *)    source        -    Source string
  19.  *  (int)    option    -
  20.  *            1    (RABLANKS)    Remove all Blanks
  21.  *            2   (RANONALPHA)    Remove all non alphabetic characters
  22.  *            4    (RANONNUM)    Remove all non numeric characters
  23.  *            8    (RALEADBLANKS)    Remove all leading blanks
  24.  *           16    (RTRBLANKS)    Remove trailing blanks
  25.  *           32    (RAALPHA)    Remove all alphabetic characters
  26.  *           64    (RANUMERIC)    Remove all numeric characters
  27.  *          128    (RAPUNCT)    Remove all punctuation
  28.  *          256    (CMBTOSINGLE)    Convert multiple blanks to single
  29.  *          512    (CLCTOUPPER)    Convert lower case characters to upper
  30.  *         1024    (CUCTOLOWER)    Convert upper case characters to lower
  31.  *         2048    (UCWORDS)    Capitalize all words
  32.  *         4096    (NUCWORDS)    Un-Capitalize all words
  33.  *
  34.  *  (int)    maxoutlength    -    Maximum length of output
  35.  *
  36.  * DESCRIPTION
  37.  *  The source string is copied to the destination string with character
  38.  *  conversions as specified in the (option) parameter.
  39.  *
  40.  * RETURNS
  41.  *  pointer to destination string or NULL if source or destination pointers
  42.  *  are NULL.
  43.  *
  44.  * AUTHOR
  45.  *  ""   14-APR-1987  09:45:03.68
  46.  */
  47. char* GF_CONV strconvert(destination,source,option,maxoutlength)
  48. char *destination,*source;
  49. int option,maxoutlength;
  50. {
  51.     int bumpdest,counter,nbcounter,skipcopy;
  52.     char *savdest,*smaxout,prevchar;
  53.     
  54.     if(!destination||!source||!maxoutlength)
  55.         return((char *)0);
  56.     smaxout=source+strlen(source);
  57.     if(option&RTRBLANKS)
  58.         for(--smaxout;*smaxout==' '&&smaxout>=source;--smaxout)
  59.             ;
  60.     for(prevchar='\0',savdest=destination,bumpdest=skipcopy=nbcounter=
  61.         counter=0;source<=smaxout&&maxoutlength&&*source;++counter,
  62.         ++source,skipcopy=bumpdest=FALSE) {
  63.         if(*source!=' ')
  64.             ++nbcounter;
  65.         if(option&RABLANKS && *source==' ')
  66.             continue;
  67.         if(option&RANONALPHA && (!_gisalpha(*source)&&
  68.            !_gisspace(*source)))
  69.             continue;
  70.         if(option&RANONNUM && !_gisnum(*source))
  71.             continue;
  72.         if(option&RALEADBLANKS && !nbcounter) {
  73.             prevchar=' ';
  74.             continue;
  75.         }
  76.         if(option&RAALPHA && _gisalpha(*source))
  77.             continue;
  78.         if(option&RANUMERIC && _gisnum(*source))
  79.             continue;
  80.         if(option&RAPUNCT && _gispunct(*source))
  81.             continue;
  82.         if(option&CMBTOSINGLE && (*source==' '&&prevchar==' '))
  83.             continue;
  84.         if(option&CLCTOUPPER) {
  85.             *destination=_gtoupper(*source);
  86.             skipcopy=bumpdest=TRUE;
  87.         }
  88.         if(option&CUCTOLOWER) {
  89.             *destination=_gtolower(*source);
  90.             skipcopy=bumpdest=TRUE;
  91.         }
  92.         if(option&UCWORDS && (prevchar==' '||prevchar==','||
  93.            prevchar=='.'||prevchar=='\0')) {
  94.                 *destination=_gtoupper(*source);
  95.             skipcopy=bumpdest=TRUE;
  96.         }
  97.         if(option&NUCWORDS && (prevchar==' '||prevchar==','||
  98.            prevchar=='.'||prevchar=='\0')) {
  99.                 *destination=_gtolower(*source);
  100.             skipcopy=bumpdest=TRUE;
  101.         }
  102.         if(!skipcopy) {
  103.             *destination++=*source;    /* Default case, copy */
  104.             prevchar=*source;
  105.             --maxoutlength;        /* character and decrement */
  106.         } else if(bumpdest) {
  107.             prevchar=*destination;
  108.             ++destination;
  109.             --maxoutlength;
  110.         }
  111.     }
  112.     *destination='\0';
  113.     return(savdest);
  114. }
  115.  
  116. /*
  117.  *  char
  118.  * _gtolower(c)
  119.  *
  120.  * ARGUMENT
  121.  *  (char)    c    -    Character to translate
  122.  *
  123.  * DESCRIPTION
  124.  *  Translate an upper case character to it's lower case equivalent
  125.  *
  126.  * RETURNS
  127.  *  lower case if c>='A'&&c<='Z' else c
  128.  *
  129.  * AUTHOR
  130.  *  ""   14-APR-1987  15:45:16.54
  131.  */
  132. char GF_CONV _gtolower(c)
  133. char c;
  134. {
  135.     return((char)((c>='A'&&c<='Z')?(char)(c+' '):c));
  136. }
  137.  
  138. /*
  139.  *  char
  140.  * _gtoupper(c)
  141.  *
  142.  * ARGUMENT
  143.  *  (char)    c    -    Character to translate
  144.  *
  145.  * DESCRIPTION
  146.  *  Translate a lower case character to it's upper case equivalent
  147.  *
  148.  * RETURNS
  149.  *  upper case if c>='a'&&c<='z' else c
  150.  *
  151.  * AUTHOR
  152.  *  ""   14-APR-1987  15:47:47.15
  153.  */
  154. char GF_CONV _gtoupper(c)
  155. char c;
  156. {
  157.     return((char)((c>='a'&&c<='z')?(char)(c-' '):c));
  158. }
  159.  
  160. /*
  161.  *  int
  162.  * _gisalpha(c)
  163.  *
  164.  * ARGUMENT
  165.  *  (char)    c    -    Character to check
  166.  *
  167.  * DESCRIPTION
  168.  *  Test if character is an alpha character
  169.  *
  170.  * RETURNS
  171.  *  1 if [a-z][A-Z] else 0
  172.  *
  173.  * AUTHOR
  174.  *  ""   14-APR-1987  15:49:01.45
  175.  */
  176. int GF_CONV _gisalpha(c)
  177. char c;
  178. {
  179.     return(((c>='a'&&c<='z')||(c>='A'&&c<='Z'))?TRUE:FALSE);
  180. }
  181.  
  182.  
  183. /*
  184.  *  int
  185.  * _gispunct(c)
  186.  *
  187.  * ARGUMENT
  188.  *  (char)    c    -    Character to check
  189.  *
  190.  * DESCRIPTION
  191.  *  Test if character is a punctuation character
  192.  *
  193.  * RETURNS
  194.  *  1 if [.,!;:'"?] else 0
  195.  *
  196.  * AUTHOR
  197.  *  ""   14-APR-1987  15:49:57.71
  198.  */
  199. int GF_CONV _gispunct(c)
  200. char c;
  201. {
  202.     return((c=='.'||c==','||c=='!'||c==';'||c==':'||c=='\''||c=='"'||
  203.         c=='?')?TRUE:FALSE);
  204. }
  205.  
  206. /*
  207.  *  int
  208.  * _gisspace(c)
  209.  *
  210.  * ARGUMENT
  211.  *  (char)    c    -    Character to check
  212.  *
  213.  * DESCRIPTION
  214.  *  Test if character is a space character.
  215.  *
  216.  * RETURNS
  217.  *  1 if c==' ' else 0
  218.  *
  219.  * AUTHOR
  220.  *  ""   14-APR-1987  15:50:35.71
  221.  */
  222. int GF_CONV _gisspace(c)
  223. char c;
  224. {
  225.     return((c==' ')?TRUE:FALSE);
  226. }
  227.  
  228. /*
  229.  *  int
  230.  * _gisnum(c)
  231.  *
  232.  * ARGUMENT
  233.  *  (char)    c    -    Character to check
  234.  *
  235.  * DESCRIPTION
  236.  *  Test if character is a numeric.
  237.  *
  238.  * RETURNS
  239.  *  1 if [0=9] else 0
  240.  *
  241.  * AUTHOR
  242.  *  ""   14-APR-1987  15:51:20.19
  243.  */
  244. int GF_CONV _gisnum(c)
  245. char c;
  246. {
  247.     return((c>='0'&&c<='9')?TRUE:FALSE);
  248. }
  249.