GnuCash  5.6-150-g038405b370+
Files | Data Structures | Macros | Typedefs | Enumerations | Functions
Utility functions for the GnuCash GUI

Files

file  gnc-ui-util.h
 utility functions for the GnuCash UI
 

Data Structures

struct  GNCPrintAmountInfo
 

Macros

#define GNC_PREFS_GROUP_OPEN_SAVE   "dialogs.open-save"
 
#define GNC_PREFS_GROUP_EXPORT   "dialogs.export-accounts"
 
#define GNC_PREFS_GROUP_REPORT   "dialogs.report"
 
#define GNC_PREF_AUTO_DECIMAL_POINT   "auto-decimal-point"
 
#define GNC_PREF_AUTO_DECIMAL_PLACES   "auto-decimal-places"
 
#define WLINK   'w'
 
#define FLINK   'f'
 

Typedefs

typedef QofSession *(* QofSessionCB) (void)
 

Enumerations

enum  GNCEquityType { EQUITY_OPENING_BALANCE, EQUITY_RETAINED_EARNINGS, NUM_EQUITY_TYPES }
 

Functions

char * gnc_normalize_account_separator (const char *separator)
 
gboolean gnc_reverse_balance (const Account *account)
 
void gnc_set_use_equity_type_opening_balance_account (QofBook *book)
 
gboolean gnc_using_equity_type_opening_balance_account (QofBook *book)
 
char * gnc_get_default_directory (const char *section)
 
void gnc_set_default_directory (const char *section, const char *directory)
 
QofBook * gnc_get_current_book (void)
 
gboolean gnc_is_new_book (void)
 
void gnc_set_current_book_tax_name_type (gboolean name_changed, const char *tax_name, gboolean type_changed, const char *tax_type)
 
const char * gnc_get_current_book_tax_name (void)
 
const char * gnc_get_current_book_tax_type (void)
 
void gnc_book_option_num_field_source_change_cb (gboolean num_action)
 Calls gnc_book_option_num_field_source_change to initiate registered callbacks when num_field_source book option changes so that registers/reports can update themselves; sets feature flag.
 
Accountgnc_get_current_root_account (void)
 
gnc_commodity_table * gnc_get_current_commodities (void)
 
char * gnc_get_account_name_for_register (const Account *account)
 Get either the full name of the account or the simple name, depending on the configuration parameter general/register/show_leaf_account_names. More...
 
Accountgnc_account_lookup_for_register (const Account *base_account, const char *name)
 Retrieve the account matching the given name starting from the descendants of base_account. More...
 
char * gnc_get_account_name_for_split_register (const Account *account, gboolean show_leaf_accounts)
 Get either the full name of the account or the simple name, depending on the show_leaf_accounts. More...
 
const char * gnc_get_reconcile_str (char reconciled_flag)
 
const char * gnc_get_reconcile_valid_flags (void)
 
const char * gnc_get_reconcile_flag_order (void)
 
const char * gnc_get_doclink_valid_flags (void)
 Get a string containing documentation link valid flags. More...
 
const char * gnc_get_doclink_flag_order (void)
 Get a string containing document link flag order. More...
 
const char * gnc_get_doclink_str (char link_flag)
 Get a string representing the document link type. More...
 
Accountgnc_find_or_create_equity_account (Account *root, GNCEquityType equity_type, gnc_commodity *currency)
 
gboolean gnc_account_create_opening_balance (Account *account, gnc_numeric balance, time64 date, QofBook *book)
 
gnc_commodity * gnc_locale_default_currency_nodefault (void)
 Returns the default currency of the current locale, or NULL if no sensible currency could be identified from the locale. More...
 
gnc_commodity * gnc_locale_default_currency (void)
 Returns the default currency of the current locale. More...
 
gnc_commodity * gnc_default_currency (void)
 Return the default currency set by the user. More...
 
gnc_commodity * gnc_account_or_default_currency (const Account *account, gboolean *currency_from_account_found)
 Returns a gnc_commodity that is a currency, suitable for being a Transaction's currency. More...
 
gnc_commodity * gnc_default_report_currency (void)
 Return the default currency for use in reports, as set by the user. More...
 
GNCPrintAmountInfo gnc_default_print_info (gboolean use_symbol)
 
