about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2006-01-12 02:03:42 +0000
committerUlrich Drepper <drepper@redhat.com>2006-01-12 02:03:42 +0000
commitbb10459f2d534f67ebe771bed85e4ebfcd9eacd1 (patch)
treed8f06d6ce03d5289e07b68f0d1726b31a6d9339d
parent37169cccddb401c9efeac446703dff7f815b5d1d (diff)
downloadglibc-bb10459f2d534f67ebe771bed85e4ebfcd9eacd1.tar.gz
glibc-bb10459f2d534f67ebe771bed85e4ebfcd9eacd1.tar.xz
glibc-bb10459f2d534f67ebe771bed85e4ebfcd9eacd1.zip
* libio/wmemstream.c: New file.
	* libio/Makefile (routines): Add wmemstream.
	(tests): Add tst-memstream1, tst-memstream2, tst-wmemstream2, and
	tst-wmemstream2.
	* libio/tst-memstream1.c: New file.
	* libio/tst-memstream2.c: New file.
	* libio/tst-wmemstream1.c: New file.
	* libio/tst-wmemstream2.c: New file.

	* libio/memstream.c (_IO_mem_sync): Remove useless call to
	_IO_default_sync.
-rw-r--r--ChangeLog12
-rw-r--r--libio/memstream.c5
-rw-r--r--libio/tst-memstream1.c89
-rw-r--r--libio/tst-memstream2.c104
-rw-r--r--libio/tst-wmemstream1.c5
-rw-r--r--libio/tst-wmemstream2.c5
-rw-r--r--libio/wmemstream.c149
7 files changed, 364 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index aca264267e..baf26ddf1f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2006-01-11  Ulrich Drepper  <drepper@redhat.com>
 
+	* libio/wmemstream.c: New file.
+	* libio/Makefile (routines): Add wmemstream.
+	(tests): Add tst-memstream1, tst-memstream2, tst-wmemstream2, and
+	tst-wmemstream2.
+	* libio/tst-memstream1.c: New file.
+	* libio/tst-memstream2.c: New file.
+	* libio/tst-wmemstream1.c: New file.
+	* libio/tst-wmemstream2.c: New file.
+
+	* libio/memstream.c (_IO_mem_sync): Remove useless call to
+	_IO_default_sync.
+
 	* elf/check-textrel.c: Add missing argument.
 
 	* Makerules: Define build-module-asneeded.
