GNOME Bugzilla – Bug 615950
Mixed use of Glib::ustring and std::string for content type (and MIME type)
Last modified: 2016-12-18 11:38:17 UTC
These interfaces use Glib::ustring for content type (and MIME type): Gio::content_type_can_be_executable Gio::content_type_equals Gio::content_type_from_mime_type Gio::content_type_get_description Gio::content_type_get_icon Gio::content_type_get_mime_type Gio::content_type_guess Gio::content_type_is_a Gio::content_type_is_unknown Gio::content_type_get_registered These interface use std::string for content type: Gio::FileInfo::get_content_type Gio::FileInfo::set_content_type
Thanks. Do you have a suggestion for what should be used?
According to documentation in glib/gio, the strings in g_file_info_[get|set]_content_type() are UTF-8 strings. Thus Glib::ustring is most appropriate. See gfileinfo.h, gfileattribute.c, gioenums.h. There are many methods in Gio::FileInfo that get or set a file attribute of type G_FILE_ATTRIBUTE_TYPE_STRING (UTF-8 encoded strings). Most of them return or take as input parameter an std::string. They are [get|set]_attribute_string get_attribute_as_string (returns a Glib::ustring) [get|set]_display_name [get|set]_edit_name [get|set]_content_type get_etag The following methods get and set byte-strings (for instance encoding used for filenames). They ought to continue to use std::string. [get|set]_attribute_byte_string [get|set]_name [get|set]_symlink_target All these methods, and others, take a "const std::string& attribute" parameter. I don't know if const Glib::ustring& would be better. All predefined attribute keys consist of only ASCII lowercase letters, hyphens and colons. It's possible to define new attributes with the set_attribute_* methods. I think they accept any nul-terminated byte string as attribute key, without making any assumption about a particular character encoding.
(In reply to comment #2) > According to documentation in glib/gio, the strings in > g_file_info_[get|set]_content_type() are UTF-8 strings. What documentation? URL, please. > Thus Glib::ustring is most appropriate. > See gfileinfo.h, gfileattribute.c, gioenums.h. > > There are many methods in Gio::FileInfo that get or set a file attribute > of type G_FILE_ATTRIBUTE_TYPE_STRING (UTF-8 encoded strings). Yes: http://library.gnome.org/devel/gio/stable/gio-GFileAttribute.html#GFileAttributeType > Most of them > return or take as input parameter an std::string. They are > > [get|set]_attribute_string > get_attribute_as_string (returns a Glib::ustring) > [get|set]_display_name > [get|set]_edit_name > [get|set]_content_type > get_etag > > The following methods get and set byte-strings (for instance encoding used for > filenames). They ought to continue to use std::string. > > [get|set]_attribute_byte_string > [get|set]_name > [get|set]_symlink_target > > All these methods, and others, take a "const std::string& attribute" parameter. > I don't know if const Glib::ustring& would be better. > All predefined attribute keys consist of only ASCII lowercase letters, hyphens > and colons. It's possible to define new attributes with the set_attribute_* > methods. I think they accept any nul-terminated byte string as attribute key, > without making any assumption about a particular character encoding.
(In reply to comment #3) > (In reply to comment #2) > > According to documentation in glib/gio, the strings in > > g_file_info_[get|set]_content_type() are UTF-8 strings. > > What documentation? URL, please. g_file_info_set_content_type() is described at http://library.gnome.org/devel/gio/stable/GFileInfo.html#g-file-info-set-content-type with a link to the description of G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE at http://library.gnome.org/devel/gio/stable/GFileInfo.html#G-FILE-ATTRIBUTE-STANDARD-CONTENT-TYPE:CAPS which is of type G_FILE_ATTRIBUTE_TYPE_STRING, described at http://library.gnome.org/devel/gio/stable/gio-GFileAttribute.html#G-FILE-ATTRIBUTE-TYPE-STRING:CAPS G_FILE_ATTRIBUTE_TYPE_STRING a null terminated UTF8 string. G_FILE_ATTRIBUTE_TYPE_BYTE_STRING a zero terminated string of non-zero bytes. Similar links can be followed starting from the other g_file_info_set_xxx() functions.
Thanks. For Gio::FileInfo, I have added TODOs, so we can fix that when we can break ABI one day: http://git.gnome.org/browse/glibmm/commit/?id=158d2cb186fda90faa097ffa4e401c199c44660b For the Gio::content_type_*() API, I still need documentation for the C API saying that it is UTF-8. At the moment I don't see that. For instance: http://library.gnome.org/devel/gio/unstable/gio-GContentType.html#g-content-type-can-be-executable
(In reply to comment #5) > For Gio::FileInfo, I have added TODOs, so we can fix that when we can > break ABI one day: I noticed that you did not add TODOs at [get|set]_attribute_string. Did you forget, or is it because the C API documentation is not as clear as for the other functions? It seems to be implicit that "string" means UTF-8 string, "byte string" means a string with unspecified character encoding. From the source code it's clear that the return value of g_file_info_get_attribute_string() and the parameter 'value' in g_file_info_set_attribute_string() are of type G_FILE_ATTRIBUTE_TYPE_STRING. > For the Gio::content_type_*() API, I still need documentation for the C API > saying that it is UTF-8. At the moment I don't see that. I haven't found any unambiguous documentation for g_content_type_*(). Perhaps it's implicit here too that "string" means UTF-8 string, but I can't find a statement to that effect.
(In reply to comment #6) > From the source code it's clear that the return value of > g_file_info_get_attribute_string() and the parameter 'value' in > g_file_info_set_attribute_string() are of type G_FILE_ATTRIBUTE_TYPE_STRING. Yes, I agree. So I improved the C documentation (they let me make simple fixes): http://git.gnome.org/browse/glib/commit/?id=c1a75ca783f602d3edf465c28918dac7ea57a1e7 And I have added the TODOs for glibmm: http://git.gnome.org/browse/glibmm/commit/?id=4c4096d2901c4cbdbffac472fb032ad956f53fa8
glibmm-2.52: I've changed to Glib::ustring in the Gio::FileInfo methods listed in comment 2. I've also changed Gio::File::set_attribute_string() Gio::FileIOStream::get_etag() Gio::FileOutputStream::get_etag()