GNCPrintAmountInfo gnc_commodity_print_info (const gnc_commodity *commodity, gboolean use_symbol)
 
GNCPrintAmountInfo gnc_account_print_info (const Account *account, gboolean use_symbol)
 
GNCPrintAmountInfo gnc_split_amount_print_info (Split *split, gboolean use_symbol)
 
GNCPrintAmountInfo gnc_price_print_info (const gnc_commodity *curr, gboolean use_symbol)
 
GNCPrintAmountInfo gnc_share_print_info_places (int decplaces)
 
GNCPrintAmountInfo gnc_default_share_print_info (void)
 
GNCPrintAmountInfo gnc_default_price_print_info (const gnc_commodity *curr)
 
GNCPrintAmountInfo gnc_integral_print_info (void)
 
const char * xaccPrintAmount (gnc_numeric val, GNCPrintAmountInfo info)
 Make a string representation of a gnc_numeric. More...
 
int xaccSPrintAmount (char *buf, gnc_numeric val, GNCPrintAmountInfo info)
 Make a string representation of a gnc_numeric. More...
 
const char * printable_value (gdouble val, gint denom)
 
char * number_to_words (gdouble val, gint64 denom)
 
char * numeric_to_words (gnc_numeric val)
 
gboolean xaccParseAmount (const char *in_str, gboolean monetary, gnc_numeric *result, char **endstr)
 Parses in_str to obtain a numeric result. More...
 
gboolean xaccParseAmountExtended (const char *in_str, gboolean monetary, gunichar negative_sign, gunichar decimal_point, gunichar group_separator, const char *ignore_list, gnc_numeric *result, char **endstr)
 Converts a string to a gnc_numeric. More...
 
gboolean xaccParseAmountImport (const char *in_str, gboolean monetary, gnc_numeric *result, char **endstr, gboolean skip)
 Similar to xaccParseAmount, but with two differences. More...
 
gboolean xaccParseAmountExtImport (const char *in_str, gboolean monetary, gunichar negative_sign, gunichar decimal_point, gunichar group_separator, const char *ignore_list, gnc_numeric *result, char **endstr)
 Similar to xaccParseAmountExtended, but will not automatically set a decimal point, regardless of what the user has set for this option. More...
 
const char * gnc_print_amount_with_bidi_ltr_isolate (gnc_numeric val, GNCPrintAmountInfo info)
 Make a string representation of a gnc_numeric. More...
 
char * gnc_wrap_text_with_bidi_ltr_isolate (const char *text)
 This function helps with GTK's use of 'Unicode Bidirectional Text Algorithm'. More...
 
void gnc_ui_util_init (void)
 
void gnc_ui_util_remove_registered_prefs (void)
 
char * gnc_filter_text_for_control_chars (const char *incoming_text)
 Returns the incoming text removed of control characters. More...
 
void gnc_filter_text_set_cursor_position (const char *incoming_text, const char *symbol, int *zcursor_position)
 Updates cursor_position after removal of currency symbols. More...
 
char * gnc_filter_text_for_currency_symbol (const char *incoming_text, const char *symbol)
 Returns the incoming text removed of a currency symbol. More...
 
char * gnc_filter_text_for_currency_commodity (const gnc_commodity *comm, const char *incoming_text, const char **symbol)
 Returns the incoming text removed of currency symbol. More...
 
gchar * gnc_list_formatter (GList *strings)
 This function takes a GList of char*, and uses locale-sensitive list formatter. More...
 

Detailed Description

Function Documentation

◆ gnc_account_lookup_for_register()

Account* gnc_account_lookup_for_register ( const Account base_account,
const char *  name 
)

Retrieve the account matching the given name starting from the descendants of base_account.

name is either considered to be the name of the leaf in the account tree or to be the full account path, depending on the configuration parameter general.register/show_leaf_account_names.

Parameters
base_accountThe account to start the search at.
nameThe name to search for.
Returns
A pointer to the account, or NULL if the account was not found.

Definition at line 447 of file gnc-ui-util.cpp.

