home *** CD-ROM | disk | FTP | other *** search
- /*
- File: PasclStr.cpp
-
- Contains: Pascal string manipulation routines
-
- Owned by: Nick Pilch
-
- Copyright: © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <2> 5/3/96 EL 1340146: Correct truncation of
- international strings.
-
- To Do:
- */
-
- #ifndef _ODTYPES_
- #include "ODTypes.h"
- #endif
-
- #ifndef _PASCLSTR_
- #include "PasclStr.h"
- #endif
-
- #ifndef _ISOSTR_
- #include "ISOStr.h"
- #endif
-
- #ifndef _ODMEMORY_
- #include "ODMemory.h"
- #endif
-
- #ifndef _ITEXT_
- #include "IText.h"
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __STRING__
- #include <string.h>
- #endif
-
- #ifndef __CTYPE__
- #include <ctype.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #ifdef __SC__
- #ifndef __LANGUAGE__
- #include <Language.h>
- #endif
- #endif // __SC__
-
- #pragma segment ODPascalString
-
- //==============================================================================
- // Functions
- //==============================================================================
-
- void CopyPascalString(Str255 destString, ConstStr255Param srcString)
- {
- ODBlockMove((ODPtr)srcString, (ODPtr)destString, srcString[0] + 1L);
- }
-
- void CopyISOStr2PStr(Str255 destString, const ODISOStr srcString)
- {
- ODSize len = ODISOStrLength(srcString);
- ODBlockMove((ODPtr)srcString, &(destString[1]), (ODULong)len);
- destString[0] = (unsigned char) len;
- }
-
- void ConcatPascalStrings(Str255 destString, ConstStr255Param srcString)
- {
- ODSShort maxSrcLength = 255 - destString[0];
- if ( srcString[0] > maxSrcLength )
- {
- Str255 tmpStr;
- ODBlockMove( srcString, tmpStr, srcString[0] + 1 );
- maxSrcLength = ClipStringToBytes( (StringPtr)srcString,
- maxSrcLength, smCurrentScript );
- }
- else
- maxSrcLength = srcString[0];
-
- ODBlockMove((ODPtr)(srcString + 1),
- (ODPtr)(destString + destString[0] + 1), maxSrcLength);
- destString[0] += maxSrcLength;
- }
-
- ODBoolean EqualPascalStrings(ConstStr255Param str1, ConstStr255Param str2)
- {
- if (str1[0] != str2[0])
- return kODFalse;
- for (int i = 1; i <= str1[0]; i++)
- {
- if (tolower(str1[i]) != tolower(str2[i]))
- return kODFalse;
- }
- return kODTrue;
- }
-
- StringPtr IntlToPStr(ODIText* intlText, StringPtr pstr)
- {
- return GetITextString(intlText,pstr);
- }
-
- ODIText* PStrToIntl(StringPtr pstr, ODIText** intlText)
- {
- ODIText *result;
- if( intlText && *intlText ) {
- result = *intlText;
- SetITextString(result,pstr);
- SetITextScriptCode(result, smRoman);
- SetITextLangCode(result, langEnglish);
- } else {
- result = CreateIText(smRoman,langEnglish,pstr);
- if( intlText )
- *intlText = result;
- }
- return result;
- }
-
- //------------------------------------------------------------------------------
- // PStrToText
- //------------------------------------------------------------------------------
-
- ODHandle PStrToText(ConstStr255Param pstr)
- {
- ODHandle textHandle = kODNULL;
-
- if ( pstr )
- {
- textHandle = ODNewHandle(pstr[0]);
- if ( textHandle )
- {
- ODPtr textPtr = ODLockHandle(textHandle);
- ODBlockMove((ODPtr)&(pstr[1]), textPtr, pstr[0]);
- ODUnlockHandle(textHandle);
- }
- }
-
- return textHandle;
- }
-
- //------------------------------------------------------------------------------
- // TextToPStr
- //------------------------------------------------------------------------------
-
- void TextToPStr(ODHandle textHandle, Str255 destString)
- {
- if ( textHandle )
- {
- ODUShort textSize = GetHandleSize((Handle) textHandle);
- if (textSize > 255)
- textSize = 255;
-
- ODBlockMove(*textHandle, (ODPtr) &destString[1], textSize);
- destString[0] = textSize;
- }
- else
- {
- destString[0] = 0;
- }
- }
-
- //------------------------------------------------------------------------------
- // ClipStringToBytes
- //------------------------------------------------------------------------------
-
- ODSShort ClipStringToBytes( Str255 string, ODSShort numBytes,
- ODScriptCode scriptCode )
- {
- // We must start from the beginning to find out what kind is last char.
- // If the answer is SecondByte, we're done. If it's SingleByte, we're
- // also done. If it's FirstByte, we back up one and then we're done.
- // Note that this use of CharacterByteType may cause it to erroneously
- // identify a secondByte as a singleByte, but that that's ok in this case
- // as both are treated the same.
-
- short cbResult = CharacterByteType( (Ptr)&string[1], numBytes-1, scriptCode );
- if ( cbResult == smFirstByte )
- --numBytes;
-
- string[0] = numBytes;
- return numBytes;
- }
-
-