Need sprintf where format specifies order of parameters 
Author Message
 Need sprintf where format specifies order of parameters

For localization purposes, I need a version of sprintf where the
format specifies the *position* of the parameter in the parameter
list.  I believe I've read somewhere a spec for this, but I can't
remember where.

Example: Some languages specify the day before the month in a date
string.  If I have a statement sprintf(buf, format, month, day),
I would like to be able to pass in format = "Month %s, Day %d" but
also format = "Day %{2}d, Month %{1}s"; where the {#} specifies the
order in the parameter list (I don't really care what form the order
specifier is).

For example if month="August" and day=13,
format="Today is %s %d"         --> "Today is August 13"
format="Heute ist %{2}d %{1}s"  --> "Heute ist 13 August"

If there exist specifications (or even public code) for this sort of
sprintf, I would appreciate hearing about it.

    - Stepan
--



Sun, 20 Jul 1997 06:47:04 GMT  
 Need sprintf where format specifies order of parameters

Quote:
> If there exist specifications (or even public code) for this sort of
> sprintf, I would appreciate hearing about it.

    One approach I've seen used is to have a mask string that
    represents how the date should appear. For example...

        d2/m2/y2    <- 2-char-day '/' 2-char-month '/' 2-char-year
        d1 ms cy2   <- 1-or-2-char-day ' ' month-string ' ' century-2-char-year
        d1 m3 cy2   <- 1-or-2-char-day ' ' month-abbrev ' ' century-2-char-year

    Then building a date string is a simple matter of parsing one of
    these fields.

Quote:
>     - Stepan

--

***  "Radioactivity - It's in the air, for you and for me" - Kraftwerk  ***


Sun, 20 Jul 1997 13:59:53 GMT  
 Need sprintf where format specifies order of parameters

Quote:
>For localization purposes, I need a version of sprintf where the
>format specifies the *position* of the parameter in the parameter
>list.  I believe I've read somewhere a spec for this, but I can't
>remember where.

X/Open compliant *printf's support this feature.  This is the description:

      Each conversion specification is introduced by the character % or %n$,
      where n is a decimal integer in the range 1 through {NL_ARGMAX}
      (NL_ARGMAX is defined in <limits.h>).  The %n$ construction indicates
      that this conversion should be applied to the nth argument, rather
      than to the next unused one.

      An argument can be referenced by a %n$ specification more than once.
      The two forms of introducing a conversion specification, % and %n$,
      cannot be mixed within a single format string.  Improper use of %n$ in
      a format string results in a negative return value.

Note that this is not an ANSI C feature, so you can't expect any portability
to non-XPGn platforms.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Mon, 21 Jul 1997 01:12:59 GMT  
 Need sprintf where format specifies order of parameters

Quote:

>> If there exist specifications (or even public code) for this sort of
>> sprintf, I would appreciate hearing about it.
>    One approach I've seen used is to have a mask string that
>    represents how the date should appear. For example...
>      d2/m2/y2    <- 2-char-day '/' 2-char-month '/' 2-char-year
>      d1 ms cy2   <- 1-or-2-char-day ' ' month-string ' ' century-2-char-year
>      d1 m3 cy2   <- 1-or-2-char-day ' ' month-abbrev ' ' century-2-char-year
>    Then building a date string is a simple matter of parsing one of
>    these fields.

I haven't seen the full original question, but the kind of thing
described above is handled very simply with the Standard Library
function strftime().

--



Fri, 25 Jul 1997 04:36:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Need function to convert float/double/long to string using a specified format

2. Specifying a Quote using sprintf

3. Specifying link order

4. formatting output with printf, sprintf

5. sprintf format question

6. sprintf Formatting

7. formatting long strings using sprintf()

8. formatting long strings using sprintf() without including tabs or newlines

9. Writing to CString using sprintf format

10. specify the width of a format string dynamically

11. Using biosdisk() to format a specify track

12. Printing in specified format..

 

 
Powered by phpBB® Forum Software