448 {
449  auto show_leaf_accounts =
450  gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL_REGISTER,
451  GNC_PREF_SHOW_LEAF_ACCT_NAMES);
452 
453  if (show_leaf_accounts)
454  return gnc_account_lookup_by_name (base_account, name);
455  else
456  return gnc_account_lookup_by_full_name (base_account, name);
457 }
Account * gnc_account_lookup_by_name(const Account *parent, const char *name)
The gnc_account_lookup_by_name() subroutine fetches the account by name from the descendants of the s...
Definition: Account.cpp:3063
Account * gnc_account_lookup_by_full_name(const Account *any_acc, const gchar *name)
The gnc_account_lookup_full_name() subroutine works like gnc_account_lookup_by_name, but uses fully-qualified names using the given separator.
Definition: Account.cpp:3133
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.

◆ gnc_account_or_default_currency()

gnc_commodity* gnc_account_or_default_currency ( const Account account,
gboolean *  currency_from_account_found 
)

Returns a gnc_commodity that is a currency, suitable for being a Transaction's currency.

The gnc_commodity is taken either from the current account, or from the next parent account that has a gnc_commodity that is a currency, or from gnc_default_currency().

If the given account or any of its parent account have a commodity that is a currency, it is returned and the gboolean currency_from_account_found is set to TRUE (if non-NULL). If neither this account nor any of its parent accounts have such a commodity, gnc_default_currency() is returned and the gboolean currency_from_account_found is set to FALSE (if non-NULL). This can be used to show an appropriate warning message.

If account is NULL, gnc_default_currency() is returned and currency_from_account_found is set to FALSE.

Parameters
accountThe account where the currency should be looked up. May be NULL.
currency_from_account_foundA gboolean pointer that takes the output argument of whether the returned currency was found in the account. May be NULL.
Returns
A currency pointer (and never NULL).

Definition at line 787 of file gnc-ui-util.cpp.

789 {
790  gnc_commodity *currency;
791  if (!account)
792  {
793  if (currency_from_account_found)
794  *currency_from_account_found = FALSE;
795  return gnc_default_currency();
796  }
797 
798  currency = gnc_account_get_currency_or_parent(account);
799  if (currency)
800  {
801  if (currency_from_account_found)
802  *currency_from_account_found = TRUE;
803  }
804  else
805  {
806  if (currency_from_account_found)
807  *currency_from_account_found = FALSE;
808  currency = gnc_default_currency();
809  }
810  return currency;
811 }
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
gnc_commodity * gnc_account_get_currency_or_parent(const Account *account)
Returns a gnc_commodity that is a currency, suitable for being a Transaction's currency.
Definition: Account.cpp:3378

◆ gnc_default_currency()

gnc_commodity* gnc_default_currency ( void  )

Return the default currency set by the user.

If the user's preference is invalid, then this routine will return the default currency for the user's locale.

Returns
A pointer to a currency.

Definition at line 780 of file gnc-ui-util.cpp.

781 {
782  return gnc_default_currency_common (user_default_currency,
783  GNC_PREFS_GROUP_GENERAL);
784 }

◆ gnc_default_report_currency()

gnc_commodity* gnc_default_report_currency ( void  )

Return the default currency for use in reports, as set by the user.

If the user's preference is invalid, then this routine will return the default currency for the user's locale.

Returns
A pointer to a currency.

Definition at line 816 of file gnc-ui-util.cpp.

817 {
818  return gnc_default_currency_common (user_report_currency,
819  GNC_PREFS_GROUP_GENERAL_REPORT);
820 }

◆ gnc_filter_text_for_control_chars()

char* gnc_filter_text_for_control_chars ( const char *  incoming_text)

Returns the incoming text removed of control characters.

Parameters
incoming_textThe text to filter
Returns
The incoming text filtered of control characters to be freed by the caller.

Definition at line 2160 of file gnc-ui-util.cpp.

