Bug 798885 - Accented character in folder name on Account Export (bis)

Pass a boost::filesystem's c_str() rv to the ofstream constructor to
keep libstdc++ from transcoding it back to UTF8 and creating a broken
name or failing to match the directory name. Implemented in
gnc-filepath-utils to avoid spreading the boost::filesystem dependency
throughout the code base.

See https://github.com/boostorg/filesystem/issues/181 for why other
approaches don't work.
This commit is contained in:
John Ralls 2023-05-07 15:19:27 -07:00
parent 73337cff5a
commit d696f0cfcb
5 changed files with 31 additions and 6 deletions

View File

@ -32,6 +32,7 @@
#include "gnucash-commands.hpp"
#include "gnucash-core-app.hpp"
#include <gnc-filepath-utils.h>
#include <gnc-engine-guile.h>
#include <gnc-prefs.h>
#include <gnc-prefs-utils.h>
@ -107,7 +108,7 @@ static inline void
write_report_file (const char *html, const char* file)
{
if (!file || !html || !*html) return;
std::ofstream ofs{file};
auto ofs{gnc_open_filestream(file)};
if (!ofs)
{
std::cerr << "Failed to open file " << file << " for writing\n";

View File

@ -32,6 +32,7 @@
#include <string>
#include <unordered_set>
#include <gnc-filepath-utils.h>
#include "gnc-commodity.h"
#include "gnc-ui-util.h"
#include "Query.h"
@ -350,9 +351,6 @@ void csv_transactions_export (CsvExportInfo *info)
ENTER("");
DEBUG("File name is : %s", info->file_name);
/* Open File for writing */
auto ss{std::ofstream (info->file_name, std::ofstream::out)};
StringVec headers;
bool num_action = qof_book_use_split_action_for_num_field (gnc_get_current_book());
@ -398,6 +396,7 @@ void csv_transactions_export (CsvExportInfo *info)
};
/* Write header line */
auto ss{gnc_open_filestream(info->file_name)};
info->failed = !gnc_csv_add_line (ss, headers, info->use_quotes, info->separator_str);
/* Go through list of accounts */

View File

@ -32,6 +32,7 @@
#include <vector>
#include <algorithm>
#include <gnc-filepath-utils.h>
#include "gnc-commodity.h"
#include "gnc-ui-util.h"
#include "csv-tree-export.h"
@ -52,7 +53,7 @@ csv_tree_export (CsvExportInfo *info)
DEBUG("File name is : %s", info->file_name);
/* Open File for writing */
auto ss{std::ofstream (info->file_name, std::ofstream::out)};
auto ss{gnc_open_filestream(info->file_name)};
/* Header string */
StringVec headervec = {

View File

@ -1347,4 +1347,11 @@ gboolean gnc_filename_is_datafile (const char *filename)
std::regex_match (filename, datafile_regex);
}
std::ofstream
gnc_open_filestream(const char* path)
{
bfs::path bfs_path(path, cvt);
bfs_path.imbue(bfs_locale);
return std::ofstream(bfs_path.c_str());
}
/* =============================== END OF FILE ========================== */

View File

@ -29,6 +29,8 @@
#ifndef GNC_FILEPATH_UTILS_H
#define GNC_FILEPATH_UTILS_H
#include <glib-2.0/glib.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -199,7 +201,22 @@ gboolean gnc_filename_is_backup (const char *filename);
gboolean gnc_filename_is_datafile (const char *filename);
#ifdef __cplusplus
}
} //extern "C"
#include <fstream>
/** Open std::ofstream from a UTF-8 encoded path. This is harder than
* it should because std::ofstream's constructor needs to be tricked
* into taking a wchar_t filename: Simply converting path to a
* wchar_t* with g_utf8_to_utf16() wouldn't compile. The workaround
* came from https://github.com/boostorg/filesystem/issues/181. As
* noted there passing the boost path directly to
* boost::filesystem::fstream doesn't work either.
* @param path UTF-8 path to the file
* @return a std::ofstream on the stack.
*/
std::ofstream gnc_open_filestream(const char *path);
#endif
#endif /* GNC_FILEPATH_UTILS_H */