libraries/ltkutils/inc/descriptorutils.h
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // descriptorutils.h
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 #ifndef FSHELL_DESCRIPTORUTILS_H
       
    13 #define FSHELL_DESCRIPTORUTILS_H
       
    14 
       
    15 #include <e32std.h>
       
    16 
       
    17 namespace LtkUtils
       
    18 	{
       
    19 	class TUtf8State;
       
    20 
       
    21 	NONSHARABLE_CLASS(RLtkBuf16) : public TDes16
       
    22 		{
       
    23 	public:
       
    24 		IMPORT_C RLtkBuf16();
       
    25 		IMPORT_C RLtkBuf16(HBufC16* aBuf); // Takes ownership
       
    26 
       
    27 		// The new, useful functions that RBuf doesn't give you
       
    28 		IMPORT_C HBufC16* ToHBuf(); // Caller is responsible for deleting the result. This function Closes the RLtkBuf.
       
    29 		IMPORT_C HBufC16* GetHBuf() const;
       
    30 		IMPORT_C void AppendL(const TDesC16& aText);
       
    31 		IMPORT_C void AppendL(const TDesC8& aText);
       
    32 		IMPORT_C void AppendL(TChar aChar);
       
    33 		IMPORT_C void AppendFormatL(TRefByValue<const TDesC16> aFmt, ...);
       
    34 		IMPORT_C void ReplaceL(TInt aPos, TInt aLength, const TDesC16 &aDes);
       
    35 		IMPORT_C void ReplaceAllL(const TDesC& aFrom, const TDesC16& aTo);
       
    36 		IMPORT_C void ReserveExtraL(TInt aExtraCapacity);
       
    37 		IMPORT_C TInt ReserveExtra(TInt aExtraCapacity);
       
    38 		IMPORT_C void CreateLC(TInt aMaxLength);
       
    39 
       
    40 		/**
       
    41 		Leaves on OOM. Converts the argument aUtf8EncodedText from UTF-8 to the native UTF-16 and appends the result to this
       
    42 		descriptor. You can handle your data in chunks by calling AppendUtf8L repeatedly. You don't have to worry about UTF-8
       
    43 		sequences being split between chunks - fragmented end sequences are cached and will be reassembled if a further call
       
    44 		to AppendUtf8L is made with the rest of the bytes. Such sequences are not considered malformed unless (a) the next call
       
    45 		to AppendUtf8L doesn't produce a valid sequence, or (b) FinalizeUtf8 is called instead of a further append. Sequences 
       
    46 		that are decided to be malformed are replaced with the unicode replacement char U+FFFD. You are free to mix calls to
       
    47 		AppendUtf8L with other normal descriptor operations such as Delete, Insert or ReAlloc. You shouldn't however copy
       
    48 		the buffer or call any other Append functions without finalizing it first. Once you have finished appending UTF-8
       
    49 		you must call FinalizeUtf8().
       
    50 		*/
       
    51 		IMPORT_C void AppendUtf8L(const TDesC8& aUtf8EncodedText);
       
    52 
       
    53 		/**
       
    54 		Like the above, but returns by reference the index of the first byte of the first malformed sequence in aUtf8EncodedText (or
       
    55 		KErrNotFound if it was totally valid). Useful if you want to stop appending the minute you see a malformed sequence
       
    56 		and thus don't want to have to wait until you call FinalizeUtf8() to get that information.
       
    57 		*/
       
    58 		IMPORT_C void AppendUtf8L(const TDesC8& aUtf8EncodedText, TInt& aFirstBadBytePosition);
       
    59 
       
    60 		/**
       
    61 		Must be called at the end of a sequence of AppendUtf8L() calls. Clears all UTF-8 related state from the descriptor.
       
    62 		Returns KErrCorrupt if any malformed sequences were encountered in any of the AppendUtf8L() calls. You are free to
       
    63 		ignore this return code if you wish: malformed sequences get replaced with U+FFFD and the rest of the string continues
       
    64 		to be parsed. Returns KErrNone if there were no problems.
       
    65 		*/
       
    66 		IMPORT_C TInt FinalizeUtf8();
       
    67 
       
    68 		/**
       
    69 		Like the above, but returns by reference the index of the first byte of the first malformed sequence found cumulatively
       
    70 		across all invocations of AppendUtf8L(). aFirstBadBytePosition is set to KErrNotFound if the data was totally valid.
       
    71 		*/
       
    72 		IMPORT_C void FinalizeUtf8(TInt& aFirstBadBytePosition);
       
    73 
       
    74 
       
    75 		/**
       
    76 		Convenience method. Equivalent to:
       
    77 		this->Zero();
       
    78 		this->AppendUtf8L(aText);
       
    79 		User::LeaveIfError(this->FinalizeUtf8());
       
    80 		*/
       
    81 		IMPORT_C void CopyFromUtf8L(const TDesC8& aUtf8EncodedText);
       
    82 
       
    83 		// The usual RBuf-like APIs (for everything else, it just inherits from TDes16)
       
    84 		IMPORT_C TInt ReAlloc(TInt aMaxLength);
       
    85 		IMPORT_C void ReAllocL(TInt aMaxLength);
       
    86 		IMPORT_C void Assign(HBufC16* aBuf);
       
    87 		IMPORT_C TInt Create(TInt aMaxLength);
       
    88 		IMPORT_C void CreateL(TInt aMaxLength);
       
    89 		IMPORT_C TInt CreateMax(TInt aMaxLength);
       
    90 		IMPORT_C void CreateMaxL(TInt aMaxLength);
       
    91 		IMPORT_C void Close();
       
    92 
       
    93 	private:
       
    94 		TUtf8State* GetUtf8State() const;
       
    95 		void ClearUtf8State();
       
    96 
       
    97 	private:
       
    98 		HBufC16* iBuf;
       
    99 		};
       
   100 
       
   101 	NONSHARABLE_CLASS(RLtkBuf8) : public TDes8
       
   102 		{
       
   103 	public:
       
   104 		IMPORT_C RLtkBuf8();
       
   105 		IMPORT_C RLtkBuf8(HBufC8* aBuf); // Takes ownership
       
   106 
       
   107 		// The new, useful functions that RBuf doesn't give you
       
   108 		IMPORT_C HBufC8* ToHBuf(); // Caller is responsible for deleting the result. This function Closes the RLtkBuf.
       
   109 		IMPORT_C HBufC8* GetHBuf() const;
       
   110 		IMPORT_C void AppendL(const TDesC8& aText);
       
   111 		IMPORT_C void AppendL(const TDesC16& aText);
       
   112 		IMPORT_C void AppendL(TChar aChar);
       
   113 		IMPORT_C void AppendFormatL(TRefByValue<const TDesC8> aFmt, ...);
       
   114 		IMPORT_C void ReplaceL(TInt aPos, TInt aLength, const TDesC8& aDes);
       
   115 		IMPORT_C void ReplaceAllL(const TDesC8& aFrom, const TDesC8& aTo);
       
   116 		IMPORT_C void ReserveExtraL(TInt aExtraCapacity);
       
   117 		IMPORT_C TInt ReserveExtra(TInt aExtraCapacity);
       
   118 		IMPORT_C void CreateLC(TInt aMaxLength);
       
   119 		IMPORT_C void CopyAsUtf8L(const TDesC16& aString);
       
   120 
       
   121 		// The usual RBuf-like APIs (for everything else, it just inherits from TDes8)
       
   122 		IMPORT_C TInt ReAlloc(TInt aMaxLength);
       
   123 		IMPORT_C void ReAllocL(TInt aMaxLength);
       
   124 		IMPORT_C void Assign(HBufC8* aBuf);
       
   125 		IMPORT_C TInt Create(TInt aMaxLength);
       
   126 		IMPORT_C void CreateL(TInt aMaxLength);
       
   127 		IMPORT_C TInt CreateMax(TInt aMaxLength);
       
   128 		IMPORT_C void CreateMaxL(TInt aMaxLength);
       
   129 		IMPORT_C void Close();
       
   130 
       
   131 	private:
       
   132 		HBufC8* iBuf;
       
   133 		};
       
   134 
       
   135 	typedef RLtkBuf16 RLtkBuf;
       
   136 
       
   137 	/* Use this class for const static arrays of descriptors. Eg:
       
   138 	 *
       
   139 	 * const SLitC KData[] = 
       
   140 	 *     {
       
   141 	 *     DESC("A string"),
       
   142 	 *     DESC("Another string"),
       
   143 	 *     };
       
   144 	 *
       
   145 	 * You can treat the array items the same as TLitCs - ie most of the time they behave as descriptors but
       
   146 	 * sometimes you may need to use operator().
       
   147 	 */
       
   148 	struct SLitC
       
   149 		{
       
   150 		inline const TDesC* operator&() const { return reinterpret_cast<const TDesC*>(this); }
       
   151 		inline operator const TDesC&() const { return *reinterpret_cast<const TDesC*>(this); }
       
   152 		inline const TDesC& operator()() const { return *reinterpret_cast<const TDesC*>(this); }
       
   153 
       
   154 		TInt iLength;
       
   155 		const TUint16* iPtr;
       
   156 		};
       
   157 	struct SLitC8
       
   158 		{
       
   159 		inline const TDesC8* operator&() const { return reinterpret_cast<const TDesC8*>(this); }
       
   160 		inline operator const TDesC8&() const { return *reinterpret_cast<const TDesC8*>(this); }
       
   161 		inline const TDesC8& operator()() const { return *reinterpret_cast<const TDesC8*>(this); }
       
   162 
       
   163 		TInt iLength;
       
   164 		const TUint8* iPtr;
       
   165 		};
       
   166 
       
   167 #define DESC(x) { ((sizeof(L##x) / 2)-1) | 0x10000000, (const TUint16*) L##x }
       
   168 #define DESC8(x) { (sizeof(x) - 1) | 0x10000000, (const TUint8*)x }
       
   169 // 0x10000000 is EPtrC<<KShiftDesType
       
   170 
       
   171 	}
       
   172 
       
   173 #endif