2161 {
2162  bool cntrl = false;
2163  bool text_found = false;
2164 
2165  if (!text)
2166  return nullptr;
2167 
2168  if (!g_utf8_validate (text, -1, nullptr))
2169  return nullptr;
2170 
2171  auto filtered = g_string_sized_new (strlen (text) + 1);
2172 
2173  auto ch = text;
2174 
2175  while (*ch)
2176  {
2177  auto uc = g_utf8_get_char (ch);
2178 
2179  // check for starting with control characters
2180  if (unichar_is_cntrl (uc) && !text_found)
2181  {
2182  ch = g_utf8_next_char (ch);
2183  continue;
2184  }
2185  // check for alpha, num and punctuation
2186  if (!unichar_is_cntrl (uc))
2187  {
2188  filtered = g_string_append_unichar (filtered, uc);
2189  text_found = true;
2190  }
2191  // check for control characters after text
2192  if (unichar_is_cntrl (uc))
2193  cntrl = true;
2194 
2195  ch = g_utf8_next_char (ch);
2196 
2197  if (cntrl) // if control characters in text replace with space
2198  {
2199  auto uc2 = g_utf8_get_char (ch);
2200 
2201  if (!unichar_is_cntrl (uc2))
2202  filtered = g_string_append_unichar (filtered, ' ');
2203  }
2204  cntrl = false;
2205  }
2206  return g_string_free (filtered, FALSE);
2207 }

◆ gnc_filter_text_for_currency_commodity()

char* gnc_filter_text_for_currency_commodity ( const gnc_commodity *  comm,
const char *  incoming_text,
const char **  symbol 
)

Returns the incoming text removed of currency symbol.

Parameters
commcommodity of entry if known
incoming_textThe text to filter
symbolreturn the symbol used
Returns
The incoming text with symbol removed to be freed by the caller

Definition at line 2262 of file gnc-ui-util.cpp.

2265 {
2266  if (!incoming_text)
2267  {
2268  *symbol = nullptr;
2269  return nullptr;
2270  }
2271 
2272  if (!gnc_commodity_is_currency (comm))
2273  {
2274  *symbol = nullptr;
2275  return g_strdup (incoming_text);
2276  }
2277 
2278  if (comm)
2279  *symbol = gnc_commodity_get_nice_symbol (comm);
2280  else
2282 
2283  return gnc_filter_text_for_currency_symbol (incoming_text, *symbol);
2284 }
gboolean gnc_commodity_is_currency(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency or a legacy currency...
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
const char * gnc_commodity_get_nice_symbol(const gnc_commodity *cm)
Retrieve a symbol for the specified commodity, suitable for display to the user.
char * gnc_filter_text_for_currency_symbol(const char *incoming_text, const char *symbol)
Returns the incoming text removed of a currency symbol.

◆ gnc_filter_text_for_currency_symbol()

char* gnc_filter_text_for_currency_symbol ( const char *  incoming_text,
const char *  symbol 
)

Returns the incoming text removed of a currency symbol.

Parameters
incoming_textThe text to filter
symbolto remove
cursor_positionthe position of cursor in the incoming text
Returns
The incoming text with symbol removed to be freed by the caller

Definition at line 2241 of file gnc-ui-util.cpp.

2243 {
2244  if (!incoming_text)
2245  return nullptr;
2246 
2247  if (!symbol)
2248  return g_strdup (incoming_text);
2249 
2250  if (g_strrstr (incoming_text, symbol) == nullptr)
2251  return g_strdup (incoming_text);
2252 
2253  auto split = g_strsplit (incoming_text, symbol, -1);
2254 
2255  auto ret_text = g_strjoinv (nullptr, split);
2256 
2257  g_strfreev (split);
2258  return ret_text;
2259 }

◆ gnc_filter_text_set_cursor_position()

void gnc_filter_text_set_cursor_position ( const char *  incoming_text,
const char *  symbol,
int *  zcursor_position 
)

Updates cursor_position after removal of currency symbols.

Parameters
incoming_textThe text to filter
symbolto remove
cursor_positionthe position of cursor in the incoming text
Returns
nothing

◆ gnc_get_account_name_for_register()

char* gnc_get_account_name_for_register ( const Account account)

Get either the full name of the account or the simple name, depending on the configuration parameter general/register/show_leaf_account_names.

Parameters
accountThe account to retrieve the name for.
Returns
A newly allocated string.

Definition at line 437 of file gnc-ui-util.cpp.

438 {
439  auto show_leaf_accounts =
440  gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL_REGISTER,
441  GNC_PREF_SHOW_LEAF_ACCT_NAMES);
442 
443  return gnc_get_account_name_for_split_register(account, show_leaf_accounts);
444 }
char * gnc_get_account_name_for_split_register(const Account *account, gboolean show_leaf_accounts)
Get either the full name of the account or the simple name, depending on the show_leaf_accounts.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.

