sprintf container
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > sprintf container




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    sprintf container  
vertigo


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

Hello
I want to write function which will return me string
formatted by sprintf:

char* sprintf_container(const char *format,...){
str=malloc('what size' ??);
sprintf(str,format,???);
return str;
}

But how should i pass other parameters to sprintf ?

Thanx
Michal






[ Post a follow-up to this message ]



    Re: sprintf container  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

vertigo <none@microsoft.com> writes:

> Hello
> I want to write function which will return me string
> formatted by sprintf:
>
> char* sprintf_container(const char *format,...){
> 	str=malloc('what size' ??);
> 	sprintf(str,format,???);
> 	return str;
> }
>
> But how should i pass other parameters to sprintf ?

man vsprintf

--
Måns Rullgård
mru@mru.ath.cx





[ Post a follow-up to this message ]



    Re: sprintf container  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

vertigo <none@microsoft.com> writes:

> Hello
> I want to write function which will return me string
> formatted by sprintf:
>
> char* sprintf_container(const char *format,...){
> 	str=malloc('what size' ??);
> 	sprintf(str,format,???);
> 	return str;
> }
>
> But how should i pass other parameters to sprintf ?

You won't be using sprintf.

You should really, as always, read the manual page for sprintf: man sprintf

Where you'll notice vsnprintf.
Then you'll refer to the stdarg man page: man stdarg


--
__Pascal Bourguignon__                     http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.





[ Post a follow-up to this message ]



    Re: sprintf container  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

vertigo <none@microsoft.com> wrote:
> I want to write function which will return me string
> formatted by sprintf:

> char* sprintf_container(const char *format,...){
> 	str=malloc('what size' ??);
> 	sprintf(str,format,???);
> 	return str;
> }

Here's what I use for that:

#define SPRINTF_CONTAINER_TRY_LENGTH 128

char *sprintf_container( const char *fmt, ... )
{
char *c = NULL, *new_c;
size_t len = SPRINTF_CONTAINER_TRY_LENGTH;
va_list ap;
int wr;


while ( 1 )
{
if ( ( new_c = realloc( c, len ) ) == NULL )
{
free( c );
return NULL;
}
c = new_c;

va_start( ap, fmt );
wr = vsnprintf( c, len, fmt, ap );
va_end( ap );

if ( wr < 0 )         /* indicates not enough space with older glibc */
{
len *= 2;
continue;
}

if ( ( size_t ) wr + 1 > len )   /* new glibc returns the number of */
{                                /* chars needed, not counting the */
len = wr + 1;                /* trailing '\0' */
continue;
}

break;
}

/* Trim the string to the number of required characters */

if ( ( size_t ) wr + 1 < len )
{
if ( ( new_c = realloc( c, ( size_t ) wr + 1 ) ) == NULL )
return c;
else
c = new_c;
}

return c;
}

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: sprintf container  
sean larsson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

On Fri, 27 Aug 2004 11:48:53 +0200
vertigo <none@microsoft.com> wrote:

> Hello
> I want to write function which will return me string
> formatted by sprintf:
>
> char* sprintf_container(const char *format,...){
> 	str=malloc('what size' ??);
> 	sprintf(str,format,???);
> 	return str;
> }
>
> But how should i pass other parameters to sprintf ?
>
> Thanx
> Michal
>

or you could use asprintf().  not standard C, but it is portable on linux an
d
bsd.

--
-sean





[ Post a follow-up to this message ]



    Re: sprintf container  
Lev Walkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

Jens.Toerring@physik.fu-berlin.de wrote:
> vertigo <none@microsoft.com> wrote:
> 
>
> 
>
>
> Here's what I use for that:
>
> #define SPRINTF_CONTAINER_TRY_LENGTH 128
>
> char *sprintf_container( const char *fmt, ... )
> {
>     char *c = NULL, *new_c;
>     size_t len = SPRINTF_CONTAINER_TRY_LENGTH;
>     va_list ap;
>     int wr;
>
>
>     while ( 1 )
>     {
>         if ( ( new_c = realloc( c, len ) ) == NULL )
>         {
>             free( c );

if realloc() fails, your program will crash on most free() implementations.



--
Lev Walkin
vlm@lionet.info





[ Post a follow-up to this message ]



    Re: sprintf container  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

Lev Walkin <vlm@lionet.info> wrote:[vbcol=seagreen]
> Jens.Toerring@physik.fu-berlin.de wrote: 
[vbcol=seagreen]
> if realloc() fails, your program will crash on most free() implementations.[/vbcol
]

In that case 'c' should be either a value returned from a previous call
of alloc(), in which case I don't see a problem, or NULL, and then it
should be a no-op (as far as I can see from the C89 standard). Or am I
overlooking something?
Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: sprintf container  
Lev Walkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

Jens.Toerring@physik.fu-berlin.de wrote:
> Lev Walkin <vlm@lionet.info> wrote:
> 
>
> 
>
>
> In that case 'c' should be either a value returned from a previous call
> of alloc(), in which case I don't see a problem, or NULL, and then it
> should be a no-op (as far as I can see from the C89 standard). Or am I
> overlooking something?

you're overlooking the real world.

however, I've just checked, and indeed it seem to have improved on newer
systems. however, I remember this being a common inconvenience on the older
ones, so it's better not to rely on it.


--
Lev Walkin
vlm@lionet.info





[ Post a follow-up to this message ]



    Re: sprintf container  
Lev Walkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

William Ahern wrote:
> Lev Walkin <vlm@lionet.info> wrote:
> 
>
>
> Care to elaborate? I'm quite surprised to hear that.

The code checks for old behavior of vsnprintf(), which may return -1 instead
of the "estimated" numer of characters. But the code does not go further and
check if the c is non-0 before free()'ing. See my other follow-up on the C89
quote.


--
Lev Walkin
vlm@lionet.info





[ Post a follow-up to this message ]



    Re: sprintf container  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-27-04 11:21 PM

Lev Walkin <vlm@lionet.info> wrote:
> William Ahern wrote: 
[vbcol=seagreen]
> The code checks for old behavior of vsnprintf(), which may return -1 inste
ad
> of the "estimated" numer of characters. But the code does not go further a
nd
> check if the c is non-0 before free()'ing. See my other follow-up on the C
89
> quote.

Luckily, I don't remember having to deal with a C version that didn't
work correctly with free() on NULL (but fortunately one tends to forget
about such annoyances and normally, i.e. in the code I really use, I
avoid it anyway because I usually would take it to be an indication
that something went wrong when free() is called with a NULL pointer).
But vsnprintf() is a different case because it's in C99 only, so one
can't reasonably expect that all implementations got it already right
(if they have it at all), so additional checks are (still) in order.
Perhaps I sshould have made it more clear that the code I posted only
works with a system that conforms to C89 and has a vsnprintf() imple-
mentation that either is C99 conformant or returns a negative value
when the the length of the buffer is insufficient.

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:39 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register