string.c, introduce tprintf()

This commit is contained in:
rlar 2014-03-29 13:11:56 +01:00
parent 2881eef750
commit a4780f8e34
2 changed files with 48 additions and 0 deletions

View File

@ -24,6 +24,12 @@ char * gettok(char **s);
char * gettok_instance(char **);
char * gettok_char(char **s, char p, bool inc_p, bool nested);
#ifdef __GNUC__
extern char *tprintf(const char *fmt, ...) __attribute__ ((format (__printf__, 1, 2)));
#else
extern char *tprintf(const char *fmt, ...);
#endif
#ifdef CIDER
/* cider integration */

View File

@ -10,6 +10,9 @@ Copyright 1990 Regents of the University of California. All rights reserved.
#include "ngspice/stringutil.h"
#include "ngspice/dstring.h"
#include <stdarg.h>
int
prefix(register char *p, register char *s)
{
@ -49,6 +52,45 @@ copy_substring(const char *str, const char *end)
return(p);
}
char*
tprintf(const char *fmt, ...)
{
char buf[1024];
char *p = buf;
int size = sizeof(buf);
va_list args;
for (;;) {
int nchars;
va_start(args, fmt);
nchars = vsnprintf(p, (size_t) size, fmt, args);
va_end(args);
if (nchars == -1) { // compatibility to old implementations
size *= 2;
} else if (size < nchars + 1) {
size = nchars + 1;
} else {
break;
}
if (p == buf)
p = TMALLOC(char, size);
else
p = TREALLOC(char, p, size);
}
return (p == buf) ? copy(p) : p;
}
/* Determine whether sub is a substring of str. */
/* Like strstr( ) XXX */