home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / PROPER.C < prev    next >
C/C++ Source or Header  |  1992-09-28  |  3KB  |  133 lines

  1. /*
  2.  * File......: PROPER.C
  3.  * Author....: Robert DiFalco and Glenn Scott
  4.  * Date......: $Date:   28 Sep 1992 00:54:58  $
  5.  * Revision..: $Revision:   1.3  $
  6.  * Log file..: $Logfile:   C:/nanfor/src/proper.c_v  $
  7.  * 
  8.  * This is an original work by Glenn Scott and Robert DiFalco
  9.  * and is placed in the public domain.
  10.  *
  11.  * Modification history:
  12.  * ---------------------
  13.  *
  14.  * $Log:   C:/nanfor/src/proper.c_v  $
  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.h"
  70.  
  71.  
  72. CLIPPER FT_PROPER()
  73. {
  74.   int  iLen   =  _parclen(1);
  75.   char *cStr;
  76.  
  77.   int i, fCap = TRUE, iPos = 0;
  78.  
  79.   _storc( NULL, 1 );
  80.   cStr = _parc(1);
  81.  
  82.   for( i = 0; i < iLen + 1; i++ ) {
  83.      if( _ftIsAlpha( cStr[i] ) == TRUE )  {
  84.         if( fCap == TRUE )
  85.            cStr[i] = _ftToUpper( cStr[i] );
  86.         else cStr[i] = _ftToLower( cStr[i] );
  87.         }
  88.      fCap = ( cStr[i] == ' ' || cStr[i] == '-' || cStr[i] == 0x27 );
  89.   }
  90.  
  91.   // Find "Mc"
  92.   for( i = 0; i <= iLen; i++ )
  93.      if( cStr[i] == 'M' && cStr[i+1] == 'c' ) {
  94.         cStr[i+2] = _ftToUpper( cStr[i+2] );
  95.      }
  96.  
  97.   /* // If "Mc" was found, Cap next letter if Alpha
  98.   if( iPos > 1 )
  99.      if( iPos < iLen )
  100.         if( _ftIsUpper( cStr[iPos] ) == FALSE )
  101.            cStr[iPos] = _ftToUpper( cStr[iPos] );
  102.   */   
  103.   _retc( cStr );
  104.   return;
  105. }
  106.  
  107. static int _ftIsAlpha( char c )
  108. {
  109.   return( _ftIsUpper(c) || _ftIsLower(c));
  110. }
  111.  
  112. static int _ftToLower( char c )
  113. {
  114.   return(c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c);
  115. }
  116.  
  117.  
  118. static int _ftToUpper( char c )
  119. {
  120.   return(c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c);
  121. }
  122.  
  123. static int _ftIsUpper( char c )
  124. {
  125.   return(c >= 'A' && c <= 'Z');
  126. }
  127.  
  128.  
  129. static int _ftIsLower( char c )
  130. {
  131.   return(c >= 'a' && c <= 'z');
  132. }
  133.