Commit f022473f authored by Ben Pfaff's avatar Ben Pfaff

New 'c-*printf' modules for formatted output in C locale.

New module 'c-vasnprintf'.
* modules/c-vasnprintf: New file.
* lib/c-vasnprintf.c: New file.
* lib/c-vasnprintf.h: New file.

New module 'c-snprintf'.
* modules/c-snprintf: New file.
* modules/c-snprintf-tests: New file.
* lib/c-snprintf.c: New file.
* lib/c-snprintf.h: New file.
* tests/test-c-snprintf.c: New file.
* tests/test-c-snprintf.sh: New file.

New module 'c-vsnprintf'.
* modules/c-vsnprintf: New file.
* modules/c-vsnprintf-tests: New file.
* lib/c-vsnprintf.c: New file.
* lib/c-vsnprintf.h: New file.
* tests/test-c-vsnprintf.c: New file.
* tests/test-c-vsnprintf.sh: New file.

New module 'c-vasprintf'.
* modules/c-vasprintf: New file.
* modules/c-vasprintf-tests: New file.
* lib/c-asprintf.c: New file.
* lib/c-vasprintf.c: New file.
* lib/c-vasprintf.h: New file.
* tests/test-c-vasprintf.c  +: New file.
* tests/test-c-vasprintf.sh: New file.

