Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #94269

Re: When parameters to printf are in memory

From BartC <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: When parameters to printf are in memory
Date 2016-09-29 23:11 +0100
Organization A noiseless patient Spider
Message-ID <nsk3ic$kh3$1@dont-email.me> (permalink)
References (2 earlier) <nsjcq7$sit$1@dont-email.me> <array-20160929164603@ram.dialup.fu-berlin.de> <print-20160929171320@ram.dialup.fu-berlin.de> <nsjvj1$4c2$1@dont-email.me> <nsk1e9$ai1$1@dont-email.me>

Show all headers | View raw


On 29/09/2016 22:35, James Harris wrote:
> On 29/09/2016 22:03, BartC wrote:
>> On 29/09/2016 17:13, Stefan Ram wrote:
>>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>>> When you want something like:
>>>> printf( "%d %d %d \n", $array );
>>>
>>>   When there is a limited set of conversion specifiers,
>>>   it does not seem too hard to write a custom array
>>>   print function. I just did it in five minutes:
>>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> void print
>>> ( char const * const restrict source,
>>>   int  const * const restrict array )
>>> { char const * p = source;
>>>   int  const * q = array;
>>>   int escape = 0; int looping = 1;
>>>   while( looping )
>>>   { char c = *p++;
>>>     if( !c )looping = 0; else
>>>     { if( escape )
>>>       { escape = 0;
>>>         switch( c )
>>>         { case 'd': printf( "%d", *q++ ); break; }}
>>>       else
>>>       { if( c == '%' )escape = 1; else
>>>         printf( "%c", c ); }}}}
>>>
>>> int main( void )
>>> { int array[] = { 1, 2, 3 };
>>>   print( "array = %d %d %d\n", array ); }
>>
>> Even with a limited set of format specifiers, you will need %12d, %012d,
>> %10.4f and so on. It's quite a bit of work. I don't know if this
>> approach is worthwhile just for %d and %f and perhaps %s with no width,
>> precision, or padding controls.
>
> Why not simply pass the format specifiers to snprintf?
>
> If p points at the current % sign:
>
>   written = snprintf(dest, size, p, args[i]);
>   dest += written;
>
> The only fiddly issue, AFAICS, is to increment p correctly past the
> format specifier.

Well, that's the main problem! Getting snprintf to stop after the first 
format. If the format string is:

    "(%2d-%2d-%4d)"

and you start at the first "(", should it stop on (but not including) 
the first "-", or at the second "%"? And how to make it do that.

There might be some hairy methods of doing this: copying the format 
string, detecting the start of a format (first "%"), skipping to the 
start of the next "%", temporarily putting a null there, calling 
snprintf on the first bit, then restoring the second "%" and repeating 
with the next bit. With special handling for "%%" and for the last format.

Even there, there are some tricky formats, if you plan to deal with all 
of them, such as "%.*s" which requires /two/ operands, one for "*" and 
one for "s".

How many elements are there likely to be altogether? One easy workaround 
if you put an reasonable upper limit M is to just to do this:

  snprintf(dest,size,text,parms[0],parms[1],parms[2],...parms[M-1]);

The formatting will stop when it runs out of format codes. But there 
might still be issues if each parms[i] is 64 bits; on 32-bit targets 
(and I think you work with 16-bit ones too), it might expect arguments 
for %d formats to occupy 4 bytes on the stack not 8.

That's apart from this being inefficient (although that can be solved by 
using a switch with M different calls depending on the expected number 
of formats).

The stack problem might also be solvable, on 32-bits, by making parms[] 
a 32-bit array, and requiring 64-bit values to be split across two 
parms[] entries. But this makes it harder to work with parms[].

-- 
Bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-09-29 16:20 +0100
  Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-09-29 16:43 +0100
    Re: When parameters to printf are in memory Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-29 20:30 +0100
    Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-09-29 21:23 +0100
    Re: When parameters to printf are in memory BartC <bc@freeuk.com> - 2016-09-29 22:03 +0100
      Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-09-29 22:35 +0100
        Re: When parameters to printf are in memory BartC <bc@freeuk.com> - 2016-09-29 23:11 +0100
          Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-09-30 19:53 +0100
            Re: When parameters to printf are in memory Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-30 20:53 +0100
              Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-01 09:08 +0100
                Re: When parameters to printf are in memory BartC <bc@freeuk.com> - 2016-10-01 11:15 +0100
                Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-04 08:32 +0100
                Re: When parameters to printf are in memory Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-01 11:15 +0100
                Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-04 08:28 +0100
                Re: When parameters to printf are in memory Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-04 09:49 +0100
                Re: When parameters to printf are in memory BartC <bc@freeuk.com> - 2016-10-04 11:43 +0100
                Re: When parameters to printf are in memory Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-04 13:42 +0100
                Re: When parameters to printf are in memory BartC <bc@freeuk.com> - 2016-10-04 14:03 +0100
                Re: When parameters to printf are in memory Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-04 14:16 +0100
                Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-04 20:01 +0100
                Re: When parameters to printf are in memory Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-04 20:24 +0100
                Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-04 20:40 +0100
                Re: When parameters to printf are in memory Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-04 22:51 +0100
            Re: When parameters to printf are in memory "James R. Kuyper" <jameskuyper@verizon.net> - 2016-10-04 14:25 -0400
              Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-04 20:31 +0100
  Re: When parameters to printf are in memory BartC <bc@freeuk.com> - 2016-09-29 17:03 +0100
    Re: When parameters to printf are in memory gazelle@shell.xmission.com (Kenny McCormack) - 2016-09-30 12:20 +0000
      Re: When parameters to printf are in memory Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-30 05:55 -0700
      Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-09-30 20:06 +0100
        Re: When parameters to printf are in memory gazelle@shell.xmission.com (Kenny McCormack) - 2016-09-30 22:01 +0000
          Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-01 09:15 +0100
        Re: When parameters to printf are in memory Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-30 23:18 -0700
          Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-01 09:32 +0100
  Re: When parameters to printf are in memory jacobnavia <jacob@jacob.remcomp.fr> - 2016-09-29 18:35 +0200
    Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-09-29 19:55 +0100
      Re: When parameters to printf are in memory jacobnavia <jacob@jacob.remcomp.fr> - 2016-09-29 21:22 +0200
  Re: When parameters to printf are in memory Barry Schwarz <schwarzb@dqel.com> - 2016-09-29 11:20 -0700
    Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-09-29 20:08 +0100
  Re: When parameters to printf are in memory Lőrinczy Zsigmond <nospam@for.me> - 2016-10-02 20:15 +0200
    Re: When parameters to printf are in memory Lőrinczy Zsigmond <nospam@for.me> - 2016-10-02 20:19 +0200
      Re: When parameters to printf are in memory gazelle@shell.xmission.com (Kenny McCormack) - 2016-10-02 18:29 +0000
        Re: When parameters to printf are in memory Lőrinczy Zsigmond <nospam@for.me> - 2016-10-03 07:04 +0200
          Re: When parameters to printf are in memory James Harris <james.harris.1@gmail.com> - 2016-10-04 08:36 +0100
    Re: When parameters to printf are in memory jacobnavia <jacob@jacob.remcomp.fr> - 2016-10-02 21:27 +0200

csiph-web