home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / NFSRC305.ZIP / C / PROPER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-01  |  3.5 KB  |  139 lines

  1. /*
  2.  * File......: PROPER.C
  3.  * Author....: Robert DiFalco and Glenn Scott
  4.  * CIS ID....: 71610,1705
  5.  *
  6.  * This is an original work by Glenn Scott and Robert DiFalco
  7.  * and is placed in the public domain.
  8.  *
  9.  * Modification history:
  10.  * ---------------------
  11.  *
  12.  *    Rev 1.4   01 Jan 1995 03:01:00   TED
  13.  * Ted Means made a couple of minor mods to eliminate some (mostly
  14.  * benign) compiler warnings.
  15.  *
  16.  *    Rev 1.3   28 Sep 1992 00:54:58   GLENN
  17.  * Don Caton fixed the function to conform to extend system rules.
  18.  *
  19.  *    Rev 1.2   15 Aug 1991 23:08:22   GLENN
  20.  * Forest Belt proofread/edited/cleaned up doc
  21.  *
  22.  *    Rev 1.1   14 Jun 1991 19:53:50   GLENN
  23.  * Minor edit to file header
  24.  *
  25.  *    Rev 1.0   01 Apr 1991 01:02:56   GLENN
  26.  * Nanforum Toolkit
  27.  *
  28.  *
  29.  */
  30.  
  31.  
  32. /*  $DOC$
  33.  *  $FUNCNAME$
  34.  *     FT_PROPER()
  35.  *  $CATEGORY$
  36.  *     String
  37.  *  $ONELINER$
  38.  *     Convert a string to proper-name case
  39.  *  $SYNTAX$
  40.  *     FT_PROPER( <cString> ) -> cProperName
  41.  *  $ARGUMENTS$
  42.  *     <cString> is the string to be converted.
  43.  *  $RETURNS$
  44.  *     A string of the same length as <cString>, only converted to
  45.  *     proper name case (upper/lower case).
  46.  *  $DESCRIPTION$
  47.  *     FT_PROPER() uses a brute-force algorithm to convert a string
  48.  *     to propername case.  First, it capitalizes the first letter of
  49.  *     all words starting after a blank, dash, or apostrophe.  This
  50.  *     catches most names, including special cases such as names
  51.  *     beginning with O' (O'Malley, O'Reilly) and hyphenated names
  52.  *     (such as Susan Chia-Mei Lo).
  53.  *
  54.  *     Next, it does a specific adjustment for words beginning in "Mc"
  55.  *     It finds the first 'Mc' and capitalizes the next character after
  56.  *     it.  It does this for all occurrences of Mc.
  57.  *
  58.  *     The original FT_PROPER() was written in Clipper by Glenn Scott
  59.  *     and Mark Zechiel; it was re-written in C (and thus, optimized
  60.  *     and enhanced) by Robert DiFalco.
  61.  *  $EXAMPLES$
  62.  *       FUNCTION main( cStr )
  63.  *         OutStd( FT_PROPER( cStr ) + chr(13) + chr(10) )
  64.  *       RETURN ( nil )
  65.  *  $END$
  66.  */
  67.  
  68.  
  69. #include "EXTEND.API"
  70.  
  71. static int _ftIsAlpha( char );
  72. static char _ftToLower( char );
  73. static char _ftToUpper( char );
  74. static int _ftIsUpper( char );
  75. static int _ftIsLower( char );
  76.  
  77. CLIPPER FT_PROPER( void )
  78. {
  79.   int  iLen   =  _parclen(1);
  80.   char *cStr;
  81.  
  82.   int i, fCap = TRUE, iPos = 0;
  83.  
  84.   _storc( NULL, 1 );
  85.   cStr = _parc(1);
  86.  
  87.   for( i = 0; i < iLen + 1; i++ ) {
  88.      if( _ftIsAlpha( cStr[i] ) == TRUE )  {
  89.         if( fCap == TRUE )
  90.            cStr[i] = _ftToUpper( cStr[i] );
  91.         else cStr[i] = _ftToLower( cStr[i] );
  92.         }
  93.      fCap = ( cStr[i] == ' ' || cStr[i] == '-' || cStr[i] == 0x27 );
  94.   }
  95.  
  96.   // Find "Mc"
  97.   for( i = 0; i <= iLen; i++ )
  98.      if( cStr[i] == 'M' && cStr[i+1] == 'c' ) {
  99.         cStr[i+2] = _ftToUpper( cStr[i+2] );
  100.      }
  101.  
  102.   /* // If "Mc" was found, Cap next letter if Alpha
  103.   if( iPos > 1 )
  104.      if( iPos < iLen )
  105.         if( _ftIsUpper( cStr[iPos] ) == FALSE )
  106.            cStr[iPos] = _ftToUpper( cStr[iPos] );
  107.   */
  108.   _retc( cStr );
  109.   return;
  110. }
  111.  
  112.  
  113.  
  114. static int _ftIsAlpha( char c )
  115. {
  116.   return( _ftIsUpper(c) || _ftIsLower(c));
  117. }
  118.  
  119. static char _ftToLower( char c )
  120. {
  121.   return(c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c);
  122. }
  123.  
  124.  
  125. static char _ftToUpper( char c )
  126. {
  127.   return(c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c);
  128. }
  129.  
  130. static int _ftIsUpper( char c )
  131. {
  132.   return(c >= 'A' && c <= 'Z');
  133. }
  134.  
  135.  
  136. static int _ftIsLower( char c )
  137. {
  138.   return(c >= 'a' && c <= 'z');
  139. }