home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Documentation / Engineering Notes / Foundation / Strings < prev   
Encoding:
Text File  |  1996-08-16  |  7.8 KB  |  97 lines  |  [TEXT/ttxt]

  1. OpenDoc
  2. Development
  3. Framework
  4.                                                                                                                                                                                     
  5. Strings
  6. ODF Release 1                                                                                                                                                             
  7.  
  8. Table of Content
  9. ------------------
  10. • Overview
  11. • What's Changed Since 1.0d11
  12. • Theory of Operation
  13.  
  14.  
  15. The Strings subsystem was rewritten since the last prerelease version of ODF (1.0d11).  The subsystem was modified to work more closely with the OpenDoc ODIText data structure, improving compatiblity with OpenDoc and improving support for international character sets.  The Strings chapter from the ODF Class Reference could not be updated in time for this release; so this document serves as the primary reference for the Strings subsystem.
  16.  
  17.  
  18. Overview
  19.  
  20. The Strings subsystem provides only a few public classes, and most ODF parts will need to use only one of these classes: FW_CString.  FW_CString  supports localizable strings.  A string is a run of characters from one locale.  The run of characters is stored as a contiguous sequence of bytes.  The OpenDoc ODIText data structure is used for the actual storage of the bytes.  The ODIText data structure is stored inside a private ODF data structure (FW_SPrivStringRep), along with a reference count.  FW_CString objects can share string storage, and use copy-on-write semantics to defer the duplication of string storage until absolutely necessary, thus eliminating unneeded duplication (improving performance in both space and time).
  21.  
  22. FW_CString provides a rich API for manipulating strings.  Functions exist to Append, Insert, and Prepend characters to a string, where the characters may be provided in several forms (NUL terminated ISO string, pointer and length, another FW_CString, an ODIText, etc.). Other functions exist to do simple search and replace operations, to monocase, truncate, etc.  Accessor functions exist to get the length in bytes and the length in characters.  Other functions exist to convert a number into a string representation, or to parse a string to extract a number.  See the FW_CString class interface for a complete list of functions.
  23.  
  24. If your part's needs for strings are simple (and we expect most parts to be), you may not need to know anything else about the Strings subsystem.  However, you may want to quickly skim through the remainder of this document so that you'll be aware of what other features the Strings subsystem provides.
  25.  
  26. In addition to the class FW_CString, there are two subclasses FW_CString32 and FW_CString255.  These classes add arrays of 32 bytes and 256 bytes respectively to the string object, and the string object is initialized to use this storage instead of dynamically allocating storage for the string.  You may want to consider using these classes for stack-based strings local to a function, though the savings will probably be minimal in normal circumstances.
  27.  
  28. The Strings subystem also provides classes for iterating through text data structures a character at a time.  These classes includes the class FW_CTextReader and its subclasses FW_CStringReader and FW_CMemoryReader, and the class FW_CTextWriter and its subclasses FW_CStringWriter and FW_CMemoryWriter.  You can also make your own subclasses of FW_CTextReader and FW_CTextWriter for interfacing to your own text data structures.  These classes may be useful if you have algorithms for processing text that must operate on various data structures that don't share a common base class.
  29.  
  30.  
  31. What's Changed Since 1.0d11
  32.  
  33. If you have used ODF Strings from any of the prereleases of ODF, you should be aware of the following:
  34.  
  35. 1) FW_CString and subclasses are now reference-counted with copy-on-write behavior (explained below).  This doesn't affect APIs or the way you use strings.
  36. 2) FW_CDynamicString no longer exists.  Use FW_CString instead.
  37. 3) The String tool classes were eliminated.  Much of there functionality was integrated into FW_CString member functions.
  38. 4) Ranges of bytes are now used instead of ranges of characters.  Member functions that took FW_CharacterCount parameters now take FW_ByteCount parameters; likewise FW_CharacterPosition parameters are now FW_BytePosition parameters. (In single-byte character sets byte counts and character counts are the same. When double-byte characters are used, there will be more bytes than characters).
  39.  
  40.  
  41. Theory of Operation
  42.  
  43. For anyone curious as to how ODF Strings work, the following information may be useful. 
  44.  
  45. The bulk of the implementation of the Strings subsystem is contained in ODFLibrary.  The ODFFoundation static library contains thin C++ wrapper classes that call the Strings API exported by the ODFLibrary.  From a user's perspective, this separation of code should be invisible.
  46.  
  47. The ODFLibrary defines the following types:
  48.  
  49. struct FW_SPrivStringRep;
  50.     // An opaque data structure!
  51.     
  52. typedef FW_SPrivStringRep* FW_HString;
  53.     // A handle to a string. The preferred way to reference a PrivStringRep
  54.  
  55. The ODFFoundation static library defines a class FW_CString, which has a protected FW_String data member:
  56.  
  57. class FW_CString
  58. {
  59. public:
  60.     FW_DECLARE_CLASS
  61.     FW_DECLARE_AUTO(FW_CString)
  62.     
  63. public:
  64.     ~FW_CString();
  65.     FW_CString();
  66.     FW_CString(const FW_CString& other);
  67.     FW_CString(FW_HString rep);
  68.  
  69.     FW_CString(ODIText *text);
  70.     FW_CString(const char* text);
  71.     FW_CString(const char* text, FW_ByteCount byteLength);
  72.  
  73.     ... // Rest of API omitted
  74.     
  75. protected:
  76.     FW_HString    fRep;
  77. };
  78.  
  79. The class FW_CString is the thin wrapper class.  It has a single data member, fRep, which is an FW_HString.  All member functions in FW_CString are forwarded to the Strings API defined in the ODFLibrary.  The Strings API in the ODFLibrary is a collection of extern "C" functions, not C++ member functions.  These functions all take an FW_HString parameter (which can be thought of as a "this" parameter), and operate on the private data pointed to by the FW_HString.  The FW_CString class passes its fRep member to these functions.
  80.  
  81. The private data structure (FW_CPrivStringRep) pointed to by an FW_HString is designed to be shared, and maintains a references count of the number of references to it. The FW_CString class implements the sharing and reference counting protocol.  This means that copying an FW_CString object is a very inexpensive operation.  Consider the following code:
  82.  
  83. void Foo(const FW_CString& aString)
  84. {
  85.     FW_CString aCopy;
  86.     ...
  87.     aCopy = aString;
  88.     ...
  89. }
  90.  
  91. When the assignment executes, the string aCopy must decrement the reference count of the representation object it currently  is using, copy the FW_HString pointer from the aString object, and then increment the reference count for that representation object.  The storage for the string is not copied, but is simply reused.
  92.  
  93. Of course, if multiple strings are sharing the same storage, and one of them attempts to modify the storage, it's necessary to make sure that not all FW_CString's using that storage are modified. To prevent this, FW_CString uses copy-on-write semantics.  Whenever an FW_CString member function is executed that could potentially modify the string, the reference count of the representation object is checked.  If the reference count is one, then no other FW_CString could be also be using the representation, so the representation is directly modified.  If the reference count is greater than one, the FW_CString object being modifed duplicates the representation object, decrements the reference count of the original, and sets the reference count of the new copy to 1.  The FW_CString is then attached to the new representation object, which can then be safely modified without affecting other FW_CStrings.
  94.  
  95.  
  96. © 1993 - 1996 Apple Computer, Inc. All rights reserved.
  97. Apple, the Apple Logo, Macintosh, and OpenDoc are trademarks of Apple Computer, Inc., registered in the United States and other countries.