Skip to main content

News

Topic: strnewf(): malloc + sprintf in one (Read 1146 times) previous topic - next topic

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
strnewf(): malloc + sprintf in one
While working on SSJ, I wrote the following function:
Code: (c) [Select]
char*
strnewf(const char* fmt, ...)
{
va_list ap, apc;
char* buffer;
int   buf_size;

va_start(ap, fmt);
va_copy(apc, ap);
buf_size = vsnprintf(NULL, 0, fmt, apc) + 1;
va_end(apc);
buffer = malloc(buf_size);
va_copy(apc, ap);
vsnprintf(buffer, buf_size, fmt, apc);
va_end(apc);
va_end(ap);
return buffer;
}


Usage:
Code: (c) [Select]
char* string = strnewf("%s ate %d pigs", "maggie", 812);
/* do stuff with string... */
free(string);


It's quite useful when you need a formatted string on-the-fly but don't want to have to guess at the correct buffer size and risk a buffer overflow.
miniSphere 5.0.1 - Cell compiler - SSj debugger - thread | on GitHub
For the sake of our continued health I very much hope that Fat Cerberus does not become skilled enough at whatever arcane art it would require to cause computers to spawn enourmous man eating pigs ~Rhuan

Re: strnewf(): malloc + sprintf in one
Reply #1
Isn't this already what `asprintf` does?

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: strnewf(): malloc + sprintf in one
Reply #2
Huh, I never heard of asprintf before!  It looks like it's GNU-only though, it's not even a POSIX function (like strdup is).
miniSphere 5.0.1 - Cell compiler - SSj debugger - thread | on GitHub
For the sake of our continued health I very much hope that Fat Cerberus does not become skilled enough at whatever arcane art it would require to cause computers to spawn enourmous man eating pigs ~Rhuan

Re: strnewf(): malloc + sprintf in one
Reply #3
BSD has an implementation, too, and I think there is a musl one as well.

I tend to take non-standard things like that and strcasecmp and the like from the BSD libc. Each file corresponds to one function or one small family of functions, and doesn't require many (if any) other files to work.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: strnewf(): malloc + sprintf in one
Reply #4
strcasecmp was at least easy to get in MSVC:
Code: (c) [Select]
#define strcasecmp stricmp


Same goes for strtok_r vs. strtok_s and a few other POSIX functions, they usually have reasonable drop-in replacements in the MS CRT.  asprintf is an exception though, MSVC has no equivalent to that as far as I'm aware.

Nice to see I reinvented the wheel without even intending to. :P
miniSphere 5.0.1 - Cell compiler - SSj debugger - thread | on GitHub
For the sake of our continued health I very much hope that Fat Cerberus does not become skilled enough at whatever arcane art it would require to cause computers to spawn enourmous man eating pigs ~Rhuan