28 #define CONVS "diouxXeEfFgGaAcsCSpnm%" 31 #define SPEC_BUF_SIZE 16 40 static int count_wide_chars(
const char *);
41 static int count_wide_chars_in_cols(
const char *,
int,
int *);
42 static int ovprintf(
struct options *,
const char *, va_list);
43 static int oprintf(
struct options *,
const char *, ...);
44 static int oaprintf(
struct options *,
const char *, va_list);
52 static int count_wide_chars(
const char *str)
54 int nwchars = 0, lead = 0;
59 if ((*str++ & 0xc0) != 0x80)
79 static int count_wide_chars_in_cols(
const char *str,
int ncols,
int *nbytes)
81 const char *p = str - 1;
82 int lead = 0, nwchars = 0;
85 while (ncols >= 0 && *++p)
86 if ((*p & 0xc0) != 0x80) {
101 if ((*p & 0xc0) == 0x80)
119 static int ovprintf(
struct options *opts,
const char *format, va_list ap)
123 if (opts ==
NULL || (opts->stream ==
NULL && opts->_str ==
NULL))
124 nbytes = vprintf(format, ap);
125 else if (opts->stream)
126 nbytes = vfprintf(opts->stream, format, ap);
128 if ((
long int)opts->size >= 0) {
130 nbytes = vsnprintf(opts->_str, opts->_size, format, ap);
131 opts->_size -= nbytes;
135 nbytes = vsprintf(opts->_str, format, ap);
136 opts->_str += nbytes;
153 static int oprintf(
struct options *opts,
const char *format, ...)
158 va_start(ap, format);
159 nbytes = ovprintf(opts, format, ap);
176 static int oaprintf(
struct options *opts,
const char *format, va_list ap)
182 p = asis = fmt = (
char *)
G_malloc(strlen(format) + 1);
187 char *q = p, *p_spec = spec;
191 nbytes += oprintf(opts, asis);
198 while (*++c && *q != *c);
204 va_copy(ap_copy, ap);
209 int width = -1, prec = -1, use_ovprintf = 1;
217 if (*p_spec ==
'*') {
219 width = va_arg(ap,
int);
221 }
else if (*p_spec >=
'0' && *p_spec <=
'9') {
224 while (*p_spec >=
'0' && *p_spec <=
'9')
231 if (*p_spec ==
'.') {
234 if (*p_spec ==
'*') {
236 prec = va_arg(ap,
int);
238 }
else if (*p_spec >=
'0' && *p_spec <=
'9') {
241 while (*p_spec >=
'0' && *p_spec <=
'9')
254 _(
"Failed to parse string specifier: %s"),
258 s = va_arg(ap,
char *);
261 int wcount = count_wide_chars(s);
266 width += count_wide_chars_in_cols(s, prec,
271 p_spec += sprintf(p_spec,
"%%%s%d",
272 spec[0] ==
'-' ?
"-" :
"", width);
274 p_spec += sprintf(p_spec,
".%d", prec);
277 nbytes += oprintf(opts, spec, s);
286 nbytes += ovprintf(opts, p, ap_copy);
293 nbytes += ovprintf(opts, p, ap_copy);
343 _(
"Format specifier exceeds the buffer size (%d)"),
353 nbytes += oprintf(opts, asis);
368 return oaprintf(
NULL, format, ap);
383 opts.stream = stream;
387 return oaprintf(&opts, format, ap);
403 opts.str = opts._str = str;
406 return oaprintf(&opts, format, ap);
419 int G_vsnaprintf(
char *str,
size_t size,
const char *format, va_list ap)
424 opts.str = opts._str = str;
425 opts.size = opts._size = size;
427 return oaprintf(&opts, format, ap);
457 va_start(ap, format);
477 va_start(ap, format);
497 va_start(ap, format);
519 va_start(ap, format);
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int G_faprintf(FILE *stream, const char *format,...)
fprintf() version of G_aprintf(). See G_aprintf() for more details.
int G_vsaprintf(char *str, const char *format, va_list ap)
vsprintf() version of G_aprintf(). See G_aprintf() for more details.
int G_vsnaprintf(char *str, size_t size, const char *format, va_list ap)
vsnprintf() version of G_aprintf(). See G_aprintf() for more details.
int G_aprintf(const char *format,...)
Adjust the width of string specifiers to the display space instead of the number of bytes for wide ch...
int G_vaprintf(const char *format, va_list ap)
vprintf() version of G_aprintf(). See G_aprintf() for more details.
int G_saprintf(char *str, const char *format,...)
sprintf() version of G_aprintf(). See G_aprintf() for more details.
int G_snaprintf(char *str, size_t size, const char *format,...)
snprintf() version of G_aprintf(). See G_aprintf() for more details.
int G_vfaprintf(FILE *stream, const char *format, va_list ap)
vfprintf() version of G_aprintf(). See G_aprintf() for more details.