Skip to content

Commit

Permalink
Move gnc_list_formatter from gnc-date to gnc-ui-util.
Browse files Browse the repository at this point in the history
It has nothing at all to do with dates.
  • Loading branch information
jralls committed Jan 7, 2024
1 parent d52d226 commit c87d480
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
31 changes: 31 additions & 0 deletions libgnucash/app-utils/gnc-ui-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <stdio.h>
#include <string.h>
#include <cinttypes>
#include <unicode/listformatter.h>

#include "qof.h"
#include "gnc-prefs.h"
Expand All @@ -62,6 +63,8 @@
#define GNC_PREF_REVERSED_ACCTS_INC_EXP "reversed-accounts-incomeexpense"
#define GNC_PREF_PRICES_FORCE_DECIMAL "force-price-decimal"

using UniStr = icu::UnicodeString;

static QofLogModule log_module = GNC_MOD_GUI;

static bool auto_decimal_enabled = false;
Expand Down Expand Up @@ -2281,3 +2284,31 @@ gnc_filter_text_for_currency_commodity (const gnc_commodity *comm,

return gnc_filter_text_for_currency_symbol (incoming_text, *symbol);
}

gchar*
gnc_list_formatter (GList *strings)
{
g_return_val_if_fail (strings, nullptr);

UErrorCode status = U_ZERO_ERROR;
auto formatter = icu::ListFormatter::createInstance(status);
std::vector<UniStr> strvec;
UniStr result;
std::string retval;

for (auto n = strings; n; n = g_list_next (n))
{
auto utf8_str{static_cast<const char*>(n->data)};
strvec.push_back (UniStr::fromUTF8(utf8_str));
}

formatter->format (strvec.data(), strvec.size(), result, status);

if (U_FAILURE(status))
PERR ("Unicode error");
else
result.toUTF8String(retval);

delete formatter;
return g_strdup (retval.c_str());
}
9 changes: 9 additions & 0 deletions libgnucash/app-utils/gnc-ui-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,15 @@ char* gnc_filter_text_for_currency_commodity (const gnc_commodity *comm,
const char* incoming_text,
const char** symbol);

/** This function takes a GList of char*, and uses locale-sensitive
* list formatter.
*
* @param strings The GList* of char*.
*
* @returns a newly allocated char*
*/
gchar* gnc_list_formatter (GList* strings);

#ifdef __cplusplus
}
#endif
Expand Down
30 changes: 0 additions & 30 deletions libgnucash/engine/gnc-date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

#include <cinttypes>
#include <unicode/calendar.h>
#include <unicode/listformatter.h>

#include "gnc-date.h"
#include "gnc-date-p.h"
Expand Down Expand Up @@ -1651,32 +1650,3 @@ gnc_date_load_funcs (void)
Testfuncs *tf = g_slice_new (Testfuncs);
return tf;
}


gchar*
gnc_list_formatter (GList *strings)
{
g_return_val_if_fail (strings, nullptr);

UErrorCode status = U_ZERO_ERROR;
auto formatter = icu::ListFormatter::createInstance(status);
std::vector<icu::UnicodeString> strvec;
icu::UnicodeString result;
std::string retval;

for (auto n = strings; n; n = g_list_next (n))
{
auto utf8_str{static_cast<const char*>(n->data)};
strvec.push_back (icu::UnicodeString::fromUTF8(utf8_str));
}

formatter->format (strvec.data(), strvec.size(), result, status);

if (U_FAILURE(status))
PERR ("Unicode error");
else
result.toUTF8String(retval);

delete formatter;
return g_strdup (retval.c_str());
}
11 changes: 0 additions & 11 deletions libgnucash/engine/gnc-date.h
Original file line number Diff line number Diff line change
Expand Up @@ -813,17 +813,6 @@ void gnc_gdate_set_prev_fiscal_year_start (GDate *date, const GDate *year_end);
* fiscal year. The year field of this argument is ignored. */
void gnc_gdate_set_prev_fiscal_year_end (GDate *date, const GDate *year_end);



/** This function takes a GList of char*, and uses locale-sensitive
* list formatter.
*
* @param strings The GList* of char*.
*
* @returns a newly allocated char*
*/
gchar* gnc_list_formatter (GList* strings);

//@}

//@}
Expand Down

0 comments on commit c87d480

Please sign in to comment.