home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / grantscgi / Util / StringUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-20  |  5.8 KB  |  285 lines

  1. /*****
  2.  *
  3.  *    StringUtil.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/cgi/framework/
  12.  *
  13.  *****/
  14.  
  15. #include <string.h>
  16.  
  17. #include "MyConfiguration.h"
  18.  
  19. #include "DebugUtil.h"
  20.  
  21. #include "StringUtil.h"
  22.  
  23.  
  24. /***  FUNCTIONS  ***/
  25.  
  26. /* Copy pascal format srcStr to the destStr */
  27. void
  28. StringPascalCopy ( char *srcStr, char *destStr )
  29. {
  30.     BlockMove ( srcStr, destStr, (srcStr[nil]) + 1 );
  31. } /* StringPascalCopy */
  32.  
  33.  
  34. /* Case-insensitive comparison of two strings.
  35.     Returns:
  36.         nil if no difference between the strings
  37.         <0 if str1 < str2
  38.         >0 if str1 > str2 */
  39. p_export
  40. short
  41. StringCompareNoCase ( char *str1, char *str2 )
  42. {
  43.     char    char1;
  44.     char    char2;
  45.     
  46.     my_assert ( str1 != NULL, "\pStringCompareNoCase: str1 is NULL" );
  47.     my_assert ( str2 != NULL, "\pStringCompareNoCase: str2 is NULL" );
  48.     
  49.     do
  50.     {
  51.         char1 = CharMakeUpper ( *str1 );
  52.         char2 = CharMakeUpper ( *str2 );
  53.         
  54.         if ( char1 < char2 )
  55.         {
  56.             return -1;
  57.         }
  58.         else if ( char1 > char2 )
  59.         {
  60.             return 1;
  61.         }
  62.         else
  63.         {
  64.             str1++;
  65.             str2++;
  66.         }
  67.     } while ( (*str1 != nil) && (*str2 != nil) );
  68.     
  69.     if ( *str1 != nil )
  70.     {
  71.         /* str1 still has chars, so is greater than str2 */
  72.         return 1;
  73.     }
  74.     else if ( *str1 != nil )
  75.     {
  76.         /* str2 still has chars, so is greater than str1 */
  77.         return -1;
  78.     }
  79.     else
  80.     {
  81.         /* all the characters in both strings matched */
  82.         return 0;
  83.     }
  84. } /* StringCompareNoCase */
  85.  
  86.  
  87. /* Case-insensitive comparison of two strings.
  88.     Returns:
  89.         nil if no difference between the strings
  90.         <0 if str1 < str2
  91.         >0 if str1 > str2 */
  92. p_export
  93. short
  94. StringNCompareNoCase ( char *str1, char *str2, UInt16 theSize )
  95. {
  96.     char    char1;
  97.     char    char2;
  98.     UInt16    count;
  99.     
  100.     my_assert ( str1 != NULL, "\pStringNCompareNoCase: str1 is NULL" );
  101.     my_assert ( str2 != NULL, "\pStringNCompareNoCase: str2 is NULL" );
  102.     my_assert ( theSize != nil, "\pStringNCompareNoCase: theSize is zero" );
  103.     
  104.     count = nil;
  105.     
  106.     do
  107.     {
  108.         char1 = CharMakeUpper ( *str1 );
  109.         char2 = CharMakeUpper ( *str2 );
  110.         
  111.         if ( char1 < char2 )
  112.         {
  113.             return -1;
  114.         }
  115.         else if ( char1 > char2 )
  116.         {
  117.             return 1;
  118.         }
  119.         else
  120.         {
  121.             str1++;
  122.             str2++;
  123.         }
  124.         
  125.         count++;
  126.     } while ( (*str1 != nil) && (*str2 != nil) && (count < theSize) );
  127.     
  128.     /* all the characters in both strings, up to theSize, matched */
  129.     return 0;
  130. } /* StringNCompareNoCase */
  131.  
  132.  
  133. /* convert the given character to uppercase.
  134.     just return theChar if it is not between a and z */
  135. char
  136. CharMakeUpper ( char theChar )
  137. {
  138.     if ( (theChar >= 'a') && (theChar <= 'z') )
  139.     {
  140.         return theChar + ('A' - 'a');
  141.     }
  142.     else
  143.     {
  144.         return theChar;
  145.     }
  146. } /* CharMakeUpper */
  147.  
  148.  
  149. /* count the occurances of theChar in theString */
  150. long
  151. StringCountChar ( char *theString, char theChar )
  152. {
  153.     long    total;
  154.     char *    tempStr;
  155.     
  156.     total    = nil;
  157.     tempStr    = theString;
  158.     
  159.     do
  160.     {
  161.         /* get a ptr to the next instance of theChar */
  162.         tempStr = StringChar ( tempStr, theChar );
  163.         
  164.         if ( (tempStr != NULL) && (*tempStr != nil) )
  165.         {
  166.             tempStr++;
  167.             total++;
  168.         }
  169.     } while ( (tempStr != NULL) && (*tempStr != nil) );
  170.     
  171.     return total;
  172. } /* StringCountChar */
  173.  
  174.  
  175. /* Returns true if any chars were converted */
  176. Boolean
  177. StringConvertCharToChar ( char *ioString, char fromChar, char toChar )
  178. {
  179.     Boolean    didConvert;
  180.     
  181.     didConvert = false;
  182.     
  183.     while ( ioString[0] != nil )
  184.     {
  185.         if ( ioString[0] == fromChar )
  186.         {
  187.             ioString[0] = toChar;
  188.             didConvert  = true;
  189.         }
  190.         ioString++;
  191.     }
  192.     
  193.     return didConvert;
  194. } /* StringConvertCharToChar */
  195.  
  196.  
  197. /* working version of strchr for TPM */
  198. #if kCompiling_For_Symantec
  199. char *
  200. StringChar ( const char *theString, unsigned char theChar )
  201. {
  202.     do
  203.     {
  204.         if ( *theString == theChar )
  205.         {
  206.             /* found the character, return a pointer to it */
  207.             return (char *)theString;
  208.         }
  209.         theString++;
  210.     } while ( *theString != nil );
  211.     
  212.     /* didn't find the character */
  213.     return NULL;
  214. } /* StringChar */
  215. #endif
  216.  
  217.  
  218. /**  TEXT DRAWING FUNCTIONS  **/
  219.  
  220. /* Font, size, style, etc. must be set before this function is called. */
  221. void
  222. StringDrawInRect ( StringPtr theString, Rect theRect )
  223. {
  224.     if ( theString[nil] > nil )
  225.     {
  226.         StringDrawTextInRect ( (char *)(theString + 1), theString[nil], theRect );
  227.     }
  228. } /* StringDrawInRect */
  229.  
  230.  
  231. /* returns the number of characters not drawn because of insufficient space.
  232.     zero if every character drawn */
  233. long
  234. StringDrawTextInRect ( char *theText, long totalLength, Rect theRect )
  235. {
  236.     char *        currentStartOfText;
  237.     long        charsLeftInText;
  238.     long        currentNumCharsToPrint;
  239.     FontInfo    fInfo;
  240.     short        textLineHeight;
  241.     short        currentVPosition;
  242.     short        hPosition;
  243.     StyledLineBreakCode        theBreakCode;
  244.     Fixed        rectWidth;
  245.     Fixed        textWidth;
  246.     
  247.     /* initialize text values */
  248.     currentStartOfText        = theText;
  249.     charsLeftInText            = totalLength;
  250.     currentNumCharsToPrint    = charsLeftInText;
  251.     
  252.     GetFontInfo ( &fInfo );
  253.     
  254.     /* initialize spacing */
  255.     textLineHeight        = fInfo.ascent + fInfo.descent + fInfo.leading;
  256.     currentVPosition    = theRect.top + fInfo.ascent;
  257.     hPosition            = theRect.left;
  258.     rectWidth            = Long2Fix ( theRect.right - theRect.left );
  259.     
  260.     /* clear the rectangle where the text is to be displayed */
  261.     EraseRect ( &theRect );
  262.     
  263.     /* while there are still characters in the text that haven't been drawn
  264.         and there is still room in the Rect for drawing more characters */
  265.     while ( (charsLeftInText > nil) && (currentVPosition < theRect.bottom) )
  266.     {
  267.         textWidth = rectWidth;
  268.         
  269.         theBreakCode = StyledLineBreak ( currentStartOfText, charsLeftInText, nil,
  270.             charsLeftInText, nil, &textWidth, ¤tNumCharsToPrint);
  271.         
  272.         MoveTo   ( hPosition, currentVPosition );
  273.         DrawText ( currentStartOfText, nil, currentNumCharsToPrint );
  274.         
  275.         currentVPosition   += textLineHeight;
  276.         charsLeftInText    -= currentNumCharsToPrint;
  277.         currentStartOfText += currentNumCharsToPrint;
  278.     } /* end of while */
  279.     
  280.     return charsLeftInText;
  281. } /* StringDrawTextInRect */
  282.  
  283.  
  284. /***  EOF  ***/
  285.