Skip to main content

News

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

0 Members and 1 Guest are viewing this topic.
  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere 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.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

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

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere 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).
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

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
  • Sphere 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
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub