]> Savannah Git Hosting - gnulib.git/commitdiff
obstack-zprintf: New module.
authorBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 19:31:46 +0000 (21:31 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 19:31:46 +0000 (21:31 +0200)
* lib/stdio.in.h (obstack_zprintf, obstack_vzprintf): New declarations.
(obstack_printf, obstack_vprintf): Tweak comment.
* lib/obstack_printf.c: Parameterize.
(RESULT_TYPE, OBSTACK_PRINTF, OBSTACK_VPRINTF): New macros.
* lib/obstack_zprintf.c: New file.
* m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
GNULIB_OBSTACK_ZPRINTF.
* modules/stdio (Makefile.am): Substitute GNULIB_OBSTACK_ZPRINTF.
* modules/obstack-zprintf: New file.

ChangeLog
lib/obstack_printf.c
lib/obstack_zprintf.c [new file with mode: 0644]
lib/stdio.in.h
m4/stdio_h.m4
modules/obstack-zprintf [new file with mode: 0644]
modules/stdio

index 8d28459959d126d647c59a4097b5bcbaccd10230..156dd401383503836046b27b0400ee30272d5612 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-06-22  Bruno Haible  <bruno@clisp.org>
+
+       obstack-zprintf: New module.
+       * lib/stdio.in.h (obstack_zprintf, obstack_vzprintf): New declarations.
+       (obstack_printf, obstack_vprintf): Tweak comment.
+       * lib/obstack_printf.c: Parameterize.
+       (RESULT_TYPE, OBSTACK_PRINTF, OBSTACK_VPRINTF): New macros.
+       * lib/obstack_zprintf.c: New file.
+       * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
+       GNULIB_OBSTACK_ZPRINTF.
+       * modules/stdio (Makefile.am): Substitute GNULIB_OBSTACK_ZPRINTF.
+       * modules/obstack-zprintf: New file.
+
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
        physmem: Fix typo.
index 57979e35093e1f4e2b86b3e75a2060f120657797..ed996dd180cd1bc7b4c7d4ba50ce2433c1472168 100644 (file)
 #include <stdarg.h>
 #include <stdlib.h>
 
+#ifndef RESULT_TYPE
+# define RESULT_TYPE int
+# define OBSTACK_PRINTF obstack_printf
+# define OBSTACK_VPRINTF obstack_vprintf
+#endif
+
 /* Grow an obstack with formatted output.  Return the number of bytes
    added to OBS.  No trailing nul byte is added, and the object should
    be closed with obstack_finish before use.
 
    Upon memory allocation error, call obstack_alloc_failed_handler.
    Upon other error, return -1.  */
-int
-obstack_printf (struct obstack *obs, const char *format, ...)
+RESULT_TYPE
+OBSTACK_PRINTF (struct obstack *obs, const char *format, ...)
 {
   va_list args;
-  int result;
+  RESULT_TYPE result;
 
   va_start (args, format);
-  result = obstack_vprintf (obs, format, args);
+  result = OBSTACK_VPRINTF (obs, format, args);
   va_end (args);
   return result;
 }
@@ -50,8 +56,8 @@ obstack_printf (struct obstack *obs, const char *format, ...)
 
    Upon memory allocation error, call obstack_alloc_failed_handler.
    Upon other error, return -1.  */
-int
-obstack_vprintf (struct obstack *obs, const char *format, va_list args)
+RESULT_TYPE
+OBSTACK_VPRINTF (struct obstack *obs, const char *format, va_list args)
 {
   /* If we are close to the end of the current obstack chunk, use a
      stack-allocated buffer and copy, to reduce the likelihood of a
diff --git a/lib/obstack_zprintf.c b/lib/obstack_zprintf.c
new file mode 100644 (file)
index 0000000..b802f4d
--- /dev/null
@@ -0,0 +1,20 @@
+/* Formatted output to obstacks.
+   Copyright (C) 2008-2024 Free Software Foundation, Inc.
+
+   This file 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 file 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 <https://www.gnu.org/licenses/>.  */
+
+#define RESULT_TYPE ptrdiff_t
+#define OBSTACK_PRINTF obstack_zprintf
+#define OBSTACK_VPRINTF obstack_vzprintf
+#include "obstack_printf.c"
index 4844ea1ebfd985b7310986b95407e6cc90c97480..cf2d8c999bcada55766a35d28c511af19f6dc128 100644 (file)
@@ -1075,13 +1075,39 @@ _GL_CXXALIASWARN (getw);
 # endif
 #endif
 
+#if @GNULIB_OBSTACK_ZPRINTF@
+struct obstack;
+/* Grows an obstack with formatted output.  Returns the number of
+   bytes added to OBS.  No trailing nul byte is added, and the
+   object should be closed with obstack_finish before use.
+   Upon memory allocation error, calls obstack_alloc_failed_handler.
+   Upon other error, returns -1 with errno set.
+
+   Failure code EOVERFLOW can only occur when a width > INT_MAX is used.
+   Therefore, if the format string is valid and does not use %ls/%lc
+   directives nor widths, the only possible failure code is through
+   obstack_alloc_failed_handler.  */
+_GL_FUNCDECL_SYS (obstack_zprintf, ptrdiff_t,
+                  (struct obstack *obs, const char *format, ...)
+                  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3)
+                  _GL_ARG_NONNULL ((1, 2)));
+_GL_CXXALIAS_SYS (obstack_zprintf, ptrdiff_t,
+                  (struct obstack *obs, const char *format, ...));
+_GL_FUNCDECL_SYS (obstack_vzprintf, ptrdiff_t,
+                  (struct obstack *obs, const char *format, va_list args)
+                  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 0)
+                  _GL_ARG_NONNULL ((1, 2)));
+_GL_CXXALIAS_SYS (obstack_vzprintf, ptrdiff_t,
+                  (struct obstack *obs, const char *format, va_list args));
+#endif
+
 #if @GNULIB_OBSTACK_PRINTF@ || @GNULIB_OBSTACK_PRINTF_POSIX@
 struct obstack;