New module 'c-xvasprintf'.
* modules/c-xvasprintf: New file.
* modules/c-xvasprintf-tests: New file.
* lib/c-xasprintf.c: New file.
* lib/c-xvasprintf.c: New file.
* lib/c-xvasprintf.h: New file.
* tests/test-c-xvasprintf.c: New file.
* tests/test-c-xvasprintf.sh: New file.
parent 37e3ad9a
2012-12-15 Ben Pfaff <blp@cs.stanford.edu>
New 'c-*printf' modules for formatted output in C locale.
New module 'c-vasnprintf'.
* modules/c-vasnprintf: New file.
* lib/c-vasnprintf.c: New file.
* lib/c-vasnprintf.h: New file.
New module 'c-snprintf'.
* modules/c-snprintf: New file.
* modules/c-snprintf-tests: New file.
* lib/c-snprintf.c: New file.
* lib/c-snprintf.h: New file.
* tests/test-c-snprintf.c: New file.
* tests/test-c-snprintf.sh: New file.
New module 'c-vsnprintf'.
* modules/c-vsnprintf: New file.
* modules/c-vsnprintf-tests: New file.
* lib/c-vsnprintf.c: New file.
* lib/c-vsnprintf.h: New file.
* tests/test-c-vsnprintf.c: New file.
* tests/test-c-vsnprintf.sh: New file.
New module 'c-vasprintf'.
* modules/c-vasprintf: New file.
* modules/c-vasprintf-tests: New file.
* lib/c-asprintf.c: New file.
* lib/c-vasprintf.c: New file.
* lib/c-vasprintf.h: New file.
* tests/test-c-vasprintf.c +: New file.
* tests/test-c-vasprintf.sh: New file.
New module 'c-xvasprintf'.
* modules/c-xvasprintf: New file.
* modules/c-xvasprintf-tests: New file.
* lib/c-xasprintf.c: New file.
* lib/c-xvasprintf.c: New file.
* lib/c-xvasprintf.h: New file.
* tests/test-c-xvasprintf.c: New file.
* tests/test-c-xvasprintf.sh: New file.
2012-12-18 Paul Eggert <eggert@cs.ucla.edu>
argp: better 'inline'
/* Formatted output to strings in C locale.
Copyright (C) 1999, 2002, 2006-2007, 2009-2012 Free Software Foundation,
Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include "c-vasprintf.h"
#include <stdarg.h>
int
c_asprintf (char **resultp, const char *format, ...)
{
va_list args;
int result;
va_start (args, format);
result = c_vasprintf (resultp, format, args);
va_end (args);
return result;
}
/* Formatted output to strings in C locale.
Copyright (C) 2004, 2006-2012 Free Software Foundation, Inc.
Written by Simon Josefsson and Paul Eggert.
Modified for C locale by Ben Pfaff.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "c-vasnprintf.h"
/* Print formatted output to string STR. Similar to sprintf, but
additional length SIZE limit how much is written into STR. Returns
string length of formatted string (which may be larger than SIZE).
STR may be NULL, in which case nothing will be written. On error,
return a negative value.
Formatting takes place in the C locale, that is, the decimal point
used in floating-point formatting directives is always '.'. */
int
c_snprintf (char *str, size_t size, const char *format, ...)
{
char *output;
size_t len;
size_t lenbuf = size;
va_list args;
va_start (args, format);
output = c_vasnprintf (str, &lenbuf, format, args);
len = lenbuf;
va_end (args);
if (!output)
return -1;
if (output != str)
{
if (size)
{
size_t pruned_len = (len < size ? len : size - 1);
memcpy (str, output, pruned_len);
str[pruned_len] = '\0';
}
free (output);
}
if (INT_MAX < len)
{
errno = EOVERFLOW;
return -1;
}
return len;
}
/* snprintf in C locale.
Copyright (C) 2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
#ifndef _C_SNPRINTF_H
#define _C_SNPRINTF_H
/* Get size_t. */
#include <stddef.h>
/* The __attribute__ feature is available in gcc versions 2.5 and later.
The __-protected variants of the attributes 'format' and 'printf' are
accepted by gcc versions 2.6.4 (effectively 2.7) and later.
We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
gnulib and libintl do '#define printf __printf__' when they override
the 'printf' function. */
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
# define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
#else
# define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
#endif
#ifdef __cplusplus
extern "C" {
#endif
int c_snprintf (char *str, size_t size, const char *format, ...)
_GL_ATTRIBUTE_FORMAT ((__printf__, 3, 4));
#ifdef __cplusplus
}
#endif
#endif /* _C_SNPRINTF_H */
/* Formatted output to strings in C locale.
Copyright (C) 2009-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <string.h>
#include "printf-parse.h"
#define VASNPRINTF c_vasnprintf
#define FCHAR_T char
#define DCHAR_T char
#define DIRECTIVE char_directive
#define DIRECTIVES char_directives
#define PRINTF_PARSE printf_parse
#define DCHAR_CPY memcpy
#define DCHAR_SET memset
#define DCHAR_IS_TCHAR 1
#define TCHAR_T char
#define NEED_PRINTF_DOUBLE 1
#define NEED_PRINTF_LONG_DOUBLE 1
#define decimal_point_char_defined 1
static char
decimal_point_char (void)
{
return '.';
}
#include "vasnprintf.c"
/* vsprintf with automatic memory allocation in C locale.
Copyright (C) 2002-2004, 2007-2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
#ifndef _C_VASNPRINTF_H
#define _C_VASNPRINTF_H
/* Get va_list. */
#include <stdarg.h>
/* Get size_t. */
#include <stddef.h>
/* The __attribute__ feature is available in gcc versions 2.5 and later.
The __-protected variants of the attributes 'format' and 'printf' are
accepted by gcc versions 2.6.4 (effectively 2.7) and later.
We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
gnulib and libintl do '#define printf __printf__' when they override
the 'printf' function. */
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
# define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
#else
# define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* Write formatted output to a string dynamically allocated with malloc().
You can pass a preallocated buffer for the result in RESULTBUF and its
size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
If successful, return the address of the string (this may be = RESULTBUF
if no dynamic memory allocation was necessary) and set *LENGTHP to the
number of resulting bytes, excluding the trailing NUL. Upon error, set
errno and return NULL.
When dynamic memory allocation occurs, the preallocated buffer is left
alone (with possibly modified contents). This makes it possible to use
a statically allocated or stack-allocated buffer, like this:
char buf[100];
size_t len = sizeof (buf);
char *output = vasnprintf (buf, &len, format, args);
if (output == NULL)
... error handling ...;
else
{
... use the output string ...;
if (output != buf)
free (output);
}
Formatting takes place in the C locale, that is, the decimal point used in
floating-point formatting directives is always '.'.
*/
extern char *c_vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT ((__printf__, 3, 0));
#ifdef __cplusplus
}
#endif
#endif /* _C_VASNPRINTF_H */
/* Formatted output to strings in C locale.
Copyright (C) 1999, 2002, 2006-2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include "c-vasprintf.h"
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include "c-vasnprintf.h"
int
c_vasprintf (char **resultp, const char *format, va_list args)
{
size_t length;
char *result = c_vasnprintf (NULL, &length, format, args);
if (result == NULL)
return -1;
if (length > INT_MAX)
{
free (result);
errno = EOVERFLOW;
return -1;
}
*resultp = result;
/* Return the number of resulting bytes, excluding the trailing NUL. */
return length;
}
/* vasprintf and asprintf, in C locale.
Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _C_VASPRINTF_H
#define _C_VASPRINTF_H
/* Get va_list. */
#include <stdarg.h>
/* The __attribute__ feature is available in gcc versions 2.5 and later.
The __-protected variants of the attributes 'format' and 'printf' are
accepted by gcc versions 2.6.4 (effectively 2.7) and later.
We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
gnulib and libintl do '#define printf __printf__' when they override
the 'printf' function. */
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
# define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
#else
# define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* asprintf() and vasprintf(), but formatting takes place in the C locale, that
is, the decimal point used in floating-point formatting directives is always
'.'. */
int c_asprintf (char **resultp, const char *format, ...);
_GL_ATTRIBUTE_FORMAT ((__printf__, 2, 3));
int c_vasprintf (char **resultp, const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT ((__printf__, 2, 0));
#ifdef __cplusplus
}
#endif
#endif /* _C_VASPRINTF_H */
/* Formatted output to strings in C locale.
Copyright (C) 2004, 2006-2012 Free Software Foundation, Inc.
Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
Modified for C locale by Ben Pfaff.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* Specification. */
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "c-vasnprintf.h"
/* Print formatted output to string STR. Similar to vsprintf, but
additional length SIZE limit how much is written into STR. Returns
string length of formatted string (which may be larger than SIZE).
STR may be NULL, in which case nothing will be written. On error,
return a negative value.
Formatting takes place in the C locale, that is, the decimal point
used in floating-point formatting directives is always '.'. */
int
c_vsnprintf (char *str, size_t size, const char *format, va_list args)
{
char *output;
size_t len;
size_t lenbuf = size;
output = c_vasnprintf (str, &lenbuf, format, args);
len = lenbuf;
if (!output)
return -1;
if (output != str)
{
if (size)
{
size_t pruned_len = (len < size ? len : size - 1);
memcpy (str, output, pruned_len);
str[pruned_len] = '\0';
}
free (output);
}
if (len > INT_MAX)
{
errno = EOVERFLOW;
return -1;
}
return len;
}
/* vsnprintf in C locale.
Copyright (C) 2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
#ifndef _C_VSNPRINTF_H
#define _C_VSNPRINTF_H
/* Get size_t. */
#include <stddef.h>
/* Get va_list. */
#include <stdarg.h>
/* The __attribute__ feature is available in gcc versions 2.5 and later.
The __-protected variants of the attributes 'format' and 'printf' are
accepted by gcc versions 2.6.4 (effectively 2.7) and later.
We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
gnulib and libintl do '#define printf __printf__' when they override
the 'printf' function. */
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
# define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
#else
# define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
#endif
#ifdef __cplusplus
extern "C" {
#endif
int c_vsnprintf (char *str, size_t size, const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT ((__printf__, 3, 0));
#ifdef __cplusplus
}
#endif
#endif /* _C_VSNPRINTF_H */
/* vasprintf and asprintf with out-of-memory checking in C locale.
Copyright (C) 1999, 2002-2004, 2006, 2009-2012 Free Software Foundation,
Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include "c-xvasprintf.h"
char *
c_xasprintf (const char *format, ...)
{
va_list args;
char *result;
va_start (args, format);
result = c_xvasprintf (format, args);
va_end (args);
return result;
}
/* vasprintf with out-of-memory checking in C locale.
Copyright (C) 1999, 2002-2004, 2006-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include "c-xvasprintf.h"
#include <errno.h>
#include <stdio.h>
#include "xalloc.h"
char *
c_xvasprintf (const char *format, va_list args)
{
char *result;
if (c_vasprintf (&result, format, args) < 0)
{
if (errno == ENOMEM)
xalloc_die ();
return NULL;
}
return result;
}
/* vasprintf and asprintf, with out-of-memory checking, in C locale.
Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _C_XVASPRINTF_H
#define _C_XVASPRINTF_H
/* Get va_list. */
#include <stdarg.h>
/* The __attribute__ feature is available in gcc versions 2.5 and later.
The __-protected variants of the attributes 'format' and 'printf' are
accepted by gcc versions 2.6.4 (effectively 2.7) and later.
We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
gnulib and libintl do '#define printf __printf__' when they override
the 'printf' function. */
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
# define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
#else
# define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* Write formatted output to a string dynamically allocated with malloc(),
and return it. Upon [ENOMEM] memory allocation error, call xalloc_die.
On some other error
- [EOVERFLOW] resulting string length is > INT_MAX,
- [EINVAL] invalid format string,
- [EILSEQ] error during conversion between wide and multibyte characters,
return NULL.
Formatting takes place in the C locale, that is, the decimal point
used in floating-point formatting directives is always '.'. */
extern char *c_xasprintf (const char *format, ...)
_GL_ATTRIBUTE_FORMAT ((__printf__, 1, 2));
extern char *c_xvasprintf (const char *format, va_list args)
_GL_ATTRIBUTE_FORMAT ((__printf__, 1, 0));
#ifdef __cplusplus
}
#endif
#endif /* _C_XVASPRINTF_H */
Description:
c_snprintf(): print formatted output to a fixed length string in C locale
Files:
lib/c-snprintf.h
lib/c-snprintf.c
Depends-on:
c-vasnprintf
configure.ac:
Makefile.am:
lib_SOURCES += c-snprintf.c
Include:
"c-snprintf.h"
License:
GPL
Maintainer:
Ben Pfaff
Files:
tests/test-c-snprintf.c
tests/test-c-snprintf.sh
m4/locale-fr.m4
tests/macros.h
Depends-on:
setlocale
snprintf
configure.ac:
gt_LOCALE_FR
Makefile.am:
TESTS += test-c-snprintf.sh
check_PROGRAMS += test-c-snprintf
TESTS_ENVIRONMENT += LOCALE_FR='@LOCALE_FR@'
Description:
Formatted output to strings in C locale.
Files:
lib/c-vasnprintf.h
lib/c-vasnprintf.c
lib/float+.h
lib/printf-args.h
lib/printf-args.c
lib/printf-parse.h
lib/printf-parse.c
lib/vasnprintf.h
lib/vasnprintf.c
m4/wchar_t.m4
m4/wint_t.m4
m4/longlong.m4
m4/intmax_t.m4
m4/stdint_h.m4
m4/inttypes_h.m4
m4/vasnprintf.m4
m4/printf.m4
m4/math_h.m4
m4/exponentd.m4
Depends-on:
isnand-nolibm
isnanl-nolibm
frexpl-nolibm
printf-frexp
printf-frexpl
signbit
fpucw
nocrash
printf-safe
alloca-opt
xsize
errno
memchr
multiarch