home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IBUFFER.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  18KB  |  381 lines

  1. #ifndef _IBUFFER_
  2. #define _IBUFFER_
  3. /*******************************************************************************
  4. * FILE NAME: ibuffer.hpp                                                       *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   This file contains the declaration(s) of the class(es):                    *
  8. *     IBuffer - class to hold IString contents                                 *
  9. *                                                                              *
  10. * COPYRIGHT:                                                                   *
  11. *   Licensed Materials - Property of IBM                                       *
  12. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  13. *   All Rights Reserved                                                        *
  14. *   US Government Users Restricted Rights - Use, duplication, or               *
  15. *   disclosure                                                                 *
  16. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  17. *                                                                              *
  18. *******************************************************************************/
  19. extern "C"
  20.   {
  21.   #include <stddef.h>
  22.   }
  23.  
  24. #ifndef _IVBASE_
  25.   #include <ivbase.hpp>
  26. #endif
  27.  
  28. #ifndef _ISTRENUM_
  29.   #include <istrenum.hpp>
  30. #endif
  31.  
  32. /*----------------------------------------------------------------------------*/
  33. /* Align classes on four byte boundary.                                       */
  34. /*----------------------------------------------------------------------------*/
  35. #pragma pack(4)
  36.  
  37. class IStringTest;
  38.  
  39. class IBuffer : public IVBase {
  40. /*******************************************************************************
  41. * The IBuffer class defines the contents of an IString.                        *
  42. *                                                                              *
  43. *   Notes:                                                                     *
  44. *                                                                              *
  45. *     1) This class is intended only to support the implementation of the      *
  46. *        IString class.  Objects of this class should not be created or        *
  47. *        manipulated except through the containing IString object.             *
  48. *                                                                              *
  49. *     2) Use the following information to guide you when allocating            *
  50. *        objects of this class:                                                *
  51. *                                                                              *
  52. *        - Subclasses must not add any data members.                           *
  53. *                                                                              *
  54. *        - The operator new is protected so that it can be called only from    *
  55. *          the newBuffer member function.                                      *
  56. *                                                                              *
  57. *        - The destructor is protected so that instances of this class cannot  *
  58. *          easily be created on the stack or as statics.                       *
  59. *******************************************************************************/
  60. public:
  61. /*------------------------------- Reallocating ---------------------------------
  62. | The following function manages the reallocation of IBuffers when             |
  63. | strings' contents are modified:                                              |
  64. |   newBuffer           - Allocates a new buffer and initializes it with the   |
  65. |                         contents of up to three argument buffers.            |
  66. |   checkAddition       - Verifies that the two arguments, when added, will    |
  67. |                         not overflow an unsigned int.                        |
  68. |   checkMultiplication - Verifies that the two arguments, when multiplied,    |
  69. |                         will not overflow an unsigned int.                   |
  70. |   overflow            - Function that throws exception when overflow is      |
  71. |                         detected by checkAddition or checkMultiplication.    |
  72. |   setDefaultBuffer    - Sets the default (null) buffer; the argument buffer  |
  73. |                         must be comprised of a single null byte.             |
  74. ------------------------------------------------------------------------------*/
  75. IBuffer
  76.  *newBuffer( const void *p1, unsigned len1,
  77.              const void *p2 = 0, unsigned len2 = 0,
  78.              const void *p3 = 0, unsigned len3 = 0,
  79.              char padChar = 0 ) const;
  80.  
  81. static unsigned
  82.   checkAddition       ( unsigned addend1, unsigned addend2 ),
  83.   checkMultiplication ( unsigned factor1, unsigned factor2 ),
  84.   overflow            ( );
  85.  
  86. static void
  87.   setDefaultBuffer( IBuffer *newDefaultBuffer );
  88.  
  89. /*-------------------------------- Accessing -----------------------------------
  90. | The following functions provide access to various attributes of a buffer:    |
  91. |   null          - Returns the address of the null buffer.                    |
  92. |   useCount      - Returns the number of IStrings that refer to the buffer.   |
  93. |   length        - Returns the length of the buffer's contents.               |
  94. |   contents      - Returns the address of the buffer's contents.              |
  95. |   defaultBuffer - Static member function that returns the address of the     |
  96. |                   null buffer for the class.                                 |
  97. |   fromContents  - Static member function that returns the address of         |
  98. |                   IBuffer, given a pointer to its contents.                  |
  99. |   charType      - Returns the type of a character at a given index.          |
  100. |   next          - Returns a pointer to the next character, not the next      |
  101. |                   byte, in the buffer.                                       |
  102. ------------------------------------------------------------------------------*/
  103. static IBuffer
  104.  *defaultBuffer ( ),
  105.  *fromContents  ( const char *pBuffer );
  106.  
  107. IBuffer
  108.  *null ( ) const;
  109.  
  110. unsigned
  111.   useCount ( ) const,
  112.   length   ( ) const;
  113.  
  114. const char
  115.  *contents ( ) const;
  116.  
  117. char
  118.  *contents ( );
  119.  
  120. virtual IStringEnum::CharType
  121.   charType ( unsigned index ) const;
  122.  
  123. virtual char
  124.  *next ( const char *prev );
  125.  
  126. virtual const char
  127.  *next ( const char *prev ) const;
  128.  
  129. /*---------------------------- Reference Counting ------------------------------
  130. | The following functions manage the buffer reference count:                   |
  131. |   addRef    - Increments the usage count.                                    |
  132. |   removeRef - Decrements the usage count and deletes the buffer when the     |
  133. |               usage count goes to 0.                                         |
  134. ------------------------------------------------------------------------------*/
  135. void
  136.   addRef    ( ),
  137.   removeRef ( );
  138.  
  139. /*-------------------------------- Comparing -----------------------------------
  140. | The following functions permit the IBuffer's contents to be compared to      |
  141. | some other character array:                                                  |
  142. |   Comparison - Enumeration of the possible return codes from                 |
  143. |                IBuffer::compare.                                             |
  144. |   compare    - Compares the buffer to an argument character array.           |
  145. ------------------------------------------------------------------------------*/
  146. typedef enum
  147.   {
  148.   equal,
  149.   greaterThan,
  150.   lessThan
  151.   } Comparison;
  152.  
  153. virtual Comparison
  154.   compare ( const void    *p,
  155.             unsigned       len ) const;
  156.  
  157. /*-------------------------------- Character Subsetting ------------------------
  158. | The following function is used when a subset of characters is required:      |
  159. |   subString - Returns a new IBuffer, of the same type as the previous one,   |
  160. |               that contains the specified subset of characters.              |
  161. ------------------------------------------------------------------------------*/
  162. virtual IBuffer
  163.  *subString ( unsigned startPos,
  164.               unsigned len,
  165.               char     padCharacter ) const;
  166.  
  167. /*-------------------------------- Diagnostics ---------------------------------
  168. | The following function provides diagnostic information about the buffer:     |
  169. |   asDebugInfo - Returns information about the buffer's internal              |
  170. |                 representation that can be used for debugging.
  171. ------------------------------------------------------------------------------*/
  172. virtual IString
  173.   asDebugInfo ( ) const;
  174.  
  175. /*--------------------------------- Testing ------------------------------------
  176. | The following functions are called by the corresponding IString functions    |
  177. | to test the buffer's contents:                                               |
  178. |   isAlphanumeric- See IString::isAlphanumeric.                               |
  179. |   isAlphabetic  - See IString::isAlphabetic.                                 |
  180. |   isASCII       - See IString::isASCII.                                      |
  181. |   isControl     - See IString::isControl.                                    |
  182. |   isDigits      - See IString::isDigits.                                     |
  183. |   isGraphics    - See IString::isGraphics.                                   |
  184. |   isHexDigits   - See IString::isHexDigits.                                  |
  185. |   isLowerCase   - See IString::isLowerCase.                                  |
  186. |   isPrintable   - See IString::isPrintable.                                  |
  187. |   isPunctuation - See IString::isPunctuation.                                |
  188. |   isUpperCase   - See IString::isUpperCase.                                  |
  189. |   isWhiteSpace  - See IString::isWhiteSpace.                                 |
  190. |   isDBCS        - See IString::isDBCS.                                       |
  191. |   isSBCS        - See IString::isSBCS.                                       |
  192. |   isValidDBCS   - See IString::isValidDBCS.                                  |
  193. |   includesDBCS  - See IString::includesDBCS.                                 |
  194. |   includesSBCS  - See IString::includesSBCS.                                 |
  195. ------------------------------------------------------------------------------*/
  196. virtual Boolean
  197.   isAlphanumeric( ) const,
  198.   isAlphabetic  ( ) const,
  199.   isASCII       ( ) const,
  200.   isControl     ( ) const,
  201.   isDigits      ( ) const,
  202.   isGraphics    ( ) const,
  203.   isHexDigits   ( ) const,
  204.   isLowerCase   ( ) const,
  205.   isPrintable   ( ) const,
  206.   isPunctuation ( ) const,
  207.   isUpperCase   ( ) const,
  208.   isWhiteSpace  ( ) const,
  209.   isDBCS        ( ) const,
  210.   isSBCS        ( ) const,
  211.   isValidDBCS   ( ) const,
  212.   includesDBCS  ( ) const,
  213.   includesSBCS  ( ) const;
  214.  
  215. /*-------------------------------- Searching -----------------------------------
  216. | The following functions are called by the corresponding IString              |
  217. | functions to search the buffer's contents:                                   |
  218. |   indexOf           - See IString::indexOf.                                  |
  219. |   indexOfAnyBut     - See IString::indexOfAnyBut.                            |
  220. |   indexOfAnyOf      - See IString::indexOfAnyOf.                             |
  221. |   lastIndexOf       - See IString::lastIndexOf.                              |
  222. |   lastIndexOfAnyBut - See IString::lastIndexOfAnyBut.                        |
  223. |   lastIndexOfAnyOf  - See IString::lastIndexOfAnyOf.                         |
  224. ------------------------------------------------------------------------------*/
  225. virtual unsigned
  226.   indexOf           ( const char        *pString,
  227.                       unsigned           len,
  228.                       unsigned           startPos = 1 ) const,
  229.   indexOf           ( const IStringTest &aTest,
  230.                       unsigned           startPos = 1 ) const,
  231.   indexOfAnyBut     ( const char        *pString,
  232.                       unsigned           len,
  233.                       unsigned           startPos = 1 ) const,
  234.   indexOfAnyBut     ( const IStringTest &aTest,
  235.                       unsigned           startPos = 1 ) const,
  236.   indexOfAnyOf      ( const char        *pString,
  237.                       unsigned           len,
  238.                       unsigned           startPos = 1 ) const,
  239.   indexOfAnyOf      ( const IStringTest &aTest,
  240.                       unsigned           startPos = 1 ) const,
  241.   lastIndexOf       ( const char        *pString,
  242.                       unsigned           len,
  243.                       unsigned           startPos = 0 ) const,
  244.   lastIndexOf       ( const IStringTest &aTest,
  245.                       unsigned           startPos = 0 ) const,
  246.   lastIndexOfAnyBut ( const char        *pString,
  247.                       unsigned           len,
  248.                       unsigned           startPos = 0 ) const,
  249.   lastIndexOfAnyBut ( const IStringTest &aTest,
  250.                       unsigned           startPos = 0 ) const,
  251.   lastIndexOfAnyOf  ( const char        *pString,
  252.                       unsigned           len,
  253.                       unsigned           startPos = 0 ) const,
  254.   lastIndexOfAnyOf  ( const IStringTest &aTest,
  255.                       unsigned           startPos = 0 ) const;
  256.  
  257. /*--------------------------------- Editing ------------------------------------
  258.   The following functions are called by the corresponding IString functions
  259.   to edit the buffer's contents:
  260.     center       - See IString::center.
  261.     change       - See IString::change.
  262.     copy         - See IString::copy.
  263.     insert       - See IString::insert.
  264.     leftJustify  - See IString::leftJustify.
  265.     lowerCase    - See IString::lowerCase.
  266.     overlayWith  - See IString::overlayWith.
  267.     remove       - See IString::remove.
  268.     reverse      - See IString::reverse.
  269.     rightJustify - See IString::rightJustify.
  270.     strip        - See IString::strip.
  271.     translate    - See IString::translate.
  272.     upperCase    - See IString::upperCase.
  273. ------------------------------------------------------------------------------*/
  274. virtual IBuffer
  275.  *center       ( unsigned newLen,
  276.                  char     padCharacter ),
  277.  *change       ( const char *pSource,
  278.                  unsigned    sourceLen,
  279.                  const char *pTarget,
  280.                  unsigned    targetLen,
  281.                  unsigned    startPos,
  282.                  unsigned    numChanges ),
  283.  *copy         ( unsigned numCopies ),
  284.  *insert       ( const char *pInsert,
  285.                  unsigned    insertLen,
  286.                  unsigned    pos,
  287.                  char        padCharacter ),
  288.  *leftJustify  ( unsigned newLen,
  289.                  char     padCharacter ),
  290.  *lowerCase    ( ),
  291.  *overlayWith  ( const char *overlay,
  292.                  unsigned    len,
  293.                  unsigned    pos,
  294.                  char        padCharacter ),
  295.  *remove       ( unsigned startPos,
  296.                  unsigned numChars ),
  297.  *reverse      ( ),
  298.  *rightJustify ( unsigned newLen,
  299.                  char     padCharacter ),
  300.  *strip        ( const char             *pChars,
  301.                  unsigned                len,
  302.                  IStringEnum::StripMode  mode ),
  303.  *strip        ( const IStringTest      &aTest,
  304.                  IStringEnum::StripMode  mode ),
  305.  *translate    ( const char *pInputChars,
  306.                  unsigned    inputLen,
  307.                  const char *pOutputChars,
  308.                  unsigned    outputLen,
  309.                  char        padCharacter ),
  310.  *upperCase    ( );
  311.  
  312. protected:
  313. /*------------------------------ Implementing ----------------------------------
  314.   The following functions provide utilities used to implement this class:
  315.     startSearch          - Initializes an indexOf search.
  316.     startBackwardsSearch - Initializes a lastIndexOf search.
  317.     allocate             - Returns a new buffer of the required length.
  318.     operator new         - Allocates space for a buffer of the specified
  319.                            length.
  320.     operator delete      - Deallocates a buffer.
  321.     dbcsTable            - Table of DBCS first-byte flags ('dbcsTable[n] == 1'
  322.                            if and only if n is a valid DBCS first byte).
  323.     initialize           - Static member function used to initialize (set up
  324.                            null buffer, DBCS table, and so forth).
  325.     className            - Returns the name of the class (IBuffer).
  326. ------------------------------------------------------------------------------*/
  327. virtual unsigned
  328.   startSearch          ( unsigned startPos,
  329.                          unsigned searchLen ) const,
  330.   startBackwardsSearch ( unsigned startPos,
  331.                          unsigned searchLen ) const;
  332.  
  333. virtual IBuffer
  334.  *allocate ( unsigned bufLength ) const;
  335.  
  336. #ifdef __DEBUG_ALLOC__
  337. void  *operator new ( size_t t, const char * filename, size_t linenum, unsigned bufLen );
  338. void  operator delete ( void *p, const char * filename, size_t linenum );
  339. #else
  340. void  *operator new ( size_t t, unsigned bufLen );
  341. void  operator delete ( void *p );
  342. #endif
  343.  
  344. static char
  345.   dbcsTable[ 256 ];
  346.  
  347. static IBuffer
  348.  *initialize ( );
  349.  
  350. virtual const char
  351.  *className ( ) const;
  352.  
  353. /*-------------------------- Constructor/Destructor ----------------------------
  354.   The constructor for this class requires the length of the buffer (the
  355.   value to be stored in the length data member).
  356. ------------------------------------------------------------------------------*/
  357.   IBuffer ( unsigned newLen );
  358.   ~IBuffer ( );
  359.  
  360. private:
  361. /*--------------------------------- Private ----------------------------------*/
  362. unsigned
  363.   refs,
  364.   len;
  365. char
  366.   data[1];
  367. static IBuffer
  368.  *nullBuffer;
  369. };
  370.  
  371. /*----------------------------------------------------------------------------*/
  372. /* Resume compiler default packing.                                           */
  373. /*----------------------------------------------------------------------------*/
  374. #pragma pack()
  375.  
  376. #ifndef I_NO_INLINES
  377.   #include <ibuffer.inl>
  378. #endif
  379.  
  380. #endif
  381.