c - nl_langinfo() - too much information -
the function nl_langinfo(int_curr_symbol) returns pointer string constant of format:
$(three-letter-pseudoacronym) $(sign)$(symbol) so in locale (en_gb.utf-8), "gbp -£". want first 3 letters, there way apart assigning nul character third element, or using strncpy()?
strcpy(int_curr_symbol, nl_langinfo(int_curr_symbol)); // "gbp -£" int_curr_symbol[3] = '\0'; // or strncpy(int_curr_symbol, nl_langinfo(int_curr_symbol), 3)); // "gbp" additionally, nl_langinfo(crncystr), returns sign , symbol, whereas symbol in needed. "-£" -> "£"
int_curr_symbol appears gnu extension. in stead, suggest localeconv() , int_curr_symbol field of struct lconv. posix specification field:
the international currency symbol applicable current locale. first 3 characters contain alphabetic international currency symbol in accordance specified in iso 4217:1995 standard. fourth character (immediately preceding null byte) character used separate international currency symbol monetary quantity.
as can see, supposed contain getting.
about second approach:
additionally, nl_langinfo(crncystr), returns sign , symbol, whereas symbol in needed. "-£" -> "£"
this because sign defines whether symbol placed before value (+), after value (-), or if should replace radix character (.). posix specification of langinfo.h:
local currency symbol, preceded '-' if symbol should appear before value, '+' if symbol should appear after value, or '.' if symbol should replace radix character. if local currency symbol empty string, implementations may return empty string ( "" ).
as method of obtaining part of string, suggest copying relevant part out of returned values own buffer right after call. have because structure , various pointers returned either of functions allocated , maintained c library.
the reason given posix nl_langinfo() returned values:
the application shall not modify string returned. pointer returned nl_langinfo() might invalidated or string content might overwritten subsequent call nl_langinfo() in thread or nl_langinfo_l() in same thread or initial thread, subsequent calls setlocale() category corresponding category of item (see ) or category lc_all, or subsequent calls uselocale() change category corresponding category of item. pointer returned nl_langinfo_l() might invalidated or string content might overwritten subsequent call nl_langinfo_l() in same thread or nl_langinfo() in thread, or subsequent calls freelocale() or newlocale() free or modify locale object passed nl_langinfo_l().
and here similar reason given localeconv():
the localeconv() function shall return pointer filled-in object. application shall not modify structure return value points, nor storage areas pointed pointers within structure. returned pointer, , pointers within structure, might invalidated or structure or storage areas might overwritten subsequent call localeconv(). in addition, returned pointer, , pointers within structure, might invalidated or structure or storage areas might overwritten subsequent calls setlocale() categories lc_all, lc_monetary, or lc_numeric, or calls uselocale() change categories lc_monetary or lc_numeric.
Comments
Post a Comment