The OdString class (odstring.h) is the base class for string handling. It can store both a single-byte version and wide-character version — MultiByte and Unicode —which can be compared to each other. Additionally, the string data structure stores reference counts, which is important for your application’s stability.
There is one type defined for use by the OdString, which is OdChar. This internal type is wchar_t when available and unsigned short otherwise, depending on the platform. The following are constructors:
OdString(const OdString& source);
OdString(OdChar ch, int length);
OdString(const OdChar* source);
OdString(const OdChar* source, int length);
The following are additional constructors if the version of Windows allows it:
OdString(const __wchar_t* source);
OdString(const __wchar_t* source, int length);
Most important and new are the available constructors which allow specification of the code page:
OdString(const char* lpch, OdCodePageId codepage = CP_CNT);
OdString(const char* lpch, int nLength, OdCodePageId codepage = CP_CNT);
OdString(const OdAnsiString&);
The dCodePageId enumeration declares available code pages. Starting with the file format version AC21, the drawing stores text in Unicode. Before this version, strings are stored in the drawing in MBCS (multi-byte character set) coding. There are early version drawings that don't have code page information.
The expected comparison operators are available. You may find that this class should be used throughout your system, beyond the scope of .dgn or .dwg file handling.
To return the number of characters in the String object:
int getLength() const
int getLengthA() const;
To return True if the string is empty (equal to the const static OdString kEmpty):
bool isEmpty() const
Set the string to empty:
void empty();
Access an OdChar at a specified position, using the bracket operator:
OdChar operator[](int charIndex) const
Get an OdChar (a single character) at a specified position:
OdChar getAt(int charIndex) const
Set an OdChar at the specified position:
void setAt(int charIndex, OdChar ch);
Return the underlying OdChar array (buffer) of the String object:
inline operator const OdChar*() const
There are search methods, reversals, insertions and deletions, whitespace trimming, case change, copying, and substring access. You can enable the reference counting for the String objects by calling OdChar* lockBuffer() on the object and disable the reference counting using unlockBuffer().
The familiar format methods are encapsulated from standard C. You can use the printf-style format string using OdString& format(const __wchar_t* formatString, ...), or use the vsprintf-style format string and argument list using OdString& formatV(const OdChar* formatString, va_list argList). Access to a modifiable buffer for direct access is available as a Cstyle OdChar array (buffer) of a specified minimum length using the methods OdChar* getBuffer(int minBufLength) and void releaseBuffer(int newLength = –1).
Another class available for string handling is a specialized clone of the OdString class, named OdAnsiString (odansistring.h), which implements single-byte character string objects, strictly. "Clone" meaning that although OdString is not a super class and does not share the same base class, the OdAnsiString class has nearly the same methods (and operators) as the OdString class.
To convert a string between different encoding types depending on platforms (macOS, Unix, Windows), use the OdCharMapper class. For example, to get a correct string from UTF8 to Unicode, use the utf8ToUnicode() method:
OdString Convert(OdString& strSource)
{
OdArray buf;
OdCharMapper::utf8ToUnicode(strSource, 512, buf);
return OdString(buf.getPtr());
}
The previous code may have appeared:
WCHAR sText[MAX_PATH];
wsprintf(sText, L"%s", pModel->getName();
The OdString class is a more simple example:
OdString sText;
sText.format(L"%s", pModel->getName());
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.
|