openoffice osl模块学习1
由“ can i get a char* , please?"看起:
Just barely. OO.o has at least six string wrappers, although the C implementations are of little interest:
-
rtl_String
— sal/inc/rtl/string.h
"Normal" string plus reference counting.rtlstring-
>buffer
is useful, as isrtlstring-
>length
. This object encapsulates an generic 8bit string - of unknown encoding. Feel free to treatrtlstring-
>buffer
as your belovedchar *
. If you really want to look at the implementation of somertl_String
function and lxr nor grep can help you, have a look at sal/rtl/source/strtmpl.c. -
OString
— sal/inc/rtl/string.hxx
Simply a rtl_String wrapped inside aclass
; you can useostring.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. Seertl_String
above to find where the implementation of somertl_uString
functions is hidden. -
OUString
— sal/inc/rtl/ustring.hxx
An rtl_uString wrapped inside aclass
. This is what most of the OO.o code uses to pass strings around. To convert anOString
to anOUString
it is necessary to specify the character set of theOString
see;sal/inc/rtl/textenc.h
— the only interesting case isRTL_TEXTENCODING_UTF8
-
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 withGetBuffer()
, but it's Utf-16 encoded.
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);
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() );
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.
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 );
}