Here's a new module that adds a function quotearg_file:
like quotearg_buffer but it writes the output to a FILE
instead. This prevents additional memory allocation if you
are quoting large data. And it provides a simpler API.

Regards,

Oskar Liljeblad (os...@osk.mine.nu)
diff -u1 ChangeLog.v0 ChangeLog
--- ChangeLog.v0	2012-10-21 00:10:14.043857761 +0200
+++ ChangeLog	2012-10-21 00:16:32.119146504 +0200
@@ -1 +1,15 @@
+2012-10-21  Oskar Liljeblad  <os...@osk.mine.nu>
+
+	quotearg-file: new module
+	* lib/quotearg-file.c:
+	New file: set QUOTEARG_FILE and include quotearg.c.
+	* lib/quotearg-file.h: New file.
+	* lib/quotearg.c:
+	When QUOTEARG_FILE is defined, generate only quotearg_file_restyled
+	and quotearg_file non-static functions. Otherwise everything is as
+	before, with the exception of default_quoting_options which is no
+	longer static to prevent a duplicate symbol in quotearg-file.o.
+	* modules/quotearg-file: New file.
+	* MODULES.html.sh (Misc): Add it.
+
 2012-10-16  Paul Eggert  <egg...@cs.ucla.edu>
diff -u -p /dev/null lib/quotearg-file.c
--- /dev/null   2012-08-16 17:10:46.538105184 +0200
+++ lib/quotearg-file.c 2012-10-20 23:40:18.696655540 +0200
@@ -0,0 +1,19 @@
+/* quotearg-file.c - quote arguments for output, writing result to file
+
+   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 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/>.  */
+
+#define QUOTEARG_FILE
+#include "quotearg.c"
diff -u /dev/null lib/quotearg-file.h
--- /dev/null	2012-08-16 17:10:46.538105184 +0200
+++ lib/quotearg-file.h	2012-10-20 23:56:34.364873603 +0200
@@ -0,0 +1,34 @@
+/* quotearg-file.h - prototypes for quotearg-file.c
+
+   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 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 QUOTEARG_FILE_H_
+# define QUOTEARG_FILE_H_ 1
+
+#include "quotearg.h"
+
+/* Write to file handle FH a quoted version of
+   argument ARG (of size ARGSIZE), using O to control quoting.
+   If O is null, use the default.
+   Return the written size of the output, or EOF if there was an
+   error.
+   If ARGSIZE is SIZE_MAX, use the string length of the argument for
+   ARGSIZE.  */
+ssize_t
+quotearg_file (FILE *fh, char const *arg, size_t argsize,
+               struct quoting_options const *o);
+
+#endif /* !QUOTEARG_FILE_H_ */
diff -u -p quotearg.c.v0 quotearg.c
--- lib/quotearg.c.v0	2012-10-21 00:28:50.841568855 +0200
+++ lib/quotearg.c	2012-10-21 00:30:18.078326996 +0200
@@ -37,6 +37,9 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdbool.h>
+#ifdef QUOTEARG_FILE
+#include <stdio.h>
+#endif
 #include <stdlib.h>
 #include <string.h>
 #include <wchar.h>
@@ -71,6 +74,7 @@ struct quoting_options
   char const *right_quote;
 };
 
+#ifndef QUOTEARG_FILE
 /* Names of quoting styles.  */
 char const *const quoting_style_args[] =
 {
@@ -97,10 +101,16 @@ enum quoting_style const quoting_style_v
   locale_quoting_style,
   clocale_quoting_style
 };
+#endif /* !defined QUOTEARG_FILE */
 
 /* The default quoting options.  */
-static struct quoting_options default_quoting_options;
+#ifndef QUOTEARG_FILE
+struct quoting_options default_quoting_options;
+#else
+extern struct quoting_options default_quoting_options;
+#endif
 
+#ifndef QUOTEARG_FILE
 /* Allocate a new set of quoting options, with contents initially identical
    to O if O is not null, or to the default if O is null.
    It is the caller's responsibility to free the result.  */
@@ -184,6 +194,7 @@ quoting_options_from_style (enum quoting
   o.style = style;
   return o;
 }
+#endif /* !defined QUOTEARG_FILE */
 
 /* MSGID approximates a quotation mark.  Return its translation if it
    has one; otherwise, return either it or "\"", depending on S.
@@ -238,8 +249,13 @@ gettext_quote (char const *msgid, enum q
    ARGSIZE, O), except it breaks O into its component pieces and is
    not careful about errno.  */
 
