home *** CD-ROM | disk | FTP | other *** search
- /************************************************************
-
- Created: Friday, December 12, 1990 at 5:44 PM
- PascalString.h
- C Interface to the Macintosh Libraries
-
-
- Copyright Apple Computer, Inc. 1985-1992
- All rights reserved
-
- ************************************************************/
-
-
- #ifndef __PASCALSTRING__
- #define __PASCALSTRING__
-
- #ifndef __STRING__
- #include <String.h>
- #endif
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- // Forward declaration for all the CString classes.
-
- #define kCStringID "cthq:CTLK$CString,1.0"
- struct CString;
-
- #define kCStr255ID "cthq:CTLK$CStr255,1.0"
- struct CStr255;
-
- #define kCStr63ID "cthq:CTLK$CStr63,1.0"
- struct CStr63;
-
- #define kCStr32ID "cthq:CTLK$CStr32,1.0"
- struct CStr32;
-
- #define kCStr31ID "cthq:CTLK$CStr31,1.0"
- struct CStr31;
-
- typedef const unsigned char *ConstStr255Param;
- typedef ConstStr255Param ConstStr63Param, ConstStr32Param, ConstStr31Param,
- ConstStr27Param,ConstStr15Param;
-
- typedef const CStr255& ConstCStr255Param;
- typedef const CStr63& ConstCStr63Param;
- typedef const CStr32& ConstCStr32Param;
- typedef const CStr31& ConstCStr31Param;
-
- #ifndef __OSUTILS__
- #include <OSUtils.h>
- #endif
-
- // typedef unsigned char Str255[256], Str63[64], Str32[33],
- // Str31[32], Str27[28], Str15[16], *StringPtr, **StringHandle;
-
- // Some constants defining the length of each of the CString types.
-
- const short kLengthByte = 1;
- const short kBaseLen = 2;
- const short kStr255Len = 255;
- const short kStr63Len = 63;
- const short kStr32Len = 32;
- const short kStr31Len = 31;
-
- // Some external function declarations so that we don't have to include more than
- // the minimal set of header files.
-
- pascal char* PLSTRSTR(const CString& str1,
- const CString& str2); // From PaslibIntf.p
-
-
- //----------------------------------------------------------------------------------------
- // CString: Superclass of all Pascal string compatible string classes.
- //----------------------------------------------------------------------------------------
-
- typedef struct CString *CStringPtr, **CStringHandle;
-
- struct CString
- {
- public:
- unsigned char fStr[kBaseLen];
-
- protected:
- void InsertHelper(const CString& insStr,
- short pos,
- short maxLength);
- void InsertHelper(const char* insStr,
- short pos,
- short maxLength);
-
- public:
- CString();
- ~CString();
-
- // Basic length method, inherited by all derived classes. Define one that returns a
- // reference. Can be used as an lvalue and only can be applied to non-const Strings.
-
- inline unsigned char& Length()
- {
- return fStr[0];
- } // for non-const CString
-
-
- inline unsigned char Length() const
- {
- return fStr[0];
- } // for const CString
-
-
- inline Boolean IsEmpty()
- {
- return fStr[0] <= 0;
- }
-
- inline Boolean IsEmpty() const
- {
- return fStr[0] <= 0;
- }
-
- // Character selector operator.
-
- unsigned char& operator[](short pos); // for non-const CString
- // !!! try to inline later
-
- inline unsigned char operator[](short pos) const
- {
- return fStr[pos];
- } // for const CString
-
- //------------------------------------------------------------------------------------
- // CAUTION: There is a subtle difference between the (char*) and (unsigned char*)
- // converstion operators. The first converts a pascal-style string to a c-style
- // string. The second simply converts between two types (CString and Str55) both of
- // which are pascal-style strings.
-
- // Create a NULL terminated c-style string from a pascal-style CString. Used in
- // debugging to fprintf a CString.
-
- operator char*() const;
-
- // Used to create a toolbox type Str255 from our CString. This is simply a type
- // coersion! Both CString and Str255 are expected to be pascal-style strings.
-
- operator unsigned char*();
-
- operator const unsigned char*() const;
-
- //------------------------------------------------------------------------------------
-
- // Return an ID represented as a CString to the actual ID (a long).
-
- operator long() const;
-
- // Relational operators that are inherited by all the derived CString types. Three of
- // each so that literal C Strings can be conveniently used for one of the operators as
- // well as two of the derive classes as operators. These are declared here but defined
- // below all the CString classes because they use constructors for CStr255 and its class
- // definition has not been encountered yet.
-
- friend Boolean operator==(const CString& s1,
- const char* s2);
- friend Boolean operator==(const char* s1,
- const CString& s2);
- friend Boolean operator==(const CString& s1,
- const CString& s2);
-
- friend Boolean operator!=(const CString& s1,
- const char* s2);
- friend Boolean operator!=(const char* s1,
- const CString& s2);
- friend Boolean operator!=(const CString& s1,
- const CString& s2);
-
- friend Boolean operator>(const CString& s1,
- const char* s2);
- friend Boolean operator>(const char* s1,
- const CString& s2);
- friend Boolean operator>(const CString& s1,
- const CString& s2);
-
- friend Boolean operator<(const CString& s1,
- const char* s2);
- friend Boolean operator<(const char* s1,
- const CString& s2);
- friend Boolean operator<(const CString& s1,
- const CString& s2);
-
- friend Boolean operator>=(const CString& s1,
- const char* s2);
- friend Boolean operator>=(const char* s1,
- const CString& s2);
- friend Boolean operator>=(const CString& s1,
- const CString& s2);
-
- friend Boolean operator<=(const CString& s1,
- const char* s2);
- friend Boolean operator<=(const char* s1,
- const CString& s2);
- friend Boolean operator<=(const CString& s1,
- const CString& s2);
-
- // Concatenation operator that are inherited by all the derived CString types. Three
- // of each so that literal C Strings can be conveniently used for one of the operators
- // as well as using any two classes derived from CString.
-
- friend CStr255 operator+(const CString& s1,
- const char* s2);
- friend CStr255 operator+(const char* s1,
- const CString& s2);
- friend CStr255 operator+(const CString& s1,
- const CString& s2);
-
- // Methods that mimic the Pascal builtin CString functions for Pos, Insert and Delete.
- // Note that insert and copy is implemented in the derived classes.
-
- unsigned char Pos(const char* subStr, unsigned char startPos = 1);
- unsigned char Pos(const CString& subStr, unsigned char startPos = 1);
- inline void Delete(short pos, short length);
- };
-
- //----------------------------------------------------------------------------------------
- // CStr255:
- //----------------------------------------------------------------------------------------
-
- struct CStr255 : CString
- {
-
- friend struct CStr63;
- friend struct CStr31;
-
- private:
- unsigned char fData[kStr255Len - 1];
-
- public:
- CStr255();
- ~CStr255();
- CStr255(const CStr255& str);
- CStr255(const CStr63& str);
- CStr255(const CStr32& str);
- CStr255(const CStr31& str);
- CStr255(const unsigned char* str);
- CStr255(const char* str);
- CStr255(const long id);
-
- // Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
-
- void Insert(const CString& str, short pos);
- void Insert(const char* str, short pos);
- CStr255 Copy(short pos, short length);
-
- // Concatenation operator
-
- CStr255& operator +=(const CString& str);
- CStr255& operator +=(const char* str);
- CStr255& operator +=(const char ch);
-
- // Assignment operator
-
- CStr255& operator =(const CStr255& str);
- CStr255& operator =(const CStr63& str);
- CStr255& operator =(const CStr32& str);
- CStr255& operator =(const CStr31& str);
- CStr255& operator =(const unsigned char* str);
- CStr255& operator =(const char aChar);
- CStr255& operator =(const char* str);
-
- };
-
-
- //----------------------------------------------------------------------------------------
- // CStr63:
- //----------------------------------------------------------------------------------------
-
- struct CStr63 : CString
- {
-
- friend struct CStr255;
- friend struct CStr31;
-
- private:
- unsigned char fData[kStr63Len - 1];
-
- public:
- CStr63();
- ~CStr63();
- CStr63(const CStr255& str);
- CStr63(const CStr63& str);
- CStr63(const CStr32& str);
- CStr63(const CStr31& str);
- CStr63(const unsigned char* str);
- CStr63(const char* str);
- CStr63(const long id);
-
- // Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
-
- void Insert(const CString& str,
- short pos);
- void Insert(const char* str,
- short pos);
- CStr63 Copy(short pos, short length);
-
- // Concatenation operator
-
- CStr63& operator +=(const CString& str);
- CStr63& operator +=(const char* str);
- CStr63& operator +=(const char ch);
- };
-
-
- //----------------------------------------------------------------------------------------
- // CStr32:
- //----------------------------------------------------------------------------------------
-
- struct CStr32 : CString
- {
-
- friend struct CStr255;
- friend struct CStr63;
-
- private:
- unsigned char fData[kStr32Len - 1];
-
- public:
- CStr32();
- ~CStr32();
- inline CStr32(unsigned char length)
- {
- fStr[0] = length;
- }
-
-
- CStr32(const CStr255& str);
- CStr32(const CStr63& str);
- CStr32(const CStr32& str);
- CStr32(const CStr31& str);
- CStr32(const unsigned char* str);
- CStr32(const char* str);
- CStr32(const long id);
-
-
- // Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
-
- void Insert(const CString& str,
- short pos);
- void Insert(const char* str,
- short pos);
- CStr32 Copy(short pos, short length);
-
- // Concatenation operator
-
- CStr32& operator +=(const CString& str);
- CStr32& operator +=(const char* str);
- CStr32& operator +=(const char ch);
- };
-
-
- //----------------------------------------------------------------------------------------
- // CStr31:
- //----------------------------------------------------------------------------------------
-
- struct CStr31 : CString
- {
-
- friend struct CStr255;
- friend struct CStr63;
- friend struct CStr32;
-
- private:
- unsigned char fData[kStr31Len - 1];
-
- public:
- ~CStr31();
- CStr31();
- inline CStr31(unsigned char length)
- {
- fStr[0] = length;
- }
-
-
- CStr31(const CStr255& str);
- CStr31(const CStr63& str);
- CStr31(const CStr32& str);
- CStr31(const CStr31& str);
- CStr31(const unsigned char* str);
- CStr31(const char* str);
- CStr31(const long id);
-
- // Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
-
- void Insert(const CString& str,
- short pos);
- void Insert(const char* str,
- short pos);
- CStr31 Copy(short pos, short length);
-
- // Concatenation operator
-
- CStr31& operator +=(const CString& str);
- CStr31& operator +=(const char* str);
- CStr31& operator +=(const char ch);
- };
-
-
- //----------------------------------------------------------------------------------------
- // CString inline function definitions
- //----------------------------------------------------------------------------------------
-
- inline CString::operator unsigned char*()
- {
- return (unsigned char *) this;
- }
-
- inline void CString::Delete(short pos, short length)
- {
- if ((pos > 0) && (length > 0) && (pos <= Length())) // should also check that pos <= kMaxLength
- {
- if (pos + length > Length())
- fStr[0] = pos - 1;
- else
- {
- memcpy(&fStr[pos], &fStr[pos + length], Length() - (pos + length) + kLengthByte);
- fStr[0] -= length;
- }
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // CStr255 inline function definitions
- //----------------------------------------------------------------------------------------
- inline CStr255::CStr255(const CStr255& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr255::CStr255(const CStr63& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr255::CStr255(const CStr32& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr255::CStr255(const CStr31& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr255::CStr255(const unsigned char* str)
- {
- memcpy(fStr, str, kStr255Len + kLengthByte);
- }
-
- inline CStr255& CStr255::operator = (const CStr255& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
-
- return *this;
- }
-
- inline CStr255& CStr255::operator = (const CStr63& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
-
- return *this;
- }
-
- inline CStr255& CStr255::operator = (const CStr32& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
-
- return *this;
- }
-
- inline CStr255& CStr255::operator = (const CStr31& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
-
- return *this;
- }
-
- inline CStr255& CStr255::operator = (const unsigned char* str)
- {
- memcpy(fStr, str, kStr255Len + kLengthByte);
-
- return *this;
- }
-
- inline CStr255& CStr255::operator = (const char aChar)
- {
- if (aChar == '\0') // passing in an NULL char
- Length() = 0;
- else
- {
- Length() = 1; // CString gets length of 1
- fStr[1] = aChar; // assign in the character
- }
-
- return *this;
- }
-
- inline void CStr255::Insert(const CString& str, short pos)
- {
- InsertHelper(str, pos, kStr255Len);
- }
-
- inline void CStr255::Insert(const char* str, short pos)
- {
- InsertHelper(str, pos, kStr255Len);
- }
-
-
- //----------------------------------------------------------------------------------------
- // CStr63 inline function definitions
- //----------------------------------------------------------------------------------------
-
- inline CStr63::CStr63(const CStr255& str)
- {
- // Truncate the CStr255 to 63 bytes if necessary.
-
- Length() = str.Length() > kStr63Len ? kStr63Len : str.Length();
- memcpy(fStr, str.fStr, Length() + kLengthByte);
- }
-
- inline CStr63::CStr63(const CStr63& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr63::CStr63(const CStr32& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr63::CStr63(const CStr31& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr63::CStr63(const unsigned char* str)
- {
- memcpy(fStr, str, kStr63Len + kLengthByte);
- }
-
- inline void CStr63::Insert(const CString& str, short pos)
- {
- InsertHelper(str, pos, kStr63Len);
- }
-
- inline void CStr63::Insert(const char* str, short pos)
- {
- InsertHelper(str, pos, kStr63Len);
- }
-
-
- //----------------------------------------------------------------------------------------
- // CStr32 inline function definitions
- //----------------------------------------------------------------------------------------
-
- inline CStr32::CStr32(const CStr255& str)
- {
- // Truncate the CStr255 to 32 bytes if necessary.
-
- Length() = str.Length() > kStr32Len ? kStr32Len : str.Length();
- memcpy(fStr, str.fStr, Length() + kLengthByte);
- }
-
- inline CStr32::CStr32(const CStr63& str)
- {
- // Truncate the CStr63 to 32 bytes if necessary.
-
- Length() = str.Length() > kStr32Len ? kStr32Len : str.Length();
- memcpy(fStr, str.fStr, Length() + kLengthByte);
- }
-
- inline CStr32::CStr32(const CStr32& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr32::CStr32(const CStr31& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr32::CStr32(const unsigned char* str)
- {
- memcpy(fStr, str, kStr31Len + kLengthByte);
- }
-
- inline void CStr32::Insert(const CString& str, short pos)
- {
- InsertHelper(str, pos, kStr32Len);
- }
-
- inline void CStr32::Insert(const char* str, short pos)
- {
- InsertHelper(str, pos, kStr32Len);
- }
-
-
- //----------------------------------------------------------------------------------------
- // CStr31 inline function definitions
- //----------------------------------------------------------------------------------------
-
- inline CStr31::CStr31(const CStr255& str)
- {
- // Truncate the CStr255 to 31 bytes if necessary.
-
- Length() = str.Length() > kStr31Len ? kStr31Len : str.Length();
- memcpy(fStr, str.fStr, Length() + kLengthByte);
- }
-
- inline CStr31::CStr31(const CStr63& str)
- {
- // Truncate the CStr63 to 31 bytes if necessary.
-
- Length() = str.Length() > kStr31Len ? kStr31Len : str.Length();
- memcpy(fStr, str.fStr, Length() + kLengthByte);
- }
-
- inline CStr31::CStr31(const CStr32& str)
- {
- // Truncate the CStr32 to 31 bytes if necessary.
-
- Length() = str.Length() > kStr31Len ? kStr31Len : str.Length();
- memcpy(fStr, str.fStr, Length() + kLengthByte);
- }
-
- inline CStr31::CStr31(const CStr31& str)
- {
- memcpy(fStr, str.fStr, str.Length() + kLengthByte);
- }
-
- inline CStr31::CStr31(const unsigned char* str)
- {
- memcpy(fStr, str, kStr31Len + kLengthByte);
- }
-
- inline void CStr31::Insert(const CString& str, short pos)
- {
- InsertHelper(str, pos, kStr31Len);
- }
-
- inline void CStr31::Insert(const char* str, short pos)
- {
- InsertHelper(str, pos, kStr31Len);
- }
-
-
- //----------------------------------------------------------------------------------------
- // Inline friend function definitions for relational string operators.
- //----------------------------------------------------------------------------------------
-
- inline Boolean operator==(const CString& s1, const char* s2)
- {
- return RelString(s1, CStr255(s2), false, true) == 0;
- }
-
- inline Boolean operator==(const char* s1, const CString& s2)
- {
- return RelString(CStr255(s1), s2, false, true) == 0;
- }
-
- inline Boolean operator==(const CString& s1, const CString& s2)
- {
- return RelString(s1, s2, false, true) == 0;
- }
-
- inline Boolean operator!=(const CString& s1, const char* s2)
- {
- return RelString(s1, CStr255(s2), false, true) != 0;
- }
-
- inline Boolean operator!=(const char* s1, const CString& s2)
- {
- return RelString(CStr255(s1), s2, false, true) != 0;
- }
-
- inline Boolean operator!=(const CString& s1, const CString& s2)
- {
- return RelString(s1, s2, false, true) != 0;
- }
-
- inline Boolean operator>(const CString& s1, const char* s2)
- {
- return RelString(s1, CStr255(s2), false, true) > 0;
- }
-
- inline Boolean operator>(const char* s1, const CString& s2)
- {
- return RelString(CStr255(s1), s2, false, true) > 0;
- }
-
- inline Boolean operator>(const CString& s1, const CString& s2)
- {
- return RelString(s1, s2, false, true) > 0;
- }
-
- inline Boolean operator<(const CString& s1, const char* s2)
- {
- return RelString(s1, CStr255(s2), false, true) < 0;
- }
-
- inline Boolean operator<(const char* s1, const CString& s2)
- {
- return RelString(CStr255(s1), s2, false, true) < 0;
- }
-
- inline Boolean operator<(const CString& s1, const CString& s2)
- {
- return RelString(s1, s2, false, true) < 0;
- }
-
- inline Boolean operator>=(const CString& s1, const char* s2)
- {
- return RelString(s1, CStr255(s2), false, true) >= 0;
- }
-
- inline Boolean operator>=(const char* s1, const CString& s2)
- {
- return RelString(CStr255(s1), s2, false, true) >= 0;
- }
-
- inline Boolean operator>=(const CString& s1, const CString& s2)
- {
- return RelString(s1, s2, false, true) >= 0;
- }
-
- inline Boolean operator<=(const CString& s1, const char* s2)
- {
- return RelString(s1, CStr255(s2), false, true) <= 0;
- }
-
- inline Boolean operator<=(const char* s1, const CString& s2)
- {
- return RelString(CStr255(s1), s2, false, true) <= 0;
- }
-
- inline Boolean operator<=(const CString& s1, const CString& s2)
- {
- return RelString(s1, s2, false, true) <= 0;
- }
-
-
- #endif
-
-