Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/stdio PR/49279: Justin Cormack: add open_memstream



details:   https://anonhg.NetBSD.org/src/rev/1c201f8c1f03
branches:  trunk
changeset: 803116:1c201f8c1f03
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Oct 13 00:40:36 2014 +0000

description:
PR/49279: Justin Cormack: add open_memstream

diffstat:

 lib/libc/stdio/Makefile.inc      |    7 +-
 lib/libc/stdio/open_memstream.3  |  156 ++++++++++++++++++++++
 lib/libc/stdio/open_memstream.c  |  214 ++++++++++++++++++++++++++++++
 lib/libc/stdio/open_wmemstream.c |  273 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 647 insertions(+), 3 deletions(-)

diffs (truncated from 688 to 300 lines):

diff -r 28462101b8a6 -r 1c201f8c1f03 lib/libc/stdio/Makefile.inc
--- a/lib/libc/stdio/Makefile.inc       Mon Oct 13 00:25:12 2014 +0000
+++ b/lib/libc/stdio/Makefile.inc       Mon Oct 13 00:40:36 2014 +0000
@@ -1,5 +1,5 @@
 #      from: @(#)Makefile.inc  5.7 (Berkeley) 6/27/91
-#      $NetBSD: Makefile.inc,v 1.45 2014/06/18 17:50:55 christos Exp $
+#      $NetBSD: Makefile.inc,v 1.46 2014/10/13 00:40:36 christos Exp $
 
 # stdio sources
 .PATH: ${.CURDIR}/stdio
@@ -21,7 +21,7 @@
        vasprintf.c vdprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c \
        vprintf.c vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c \
        vswscanf.c vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c
-SRCS+= fmemopen.c
+SRCS+= fmemopen.c open_memstream.c open_wmemstream.c
 
 .if !defined(AUDIT)
 SRCS+= gets.c vsprintf.c tempnam.c tmpnam.c mktemp.c
@@ -31,7 +31,7 @@
        flockfile.3 fmemopen.3 fopen.3 fparseln.3 fputs.3 fputws.3 fread.3 \
        fseek.3 funopen.3 fwide.3 getc.3 getdelim.3 getwc.3 mktemp.3 printf.3 \
        putc.3 putwc.3 remove.3 scanf.3 setbuf.3 stdio.3 tmpnam.3 \
-       ungetc.3 ungetwc.3 wprintf.3 wscanf.3
+       ungetc.3 ungetwc.3 wprintf.3 wscanf.3 open_memstream.3
 
 MLINKS+=ferror.3 clearerr.3 ferror.3 feof.3 ferror.3 fileno.3
 MLINKS+=fflush.3 fpurge.3
@@ -66,3 +66,4 @@
 MLINKS+=wprintf.3 vwprintf.3 wprintf.3 vfwprintf.3 wprintf.3 vswprintf.3
 MLINKS+=wscanf.3 fwscanf.3 wscanf.3 swscanf.3 wscanf.3 vwscanf.3
 MLINKS+=wscanf.3 vswscanf.3 wscanf.3 vfwscanf.3