◆ gnc_get_account_name_for_split_register()

char* gnc_get_account_name_for_split_register ( const Account account,
gboolean  show_leaf_accounts 
)

Get either the full name of the account or the simple name, depending on the show_leaf_accounts.

Parameters
accountThe account to retrieve the name for.
show_leaf_accountsWhether the full name will be returned.
Returns
A newly allocated string.

Definition at line 427 of file gnc-ui-util.cpp.

429 {
430  if (show_leaf_accounts)
431  return g_strdup (xaccAccountGetName (account));
432  else
433  return gnc_account_get_full_name (account);
434 }
gchar * gnc_account_get_full_name(const Account *account)
The gnc_account_get_full_name routine returns the fully qualified name of the account using the given...
Definition: Account.cpp:3275
const char * xaccAccountGetName(const Account *acc)
Get the account's name.
Definition: Account.cpp:3259

◆ gnc_get_doclink_flag_order()

const char* gnc_get_doclink_flag_order ( void  )

Get a string containing document link flag order.

Returns
a string containing the document link flag change order

Definition at line 539 of file gnc-ui-util.cpp.

540 {
541  static const char flags[] = { FLINK, WLINK, ' ', 0 };
542  return flags;
543 }

◆ gnc_get_doclink_str()

const char* gnc_get_doclink_str ( char  link_flag)

Get a string representing the document link type.

Parameters
link_flagThe flag to convert into a string
Returns
the i18n'd doclink string

Definition at line 515 of file gnc-ui-util.cpp.

