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);

View File

@ -19,6 +19,7 @@
#include "extprog.h"
#include <cstring>
#include <iostream>
#ifdef WIN32
#include <windows.h>

View File

@ -66,6 +66,29 @@ Glib::ustring argv1;
bool simpleEditor;
Glib::Thread* mainThread;
namespace
{
// For an unknown reason, Glib::filename_to_utf8 doesn't work on reliably Windows,
// so we're using Glib::filename_to_utf8 for Linux/Apple and Glib::locale_to_utf8 for Windows.
Glib::ustring fname_to_utf8 (const char* fname)
{
#ifdef WIN32
try {
return Glib::locale_to_utf8 (fname);
} catch (Glib::Error&) {
return Glib::convert_with_fallback (fname, "UTF-8", "ISO-8859-1", "?");
}
#else
return Glib::filename_to_utf8 (fname);
#endif
}
}
// This recursive mutex will be used by gdk_threads_enter/leave instead of a simple mutex
#ifdef WIN32
@ -170,7 +193,7 @@ int main(int argc, char **argv)
bool consoleOpened = false;
if (argc > 1 || options.rtSettings.verbose) {
if(options.rtSettings.verbose || ( !safe_file_test( safe_filename_to_utf8(argv[1]), Glib::FILE_TEST_EXISTS ) && !safe_file_test( safe_filename_to_utf8(argv[1]), Glib::FILE_TEST_IS_DIR ))) {
if (options.rtSettings.verbose || ( !Glib::file_test (fname_to_utf8 (argv[1]), Glib::FILE_TEST_EXISTS ) && !Glib::file_test (fname_to_utf8 (argv[1]), Glib::FILE_TEST_IS_DIR))) {
bool stdoutRedirectedtoFile = (GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)) == 0x0001);
bool stderrRedirectedtoFile = (GetFileType(GetStdHandle(STD_ERROR_HANDLE)) == 0x0001);
@ -395,7 +418,7 @@ int processLineParams( int argc, char **argv )
case 'o': // outputfile or dir
if( iArg + 1 < argc ) {
iArg++;
outputPath = safe_filename_to_utf8 (argv[iArg]);
outputPath = fname_to_utf8 (argv[iArg]);
if( safe_file_test (outputPath, Glib::FILE_TEST_IS_DIR)) {
outputDirectory = true;
@ -409,7 +432,7 @@ int processLineParams( int argc, char **argv )
// RT stop if any of them can't be loaded for any reason.
if( iArg + 1 < argc ) {
iArg++;
Glib::ustring fname = safe_filename_to_utf8 ( argv[iArg] );
Glib::ustring fname = fname_to_utf8 (argv[iArg]);
if (fname.at(0) == '-') {
std::cerr << "Error: filename missing next to the -p switch" << std::endl;
@ -500,7 +523,7 @@ int processLineParams( int argc, char **argv )
while (iArg + 1 < argc) {
iArg++;
const auto argument = safe_filename_to_utf8 (argv[iArg]);
const auto argument = fname_to_utf8 (argv[iArg]);
if (Glib::file_test (argument, Glib::FILE_TEST_IS_REGULAR)) {
inputFiles.emplace_back (argument);
@ -615,7 +638,7 @@ int processLineParams( int argc, char **argv )
}
}
} else {
argv1 = safe_filename_to_utf8 ( argv[iArg] );
argv1 = fname_to_utf8 (argv[iArg]);
if( outputDirectory ) {
options.savePathFolder = outputPath;