-/* Grow an obstack with formatted output.  Return the number of
+/* Grows an obstack with formatted output.  Returns the number of
    bytes added to OBS.  No trailing nul byte is added, and the
-   object should be closed with obstack_finish before use.  Upon
-   memory allocation error, call obstack_alloc_failed_handler.  Upon
-   other error, return -1.  */
+   object should be closed with obstack_finish before use.
+   Upon memory allocation error, calls obstack_alloc_failed_handler.
+   Upon other error, returns -1.  */
 # if @REPLACE_OBSTACK_PRINTF@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
 #   define obstack_printf rpl_obstack_printf
index aa06d77027eb76adc439ec7e101c9fe440188963..10e1fbb8aa9073baa116474acfa4bfed9de4af1b 100644 (file)
@@ -1,5 +1,5 @@
 # stdio_h.m4
-# serial 68
+# serial 69
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -159,6 +159,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETLINE])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_OBSTACK_PRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_OBSTACK_PRINTF_POSIX])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_OBSTACK_ZPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_PCLOSE])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_PERROR])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_POPEN])
diff --git a/modules/obstack-zprintf b/modules/obstack-zprintf
new file mode 100644 (file)
index 0000000..d78196f
--- /dev/null
@@ -0,0 +1,26 @@
+Description:
+Formatted printing into an obstack (without INT_MAX limitation).
+
+Files:
+lib/obstack_zprintf.c
+lib/obstack_printf.c
+
+Depends-on:
+obstack
+stdio
+vasnprintf
+
+configure.ac:
+gl_STDIO_MODULE_INDICATOR([obstack-zprintf])
+
+Makefile.am:
+lib_SOURCES += obstack_zprintf.c
+
+Include:
+<stdio.h>
+
+License:
+GPL
+
+Maintainer:
+all
index b0cf4ea2078054577f7fdbe6c811c787798bebc6..64fdc5fb60476b68793292d8a059605d02438124 100644 (file)
@@ -94,6 +94,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H)
              -e 's/@''GNULIB_GETLINE''@/$(GNULIB_GETLINE)/g' \
              -e 's/@''GNULIB_OBSTACK_PRINTF''@/$(GNULIB_OBSTACK_PRINTF)/g' \
              -e 's/@''GNULIB_OBSTACK_PRINTF_POSIX''@/$(GNULIB_OBSTACK_PRINTF_POSIX)/g' \
+             -e 's/@''GNULIB_OBSTACK_ZPRINTF''@/$(GNULIB_OBSTACK_ZPRINTF)/g' \
              -e 's/@''GNULIB_PCLOSE''@/$(GNULIB_PCLOSE)/g' \
              -e 's/@''GNULIB_PERROR''@/$(GNULIB_PERROR)/g' \
              -e 's/@''GNULIB_POPEN''@/$(GNULIB_POPEN)/g' \