home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / hpp.z / wsstring.hpp < prev    next >
C/C++ Source or Header  |  1996-12-23  |  18KB  |  478 lines

  1. /*************************************************************************
  2.  *
  3.  * WSimpleString
  4.  *
  5.  *************************************************************************/
  6.  
  7. #ifndef _WSSTRING_HPP_INCLUDED
  8. #define _WSSTRING_HPP_INCLUDED
  9.  
  10. #ifndef _WNO_PRAGMA_PUSH
  11. #pragma pack(push,8);
  12. #pragma enum int;
  13. #endif
  14.  
  15. #ifndef _WSTRDATA_HPP_INCLUDED
  16. #  include "wstrdata.hpp"
  17. #endif
  18.  
  19. class WSimpleString;
  20.  
  21. extern template WArrayReference<WSimpleString>;
  22. extern template WArray<WSimpleString>;
  23.  
  24. typedef WArray<WSimpleString> WSimpleStringArray;
  25.  
  26. //
  27. // WSimpleString
  28. //
  29.  
  30. class WCMCLASS WSimpleString {
  31.  
  32.     public:
  33.  
  34.         /**********************************************************
  35.          * Constructors and Destructors
  36.          *********************************************************/
  37.  
  38.         WSimpleString() : _string( NULL ) {}
  39.         WSimpleString( const WChar *string, WULong numChars=USE_STR_LEN ) : _string( NULL ) { Create( string, numChars ); }
  40.         WSimpleString( const WResourceID & id, WModuleHandle module=_ApplicationModule ) : _string( NULL ) { Create( id, module ); }
  41.         WSimpleString( const WMessageID & id, WModuleHandle module=_ApplicationModule ) : _string( NULL ) { Create( id, module ); }
  42.         WSimpleString( const WString & s, WBool makeCopy=FALSE ) : _string( NULL ) { Create( s, makeCopy ); }
  43.         WSimpleString( const WSimpleString & s, WBool makeCopy=FALSE ) : _string( NULL ) { Create( s, makeCopy ); }
  44.  
  45.         ~WSimpleString() { Clear(); }
  46.  
  47.         /**********************************************************
  48.          * Properties
  49.          *********************************************************/
  50.  
  51.         // Character
  52.         //
  53.         //    Set/get an individual character in the string.  Forces
  54.         //    the string to be the default type.
  55.  
  56.         WWidestChar GetCharacter( WULong index ) const;
  57.         WBool       SetCharacter( WULong , WWidestChar  ) { return FALSE; }
  58.  
  59.         // Dirty
  60.  
  61.         WBool GetDirty() const { return TRUE; }
  62.         WBool SetDirty( WBool =TRUE ) { return FALSE; }
  63.  
  64.         // Double
  65.         //
  66.         //    Convert the string to a WDouble, optionally setting an error
  67.         //    flag.
  68.  
  69.         WDouble WCMRETURNSFLOAT GetDouble( WBool *ok=NULL ) const;
  70.  
  71.         // Empty
  72.         //
  73.         //    TRUE if the string is null or is empty.
  74.  
  75.         WBool GetEmpty() const { return( !_string || *_string == 0 ); }
  76.  
  77.         // Length
  78.         //
  79.         //    Returns the length of the string in characters, not
  80.         //    including the null terminator.
  81.  
  82.         WULong GetAnsiLength() const { return _string ? GetData()->_stringLength : 0; }
  83.         WULong GetUnicodeLength() const { return 0; } // TODO
  84.  
  85.         #ifdef _UNICODE
  86.             WULong GetLength() const { return GetUnicodeLength(); }
  87.         #else
  88.             WULong GetLength() const { return GetAnsiLength(); }
  89.         #endif
  90.  
  91.         // Long
  92.         //
  93.         //    Convert the string to a WLong, optionally setting an error
  94.         //    flag.
  95.  
  96.         WLong GetLong( WBool *ok=NULL ) const;
  97.  
  98.         // Null
  99.         //
  100.         //    TRUE if the string is a null (but not an empty) string.
  101.  
  102.         WBool GetNull() const { return !_string; }
  103.  
  104.         // Size
  105.         //
  106.         //    Returns the size of the string in bytes, not including
  107.         //    the null terminator.
  108.  
  109.         WULong GetAnsiSize() const { return _string ? GetData()->_stringSize : 0; }
  110.         WULong GetUnicodeSize() const { return 0; } // TODO
  111.  
  112.         #ifdef _UNICODE
  113.             WULong GetSize() const { return GetUnicodeSize(); }
  114.         #else
  115.             WULong GetSize() const { return GetAnsiSize(); }
  116.         #endif
  117.  
  118.         // Text
  119.         //
  120.         //    Set/get the actual text of the string.  The optional parm
  121.         //    on the set controls whether or not the string should store
  122.         //    just a pointer to the text or make a copy of the text.
  123.  
  124.         const WChar *const GetText() const { return GetAnsiText(); }
  125.         WBool              SetText( const WChar *str, WBool makeCopy=TRUE )
  126.                                { return SetAnsiText( str, makeCopy ); }
  127.  
  128.  
  129.         const WAnsiChar    *const GetAnsiText() const
  130.                                { return _string ? _string : ""; }
  131.         WBool                     SetAnsiText( const WAnsiChar *text,
  132.                                                WBool makeCopy=TRUE );
  133.  
  134.         const WUnicodeChar *const GetUnicodeText() const;
  135.         WBool                     SetUnicodeText( const WUnicodeChar *text,
  136.                                                   WBool makeCopy=TRUE );
  137.         // Type
  138.         //
  139.         //    Sets/gets the primary type of a string.
  140.  
  141.         WStringType GetType() const { return WAnsiString; }
  142.         WBool       SetType( WStringType ) const { return FALSE; }
  143.  
  144.         // ULong
  145.         //
  146.         //    Convert the string to a WULong, optionally setting an error
  147.         //    flag.
  148.  
  149.         WULong GetULong( WBool *ok=NULL ) const;
  150.  
  151.         /**********************************************************
  152.          * Methods
  153.          *********************************************************/
  154.  
  155.         // Chop
  156.         //
  157.         //    If given a non-negative value, removes all the characters
  158.         //    up to but not including charPos.  If given a negative value,
  159.         //    removes the last charPos*-1 characters.
  160.  
  161.         WBool Chop( WLong charPos );
  162.  
  163.         // Clear
  164.         //
  165.         //    Frees the string, leaving a null string.
  166.  
  167.         void Clear();
  168.  
  169.         // Compare
  170.  
  171.         static int Compare( const WSimpleString & a, const WSimpleString & b,
  172.                             WBool caseSensitive=TRUE, WULong numChars=0 );
  173.         static int Compare( const WSimpleString & a, const WAnsiChar *b );
  174.         static int Compare( const WSimpleString & a, const WUnicodeChar *b );
  175.         static int Compare( const WAnsiChar *a, const WSimpleString & b )
  176.             { return -Compare( b, a ); }
  177.         static int Compare( const WUnicodeChar *a, const WSimpleString & b )
  178.             { return -Compare( b, a ); }
  179.  
  180.         // Create
  181.         //
  182.         //    Various constructors for building a new string.  First
  183.         //    frees the old string.  
  184.  
  185.         WBool Create() { Clear(); return TRUE; }
  186.         WBool Create( const WResourceID & id, WModuleHandle module=_ApplicationModule );
  187.         WBool Create( const WMessageID & id, WModuleHandle module=_ApplicationModule );
  188.         WBool Create( const WString & s, WBool makeCopy=FALSE );
  189.         WBool Create( const WSimpleString & s, WBool makeCopy=FALSE );
  190.  
  191.         WBool CreateAnsi( const WAnsiChar *string,
  192.                           WULong numChars=USE_STR_LEN );
  193.         WBool CreateUnicode( const WUnicodeChar *string,
  194.                              WULong numChars=USE_STR_LEN );
  195.  
  196.         #ifdef _UNICODE
  197.         WBool Create( const WChar *string, WULong numChars=USE_STR_LEN )
  198.             { return CreateUnicode( string, numChars ); }
  199.         #else
  200.         WBool Create( const WChar *string, WULong numChars=USE_STR_LEN )
  201.             { return CreateAnsi( string, numChars ); }
  202.         #endif
  203.  
  204.         // Concat
  205.         //
  206.         //    Concatenate a string onto another.
  207.  
  208.         WBool Concat( const WSimpleString & suffix );
  209.         WBool Concat( const WAnsiChar *string );
  210.         WBool Concat( const WUnicodeChar *string );
  211.         WBool Concat( WWidestChar singleChar );
  212.  
  213.         // Concatf
  214.  
  215.         WBool Concatf( const WAnsiChar *parms, ... );
  216.         WBool Concatf( const WUnicodeChar *parms, ... );
  217.  
  218.         // ConvertToInteger
  219.  
  220.         WInt ConvertToInteger() const;
  221.  
  222.         // ConvertToLong
  223.  
  224.         WLong ConvertToLong() const;
  225.  
  226.         // Format
  227.         //
  228.         //    Formats a message.  
  229.         //    Returns number of characters
  230.         //    (not bytes) copied into the string.
  231.  
  232.         WULong Format( const WChar *format, ... );
  233.         WULong Format( WULong messageID, WULong langID, ... );
  234.         WULong Format( const WModule *module, WULong messageID,
  235.                        WULong langID, ... );
  236.  
  237.         // FormatEx
  238.         //
  239.         //    Formats a message, but with more options.
  240.  
  241.         #define WSTRF_NOFORMAT   0x0200
  242.         #define WSTRF_FROMSTRING 0x0400
  243.         #define WSTRF_FROMMODULE 0x0800
  244.         #define WSTRF_FROMSYSTEM 0x1000
  245.         #define WSTRF_USEARRAY   0x2000
  246.  
  247.         WULong FormatEx( WULong flags, const WChar *format,
  248.                          WByte maxWidth, ... );
  249.         WULong FormatEx( WULong flags, const WModule *module,
  250.                          WULong messageID, WULong langID,
  251.                          WByte maxWidth, ... );
  252.  
  253.         // FormatV
  254.         //
  255.         //    The basic form of Format/FormatEx.
  256.  
  257.         WULong FormatV( WULong flags, const WChar *format,
  258.                         WByte maxWidth, va_list args );
  259.         WULong FormatV( WULong flags, const WModule *module,
  260.                         WULong messageID, WULong langID,
  261.                         WByte maxWidth, va_list args );
  262.  
  263.         // Left
  264.         //
  265.         //    Returns the n leftmost characters of a string.  If string
  266.         //    is smaller than n, just returns the string.
  267.  
  268.         WSimpleString Left( WULong numChars ) const;
  269.  
  270.         // Lock
  271.         //
  272.         //    Call this if you want to work directly on the buffer
  273.         //    inside the WString.  It will ensure that no one else
  274.         //    is referencing it, that it has a minimum size (in bytes),
  275.         //    return its pointer.  Call Unlock when you're done.
  276.  
  277.         WChar *Lock( WULong minimumSize=0, WStringType type=WDefaultString );
  278.  
  279.         // Parse
  280.         //
  281.         //    Parse a string into an array of substrings.  The original
  282.         //    string is left unchanged.  The delimeterList specifies
  283.         //    the list of characters that delimit the substrings
  284.         //    (default is whitespace).  If quoteList is non-NULL,
  285.         //    it consists of pairs of begin-end characters which are
  286.         //    used to declare a substring with delimiter characters
  287.         //    embedded in it.  If ignoreMultiple is TRUE, multiple
  288.         //    delimiter characters (such as multiple spaces) are
  289.         //    ignored.  You can specify an index to start and end at.
  290.         //    Finally, if allowEscapes is TRUE, you can use a backslash
  291.         //    within a quoted string as an escape character to escape
  292.         //    either delimiter character or a backslash itself.
  293.  
  294.         WSimpleStringArray Parse( const WChar *delimiterList=NULL,
  295.                             WBool ignoreMultiples=TRUE,
  296.                             const WChar *quoteList=WSTRING_DEFAULT_QUOTELIST,
  297.                             WBool stripQuotes=TRUE,
  298.                             WULong startAt=0,
  299.                             WULong endAt=USE_STR_LEN,
  300.                             WBool allowEscapes=TRUE ) const;
  301.  
  302.         // Position
  303.         //
  304.         //    Search for a given character or substring in the string
  305.         //    and return its position. NOT_FOUND is returned if not
  306.         //    found.
  307.  
  308.         WULong Position( const WWidestChar character, WULong startAt=0,
  309.                          WBool ignoreCase=FALSE ) const;
  310.         WULong Position( const WChar *substring, WULong startAt=0,
  311.                          WBool ignoreCase=FALSE ) const;
  312.         WULong Position( const WSimpleString & substring, WULong startAt=0,
  313.                          WBool ignoreCase=FALSE ) const;
  314.  
  315.         // Right
  316.         //
  317.         //    Return the n rightmost characters of a string.  If string
  318.         //    is smaller than n, just returns the string.
  319.  
  320.         WSimpleString Right( WULong numChars ) const;
  321.  
  322.         // Sprintf
  323.         //
  324.         //    Calls with the C function sprintf functionality.
  325.  
  326.         WULong Sprintf( const WAnsiChar *parms, ... );
  327.         WULong Sprintf( const WUnicodeChar *parms, ... );
  328.  
  329.         // Strip
  330.         //
  331.         //    Remove spaces from one or both ends.
  332.  
  333.         WSimpleString Strip( WBool fromBeg=TRUE, WBool fromEnd=TRUE ) const;
  334.  
  335.         // SubString
  336.         //
  337.         //    Returns the substring starting at position start and
  338.         //    of length n.  Start is 0-based.  If n is not specified,
  339.         //    the rest of the string is returned.
  340.  
  341.         WSimpleString Substring( WULong startAt, WULong numChars=USE_STR_LEN ) const;
  342.  
  343.         // ToLowercase
  344.         //
  345.         //    Convert the string (in-place) to lowercase.
  346.  
  347.         WBool ToLowercase();
  348.  
  349.         // ToUppercase
  350.         //
  351.         //    Convert the string (in-place) to uppercase.
  352.  
  353.         WBool ToUppercase();
  354.  
  355.         // Trim
  356.         //
  357.         //    Strip the string in place.
  358.  
  359.         WBool Trim( WBool fromBeg=TRUE, WBool fromEnd=TRUE )
  360.             { return Create( Strip( fromBeg, fromEnd ) ); }
  361.  
  362.         // Truncate
  363.         //
  364.         //    Truncate the string to the given length.
  365.  
  366.         WBool Truncate( WULong length );
  367.  
  368.         // Unlock
  369.         //
  370.         //    Call this when done writing directly to the buffer.
  371.         //    The string then recalculates its size and length.
  372.  
  373.         WBool Unlock();
  374.  
  375.         /*********************************************************
  376.          * Operators
  377.          *********************************************************/
  378.  
  379.         //
  380.         // [] operator -- Note that a WWidestChar is always used,
  381.         //                not a WChar, to ensure that the result
  382.         //                can handle the largest type of char.
  383.  
  384.         const WWidestChar operator[]( int index ) const {
  385.             return GetCharacter( index );
  386.         }
  387.  
  388.         //
  389.         // casting operators
  390.         //
  391.  
  392.         operator const WAnsiChar*() const { return GetAnsiText(); }
  393.         operator const WUnicodeChar*() const { return GetUnicodeText(); }
  394.  
  395.         //
  396.         // = operator
  397.         //
  398.  
  399.         WSimpleString & operator=( const WAnsiChar *s ) { CreateAnsi( s ); return *this; }
  400.         WSimpleString & operator=( const WUnicodeChar *s ) { CreateUnicode( s ); return *this; }
  401.         WSimpleString & operator=( const WSimpleString & s ) { Create( s ); return *this; }
  402.         WSimpleString & operator=( const WString & s ) { Create( s ); return *this; }
  403.  
  404.         //
  405.         // == operator
  406.         //
  407.  
  408.         friend int operator==( const WSimpleString & a, const WSimpleString & b )
  409.             { return( WSimpleString::Compare( a, b ) == 0 ); }
  410.         friend int operator==( const WSimpleString & a, const WAnsiChar * b )
  411.             { return( WSimpleString::Compare( a, b ) == 0 ); }
  412.         friend int operator==( const WSimpleString & a, WAnsiChar * b )
  413.             { return( WSimpleString::Compare( a, b ) == 0 ); }
  414.         friend int operator==( const WSimpleString & a, const WUnicodeChar * b )
  415.             { return( WSimpleString::Compare( a, b ) == 0 ); }
  416.         friend int operator==( const WSimpleString & a, WUnicodeChar * b )
  417.             { return( WSimpleString::Compare( a, b ) == 0 ); }
  418.         friend int operator==( const WAnsiChar * a, const WSimpleString & b )
  419.             { return( WSimpleString::Compare( b, a ) == 0 ); }
  420.         friend int operator==( WAnsiChar * a, const WSimpleString & b )
  421.             { return( WSimpleString::Compare( b, a ) == 0 ); }
  422.         friend int operator==( const WUnicodeChar * a, const WSimpleString & b )
  423.             { return( WSimpleString::Compare( b, a ) == 0 ); }
  424.         friend int operator==( WUnicodeChar * a, const WSimpleString & b )
  425.             { return( WSimpleString::Compare( b, a ) == 0 ); }
  426.             
  427.         //
  428.         // != operator
  429.         //
  430.  
  431.         friend int operator!=( const WSimpleString & a, const WSimpleString & b )
  432.             { return( WSimpleString::Compare( a, b ) != 0 ); }
  433.         friend int operator!=( const WSimpleString & a, const WAnsiChar * b )
  434.             { return( WSimpleString::Compare( a, b ) != 0 ); }
  435.         friend int operator!=( const WSimpleString & a, WAnsiChar * b )
  436.             { return( WSimpleString::Compare( a, b ) != 0 ); }
  437.         friend int operator!=( const WSimpleString & a, const WUnicodeChar * b )
  438.             { return( WSimpleString::Compare( a, b ) != 0 ); }
  439.         friend int operator!=( const WSimpleString & a, WUnicodeChar * b )
  440.             { return( WSimpleString::Compare( a, b ) != 0 ); }
  441.         friend int operator!=( const WAnsiChar * a, const WSimpleString & b )
  442.             { return( WSimpleString::Compare( b, a ) != 0 ); }
  443.         friend int operator!=( WAnsiChar * a, const WSimpleString & b )
  444.             { return( WSimpleString::Compare( b, a ) != 0 ); }
  445.         friend int operator!=( const WUnicodeChar * a, const WSimpleString & b )
  446.             { return( WSimpleString::Compare( b, a ) != 0 ); }
  447.         friend int operator!=( WUnicodeChar * a, const WSimpleString & b )
  448.             { return( WSimpleString::Compare( b, a ) != 0 ); }
  449.  
  450.     protected:
  451.  
  452.         WStringHeader *GetHeader() const {
  453.             return ((WStringHeader *)_string) - 1;
  454.         }
  455.  
  456.         WStringData *GetData() const {
  457.             return (WStringData *) GetHeader(); 
  458.         }
  459.  
  460.         WBool        CopyOnWrite();
  461.  
  462.         WStringData *GrowTo( WULong size );
  463.  
  464.         WULong       SearchFor( const WChar *buf, WULong size, WULong at,
  465.                                 WBool ignoreCase ) const;
  466.  
  467.     protected:
  468.  
  469.         WChar *_string;
  470. };
  471.  
  472. #ifndef _WNO_PRAGMA_PUSH
  473. #pragma enum pop;
  474. #pragma pack(pop);
  475. #endif
  476.  
  477. #endif // _WSTRING_HPP_INCLUDED
  478.