+#ifndef QUOTEARG_FILE
 static size_t
 quotearg_buffer_restyled (char *buffer, size_t buffersize,
+#else
+static ssize_t
+quotearg_file_restyled   (FILE *fh,
+#endif
                           char const *arg, size_t argsize,
                           enum quoting_style quoting_style, int flags,
                           unsigned int const *quote_these_too,
@@ -254,6 +270,7 @@ quotearg_buffer_restyled (char *buffer,
   bool unibyte_locale = MB_CUR_MAX == 1;
   bool elide_outer_quotes = (flags & QA_ELIDE_OUTER_QUOTES) != 0;
 
+#ifndef QUOTEARG_FILE
 #define STORE(c) \
     do \
       { \
@@ -262,6 +279,16 @@ quotearg_buffer_restyled (char *buffer,
         len++; \
       } \
     while (0)
+#else
+#define STORE(c) \
+    do \
+      { \
+        if (fputc((c), fh) == EOF) \
+          return EOF; \
+        len++; \
+      } \
+    while (0)
+#endif
 
   switch (quoting_style)
     {
@@ -642,15 +669,21 @@ quotearg_buffer_restyled (char *buffer,
     for (; *quote_string; quote_string++)
       STORE (*quote_string);
 
+#ifndef QUOTEARG_FILE
   if (len < buffersize)
     buffer[len] = '\0';
+#endif
   return len;
 
  force_outer_quoting_style:
   /* Don't reuse quote_these_too, since the addition of outer quotes
      sufficiently quotes the specified characters.  */
-  return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
-                                   quoting_style,
+#ifndef QUOTEARG_FILE
+  return quotearg_buffer_restyled (buffer, buffersize, 
+#else
+  return quotearg_file_restyled   (fh,
+#endif
+                                   arg, argsize, quoting_style,
                                    flags & ~QA_ELIDE_OUTER_QUOTES, NULL,
                                    left_quote, right_quote);
 }
@@ -664,6 +697,7 @@ quotearg_buffer_restyled (char *buffer,
    value that would have been returned had BUFFERSIZE been large enough.
    If ARGSIZE is SIZE_MAX, use the string length of the argument for
    ARGSIZE.  */
+#ifndef QUOTEARG_FILE
 size_t
 quotearg_buffer (char *buffer, size_t buffersize,
                  char const *arg, size_t argsize,
@@ -677,7 +711,23 @@ quotearg_buffer (char *buffer, size_t bu
   errno = e;
   return r;
 }
+#else
+ssize_t
+quotearg_file (FILE *fh, char const *arg, size_t argsize,
+               struct quoting_options const *o)
+{
+  struct quoting_options const *p = o ? o : &default_quoting_options;
+  int e = errno;
+  ssize_t r = quotearg_file_restyled (fh, arg, argsize, p->style, p->flags,
+                                      p->quote_these_too, p->left_quote,
+                                      p->right_quote);
+  if (r != EOF)
+    errno = e;
+  return r;
+}
+#endif
 
+#ifndef QUOTEARG_FILE
 /* Equivalent to quotearg_alloc (ARG, ARGSIZE, NULL, O).  */
 char *
 quotearg_alloc (char const *arg, size_t argsize,
@@ -949,3 +999,4 @@ quote (char const *name)
 {
   return quote_n (0, name);
 }
+#endif /* !defined QUOTEARG_FILE*/
diff -u /dev/null modules/quotearg-file
--- /dev/null	2012-08-16 17:10:46.538105184 +0200
+++ modules/quotearg-file	2012-10-20 23:59:42.554450663 +0200
@@ -0,0 +1,23 @@
+Description:
+Quote arguments for use in error messages, writing the result to a file.
+
+Files:
+lib/quotearg-file.h
+lib/quotearg-file.c
+
+Depends-on:
+quotearg
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += quotearg-file.c
+
+Include:
+"quotearg-file.h"
+
+License:
+GPL
+
+Maintainer:
+Oskar Liljeblad
diff -u MODULES.html.sh.v0 MODULES.html.sh
--- MODULES.html.sh.v0  2012-10-21 00:02:21.195775367 +0200
+++ MODULES.html.sh     2012-10-21 00:02:33.607879091 +0200
@@ -3454,6 +3454,7 @@
   func_module ptsname_r
   func_module pty
   func_module quotearg
+  func_module quotearg-file
   func_module quote
   func_module readutmp
   func_module random_r

Reply via email to