Marine Library  1.0
C++ library for Linux Networking Development
Public Types | Public Member Functions | List of all members
CCharsetConvert Class Reference

Converting text between different character encodings. More...

#include <charset_convert.hh>

Public Types

enum  EMode { kNormal = 0, kTranslit, kIgnore }
 Conversion mode. More...
 

Public Member Functions

 CCharsetConvert ()
 Construct a default object, with no ability for conversion. More...
 
 ~CCharsetConvert ()
 Destructor for releasing resources.
 
 CCharsetConvert (const std::string &fromCode, const std::string &toCode, EMode mode=kNormal)
 Construct a usable converter. More...
 
bool init (const std::string &fromCode, const std::string &toCode, EMode mode=kNormal)
 Initialize a usable converter. More...
 
bool valid () const
 Test whether this object is initialized. More...
 
const std::string fromCode () const
 Get encoding name for source text. More...
 
const std::string toCode () const
 Get encoding name for destination text. More...
 
EMode mode () const
 Get conversion mode. More...
 
bool convert (const std::string &source, std::string &dest)
 Convert text from source encoding to destination encoding. More...
 
std::string toString () const
 Get a readable description of this object. More...
 

Detailed Description

Converting text between different character encodings.

CCharsetConvert uses iconv(3) to perform charset conversion operations. It manages system resource allocation and deallocation, conversion adjustment and error handling.

Note
CCharsetConvert object can NOT be copied.

Member Enumeration Documentation

Conversion mode.

Enumerator
kNormal 

Default mode.

kTranslit 

Characters being converted are transliterated when needed and possible. This means that when a character cannot be represented in the target character set, it can be approximated through one or several similar looking characters. Characters that are outside of the target character set and cannot be transliterated are replaced with a question mark (?) in the output.

kIgnore 

Characters that cannot be converted are discarded and an error is printed after conversion.

Constructor & Destructor Documentation

CCharsetConvert::CCharsetConvert ( )
inline

Construct a default object, with no ability for conversion.

Please use init to initialize this object for use.

See also
init
CCharsetConvert::CCharsetConvert ( const std::string &  fromCode,
const std::string &  toCode,
EMode  mode = kNormal 
)
inline

Construct a usable converter.

For a full encoding names list supported by your system, please run iconv --list.

Parameters
fromCodeEncoding name for source text, e.g. "UTF8"
toCodeEncoding name for destination text, e.g. "GB2312"
modeConversion mode, see EMode

Member Function Documentation

bool CCharsetConvert::convert ( const std::string &  source,
std::string &  dest 
)
inline

Convert text from source encoding to destination encoding.

If dest is not empty, the conversion result will append to dest.

Parameters
[in]sourceSource text
[out]destA String to receive the result of conversion
Returns
true if succeeded; otherwise false
const std::string CCharsetConvert::fromCode ( ) const
inline

Get encoding name for source text.

If this object is not initialized, an empty string will be returned.

Returns
Encoding name for source text
bool CCharsetConvert::init ( const std::string &  fromCode,
const std::string &  toCode,
EMode  mode = kNormal 
)
inline

Initialize a usable converter.

If this object is already initialized, this function will fail.
For a full encoding names list supported by your system, please run iconv --list.

Parameters
fromCodeEncoding name for source text, e.g. "UTF8"
toCodeEncoding name for destination text, e.g. "GB2312"
modeConversion mode, see EMode
Returns
true if succeeded; otherwise false
EMode CCharsetConvert::mode ( ) const
inline

Get conversion mode.

If this object is not initialized, kNormal will be returned.

Returns
Conversion mode
const std::string CCharsetConvert::toCode ( ) const
inline

Get encoding name for destination text.

If this object is not initialized, an empty string will be returned.

Returns
Encoding name for destination text
std::string CCharsetConvert::toString ( ) const
inline

Get a readable description of this object.

Returns
A readable description of this object
bool CCharsetConvert::valid ( ) const
inline

Test whether this object is initialized.

Returns
true if this object is already initialized; otherwise false

The documentation for this class was generated from the following file: