home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / central / PascalString.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  20.9 KB  |  686 lines

  1. //----------------------------------------------------------------------------------------
  2. // PascalString.cp 
  3. // Copyright ⌐ 1985-1993 by Apple Computer, Inc.  All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #include "PascalString.h"
  7. // #include "xpassert.h"
  8.  
  9. #ifndef __STDIO__
  10. #include <stdio.h>
  11. #endif
  12.  
  13. #ifndef __STRING__
  14. #include <string.h>
  15. #endif
  16.  
  17. #pragma segment Main
  18.  
  19. //========================================================================================
  20. // CLASS CString
  21. //========================================================================================
  22.  
  23.  
  24. //----------------------------------------------------------------------------------------
  25. // CString::InsertHelper(CString):
  26. //----------------------------------------------------------------------------------------
  27.  
  28. void CString::InsertHelper(const CString& insStr,
  29.                           short pos,
  30.                           short maxLength)
  31. {
  32.     if (pos > Length() + 1)
  33.     {
  34. #if qDebugMsg
  35.         fprintf(stderr, "###CString::InsertHelper: Insert position greater than length of CString.\n");
  36. #endif
  37.         if (Length() < maxLength)    
  38.             pos = Length() + 1;
  39.     }
  40.     
  41. #if qDebugMsg
  42.     if (Length() + insStr.Length() > maxLength)
  43.         fprintf(stderr, "### CString::InsertHelper: CString truncated during insert call.\n");    
  44. #endif
  45.  
  46.     short usableLengthOfInsertString;
  47.     short endPosOfInsertString;
  48.     short usableLengthOfShiftedString;
  49.     
  50.     if (pos + insStr.Length() > maxLength)
  51.         usableLengthOfInsertString = maxLength - pos + 1;
  52.     else
  53.         usableLengthOfInsertString = insStr.Length();
  54.     endPosOfInsertString = pos + usableLengthOfInsertString - 1;
  55.     
  56.     if ((endPosOfInsertString + 1) + (Length() - pos + 1) > maxLength)
  57.         usableLengthOfShiftedString = maxLength - endPosOfInsertString;
  58.     else
  59.         usableLengthOfShiftedString = Length() - pos + 1;
  60.         
  61.     memmove(&fStr[endPosOfInsertString + 1], &fStr[pos], usableLengthOfShiftedString);
  62.     memmove(&fStr[pos], &insStr.fStr[1], usableLengthOfInsertString);
  63.     Length() = usableLengthOfShiftedString + endPosOfInsertString;
  64. } // CString::InsertHelper(CString)
  65.  
  66.  
  67. //----------------------------------------------------------------------------------------
  68. // CString::InsertHelper(char*):
  69. //----------------------------------------------------------------------------------------
  70.  
  71. void CString::InsertHelper(const char* insStr,
  72.                           short pos,
  73.                           short maxLength)
  74. {
  75.     this->InsertHelper(CStr255(insStr), pos, maxLength);
  76. } // CString::InsertHelper(char*)
  77.  
  78.  
  79.  
  80. //----------------------------------------------------------------------------------------
  81. // CString::operator char*:
  82. //----------------------------------------------------------------------------------------
  83.  
  84. CString::operator char*() const
  85. {
  86.     static short currentCString = 0;
  87.     static char cStrings[kTempCStrings][kStr255Len+1];
  88.     
  89.     currentCString = (currentCString + 1) % kTempCStrings;
  90.     
  91.     strncpy(cStrings[currentCString], (char *) &fStr[1], Length());
  92.     cStrings[currentCString][Length()] = '\0';
  93.     
  94.     return cStrings[currentCString];
  95. } // CString::operator char*
  96.  
  97.  
  98. //----------------------------------------------------------------------------------------
  99. // CString::operator long:
  100. //----------------------------------------------------------------------------------------
  101.  
  102. CString::operator long() const
  103. {
  104.     // The following statement looks like it should work. Right?
  105.     //
  106.     //    return *((long *) &fStr[1]);
  107.     //
  108.     // Wrong, the C compiler generates a MOVE.L starting on a odd byte boundary for the
  109.     // preceding statement. This is illegal on the 68000. But its _NOT_ a bug, because
  110.     // according to the ANSI C reference manual, "A pointer to one type may be converted
  111.     // to a pointer to another type. The resulting pointer may cause an addressing
  112.     // exception if the subject pointer does not refer to an object suitably aligned in
  113.     // storage".
  114.     
  115.     long returnLong;
  116.     
  117.     memcpy(&returnLong, &fStr[1], sizeof(long));
  118.     return returnLong;
  119. } // CString::operator long
  120.  
  121.  
  122. //----------------------------------------------------------------------------------------
  123. // CString::Pos(char*):
  124. //----------------------------------------------------------------------------------------
  125.  
  126. unsigned char CString::Pos(const char* subStr, unsigned char startPos)
  127. {
  128.     char cStr[kStr255Len + 1];
  129.     char* ptr;
  130.     
  131.     memcpy(cStr, &fStr[1], (size_t)Length());
  132.     cStr[Length()] = 0;
  133.     ptr = strstr(&cStr[startPos - 1], subStr);
  134.     return ptr != NULL ? (ptr - cStr) + 1 : 0;
  135. } // CString::Pos(char*)
  136.  
  137.  
  138. //----------------------------------------------------------------------------------------
  139. // CString::Pos(CString):
  140. //----------------------------------------------------------------------------------------
  141.  
  142. unsigned char CString::Pos(const CString& subStr, unsigned char startPos)
  143. {
  144.     char cStr[kStr255Len + 1];
  145.  
  146.     memcpy(cStr, &subStr.fStr[1], (size_t)(subStr.Length()));
  147.     cStr[subStr.Length()] = 0;
  148.     return this->Pos(cStr, startPos);
  149. } // CString::Pos(CString)
  150.  
  151.  
  152. //----------------------------------------------------------------------------------------
  153. // CString::operator const unsigned char*
  154. //----------------------------------------------------------------------------------------
  155. CString::operator const unsigned char*() const
  156. {
  157.     return (const unsigned char *) this;
  158. } // CString::operator const unsigned char*
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // CString::Delete
  162. //----------------------------------------------------------------------------------------
  163. void CString::Delete(short pos, short length)
  164. {
  165.     if ((pos > 0) && (length > 0) && (pos <= Length()))    // should also check that pos <= kMaxLength
  166.     {
  167.         if (pos + length > Length())
  168.             fStr[0] = pos - 1;
  169.         else
  170.         {
  171.             ::memcpy(&fStr[pos], &fStr[pos + length], Length() - (pos + length) + kLengthByte);
  172.             fStr[0] -= length;
  173.         }
  174.     }
  175. } // CString::Delete
  176.  
  177. //========================================================================================
  178. // CLASS CStr255
  179. //========================================================================================
  180.  
  181. const CStr255 CStr255::sEmptyString("\p");
  182.  
  183. //----------------------------------------------------------------------------------------
  184. // CStr255::CStr255(char*):
  185. //----------------------------------------------------------------------------------------
  186.  
  187. CStr255::CStr255(const char* str)
  188. {
  189.     // Truncate the C CString to 255 bytes if necessary.
  190.     size_t len = str == NULL ? 0 : strlen(str);
  191.     Length() = len > kStr255Len ?  kStr255Len : len;
  192.     
  193.     if (Length() > kStr255Len)
  194.         Length() = kStr255Len;
  195.     memcpy(&fStr[1], str, (size_t)Length());
  196. } // CStr255::CStr255(char*)
  197.  
  198.  
  199. //----------------------------------------------------------------------------------------
  200. // CStr255::CStr255(long): Useful for converting OSType's into CStr255's.
  201. //----------------------------------------------------------------------------------------
  202.  
  203. CStr255::CStr255(const long id)
  204. {
  205.     Length() = 4;
  206.     memcpy(&fStr[1], &id, (size_t)Length());
  207. } // CStr255::CStr255(long)
  208.  
  209.  
  210. //----------------------------------------------------------------------------------------
  211. // CStr255::Copy:
  212. //----------------------------------------------------------------------------------------
  213.  
  214. CStr255 CStr255::Copy(short pos, short length)
  215. {
  216.     CStr255 newString;
  217.     
  218.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  219.     
  220.     if (length > 0)
  221.     {
  222.         memcpy(&newString.fStr[1], &fStr[pos], (size_t)length);
  223.         newString.Length() = length;
  224.     }
  225.     else
  226.         newString = "";
  227.         
  228.     return newString;
  229.     
  230. } // CStr255::Copy
  231.  
  232.  
  233. //----------------------------------------------------------------------------------------
  234. // CStr255::operator+:
  235. //----------------------------------------------------------------------------------------
  236.  
  237. CStr255 operator+(const CString& s1,
  238.                   const char* s2)
  239. {
  240.     CStr255 newStr;
  241.     short s2Len = s2 == NULL ? 0 : (short)(strlen((const char *) s2));
  242.  
  243.     if (s1.Length() + s2Len > kStr255Len)
  244.         newStr.Length() = kStr255Len;
  245.     else
  246.         newStr.Length() = s1.Length() + s2Len;
  247.         
  248.     memcpy(&newStr.fStr[1], &s1.fStr[1], (size_t)(s1.Length()));
  249.     memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2, newStr.Length() - s1.Length());
  250.  
  251.     return newStr;
  252. } // CStr255::operator+
  253.  
  254.  
  255. //----------------------------------------------------------------------------------------
  256. // CStr255::operator+(char*,CString):
  257. //----------------------------------------------------------------------------------------
  258.  
  259. CStr255 operator+(const char* s1,
  260.                   const CString& s2)
  261. {
  262.     CStr255 newStr;
  263.     short s1Len = s1 == NULL ? 0 : (short)(strlen(s1));
  264.  
  265.     if (s1Len + s2.Length() > kStr255Len)
  266.         newStr.Length() = kStr255Len;
  267.     else
  268.         newStr.Length() = s1Len + s2.Length();
  269.         
  270.     memcpy(&newStr.fStr[1], s1, (size_t)s1Len);
  271.     memcpy(&newStr.fStr[s1Len + kLengthByte], s2.fStr + 1, newStr.Length() - s1Len);
  272.  
  273.     return newStr;
  274. } // CStr255::operator+(char*,CString)
  275.  
  276.  
  277. //----------------------------------------------------------------------------------------
  278. // CStr255::operator+(CString,CString):
  279. //----------------------------------------------------------------------------------------
  280.  
  281. CStr255 operator+(const CString& s1,
  282.                   const CString& s2)
  283. {
  284.     CStr255 newStr;
  285.  
  286.     if (s1.Length() + s2.Length() > kStr255Len)
  287.         newStr.Length() = kStr255Len;
  288.     else
  289.         newStr.Length() = s1.Length() + s2.Length();
  290.         
  291.     memcpy(&newStr.fStr[1], &s1.fStr[1], (size_t)(s1.Length()));
  292.     memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2.fStr + 1, newStr.Length() - s1.Length());
  293.  
  294.     return newStr;
  295. } // CStr255::operator+(CString,CString)
  296.  
  297.  
  298. //----------------------------------------------------------------------------------------
  299. // CStr255::operator +=(CString):  Concatenate a string
  300. //----------------------------------------------------------------------------------------
  301.  
  302. CStr255& CStr255::operator += (const CString& str)
  303. {
  304.     InsertHelper (str, Length() + 1, kStr255Len);
  305.     return *this;
  306. } // CStr255::operator +=(CString)
  307.  
  308.  
  309. //----------------------------------------------------------------------------------------
  310. // CStr255::operator +=(char*):  Concatenate a string
  311. //----------------------------------------------------------------------------------------
  312.  
  313. CStr255& CStr255::operator += (const char* str)
  314. {
  315.     InsertHelper (str, Length() + 1, kStr255Len);
  316.     return *this;
  317. } // CStr255::operator +=(char*)
  318.  
  319.  
  320. //----------------------------------------------------------------------------------------
  321. // CStr255::operator +=(char):  Concatenate a single character
  322. //----------------------------------------------------------------------------------------
  323.  
  324. CStr255& CStr255::operator += (const char ch)
  325. {
  326.     if (++Length() <= kStr255Len)
  327.         fStr[Length()] = ch;
  328.     else
  329.     {
  330.         --Length();
  331. #if qDebugMsg
  332.         fprintf(stderr, "###CStr255::operator+=: Concatenation produces CStr255 overflow.\n");
  333. #endif
  334.     }
  335.     
  336.     return *this;
  337. } // CStr255::operator +=(char)
  338.  
  339.  
  340. //----------------------------------------------------------------------------------------
  341. // CStr255::operator =:
  342. //----------------------------------------------------------------------------------------
  343.  
  344. CStr255& CStr255::operator = (const char* str)
  345. {
  346.     if (str)
  347.     {
  348.         // Truncate the C CString to 255 bytes if necessary.
  349.         register size_t itsSize = strlen(str);
  350.         if (itsSize > kStr255Len)
  351.             Length() = kStr255Len;
  352.         else
  353.             Length() = (unsigned char)(itsSize);
  354.  
  355.         memcpy(&fStr[1], str, (size_t)Length());
  356.     }
  357.     else
  358.         Length() = 0;
  359.     
  360.     return *this;
  361. } // CStr255::operator =
  362.  
  363.  
  364.  
  365. //========================================================================================
  366. // CLASS CStr63
  367. //========================================================================================
  368.  
  369.  
  370. //----------------------------------------------------------------------------------------
  371. // CStr63::CStr63(char*):
  372. //----------------------------------------------------------------------------------------
  373.  
  374. CStr63::CStr63(const char* str)
  375. {
  376.     // Truncate the C CString to 63 bytes if necessary.
  377.  
  378.     Length() = str == NULL ? 0 : (unsigned char)(strlen(str));
  379.     if (Length() > kStr63Len)
  380.         Length() = kStr63Len;
  381.     memcpy(&fStr[1], str, (size_t)Length());
  382. } // CStr63::CStr63(char*)
  383.  
  384.  
  385. //----------------------------------------------------------------------------------------
  386. // CStr63::CStr63(long):
  387. //----------------------------------------------------------------------------------------
  388.  
  389. CStr63::CStr63(const long id)
  390. {
  391.     Length() = 4;
  392.     memcpy(&fStr[1], &id, (size_t)Length());
  393. } // CStr63::CStr63(long)
  394.  
  395.  
  396. //----------------------------------------------------------------------------------------
  397. // CStr63::Copy:
  398. //----------------------------------------------------------------------------------------
  399.  
  400. CStr63 CStr63::Copy(short pos, short length)
  401. {
  402.     CStr63 newString;
  403.     
  404.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  405.     
  406.     if (length > 0)
  407.     {
  408.         memcpy(&newString.fStr[1], &fStr[pos], (size_t)length);
  409.         newString.Length() = length;
  410.     }
  411.     else
  412.         newString = "";
  413.         
  414.     return newString;
  415.     
  416. } // CStr63::Copy
  417.  
  418.  
  419. //----------------------------------------------------------------------------------------
  420. // CStr63::operator +=(CString):  Concatenate a string
  421. //----------------------------------------------------------------------------------------
  422.  
  423. CStr63& CStr63::operator += (const CString& str)
  424. {
  425.     InsertHelper (str, Length() + 1, kStr63Len);
  426.     return *this;
  427. } // CStr63::operator +=(CString)
  428.  
  429.  
  430. //----------------------------------------------------------------------------------------
  431. // CStr63::operator +=(char*):  Concatenate a string
  432. //----------------------------------------------------------------------------------------
  433.  
  434. CStr63& CStr63::operator += (const char* str)
  435. {
  436.     InsertHelper (str, Length() + 1, kStr63Len);
  437.     return *this;
  438. } // CStr63::operator +=(char*)
  439.  
  440.  
  441. //----------------------------------------------------------------------------------------
  442. // CStr63::operator +=(char):  Concatenate a single character
  443. //----------------------------------------------------------------------------------------
  444.  
  445. CStr63& CStr63::operator += (const char ch)
  446. {
  447.     if (++Length() <= kStr63Len)
  448.         fStr[Length()] = ch;
  449.     else
  450.     {
  451.         --Length();
  452. #if qDebugMsg
  453.         fprintf(stderr, "###CStr63::operator+=: Concatenation produces CStr63 overflow.\n");
  454. #endif
  455.     }
  456.     
  457.     return *this;
  458. } // CStr63::operator +=(char)
  459.  
  460.  
  461.  
  462. //========================================================================================
  463. // CLASS CStr32
  464. //========================================================================================
  465.  
  466.  
  467. //----------------------------------------------------------------------------------------
  468. // CStr32::CStr32(char*):
  469. //----------------------------------------------------------------------------------------
  470.  
  471. CStr32::CStr32(const char* str)
  472. {
  473.     // Truncate the C CString to 32 bytes if necessary.
  474.  
  475.     Length() = str == NULL ? 0 : (unsigned char)(strlen(str));
  476.     if (Length() > kStr32Len)
  477.         Length() = kStr32Len;
  478.     memcpy(&fStr[1], str, (size_t)Length());
  479. } // CStr32::CStr32(char*)
  480.  
  481.  
  482. //----------------------------------------------------------------------------------------
  483. // CStr32::CStr32(long):
  484. //----------------------------------------------------------------------------------------
  485.  
  486. CStr32::CStr32(const long id)
  487. {
  488.     Length() = 4;
  489.     memcpy(&fStr[1], &id, (size_t)Length());
  490. } // CStr32::CStr32(long)
  491.  
  492.  
  493. //----------------------------------------------------------------------------------------
  494. // CStr32::Copy:
  495. //----------------------------------------------------------------------------------------
  496.  
  497. CStr32 CStr32::Copy(short pos, short length)
  498. {
  499.     CStr32 newString;
  500.     
  501.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  502.     
  503.     if (length > 0)
  504.     {
  505.         memcpy(&newString.fStr[1], &fStr[pos], (size_t)length);
  506.         newString.Length() = length;
  507.     }
  508.     else
  509.         newString = "";
  510.         
  511.     return newString;
  512.  
  513. } // CStr32::Copy
  514.  
  515.  
  516. //----------------------------------------------------------------------------------------
  517. // CStr32::operator +=(CString):  Concatenate a string
  518. //----------------------------------------------------------------------------------------
  519.  
  520. CStr32& CStr32::operator += (const CString& str)
  521. {
  522.     InsertHelper (str, Length() + 1, kStr32Len);
  523.     return *this;
  524. } // CStr32::operator +=(CString)
  525.  
  526.  
  527. //----------------------------------------------------------------------------------------
  528. // CStr32::operator +=(char*):  Concatenate a string
  529. //----------------------------------------------------------------------------------------
  530.  
  531. CStr32& CStr32::operator += (const char* str)
  532. {
  533.     InsertHelper (str, Length() + 1, kStr32Len);
  534.     return *this;
  535. } // CStr32::operator +=(char*)
  536.  
  537.  
  538. //----------------------------------------------------------------------------------------
  539. // CStr32::operator +=(char):  Concatenate a single character
  540. //----------------------------------------------------------------------------------------
  541.  
  542. CStr32& CStr32::operator += (const char ch)
  543. {
  544.     if (++Length() <= kStr32Len)
  545.         fStr[Length()] = ch;
  546.     else
  547.     {
  548.         --Length();
  549. #if qDebugMsg
  550.         fprintf(stderr,"###CStr32::operator+=: Concatenation produces CStr32 overflow.\n");
  551. #endif
  552.     }
  553.     
  554.     return *this;
  555. } // CStr32::operator +=(char)
  556.  
  557.  
  558. //========================================================================================
  559. // CLASS CStr31
  560. //========================================================================================
  561.  
  562.  
  563. //----------------------------------------------------------------------------------------
  564. // CStr31::CStr31(char*):
  565. //----------------------------------------------------------------------------------------
  566.  
  567. CStr31::CStr31(const char* str)
  568. {
  569.     // Truncate the C CString to 31 bytes if necessary.
  570.  
  571.     Length() = str == NULL ? 0 : (unsigned char)(strlen(str));
  572.     if (Length() > kStr31Len)
  573.         Length() = kStr31Len;
  574.     memcpy(&fStr[1], str, (size_t)Length());
  575. } // CStr31::CStr31(char*)
  576.  
  577.  
  578. //----------------------------------------------------------------------------------------
  579. // CStr31::CStr31(long):
  580. //----------------------------------------------------------------------------------------
  581.  
  582. CStr31::CStr31(const long id)
  583. {
  584.     Length() = 4;
  585.     memcpy(&fStr[1], &id, (size_t)Length());
  586. } // CStr31::CStr31(long)
  587.  
  588.  
  589. //----------------------------------------------------------------------------------------
  590. // CStr31::CStr31(char*):
  591. //----------------------------------------------------------------------------------------
  592.  
  593. void
  594. CStr31::operator =(const CString& str)
  595. {
  596.     Length() = str.Length();
  597.     
  598.     if (Length() > kStr31Len)
  599.         Length() = kStr31Len;
  600.     memcpy(&fStr[1], &str.fStr[1], (size_t)Length());
  601. }
  602.  
  603. void
  604. CStr31::operator =(const unsigned char* str)
  605. {
  606.     Length() = str == NULL ? 0 : str[0];
  607.     
  608.     if (Length() > kStr31Len)
  609.         Length() = kStr31Len;
  610.     memcpy(&fStr[1], str + 1, (size_t)Length());
  611. }
  612.  
  613. void
  614. CStr31::operator =(const char* str)
  615. {
  616.     Length() = str == NULL ? 0 : (unsigned char)(strlen(str));
  617.     
  618.     if (Length() > kStr31Len)
  619.         Length() = kStr31Len;
  620.     memcpy(&fStr[1], str, (size_t)Length());
  621. }
  622.  
  623. //----------------------------------------------------------------------------------------
  624. // CStr31::Copy:
  625. //----------------------------------------------------------------------------------------
  626.  
  627. CStr31 CStr31::Copy(short pos, short length)
  628. {
  629.     CStr31 newString;
  630.     
  631.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  632.     
  633.     if (length > 0)
  634.     {
  635.         memcpy(&newString.fStr[1], &fStr[pos], (size_t)length);
  636.         newString.Length() = length;
  637.     }
  638.     else
  639.         newString = "";
  640.         
  641.     return newString;
  642.  
  643. } // CStr31::Copy
  644.  
  645.  
  646. //----------------------------------------------------------------------------------------
  647. // CStr31::operator +=(CString):  Concatenate a string
  648. //----------------------------------------------------------------------------------------
  649.  
  650. CStr31& CStr31::operator += (const CString& str)
  651. {
  652.     InsertHelper (str, Length() + 1, kStr31Len);
  653.     return *this;
  654. } // CStr31::operator +=(CString)
  655.  
  656.  
  657. //----------------------------------------------------------------------------------------
  658. // CStr31::operator +=(char*):  Concatenate a string
  659. //----------------------------------------------------------------------------------------
  660.  
  661. CStr31& CStr31::operator += (const char* str)
  662. {
  663.     InsertHelper (str, Length() + 1, kStr31Len);
  664.     return *this;
  665. } // CStr31::operator +=(char*)
  666.  
  667.  
  668. //----------------------------------------------------------------------------------------
  669. // CStr31::operator +=(char):  Concatenate a single character
  670. //----------------------------------------------------------------------------------------
  671.  
  672. CStr31& CStr31::operator += (const char ch)
  673. {
  674.     if (++Length() <= kStr31Len)
  675.         fStr[Length()] = ch;
  676.     else
  677.     {
  678.         --Length();
  679. #if qDebugMsg
  680.         fprintf(stderr,"###CStr31::operator+=: Concatenation produces CStr31 overflow.\n");
  681. #endif
  682.     }
  683.     
  684.     return *this;
  685. } // CStr31::operator +=(char)
  686.