Move the UTF-8 string conversion helper to their respective places of use.

This commit is contained in:
Adam Reichold
2015-12-26 15:06:46 +01:00
parent 0eab0ebd94
commit 1297f4b2ec
6 changed files with 58 additions and 70 deletions

View File

@@ -25,6 +25,20 @@ using namespace rtengine;
extern "C" IptcData *iptc_data_new_from_jpeg_file (FILE* infile);
namespace
{
Glib::ustring to_utf8 (const std::string& str)
{
try {
return Glib::locale_to_utf8 (str);
} catch (Glib::Error&) {
return Glib::convert_with_fallback (str, "UTF-8", "ISO-8859-1", "?");
}
}
}
ImageMetaData* ImageMetaData::fromFile (const Glib::ustring& fname, RawMetaDataLocation* rml)
{
@@ -472,7 +486,7 @@ const procparams::IPTCPairs ImageData::getIPTCData () const
if (ds) {
iptc_dataset_get_data (ds, buffer, 2100);
std::vector<Glib::ustring> icValues;
icValues.push_back (safe_locale_to_utf8((char*)buffer));
icValues.push_back (to_utf8((char*)buffer));
iptcc[strTags[i].field] = icValues;
iptc_dataset_unref (ds);
@@ -484,7 +498,7 @@ const procparams::IPTCPairs ImageData::getIPTCData () const
while ((ds = iptc_data_get_next_dataset (iptc, ds, IPTC_RECORD_APP_2, IPTC_TAG_KEYWORDS))) {
iptc_dataset_get_data (ds, buffer, 2100);
keywords.push_back (safe_locale_to_utf8((char*)buffer));
keywords.push_back (to_utf8((char*)buffer));
}
iptcc["Keywords"] = keywords;
@@ -493,7 +507,7 @@ const procparams::IPTCPairs ImageData::getIPTCData () const
while ((ds = iptc_data_get_next_dataset (iptc, ds, IPTC_RECORD_APP_2, IPTC_TAG_SUPPL_CATEGORY))) {
iptc_dataset_get_data (ds, buffer, 2100);
suppCategories.push_back (safe_locale_to_utf8((char*)buffer));
suppCategories.push_back (to_utf8((char*)buffer));
iptc_dataset_unref (ds);
}

View File

@@ -75,6 +75,15 @@ FILE* g_fopen_withBinaryAndLock(const Glib::ustring& fname)
return f;
}
Glib::ustring to_utf8 (const std::string& str)
{
try {
return Glib::locale_to_utf8 (str);
} catch (Glib::Error&) {
return Glib::convert_with_fallback (str, "UTF-8", "ISO-8859-1", "?");
}
}
}
Glib::ustring ImageIO::errorMsg[6] = {"Success", "Cannot read file.", "Invalid header.", "Error while reading header.", "File reading error", "Image format not supported."};
@@ -105,12 +114,6 @@ void ImageIO::setMetadata (const rtexif::TagDirectory* eroot, const rtengine::pr
// store exif info
exifChange.clear();
exifChange = exif;
/*unsigned int j=0;
for (rtengine::procparams::ExifPairs::const_iterator i=exif.begin(); i!=exif.end(); i++) {
exifChange.at(j).first = i->first;
exifChange.at(j).second = i->second;
j++;
}*/
if (exifRoot != NULL) {
delete exifRoot;
@@ -138,7 +141,7 @@ void ImageIO::setMetadata (const rtexif::TagDirectory* eroot, const rtengine::pr
for (unsigned int j = 0; j < i->second.size(); j++) {
IptcDataSet * ds = iptc_dataset_new ();
iptc_dataset_set_tag (ds, IPTC_RECORD_APP_2, IPTC_TAG_KEYWORDS);
std::string loc = safe_locale_to_utf8(i->second.at(j));
std::string loc = to_utf8(i->second.at(j));
iptc_dataset_set_data (ds, (unsigned char*)loc.c_str(), min(static_cast<size_t>(64), loc.size()), IPTC_DONT_VALIDATE);
iptc_data_add_dataset (iptc, ds);
iptc_dataset_unref (ds);
@@ -149,7 +152,7 @@ void ImageIO::setMetadata (const rtexif::TagDirectory* eroot, const rtengine::pr
for (unsigned int j = 0; j < i->second.size(); j++) {
IptcDataSet * ds = iptc_dataset_new ();
iptc_dataset_set_tag (ds, IPTC_RECORD_APP_2, IPTC_TAG_SUPPL_CATEGORY);
std::string loc = safe_locale_to_utf8(i->second.at(j));
std::string loc = to_utf8(i->second.at(j));
iptc_dataset_set_data (ds, (unsigned char*)loc.c_str(), min(static_cast<size_t>(32), loc.size()), IPTC_DONT_VALIDATE);
iptc_data_add_dataset (iptc, ds);
iptc_dataset_unref (ds);
@@ -162,7 +165,7 @@ void ImageIO::setMetadata (const rtexif::TagDirectory* eroot, const rtengine::pr
if (i->first == strTags[j].field && !(i->second.empty())) {
IptcDataSet * ds = iptc_dataset_new ();
iptc_dataset_set_tag (ds, IPTC_RECORD_APP_2, strTags[j].tag);
std::string loc = safe_locale_to_utf8(i->second.at(0));
std::string loc = to_utf8(i->second.at(0));
iptc_dataset_set_data (ds, (unsigned char*)loc.c_str(), min(strTags[j].size, loc.size()), IPTC_DONT_VALIDATE);
iptc_data_add_dataset (iptc, ds);
iptc_dataset_unref (ds);

View File

@@ -31,55 +31,6 @@
#include <windows.h>
#endif
/*
* For an unknown reason, Glib::filename_to_utf8 doesn't work on Windows, so we're using
* Glib::filename_to_utf8 for Linux/Apple and Glib::locale_to_utf8 for Windows
*/
Glib::ustring safe_filename_to_utf8 (const std::string& src)
{
Glib::ustring utf8_str;
#ifdef WIN32
try {
utf8_str = Glib::locale_to_utf8(src);
} catch (const Glib::Error& e) {
utf8_str = Glib::convert_with_fallback(src, "UTF-8", "ISO-8859-1", "?");
}
#else
utf8_str = Glib::filename_to_utf8(src);
#endif
return utf8_str;
}
Glib::ustring safe_locale_to_utf8 (const std::string& src)
{
Glib::ustring utf8_str;
try {
utf8_str = Glib::locale_to_utf8(src);
} catch (const Glib::Error& e) {
utf8_str = Glib::convert_with_fallback(src, "UTF-8", "ISO-8859-1", "?");
}
return utf8_str;
}
std::string safe_locale_from_utf8 (const Glib::ustring& utf8_str)
{
std::string str;
try {
str = Glib::locale_from_utf8(utf8_str);
} catch (Glib::Error&) {}
return str;
}
FILE * safe_g_fopen(const Glib::ustring& src, const gchar *mode)
{
return g_fopen(src.c_str(), mode);

View File

@@ -5,10 +5,6 @@
#include <glibmm.h>
#include <giomm.h>
Glib::ustring safe_filename_to_utf8 (const std::string& src);
Glib::ustring safe_locale_to_utf8 (const std::string& src);
std::string safe_locale_from_utf8 (const Glib::ustring& utf8_str);
FILE * safe_g_fopen(const Glib::ustring& src, const gchar *mode);
bool safe_file_test (const Glib::ustring& filename, Glib::FileTest test);