516 {
517  switch (link_flag)
518  {
519  case WLINK:
520  return C_("Document Link flag for 'web'", "w");
521  case FLINK:
522  return C_("Document Link flag for 'file'", "f");
523  case ' ':
524  return " ";
525  default:
526  PERR("Bad link flag");
527  return nullptr;
528  }
529 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244

◆ gnc_get_doclink_valid_flags()

const char* gnc_get_doclink_valid_flags ( void  )

Get a string containing documentation link valid flags.

Returns
a string containing the list of valid link_flags

Definition at line 532 of file gnc-ui-util.cpp.

533 {
534  static const char flags[] = { FLINK, WLINK, ' ', 0 };
535  return flags;
536 }

◆ gnc_list_formatter()

gchar* gnc_list_formatter ( GList *  strings)

This function takes a GList of char*, and uses locale-sensitive list formatter.

Parameters
stringsThe GList* of char*.
Returns
a newly allocated char*

Definition at line 2287 of file gnc-ui-util.cpp.

2288 {
2289  g_return_val_if_fail (strings, nullptr);
2290 
2291  UErrorCode status = U_ZERO_ERROR;
2292  auto formatter = icu::ListFormatter::createInstance(status);
2293  std::vector<UniStr> strvec;
2294  UniStr result;
2295  std::string retval;
2296 
2297  for (auto n = strings; n; n = g_list_next (n))
2298  {
2299  auto utf8_str{static_cast<const char*>(n->data)};
2300  strvec.push_back (UniStr::fromUTF8(utf8_str));
2301  }
2302 
2303  formatter->format (strvec.data(), strvec.size(), result, status);
2304 
2305  if (U_FAILURE(status))
2306  PERR ("Unicode error");
2307  else
2308  result.toUTF8String(retval);
2309 
2310  delete formatter;
2311  return g_strdup (retval.c_str());
2312 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244

◆ gnc_locale_default_currency()

gnc_commodity* gnc_locale_default_currency ( void  )

Returns the default currency of the current locale.

WATCH OUT: If no currency could be identified from the locale, this one returns "USD", but this will have nothing to do with the actual locale.

Definition at line 735 of file gnc-ui-util.cpp.

736 {
737  auto currency = gnc_locale_default_currency_nodefault ();
738 
739  return (currency ? currency :
740  gnc_commodity_table_lookup (gnc_get_current_commodities (),
741  GNC_COMMODITY_NS_CURRENCY, "USD"));
742 }
gnc_commodity * gnc_locale_default_currency_nodefault(void)
Returns the default currency of the current locale, or NULL if no sensible currency could be identifi...

◆ gnc_locale_default_currency_nodefault()

gnc_commodity* gnc_locale_default_currency_nodefault ( void  )

Returns the default currency of the current locale, or NULL if no sensible currency could be identified from the locale.

Definition at line 722 of file gnc-ui-util.cpp.

723 {
724  auto table = gnc_get_current_commodities ();
725  auto code = gnc_locale_default_iso_currency_code ();
726 
727  auto currency = gnc_commodity_table_lookup (table,
728  GNC_COMMODITY_NS_CURRENCY,
729  code);
730 
731  return (currency ? currency : nullptr);
732 }

◆ gnc_print_amount_with_bidi_ltr_isolate()

const char* gnc_print_amount_with_bidi_ltr_isolate ( gnc_numeric  val,
GNCPrintAmountInfo  info 
)

Make a string representation of a gnc_numeric.

Warning, the gnc_numeric is not checked for validity and the returned char* may point to random garbage.

This is the same as xaccPrintAmount but wraps the output with BiDi left to right isolate if a symbol is displayed.

Definition at line 1443 of file gnc-ui-util.cpp.

1444 {
1445  /* hack alert -- this is not thread safe ... */
1446  static char buf[BUFLEN];
1447  static const char ltr_isolate[] = { '\xe2', '\x81', '\xa6' };
1448  static const char ltr_pop_isolate[] = { '\xe2', '\x81', '\xa9' };
1449  auto offset = info.use_symbol ? 3 : 0;
1450 
1451  if (!gnc_commodity_is_currency (info.commodity))
1452  offset = 0;
1453 
1454  memset (buf, 0, BUFLEN);
1455  if (!xaccSPrintAmount (buf + offset, val, info))
1456  {
1457  buf[0] = '\0';
1458  return buf;
1459  };
1460 
1461  if (offset == 0)
1462  return buf;
1463 
1464  memcpy (buf, ltr_isolate, 3);
1465 
1466  if (buf[BUFLEN - 4] == '\0')
1467  {
1468  auto length = strlen (buf);
1469  memcpy (buf + length, ltr_pop_isolate, 3);
1470  }
1471  else
1472  {
1473  buf[BUFLEN - 1] = '\0';
1474  memcpy (buf + BUFLEN - 4, ltr_pop_isolate, 3);
1475 
1476  PWARN("buffer length %d exceeded, string truncated was %s", BUFLEN, buf);
1477  }
1478  /* its OK to return buf, since we declared it static
1479  and is immediately g_strdup'd */
1480  return buf;
1481 }
gboolean gnc_commodity_is_currency(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency or a legacy currency...
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
int xaccSPrintAmount(char *bufp, gnc_numeric val, GNCPrintAmountInfo info)
Make a string representation of a gnc_numeric.

◆ gnc_wrap_text_with_bidi_ltr_isolate()

char* gnc_wrap_text_with_bidi_ltr_isolate ( const char *  text)

This function helps with GTK's use of 'Unicode Bidirectional Text Algorithm'.

To keep the format of the text, this function wraps the text with a BiDi isolate charatcter and a BiDi closing character.

This helps with monetary values in RTL languages that display the currency symbol.

Definition at line 1484 of file gnc-ui-util.cpp.

1485 {
1486  static const char* ltr = "\u2066"; // ltr isolate
1487  static const char* pop = "\u2069"; // pop directional formatting
1488 
1489  if (!text)
1490  return nullptr;
1491 
1492  return g_strconcat (ltr, text, pop, nullptr);
1493 }

◆ xaccParseAmount()

gboolean xaccParseAmount ( const char *  in_str,
gboolean  monetary,
gnc_numeric *  result,
char **  endstr 
)

Parses in_str to obtain a numeric result.

The routine will parse as much of in_str as it can to obtain a single number. The number is parsed using the current locale information and the 'monetary' flag. The routine will return TRUE if it successfully parsed a number and FALSE otherwise. If TRUE is returned and result is non-NULL, the value of the parsed number is stored in *result. If FALSE is returned, *result is unchanged. If TRUE is returned and endstr is non-NULL, the location of the first character in in_str not used by the parser will be returned in *endstr. If FALSE is returned and endstr is non-NULL, *endstr will point to in_str.

Definition at line 2011 of file gnc-ui-util.cpp.

2013 {
2014  return xaccParseAmountBasicInternal (in_str, monetary, auto_decimal_enabled,
2015  result, endstr, FALSE);
2016 }

◆ xaccParseAmountExtended()

gboolean xaccParseAmountExtended ( const char *  in_str,
gboolean  monetary,
gunichar  negative_sign,
gunichar  decimal_point,
gunichar  group_separator,
const char *  ignore_list,
gnc_numeric *  result,
char **  endstr 
)

Converts a string to a gnc_numeric.

The caller must provide all the locale-specific information.

ignore_list is a list of characters that are completely ignored while processing the input string. If ignore_list is NULL, nothing is ignored.

Definition at line 2029 of file gnc-ui-util.cpp.

2033 {
2034  return xaccParseAmountInternal (in_str, monetary, negative_sign,
2035  decimal_point, group_separator,
2036  ignore_list, auto_decimal_enabled,
2037  result, endstr);
2038 }

◆ xaccParseAmountExtImport()

gboolean xaccParseAmountExtImport ( const char *  in_str,
gboolean  monetary,
gunichar  negative_sign,
gunichar  decimal_point,
gunichar  group_separator,
const char *  ignore_list,
gnc_numeric *  result,
char **  endstr 
)

Similar to xaccParseAmountExtended, but will not automatically set a decimal point, regardless of what the user has set for this option.

Primarily meant for cases where numbers are coming into gnucash that are not typed in by the user (like via csv import).

Definition at line 2041 of file gnc-ui-util.cpp.

2045 {
2046  return xaccParseAmountInternal (in_str, monetary, negative_sign,
2047  decimal_point, group_separator,
2048  ignore_list, FALSE,
2049  result, endstr);
2050 }

◆ xaccParseAmountImport()

gboolean xaccParseAmountImport ( const char *  in_str,
gboolean  monetary,
gnc_numeric *  result,
char **  endstr,
gboolean  skip 
)

Similar to xaccParseAmount, but with two differences.

  • it exposes a flag to indicate whether the locale's positive sign (or in absence the '+') character is ignored. Setting skip to TRUE will cause the function to ignore any positive sign. Setting it to FALSE, and positive signs will be treated as unrecognized characters. xaccParseAmount will run as if skip is FALSE for compatibility reasons (gnc-expression-parser depends on this behaviour).
  • The other important difference with xaccParseAmount is that this function will never apply automatic decimal point logc, whereas xaccParseAmount will follow the automatic decimal point preference as set by the user.

Definition at line 2019 of file gnc-ui-util.cpp.

2022 {
2023  return xaccParseAmountBasicInternal (in_str, monetary, FALSE,
2024  result, endstr, skip);
2025 }

◆ xaccPrintAmount()

const char* xaccPrintAmount ( gnc_numeric  val,
GNCPrintAmountInfo  info 
)

Make a string representation of a gnc_numeric.

Warning, the gnc_numeric is not checked for validity and the returned char* may point to random garbage.

Definition at line 1430 of file gnc-ui-util.cpp.

1431 {
1432  /* hack alert -- this is not thread safe ... */
1433  static char buf[BUFLEN];
1434 
1435  if (!xaccSPrintAmount (buf, val, info))
1436  buf[0] = '\0';
1437 
1438  /* its OK to return buf, since we declared it static */
1439  return buf;
1440 }
int xaccSPrintAmount(char *bufp, gnc_numeric val, GNCPrintAmountInfo info)
Make a string representation of a gnc_numeric.

◆ xaccSPrintAmount()

int xaccSPrintAmount ( char *  bufp,
gnc_numeric  val,
GNCPrintAmountInfo  info 
)

Make a string representation of a gnc_numeric.

Warning, the gnc_numeric is not checked for validity and the contents of the buffer will be unchanged. It is up to the calling function to ensure that buf is large enough for the results.

Parameters
bufpShould be at least 64 chars.

Definition at line 1303 of file gnc-ui-util.cpp.

1304 {
1305  auto orig_bufp = bufp;
1306  auto currency_symbol = "";
1307  const char* sign;
1308 
1309  char cs_precedes;
1310  char sep_by_space;
1311  char sign_posn;
1312 
1313  bool print_sign = true;
1314  bool print_absolute = false;
1315 
1316  if (!bufp)
1317  return 0;
1318 
1319  auto lc = gnc_localeconv();
1320  if (info.use_locale)
1321  if (gnc_numeric_negative_p (val))
1322  {
1323  cs_precedes = lc->n_cs_precedes;
1324  sep_by_space = lc->n_sep_by_space;
1325  }
1326  else
1327  {
1328  cs_precedes = lc->p_cs_precedes;
1329  sep_by_space = lc->p_sep_by_space;
1330  }
1331  else
1332  {
1333  cs_precedes = TRUE;
1334  sep_by_space = TRUE;
1335  }
1336 
1337  if (info.commodity && info.use_symbol)
1338  {
1339  currency_symbol = gnc_commodity_get_nice_symbol (info.commodity);
1340  if (!gnc_commodity_is_iso (info.commodity))
1341  {
1342  cs_precedes = FALSE;
1343  sep_by_space = TRUE;
1344  }
1345  }
1346 
1347  if (gnc_numeric_negative_p (val))
1348  {
1349  sign = lc->negative_sign;
1350  sign_posn = lc->n_sign_posn;
1351  }
1352  else
1353  {
1354  sign = lc->positive_sign;
1355  sign_posn = lc->p_sign_posn;
1356  }
1357 
1358  if (gnc_numeric_zero_p (val) || (sign == nullptr) || (sign[0] == 0))
1359  print_sign = FALSE;
1360 
1361  /* See if we print sign now */
1362  if (print_sign && (sign_posn == 1))
1363  bufp = g_stpcpy(bufp, sign);
1364 
1365  /* Now see if we print currency */
1366  if (cs_precedes)
1367  {
1368  /* See if we print sign now */
1369  if (print_sign && (sign_posn == 3))
1370  bufp = g_stpcpy(bufp, sign);
1371 
1372  if (info.use_symbol)
1373  {
1374  bufp = g_stpcpy(bufp, currency_symbol);
1375  if (sep_by_space)
1376  bufp = g_stpcpy(bufp, " ");
1377  }
1378 
1379  /* See if we print sign now */
1380  if (print_sign && (sign_posn == 4))
1381  bufp = g_stpcpy(bufp, sign);
1382  }
1383 
1384  /* Now see if we print parentheses */
1385  if (print_sign && (sign_posn == 0))
1386  {
1387  bufp = g_stpcpy(bufp, "(");
1388  print_absolute = TRUE;
1389  }
1390 
1391  /* Now print the value */
1392  bufp += PrintAmountInternal(bufp,
1393  print_absolute ? gnc_numeric_abs(val) : val,
1394  &info);
1395 
1396  /* Now see if we print parentheses */
1397  if (print_sign && (sign_posn == 0))
1398  bufp = g_stpcpy(bufp, ")");
1399 
1400  /* Now see if we print currency */
1401  if (!cs_precedes)
1402  {
1403  /* See if we print sign now */
1404  if (print_sign && (sign_posn == 3))
1405  bufp = g_stpcpy(bufp, sign);
1406 
1407  if (info.use_symbol)
1408  {
1409  if (sep_by_space)
1410  bufp = g_stpcpy(bufp, " ");
1411  bufp = g_stpcpy(bufp, currency_symbol);
1412  }
1413 
1414  /* See if we print sign now */
1415  if (print_sign && (sign_posn == 4))
1416  bufp = g_stpcpy(bufp, sign);
1417  }
1418 
1419  /* See if we print sign now */
1420  if (print_sign && (sign_posn == 2))
1421  bufp = g_stpcpy(bufp, sign);
1422 
1423  /* return length of printed string */
1424  return (bufp - orig_bufp);
1425 }
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
gboolean gnc_numeric_negative_p(gnc_numeric a)
Returns 1 if a < 0, otherwise returns 0.
const char * gnc_commodity_get_nice_symbol(const gnc_commodity *cm)
Retrieve a symbol for the specified commodity, suitable for display to the user.
gnc_numeric gnc_numeric_abs(gnc_numeric a)
Returns a newly created gnc_numeric that is the absolute value of the given gnc_numeric value...
gboolean gnc_commodity_is_iso(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency.