home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / PascalString.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  7.2 KB  |  319 lines  |  [TEXT/MPS ]

  1. #include "PascalString.h"
  2.  
  3. #ifndef __STDIO__
  4. #include <StdIo.h>
  5. #endif
  6.  
  7. #ifndef __STRING__
  8. #include <String.h>
  9. #endif
  10.  
  11. #pragma segment Main
  12.  
  13.  
  14. // Method definitions for the class String.
  15.  
  16. // Function definitions for the string insert helper functions.
  17.  
  18. void String::InsertHelper(const String& insStr,
  19.                           short pos,
  20.                           short maxLength)
  21. {
  22.     short newLength = insStr.Length() + Length();
  23.  
  24.     // Check to see if this insertion will result in a string that is longer
  25.     // than the maximum length of this string. If so then truncate characters
  26.     // from the right side of this string before doing the insertion.
  27.  
  28.     if (newLength > maxLength)
  29.     {
  30.         short truncLength = newLength - maxLength;
  31.         Delete(maxLength - truncLength + 1, truncLength);
  32.         newLength = maxLength;
  33.     }
  34.  
  35.     // Memmove can be used on overlapping strings, which is what we have here,
  36.     // as opposed to memcpy which cannot.
  37.  
  38.     memmove(&fStr[pos + insStr.Length()], &fStr[pos], Length() - pos + 1);
  39.     memmove(&fStr[pos], &insStr.fStr[1], insStr.Length());
  40.     Length() = (unsigned char)newLength;
  41. }
  42.  
  43.  
  44. void String::InsertHelper(const char* insStr,
  45.                           short pos,
  46.                           short maxLength)
  47. {
  48. #if qDebugMsg
  49.     if (pos > Length())
  50.         fprintf(stderr, "###String::InsertHelper: Insert position greater than length of string.\n");
  51. #endif
  52.  
  53.     short insLength = insStr == NULL ? 0 : strlen(insStr);
  54.     short newLength = insLength + Length();
  55.  
  56.     // Check to see if this insertion will result in a string that is longer
  57.     // than the maximum length of this string. If so then truncate characters
  58.     // from the right side of this string before doing the insertion.
  59.  
  60.     if (newLength > maxLength)
  61.     {
  62. #if qDebugMsg
  63.         fprintf(stderr, "### String::InsertHelper: String truncated during insert call.\n");
  64. #endif
  65.         short truncLength = newLength - maxLength;
  66.         Delete(maxLength - truncLength + 1, truncLength);
  67.         newLength = maxLength;
  68.     }
  69.  
  70.     // Memmove can be used on overlapping strings, which is what we have here,
  71.     // as opposed to memcpy which cannot.
  72.  
  73.     memmove(&fStr[pos + insLength], &fStr[pos], Length() - pos + 1);
  74.     memmove(&fStr[pos], insStr, insLength);
  75.     Length() = (unsigned char)newLength;
  76. }
  77.  
  78. unsigned char& String::operator[](short pos)
  79. // !!! we'd ideally like this method to be inlined but CFront doesn't seem to 
  80. // want to inline it for us !!!
  81. {
  82.     return fStr[pos];
  83. }                                            // for non-const String
  84.  
  85. String::operator char*() const
  86. {
  87.     // Note: This method cannot be used in a multi-thread environment, since the following static
  88.     // storage is used everytime this method is called. For now this seams robust enough, since
  89.     // it is mainly used to present debugging information.
  90.     
  91.     static char cString[kStr255Len+1];
  92.     
  93.     strncpy(cString, (char *) &fStr[1], Length());
  94.     cString[Length()] = '\0';
  95.     
  96.     return cString;
  97. }
  98.  
  99. // Method definitions for the class Str255
  100.  
  101. Str255::Str255(const char* str)
  102. {
  103.     // Truncate the C String to 255 bytes if necessary.
  104.  
  105.     Length() = str == NULL ? 0 : strlen((const char*)str);
  106.     
  107.     if (Length() > kStr255Len)
  108.         Length() = kStr255Len;
  109.     memcpy(&fStr[1], str, Length());
  110. }
  111.  
  112.  
  113. Str255 Str255::Copy(short pos, short length)
  114. {
  115.     Str255 newString;
  116.     
  117.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  118.     memcpy(&newString.fStr[1], &fStr[pos], length);
  119.     newString.Length() = length;
  120.     return newString;
  121. }
  122.  
  123. Str255 operator+(const String& s1,
  124.                  const char* s2)
  125. {
  126.     Str255 newStr;
  127.  
  128.     memcpy(newStr.fStr, s1.fStr, s1.Length() + kLengthByte);
  129.     newStr.Length() += s2 == NULL ? 0 : strlen((const char*)s2);
  130.     if (newStr.Length() > kStr255Len)
  131.         newStr.Length() = kStr255Len;
  132.     memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2, newStr.Length() - s1.Length());
  133.  
  134.     return newStr;
  135. }
  136.  
  137.  
  138. Str255 operator+(const char* s1,
  139.                  const String& s2)
  140. {
  141.     Str255 newStr;
  142.     short cLen = s2 == NULL ? 0 : strlen((const char*)s1);
  143.  
  144.     memcpy(&newStr[1], s1, cLen);
  145.     newStr.Length() = s2.Length() + cLen;
  146.     if (newStr.Length() > kStr255Len)
  147.         newStr.Length() = kStr255Len;
  148.     memcpy(&newStr[cLen + kLengthByte], s2.fStr + 1, newStr.Length() - cLen);
  149.  
  150.     return newStr;
  151. }
  152.  
  153.  
  154. Str255 operator+(const String& s1,
  155.                  const String& s2)
  156. {
  157.     Str255 newStr;
  158.  
  159.     memcpy(newStr.fStr, s1.fStr, s1.Length() + kLengthByte);
  160.     newStr.Length() += s2.Length();
  161.     if (newStr.Length() > kStr255Len)
  162.         newStr.Length() = kStr255Len;
  163.     memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2.fStr + 1, newStr.Length() - s1.Length());
  164.  
  165.     return newStr;
  166. }
  167.  
  168.  
  169. // Method definitions for the concatenation operator += (Str255).
  170.  
  171. Str255& Str255::operator += (const String& str)
  172. {
  173.     InsertHelper (str, Length() + 1, kStr255Len);
  174.     return *this;
  175. }
  176.  
  177. Str255& Str255::operator += (const char* str)
  178. {
  179.     InsertHelper (str, Length() + 1, kStr255Len);
  180.     return *this;
  181. }
  182.  
  183. Str255& Str255::operator += (const char ch)
  184. {
  185.     if (++Length() <= kStr255Len)
  186.         fStr[Length()] = ch;
  187.     else
  188.     {
  189.         --Length();
  190. #if qDebugMsg
  191.         fprintf(stderr, "###Str255::operator+=: Concatenation produces str255 overflow.\n");
  192. #endif
  193.     }
  194.     
  195.     return *this;
  196. }
  197.  
  198.  
  199. // Method definitions for Str63.
  200.  
  201. Str63::Str63(const char* str)
  202. {
  203.     // Truncate the C String to 63 bytes if necessary.
  204.  
  205.     Length() = str == NULL ? 0 : strlen((const char*)str);
  206.     if (Length() > kStr63Len)
  207.         Length() = kStr63Len;
  208.     memcpy(&fStr[1], str, Length());
  209. }
  210.  
  211. Str63 Str63::Copy(short pos, short length)
  212. {
  213.     Str63 newString;
  214.     
  215.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  216.     memcpy(&newString.fStr[1], &fStr[pos], length);
  217.     newString.Length() = length;
  218.     return newString;
  219. }
  220.  
  221. Str63& Str63::operator += (const String& str)
  222. {
  223.     InsertHelper (str, Length() + 1, kStr63Len);
  224.     return *this;
  225. }
  226.  
  227. Str63& Str63::operator += (const char* str)
  228. {
  229.     InsertHelper (str, Length() + 1, kStr63Len);
  230.     return *this;
  231. }
  232.  
  233. Str63& Str63::operator += (const char ch)
  234. {
  235.     if (++Length() <= kStr63Len)
  236.         fStr[Length()] = ch;
  237.     else
  238.     {
  239.         --Length();
  240. #if qDebugMsg
  241.         fprintf(stderr, "###Str63::operator+=: Concatenation produces str63 overflow.\n");
  242. #endif
  243.     }
  244.     
  245.     return *this;
  246. }
  247.  
  248.  
  249. // Method definitions for Str32.
  250.  
  251. Str32::Str32(const char* str)
  252. {
  253.     // Truncate the C String to 32 bytes if necessary.
  254.  
  255.     Length() = str == NULL ? 0 : strlen((const char*)str);
  256.     if (Length() > kStr32Len)
  257.         Length() = kStr32Len;
  258.     memcpy(&fStr[1], str, Length());
  259. }
  260.  
  261. Str32 Str32::Copy(short pos, short length)
  262. {
  263.     Str32 newString;
  264.     
  265.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  266.     memcpy(&newString.fStr[1], &fStr[pos], length);
  267.     newString.Length() = length;
  268.     return newString;
  269. }
  270.  
  271. // Method definitions for Str31.
  272.  
  273. Str31::Str31(const char* str)
  274. {
  275.     // Truncate the C String to 31 bytes if necessary.
  276.  
  277.     Length() = str == NULL ? 0 : strlen((const char*)str);
  278.     if (Length() > kStr31Len)
  279.         Length() = kStr31Len;
  280.     memcpy(&fStr[1], str, Length());
  281. }
  282.  
  283. Str31 Str31::Copy(short pos, short length)
  284. {
  285.     Str31 newString;
  286.     
  287.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  288.     memcpy(&newString.fStr[1], &fStr[pos], length);
  289.     newString.Length() = length;
  290.     return newString;
  291. }
  292.  
  293. Str31& Str31::operator += (const String& str)
  294. {
  295.     InsertHelper (str, Length() + 1, kStr31Len);
  296.     return *this;
  297. }
  298.  
  299. Str31& Str31::operator += (const char* str)
  300. {
  301.     InsertHelper (str, Length() + 1, kStr31Len);
  302.     return *this;
  303. }
  304.  
  305. Str31& Str31::operator += (const char ch)
  306. {
  307.     if (++Length() <= kStr31Len)
  308.         fStr[Length()] = ch;
  309.     else
  310.     {
  311.         --Length();
  312. #if qDebugMsg
  313.         fprintf(stderr,"###Str31::operator+=: Concatenation produces str31 overflow.\n");
  314. #endif
  315.     }
  316.     
  317.     return *this;
  318. }
  319.