Re: [gtkmm] Problem with std::string -> Glib::ustring conversion



On Sunday 14 September 2003 3:35 am, Brian Gartner wrote:
> Hello,
>
> I'm writing an application that, during initialization, must read strings
> from a binary data file which includes non-ascii characters such as the
> unicode character U00C6 (the combined "AE" character). If I read into a
> std::string and then send the string to stdout, the character prints
> correctly on an xterm, so I know that it's being read properly. All my
> attempts to convert such a std::string to a Glib::ustring (either through
> simple assignment or calls to Glib::locale_to_utf8) result in the program
> crashing and returning an error value of 6. I would sincerely appreciate
> any suggestions for remedying this problem.

I am a bit confused, but if by a "binary data file", you mean a text file 
containing non-ASCII characters, say with UTF-8 encoding (a somewhat 
eccentric designation, so people will quickly misunderstand you), then 
std::string will handle it fine.  In fact, std::string will handle anything, 
binary or not, since it has no special characters such as null - null is just 
another character and not an end-of-string marker.

Furthermore, if your input data comprises a text file in UTF-8 encoding, then 
you can assign the data directly from a std::string object to a Glib::ustring 
object (Glib::ustring has an assignment operator taking std::string, and a 
constructor taking a std::string).  No conversion will be made, and none will 
be necessary if the source data is in UTF-8 encoding.  If the text is already 
in UTF-8 format, applying Glib::locale_to_utf8 will either do nothing (if 
your locale is UTF-8), or do the wrong thing (if your locale is not UTF-8) by 
trying to convert text it believes to be in your locale's encoding to UTF-8.

If your data truly is binary data that complies with no recognised character 
encoding, then you will certainly confuse Glib::ustring and 
Glib::locale_to_utf8(), or any other text based manipulation of the data.

First try catching any exceptions coming from the code concerned and see what 
that reveals. Glib::locale_to_utf8() throws a Glib::ConvertError exception if 
it receives invalid text, and perhaps Glib::ustring does too, so that may be 
causing the program to terminate.  Otherwise, you can find why your program 
is crashing by using a debugger such as gdb.

Chris.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]