由「 can i get a char* , please?"看起:c++
Just barely. OO.o has at least six string wrappers, although the C implementations are of little interest:app
rtl_String
— sal/inc/rtl/string.h
"Normal" string plus reference counting. rtlstring-
>buffer
is useful, as is rtlstring-
>length
. This object encapsulates an generic 8bit string - of unknown encoding. Feel free to treat rtlstring-
>buffer
as your beloved char *
. If you really want to look at the implementation of some rtl_String
function and lxr nor grep can help you, have a look at sal/rtl/source/strtmpl.c.ide
OString
— sal/inc/rtl/string.hxx
Simply a rtl_String wrapped inside a class
; you can use ostring.pData
to get at the rtl_String (it's public). OString
has reasonably useful methods for if you need them.函數
rtl_uString
— sal/inc/rtl/ustring.h
"Normal" Unicode string, similar to rtl_String, and refcounted as well. However, this one always comes in UCS-2 encoding, presumably to be compatible with Java's questionable choices. See rtl_String
above to find where the implementation of some rtl_uString
functions is hidden.ui
OUString
— sal/inc/rtl/ustring.hxx
An rtl_uString wrapped inside a class
. This is what most of the OO.o code uses to pass strings around. To convert an OString
to an OUString
it is necessary to specify the character set of the OString
see; sal/inc/rtl/textenc.h
— the only interesting case is RTL_TEXTENCODING_UTF8
this
String
— tools/inc/string.hxx
This is an obsolete string class, aliased to 'UniString'. It has a number of limitations such as a 64k length limit. You can have the buffer with GetBuffer()
, but it's Utf-16 encoded.spa
A couple of conversion functions are really useful here, particularly:rtl::OString aOString = ::rtl::OUStringToOString (aOUString, RTL_TEXTENCODING_UTF8);
And the reverse:rtl::OUString aOUString = ::rtl::OStringToOUString (aOString, RTL_TEXTENCODING_UTF8);
debug
If you just want to programattically print out a string for debugging purposes you probably want define a macro like :#define CHAR_POINTER(THE_OUSTRING) ::rtl::OUStringToOString (THE_OUSTRING, RTL_TEXTENCODING_UTF8).pData->buffer
and use it like :printf ( "SvXMLNamespaceMap::AddIfKnown : %s / %s\n", CHAR_POINTER(rPrefix), CHAR_POINTER(rName) );
.
For the obsolete String class, aliased UniString, it's like :printf ( "rGrfName : %s\n", ByteString( rGrfName, RTL_TEXTENCODING_UTF8).GetBuffer() );
rest
To print the value of rtl::OUString directly in the debugger, you can use dbg_dump()
. It is intended to be called interactively from the debugger, in both debug and non-debug builds of OOo. You can see the definition in sal/rtl/source/debugprint.cxx.code
Some code snippets about manipulating those objects can be found on the codesnippets service page : [1]
原文連接:https://wiki.openoffice.org/wiki/Hacking#Can_I_get_a_char_.2A.2C_please.3F
繼續看sal模塊源碼,摘錄:
/********************************************************************************/
/* Data types
*/
/* Boolean */
typedef unsigned char sal_Bool;
# define sal_False ((sal_Bool)0)
# define sal_True ((sal_Bool)1)
/* char is assumed to always be 1 byte long */
typedef signed char sal_Int8;
typedef unsigned char sal_uInt8;
#if SAL_TYPES_SIZEOFSHORT == 2
typedef signed short sal_Int16;
typedef unsigned short sal_uInt16;//2字節
#else
#error "Could not find 16-bit type, add support for your architecture"
#endif
typedef char sal_Char;//1字節
#if ( defined(SAL_W32) && !defined(__MINGW32__) )
typedef wchar_t sal_Unicode;
#else
#define SAL_UNICODE_NOTEQUAL_WCHAR_T
typedef sal_uInt16 sal_Unicode;//2字節
#endif
「Normal」 string, 8-bit string, unknown encoding 字符串操做c函數
/** The implementation of a byte string.
@internal
*/
typedef struct _rtl_String
{
oslInterlockedCount refCount; /* opaque */
sal_Int32 length;
sal_Char buffer[1];
} rtl_String;//1字節,8bit string
rtlstring wrapped inside a class openoffice的字符串類OString
class OString
{
public:
/** @internal */
rtl_String * pData;
usc-2 encoding 一組對unicode字符串操做的c函數
/** The implementation of a Unicode string.
@internal
*/
typedef struct _rtl_uString
{
oslInterlockedCount refCount; /* opaque */
sal_Int32 length;
sal_Unicode buffer[1];
} rtl_uString;
void SAL_CALL rtl_string2UString( rtl_uString ** newStr, const sal_Char * str, sal_Int32 len, rtl_TextEncoding encoding, sal_uInt32 convertFlags ) SAL_THROW_EXTERN_C();
rtl_ustring wrapped in a class openoffice的OUString類,封裝rtl_ustring的c++類
#include <rtl/ustring.h> //rtl_uString
#include <rtl/string.hxx>// OString
class OUString
{
public:
/** @internal */
rtl_uString * pData;
OUString( const sal_Char * value, sal_Int32 length,
rtl_TextEncoding encoding,
sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
{
pData = 0;
rtl_string2UString( &pData, value, length, encoding, convertFlags );
#if defined EXCEPTIONS_OFF
OSL_ASSERT(pData != NULL);
#else
if (pData == 0) {
throw std::bad_alloc();
}
#endif
}
inline OUString OStringToOUString( const OString & rStr,
rtl_TextEncoding encoding,
sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
{
return OUString( rStr.getStr(), rStr.getLength(), encoding, convertFlags );
}
inline OString OUStringToOString( const OUString & rUnicode,
rtl_TextEncoding encoding,
sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
{
return OString( rUnicode.getStr(), rUnicode.getLength(), encoding, convertFlags );
}