home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / PasclStr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  4.3 KB  |  194 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PasclStr.cpp
  3.  
  4.     Contains:    Pascal string manipulation routines
  5.  
  6.     Owned by:    Nick Pilch
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>      5/3/96    EL        1340146: Correct truncation of
  13.                                     international strings.
  14.  
  15.     To Do:
  16. */
  17.  
  18. #ifndef _ODTYPES_
  19. #include "ODTypes.h"
  20. #endif
  21.  
  22. #ifndef _PASCLSTR_
  23. #include "PasclStr.h"
  24. #endif
  25.  
  26. #ifndef _ISOSTR_
  27. #include "ISOStr.h"
  28. #endif
  29.  
  30. #ifndef _ODMEMORY_
  31. #include "ODMemory.h"
  32. #endif
  33.  
  34. #ifndef _ITEXT_
  35. #include "IText.h"
  36. #endif
  37.  
  38. #ifndef __MEMORY__
  39. #include <Memory.h>
  40. #endif
  41.  
  42. #ifndef __STRING__
  43. #include <string.h>
  44. #endif
  45.  
  46. #ifndef __CTYPE__
  47. #include <ctype.h>
  48. #endif
  49.  
  50. #ifndef __SCRIPT__
  51. #include <Script.h>
  52. #endif
  53.  
  54. #ifdef __SC__
  55.     #ifndef __LANGUAGE__
  56.     #include <Language.h>
  57.     #endif
  58. #endif // __SC__
  59.  
  60. #pragma segment ODPascalString
  61.  
  62. //==============================================================================
  63. // Functions
  64. //==============================================================================
  65.  
  66. void CopyPascalString(Str255 destString, ConstStr255Param srcString)
  67. {
  68.     ODBlockMove((ODPtr)srcString, (ODPtr)destString, srcString[0] + 1L);
  69. }
  70.  
  71. void CopyISOStr2PStr(Str255 destString, const ODISOStr srcString)
  72. {
  73.     ODSize len = ODISOStrLength(srcString);
  74.     ODBlockMove((ODPtr)srcString, &(destString[1]), (ODULong)len);
  75.     destString[0] = (unsigned char) len;
  76. }
  77.  
  78. void ConcatPascalStrings(Str255 destString, ConstStr255Param srcString)
  79. {
  80.     ODSShort maxSrcLength = 255 - destString[0];
  81.     if ( srcString[0] > maxSrcLength )
  82.     {
  83.         Str255 tmpStr;
  84.         ODBlockMove( srcString, tmpStr, srcString[0] + 1 );
  85.         maxSrcLength = ClipStringToBytes( (StringPtr)srcString,
  86.                 maxSrcLength, smCurrentScript );
  87.     }
  88.     else
  89.         maxSrcLength = srcString[0];
  90.  
  91.     ODBlockMove((ODPtr)(srcString + 1),
  92.                     (ODPtr)(destString + destString[0] + 1), maxSrcLength);
  93.     destString[0] += maxSrcLength;
  94. }
  95.  
  96. ODBoolean EqualPascalStrings(ConstStr255Param str1, ConstStr255Param str2)
  97. {
  98.     if (str1[0] != str2[0])
  99.         return kODFalse;
  100.     for (int i = 1; i <= str1[0]; i++)
  101.     {
  102.         if (tolower(str1[i]) != tolower(str2[i]))
  103.             return kODFalse;
  104.     }
  105.     return kODTrue;
  106. }
  107.  
  108. StringPtr IntlToPStr(ODIText* intlText, StringPtr pstr)
  109. {
  110.     return GetITextString(intlText,pstr);
  111. }
  112.  
  113. ODIText* PStrToIntl(StringPtr pstr, ODIText** intlText)
  114. {
  115.     ODIText *result;
  116.     if( intlText && *intlText ) {
  117.         result = *intlText;
  118.         SetITextString(result,pstr);
  119.         SetITextScriptCode(result, smRoman);
  120.         SetITextLangCode(result, langEnglish);
  121.     } else {
  122.         result = CreateIText(smRoman,langEnglish,pstr);
  123.         if( intlText )
  124.             *intlText = result;
  125.     }
  126.     return result;
  127. }
  128.  
  129. //------------------------------------------------------------------------------
  130. // PStrToText
  131. //------------------------------------------------------------------------------
  132.  
  133. ODHandle PStrToText(ConstStr255Param pstr)
  134. {
  135.     ODHandle textHandle = kODNULL;
  136.     
  137.     if ( pstr )
  138.     {
  139.         textHandle = ODNewHandle(pstr[0]);
  140.         if ( textHandle )
  141.         {
  142.             ODPtr textPtr = ODLockHandle(textHandle);
  143.             ODBlockMove((ODPtr)&(pstr[1]), textPtr, pstr[0]);
  144.             ODUnlockHandle(textHandle);
  145.         }
  146.     }
  147.  
  148.     return textHandle;
  149. }
  150.  
  151. //------------------------------------------------------------------------------
  152. // TextToPStr
  153. //------------------------------------------------------------------------------
  154.  
  155. void TextToPStr(ODHandle textHandle, Str255 destString)
  156. {
  157.     if ( textHandle )
  158.     {
  159.         ODUShort textSize = GetHandleSize((Handle) textHandle);
  160.         if (textSize > 255)
  161.             textSize = 255;
  162.         
  163.         ODBlockMove(*textHandle, (ODPtr) &destString[1], textSize);
  164.         destString[0] = textSize;
  165.     }
  166.     else
  167.     {
  168.         destString[0] = 0;
  169.     }
  170. }
  171.  
  172. //------------------------------------------------------------------------------
  173. // ClipStringToBytes
  174. //------------------------------------------------------------------------------
  175.  
  176. ODSShort ClipStringToBytes( Str255 string, ODSShort numBytes,
  177.         ODScriptCode scriptCode )
  178. {
  179.     // We must start from the beginning to find out what kind is last char.
  180.     // If the answer is SecondByte, we're done.  If it's SingleByte, we're
  181.     // also done.  If it's FirstByte, we back up one and then we're done.
  182.     // Note that this use of CharacterByteType may cause it to erroneously
  183.     // identify a secondByte as a singleByte, but that that's ok in this case
  184.     // as both are treated the same.
  185.  
  186.     short cbResult = CharacterByteType( (Ptr)&string[1], numBytes-1, scriptCode );
  187.     if ( cbResult == smFirstByte )
  188.         --numBytes;
  189.  
  190.     string[0] = numBytes;
  191.     return numBytes;
  192. }
  193.  
  194.