home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / drgnsmth.cpt / Dragonsmith 1.1 / Base files / Utilities / StringUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-10  |  2.2 KB  |  82 lines

  1. /*
  2.     StringUtils.c
  3.     
  4.     Created    24 May 1992    
  5.     Modified    25 May 1992    Fixed idiocies in AppendPStr (sheesh!)
  6.             09 Aug 1992    Added CopyPStrFromHandle
  7.             16 Aug 1992    Added PStrToULong
  8.             20 Aug 1992    Fixed CopyPStrFromHandle so it works with 'STR#' (and not 'STR ') resources
  9.             12 Sep 1992    Removed CopyPStrFromHandle ╤ Dragonsmith doesn't use it
  10.  
  11.     Copyright ⌐ 1992 by Paul M. Hoffman
  12.     Send comments or suggestions to paul.hoffman@um.cc.umich.edu
  13.     
  14.     This source code may be freely used, altered, and distributed in any way as long as:
  15.         1.    It is GIVEN away rather than sold (except as expressly permitted by the author)
  16.         2.    This statement and the above copyright notice are left intact.
  17.  
  18. */
  19.  
  20. #include    "StringUtils.h"
  21.  
  22. Boolean PStrToULong (Str255 str, long *num)
  23. {
  24.     // Return TRUE if it's a valid number, otherwise FALSE
  25.  
  26.     register unsigned char    i, c, *p;
  27.     unsigned char            len = *str;
  28.     unsigned char            max[] = "\p4294967295";
  29.     
  30.     // Check for too-long numbers
  31.     if (len > 10)
  32.         return FALSE;
  33.         
  34.     // Check for max-length, overly large numbers
  35.     else if (len == 10)
  36.         for (i = 1, p = str + 1; i <= 10; i++) {
  37.             if ((c = *p++) > max[i])
  38.                 return FALSE;
  39.             else if (c < max[i])
  40.                 break;
  41.         }
  42.                 
  43.     for (i = len, p = str + 1; i--; )
  44.         if ((c = *p++) < '0' || c > '9')
  45.             return FALSE;
  46.     StringToNum (str, num);
  47.     return TRUE;
  48. }
  49.  
  50. void SmartCopyPStr (register unsigned char *p1, register unsigned char *p2)
  51. {
  52.     if (p1 == NULL)
  53.         return;
  54.     if (p2 == NULL)                                        // If the destination pointer is NULL,
  55.         p2 = p1;                                        //    just copy the source POINTER
  56.     else if (StripAddress (p1) != StripAddress (p2))            // Otherwise, if the two pointers aren't equal,
  57.         CopyPStr (p1, p2);                                //    go ahead and call CopyPStr
  58. }
  59.  
  60. void CopyPStr (register unsigned char *p1, register unsigned char *p2)
  61. {
  62.     register short    len;
  63.     
  64.     len = (short) *p1;
  65.     do
  66.         *p2++ = *p1++;
  67.     while (len--);
  68. }
  69.  
  70. void AppendPStr (unsigned char *p1, unsigned char *p2)
  71. {
  72.     /* Append the string pointed to by p2 to the string pointed to by p1 */
  73.     register unsigned short    loop;
  74.     register unsigned char    *s1, *s2 = p2;
  75.     unsigned short        lenChange;
  76.     
  77.     lenChange = loop = (unsigned short) *s2++;
  78.     s1 = p1 + *p1 + 1;
  79.     for ( ; loop--; )
  80.         *s1++ = *s2++;
  81.     p1 [0] += lenChange;
  82. }