+MLINKS+=open_memstream.3 open_wmemstream.3
diff -r 28462101b8a6 -r 1c201f8c1f03 lib/libc/stdio/open_memstream.3
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/stdio/open_memstream.3   Mon Oct 13 00:40:36 2014 +0000
@@ -0,0 +1,156 @@
+.\"    $NetBSD: open_memstream.3,v 1.1 2014/10/13 00:40:36 christos Exp $
+.\" Copyright (c) 2013 Advanced Computing Technologies LLC
+.\" Written by: John H. Baldwin <jhb%FreeBSD.org@localhost>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD: head/lib/libc/stdio/open_memstream.3 247415 2013-02-27 20:09:25Z joel $
+.\"
+.Dd October 12, 2014
+.Dt OPEN_MEMSTREAM 3
+.Os
+.Sh NAME
+.Nm open_memstream ,
+.Nm open_wmemstream
+.Nd dynamic memory buffer stream open functions
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In stdio.h
+.Ft FILE *
+.Fn open_memstream "char **bufp" "size_t **sizep"
+.In wchar.h
+.Ft FILE *
+.Fn open_wmemstream "wchar_t **bufp" "size_t **sizep"
+.Sh DESCRIPTION
+The
+.Fn open_memstream
+and
+.Fn open_wmemstream
+functions create a write-only, seekable stream backed by a dynamically
+allocated memory buffer.
+The
+.Fn open_memstream
+function creates a byte-oriented stream,
+while the
+.Fn open_wmemstream
+function creates a wide-oriented stream.
+.Pp
+Each stream maintains a current position and size.
+Initially,
+the position and size are set to zero.
+Each write begins at the current position and advances it the number of
+successfully written bytes for
+.Fn open_memstream
+or wide characters for
+.Fn open_wmemstream .
+If a write moves the current position beyond the length of the buffer,
+the length of the buffer is extended and a null character is appended to the
+buffer.
+.Pp
+A stream's buffer always contains a null character at the end of the buffer
+that is not included in the current length.
+.Pp
+If a stream's current position is moved beyond the current length via a
+seek operation and a write is performed,
+the characters between the current length and the current position are filled
+with null characters before the write is performed.
+.Pp
+After a successful call to
+.Xr fclose 3
+or
+.Xr fflush 3 ,
+the pointer referenced by
+.Fa bufp
+will contain the start of the memory buffer and the variable referenced by
+.Fa sizep
+will contain the smaller of the current position and the current buffer length.
+.Pp
+After a successful call to
+.Xr fflush 3,
+the pointer referenced by
+.Fa bufp
+and the variable referenced by
+.Fa sizep
+are only valid until the next write operation or a call to
+.Xr fclose 3.
+.Pp
+Once a stream is closed,
+the allocated buffer referenced by
+.Fa bufp
+should be released via a call to
+.Xr free 3
+when it is no longer needed.
+.Sh IMPLEMENTATION NOTES
+Internally all I/O streams are effectively byte-oriented,
+so using wide-oriented operations to write to a stream opened via
+.Fn open_wmemstream
+results in wide characters being expanded to a stream of multibyte characters
+in stdio's internal buffers.
+These multibyte characters are then converted back to wide characters when
+written into the stream.
+As a result,
+the wide-oriented streams maintain an internal multibyte character conversion
+state that is cleared on any seek opertion that changes the current position.
+This should have no effect as long as wide-oriented output operations are used
+on a wide-oriented stream.
+.Sh RETURN VALUES
+Upon successful completion,
+.Fn open_memstream
+and
+.Fn open_wmemstream
+return a
+.Tn FILE
+pointer.
+Otherwise,
+.Dv NULL
+is returned and the global variable
+.Va errno
+is set to indicate the error.
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The
+.Fa bufp
+or
+.Fa sizep
+argument was
+.Dv NULL .
+.It Bq Er ENOMEM
+Memory for the stream or buffer could not be allocated.
+.El
+.Sh SEE ALSO
+.Xr fclose 3 ,
+.Xr fflush 3 ,
+.Xr fopen 3 ,
+.Xr free 3 ,
+.Xr fseek 3 ,
+.Xr sbuf 3 ,
+.Xr stdio 3
+.Sh STANDARDS
+The
+.Fn open_memstream
+and
+.Fn open_wmemstream
+functions conform to
+.St -p1003.1-2008 .
diff -r 28462101b8a6 -r 1c201f8c1f03 lib/libc/stdio/open_memstream.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/stdio/open_memstream.c   Mon Oct 13 00:40:36 2014 +0000
@@ -0,0 +1,214 @@
+/*     $NetBSD: open_memstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $      */
+
+/*-
+ * Copyright (c) 2013 Advanced Computing Technologies LLC
+ * Written by: John H. Baldwin <jhb%FreeBSD.org@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#if 0
+__FBSDID("$FreeBSD: head/lib/libc/stdio/open_memstream.c 247411 2013-02-27 19:50:46Z jhb $");
+#endif
+__RCSID("$NetBSD: open_memstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $");
+
+#include "namespace.h"
+#include <assert.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+#define        OFF_MAX LLONG_MAX
+
+struct memstream {
+       char **bufp;
+       size_t *sizep;
+       size_t len;
+       size_t offset;
+};
+
+static int
+memstream_grow(struct memstream *ms, off_t newoff)
+{
+       char *buf;
+       size_t newsize;
+
+       if (newoff < 0 || newoff >= SSIZE_MAX)
+               newsize = SSIZE_MAX - 1;
+       else
+               newsize = newoff;
+       if (newsize > ms->len) {
+               buf = realloc(*ms->bufp, newsize + 1);
+               if (buf != NULL) {
+#ifdef DEBUG
+                       fprintf(stderr, "MS: %p growing from %zd to %zd\n",
+                           ms, ms->len, newsize);
+#endif
+                       memset(buf + ms->len + 1, 0, newsize - ms->len);
+                       *ms->bufp = buf;
+                       ms->len = newsize;
+                       return (1);
+               }
+               return (0);
+       }
+       return (1);
+}
+
+static void
+memstream_update(struct memstream *ms)
+{
+
+       *ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
+}
+
+static ssize_t
+memstream_write(void *cookie, const void *buf, size_t len)
+{
+       struct memstream *ms;
+       size_t tocopy;
+       off_t more;
+
+       ms = cookie;
+       more = ms->offset;
+       more += len;
+       if (!memstream_grow(ms, more))
+               return (-1);
+       tocopy = ms->len - ms->offset;
+       if (len < tocopy)
+               tocopy = len;
+       memcpy(*ms->bufp + ms->offset, buf, tocopy);



Home | Main Index | Thread Index | Old Index