diff --git a/libio/memstream.c b/libio/memstream.c
index d8e8896144..742da740ef 100644
--- a/libio/memstream.c
+++ b/libio/memstream.c
@@ -106,11 +106,6 @@ _IO_mem_sync (fp)
      _IO_FILE* fp;
 {
   struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
-  int res;
-
-  res = _IO_default_sync (fp);
-  if (res < 0)
-    return res;
 
   if (fp->_IO_write_ptr == fp->_IO_write_end)
     {
diff --git a/libio/tst-memstream1.c b/libio/tst-memstream1.c
new file mode 100644
index 0000000000..d18f5cc22a
--- /dev/null
+++ b/libio/tst-memstream1.c
@@ -0,0 +1,89 @@
+#include <mcheck.h>
+#include <stdio.h>
+
+
+#ifndef CHAR_T
+# define CHAR_T char
+# define W(o) o
+# define OPEN_MEMSTREAM open_memstream
+#endif
+
+#define S(s) S1 (s)
+#define S1(s) #s
+
+
+static void
+mcheck_abort (enum mcheck_status ev)
+{
+  printf ("mecheck failed with status %d\n", (int) ev);
+  exit (1);
+}
+
+
+static int
+do_test (void)
+{
+  mcheck_pedantic (mcheck_abort);
+
+  CHAR_T *buf = (CHAR_T *) 1l;
+  size_t len = 12345;
+  FILE *fp = OPEN_MEMSTREAM (&buf, &len);
+  if (fp == NULL)
+    {
+      printf ("%s failed\n", S(OPEN_MEMSTREAM));
+      return 1;
+    }
+
+  if (fflush (fp) != 0)
+    {
+      puts ("fflush failed");
+      return 1;
+    }
+
+  if (len != 0)
+    {
+      puts ("string after no write not empty");
+      return 1;
+    }
+  if (buf == (CHAR_T *) 1l)
+    {
+      puts ("buf not updated");
+      return 1;
+    }
+  if (buf[0] != W('\0'))
+    {
+      puts ("buf[0] != 0");
+      return 1;
+    }
+
+  buf = (CHAR_T *) 1l;
+  len = 12345;
+  if (fclose (fp) != 0)
+    {
+      puts ("fclose failed");
+      return 1;
+    }
+
+  if (len != 0)
+    {
+      puts ("string after close with no write not empty");
+      return 1;
+    }
+  if (buf == (CHAR_T *) 1l)
+    {
+      puts ("buf not updated");
+      return 1;
+    }
+  if (buf[0] != W('\0'))
+    {
+      puts ("buf[0] != 0");
+      return 1;
+    }
+
+  free (buf);
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/libio/tst-memstream2.c b/libio/tst-memstream2.c
new file mode 100644
index 0000000000..9619d7725d
--- /dev/null
+++ b/libio/tst-memstream2.c
@@ -0,0 +1,104 @@
+#include <mcheck.h>
+#include <stdio.h>
+
+
+#ifndef CHAR_T
+# define CHAR_T char
+# define W(o) o
+# define OPEN_MEMSTREAM open_memstream
+#endif
+
+#define S(s) S1 (s)
+#define S1(s) #s
+
+
+static void
+mcheck_abort (enum mcheck_status ev)
+{
+  printf ("mecheck failed with status %d\n", (int) ev);
+  exit (1);
+}
+
+
+static int
+do_test (void)
+{
+  mcheck_pedantic (mcheck_abort);
+
+  CHAR_T *buf = (CHAR_T *) 1l;
+  size_t len = 12345;
+  FILE *fp = OPEN_MEMSTREAM (&buf, &len);
+  if (fp == NULL)
+    {
+      printf ("%s failed\n", S(OPEN_MEMSTREAM));
+      return 1;
+    }
+
+  for (int outer = 0; outer < 800; ++outer)
+    {
+      for (int inner = 0; inner < 100; ++inner)
+	if (fputc (W('a') + (outer * 100 + inner) % 26, fp) == EOF)
+	  {
+	    printf ("fputc at %d:%d failed\n", outer, inner);
+	    return 1;
+	  }
+
+      if (fflush (fp) != 0)
+	{
+	  puts ("fflush failed");
+	  return 1;
+	}
+
+      if (len != (outer + 1) * 100)
+	{
+	  printf ("string in round %d not %d bytest long\n",
+		  outer + 1, (outer + 1) * 100);
+	  return 1;
+	}
+      if (buf == (CHAR_T *) 1l)
+	{
+	  printf ("round %d: buf not updated\n", outer + 1);
+	  return 1;
+	}
+      for (int inner = 0; inner < (outer + 1) * 100; ++inner)
+	if (buf[inner] != W('a') + inner % 26)
+	  {
+	    printf ("round %d: buf[%d] != '%c'\n", outer + 1, inner,
+		    (char) (W('a') + inner % 26));
+	    return 1;
+	  }
+    }
+
+  buf = (CHAR_T *) 1l;
+  len = 12345;
+  if (fclose (fp) != 0)
+    {
+      puts ("fclose failed");
+      return 1;
+    }
+
+  if (len != 800 * 100)
+    {
+      puts ("string after close not 80000 bytes long");
+      return 1;
+    }
+  if (buf == (CHAR_T *) 1l)
+    {
+      puts ("buf not updated");
+      return 1;
+    }
+  for (int inner = 0; inner < 800 * 100; ++inner)
+    if (buf[inner] != W('a') + inner % 26)
+      {
+	printf ("after close: buf[%d] != %c\n", inner,
+		(char) (W('a') + inner % 26));
+	return 1;
+      }
+
+  free (buf);
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/libio/tst-wmemstream1.c b/libio/tst-wmemstream1.c
new file mode 100644
index 0000000000..0c59c1adcb
--- /dev/null
+++ b/libio/tst-wmemstream1.c
@@ -0,0 +1,5 @@
+#define CHAR_T wchar_t
+#define W(o) L##o
+#define OPEN_MEMSTREAM open_wmemstream
+
+#include "tst-memstream1.c"
diff --git a/libio/tst-wmemstream2.c b/libio/tst-wmemstream2.c
new file mode 100644
index 0000000000..f51a1db20c
--- /dev/null
+++ b/libio/tst-wmemstream2.c
@@ -0,0 +1,5 @@
+#define CHAR_T wchar_t
+#define W(o) L##o
+#define OPEN_MEMSTREAM open_wmemstream
+
+#include "tst-memstream2.c"
diff --git a/libio/wmemstream.c b/libio/wmemstream.c
new file mode 100644
index 0000000000..1709f2dd33
--- /dev/null
+++ b/libio/wmemstream.c
@@ -0,0 +1,149 @@
+/* Copyright (C) 1995-97,99,2000,2002-2004,2006 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include "libioP.h"
+#include "strfile.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+
+struct _IO_FILE_wmemstream
+{
+  _IO_strfile _sf;
+  wchar_t **bufloc;
+  _IO_size_t *sizeloc;
+};
+
+
+static int _IO_wmem_sync (_IO_FILE* fp) __THROW;
+static void _IO_wmem_finish (_IO_FILE* fp, int) __THROW;
+
+
+static const struct _IO_jump_t _IO_wmem_jumps =
+{
+  JUMP_INIT_DUMMY,
+  JUMP_INIT (finish, _IO_wmem_finish),
+  JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow),
+  JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow),
+  JUMP_INIT (uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
+  JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
+  JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
+  JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
+  JUMP_INIT (seekoff, _IO_wstr_seekoff),
+  JUMP_INIT (seekpos, _IO_default_seekpos),
+  JUMP_INIT (setbuf, _IO_default_setbuf),
+  JUMP_INIT (sync, _IO_wmem_sync),
+  JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
+  JUMP_INIT (read, _IO_default_read),
+  JUMP_INIT (write, _IO_default_write),
+  JUMP_INIT (seek, _IO_default_seek),
+  JUMP_INIT (close, _IO_default_close),
+  JUMP_INIT (stat, _IO_default_stat),
+  JUMP_INIT (showmanyc, _IO_default_showmanyc),
+  JUMP_INIT (imbue, _IO_default_imbue)
+};
+
+/* Open a stream that writes into a malloc'd buffer that is expanded as
+   necessary.  *BUFLOC and *SIZELOC are updated with the buffer's location
+   and the number of characters written on fflush or fclose.  */
+_IO_FILE *
+open_wmemstream (bufloc, sizeloc)
+     wchar_t **bufloc;
+     _IO_size_t *sizeloc;
+{
+  struct locked_FILE
+  {
+    struct _IO_FILE_wmemstream fp;
+#ifdef _IO_MTSAFE_IO
+    _IO_lock_t lock;
+#endif
+    struct _IO_wide_data wd;
+  } *new_f;
+  wchar_t *buf;
+
+  new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
+  if (new_f == NULL)
+    return NULL;
+#ifdef _IO_MTSAFE_IO
+  new_f->fp._sf._sbf._f._lock = &new_f->lock;
+#endif
+
+  buf = malloc (_IO_BUFSIZ);
+  if (buf == NULL)
+    return NULL;
+
+  _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
+  _IO_fwide (&new_f->fp._sf._sbf._f, 1);
+  _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf,
+			_IO_BUFSIZ / sizeof (wchar_t), buf);
+  new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
+  new_f->fp._sf._s._allocate_buffer = (_IO_alloc_type) malloc;
+  new_f->fp._sf._s._free_buffer = (_IO_free_type) free;
+
+  new_f->fp.bufloc = bufloc;
+  new_f->fp.sizeloc = sizeloc;
+
+  return (_IO_FILE *) &new_f->fp._sf._sbf;
+}
+
+
+static int
+_IO_wmem_sync (fp)
+     _IO_FILE* fp;
+{
+  struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
+
+  if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end)
+    {
+      _IO_wstr_overflow (fp, '\0');
+      --fp->_wide_data->_IO_write_ptr;
+    }
+  else
+    *fp->_wide_data->_IO_write_ptr = '\0';
+
+  *mp->bufloc = fp->_wide_data->_IO_write_base;
+  *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
+		  - fp->_wide_data->_IO_write_base);
+
+  return 0;
+}
+
+
+static void
+_IO_wmem_finish (fp, dummy)
+     _IO_FILE* fp;
+     int dummy;
+{
+  struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
+
+  *mp->bufloc = (wchar_t *) realloc (fp->_wide_data->_IO_write_base,
+				     (fp->_wide_data->_IO_write_ptr
+				      - fp->_wide_data->_IO_write_base + 1)
+				     * sizeof (wchar_t));
+  if (*mp->bufloc != NULL)
+    {
+      size_t len = (fp->_wide_data->_IO_write_ptr
+		    - fp->_wide_data->_IO_write_base);
+      (*mp->bufloc)[len] = '\0';
+      *mp->sizeloc = len;
+
+      fp->_wide_data->_IO_buf_base = NULL;
+    }
+
+  _IO_wstr_finish (fp, 0);
+}