project-navigation
Personal tools

Author Topic: Implementation of formatted number/currency  (Read 3453 times)

Punkiee

  • Guest
Implementation of formatted number/currency
« on: October 07, 2007, 03:32:26 pm »
An implementation of the idea described http://ufoai.ninex.info/forum/index.php?topic=1825.msg10174#msg10174
The numbers and the currency can be formatted, keeping your locale into account. Thus you will get the formatting you are used to -> 1.234 or 1,234. This is defined in linux by your LC_MONETARY environment variable.

Currently all money stuff is hardcoded at every place there is an amount displayed. eg
Code: [Select]
Q_strcat(pos, va(_("Sum: %i\n"), sum), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER]));

There are 2 options for deploying formatting.

A) simple substitution
Every time money or a number is displayed, you just call the format function and pass the resulting string instead of the raw number. The example becomes:
Code: [Select]
char sumStr[20];
Q_strcat(pos, va(_("Sum: %i\n"), formatNumber(sum,sumStr)), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER]));
The function formatNumber returns a pointer to the formatted string. Every printed number should be substituted like this. The disadvantage is that every programmer should be aware of it and use this substitution.

B) replacing Q_vsnprintf
Currently this passes all arguments to the vsnprintf of your libraries.
We can at this place filter all numbers and format them before they are hand down to vsnprintf. Thus internally the arguments for vsnprintf ("Your money (%i) is given to player %s",(int)money,(char*)otherplayer) are tranformed into ("Your money (%s) is given to player %s","12.445 c",(char*)otherplayer). Not literally but just to get the idea.

That means you can still code your strings as before:
Code: [Select]
Q_strcat(pos, va(_("Sum: %i\n"), sum), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER]));
This has the great advantage that is is transparant, other coders dont have to remember to call the formatting function themself as it is done behind the scenes. In this option B we still have 2 choices:

B1) Reuse the existing defined formats
This means that we can use %d for formatted numbers and %i for unformatted numbers or vice versa. For formatting money we could reuse %u as it isnt used currently in ufoai. Thus the call would be for a number:
Code: [Select]
Q_strcat(pos, va(_("Sum: %i\n"), sum), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER]));

B2) extend the defined formats
This means declaring extra formating options (%m and %v or other unused characters). The advantage is that al other options are preserved and work exactly as the vsnprintf definition. The disadvantage is that you loose the compile time type checking on the functions va, com_Printf,... This compile time checking as defined by the __attribute__(printf...  statement is *only* for gnucc.
Code: [Select]
Q_strcat(pos, va(_("Sum: %v\n"), sum), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER]));


And now the question is: What option looks best, and why?