aboutsummaryrefslogtreecommitdiff
path: root/libio/wstrops.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2007-07-12 18:26:36 +0000
committerJakub Jelinek <jakub@redhat.com>2007-07-12 18:26:36 +0000
commit0ecb606cb6cf65de1d9fc8a919bceb4be476c602 (patch)
tree2ea1f8305970753e4a657acb2ccc15ca3eec8e2c /libio/wstrops.c
parent7d58530341304d403a6626d7f7a1913165fe2f32 (diff)
downloadglibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar
glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar.gz
glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar.bz2
glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.zip
2.5-18.1
Diffstat (limited to 'libio/wstrops.c')
-rw-r--r--libio/wstrops.c142
1 files changed, 97 insertions, 45 deletions
diff --git a/libio/wstrops.c b/libio/wstrops.c
index 32f7ef3cf7..c5aae7bc6a 100644
--- a/libio/wstrops.c
+++ b/libio/wstrops.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1993,1997-1999,2001-2003,2004 Free Software Foundation, Inc.
+/* Copyright (C) 1993,1997-1999,2001-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
@@ -25,44 +25,13 @@
This exception applies to code released by its copyright holders
in files containing the exception. */
+#include <assert.h>
#include "strfile.h"
#include "libioP.h"
#include <string.h>
#include <wchar.h>
#include <stdio_ext.h>
-#if 0
-/* The following definitions are for exposition only.
- They map the terminology used in the ANSI/ISO C++ draft standard
- to the implementation. */
-
-/* allocated: set when a dynamic array object has been allocated, and
- hence should be freed by the destructor for the strstreambuf object. */
-#define ALLOCATED(FP) ((FP)->_f._IO_buf_base && DYNAMIC(FP))
-
-/* constant: set when the array object has const elements,
- so the output sequence cannot be written. */
-#define CONSTANT(FP) ((FP)->_f._IO_file_flags & _IO_NO_WRITES)
-
-/* alsize: the suggested minimum size for a dynamic array object. */
-#define ALSIZE(FP) ??? /* not stored */
-
-/* palloc: points to the function to call to allocate a dynamic array object.*/
-#define PALLOC(FP) \
- ((FP)->_s._allocate_buffer == default_alloc ? 0 : (FP)->_s._allocate_buffer)
-
-/* pfree: points to the function to call to free a dynamic array object. */
-#define PFREE(FP) \
- ((FP)->_s._free_buffer == default_free ? 0 : (FP)->_s._free_buffer)
-
-#endif
-
-#ifdef TODO
-/* An "unbounded buffer" is when a buffer is supplied, but with no
- specified length. An example is the buffer argument to sprintf.
- */
-#endif
-
void
_IO_wstr_init_static (fp, ptr, size, pstart)
_IO_FILE *fp;
@@ -98,7 +67,7 @@ _IO_wstr_init_static (fp, ptr, size, pstart)
fp->_wide_data->_IO_read_end = end;
}
/* A null _allocate_buffer function flags the strfile as being static. */
- (((_IO_strfile *) fp)->_s._allocate_buffer) = (_IO_alloc_type)0;
+ (((_IO_strfile *) fp)->_s._allocate_buffer) = (_IO_alloc_type)0;
}
_IO_wint_t
@@ -116,16 +85,19 @@ _IO_wstr_overflow (fp, c)
fp->_wide_data->_IO_write_ptr = fp->_wide_data->_IO_read_ptr;
fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end;
}
- pos = fp->_wide_data->_IO_write_ptr - fp->_wide_data->_IO_write_base;
+ pos = fp->_wide_data->_IO_write_ptr - fp->_wide_data->_IO_write_base;
if (pos >= (_IO_size_t) (_IO_wblen (fp) + flush_only))
{
- if (fp->_flags & _IO_USER_BUF) /* not allowed to enlarge */
+ if (fp->_flags2 & _IO_FLAGS2_USER_WBUF) /* not allowed to enlarge */
return WEOF;
else
{
wchar_t *new_buf;
wchar_t *old_buf = fp->_wide_data->_IO_buf_base;
- _IO_size_t new_size = 2 * _IO_wblen (fp) + 100;
+ size_t old_wblen = _IO_wblen (fp);
+ _IO_size_t new_size = 2 * old_wblen + 100;
+ if (new_size < old_wblen)
+ return EOF;
new_buf
= (wchar_t *) (*((_IO_strfile *) fp)->_s._allocate_buffer) (new_size
* sizeof (wchar_t));
@@ -136,11 +108,14 @@ _IO_wstr_overflow (fp, c)
}
if (old_buf)
{
- __wmemcpy (new_buf, old_buf, _IO_wblen (fp));
+ __wmemcpy (new_buf, old_buf, old_wblen);
(*((_IO_strfile *) fp)->_s._free_buffer) (old_buf);
/* Make sure _IO_setb won't try to delete _IO_buf_base. */
fp->_wide_data->_IO_buf_base = NULL;
}
+
+ wmemset (new_buf + old_wblen, L'\0', new_size - old_wblen);
+
INTUSE(_IO_wsetb) (fp, new_buf, new_buf + new_size, 1);
fp->_wide_data->_IO_read_base =
new_buf + (fp->_wide_data->_IO_read_base - old_buf);
@@ -163,6 +138,7 @@ _IO_wstr_overflow (fp, c)
return c;
}
+
_IO_wint_t
_IO_wstr_underflow (fp)
_IO_FILE *fp;
@@ -181,17 +157,87 @@ _IO_wstr_underflow (fp)
return WEOF;
}
-/* The size of the valid part of the buffer. */
+/* The size of the valid part of the buffer. */
_IO_ssize_t
_IO_wstr_count (fp)
_IO_FILE *fp;
{
- return ((fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_read_end
- ? fp->_wide_data->_IO_write_ptr : fp->_wide_data->_IO_read_end)
- - fp->_wide_data->_IO_read_base);
+ struct _IO_wide_data *wd = fp->_wide_data;
+
+ return ((wd->_IO_write_ptr > wd->_IO_read_end
+ ? wd->_IO_write_ptr : wd->_IO_read_end)
+ - wd->_IO_read_base);
}
+
+static int
+enlarge_userbuf (_IO_FILE *fp, _IO_off64_t offset, int reading)
+{
+ if ((_IO_ssize_t) offset <= _IO_blen (fp))
+ return 0;
+
+ struct _IO_wide_data *wd = fp->_wide_data;
+
+ _IO_ssize_t oldend = wd->_IO_write_end - wd->_IO_write_base;
+
+ /* Try to enlarge the buffer. */
+ if (fp->_flags2 & _IO_FLAGS2_USER_WBUF)
+ /* User-provided buffer. */
+ return 1;
+
+ _IO_size_t newsize = offset + 100;
+ wchar_t *oldbuf = wd->_IO_buf_base;
+ wchar_t *newbuf
+ = (wchar_t *) (*((_IO_strfile *) fp)->_s._allocate_buffer) (newsize
+ * sizeof (wchar_t));
+ if (newbuf == NULL)
+ return 1;
+
+ if (oldbuf != NULL)
+ {
+ __wmemcpy (newbuf, oldbuf, _IO_wblen (fp));
+ (*((_IO_strfile *) fp)->_s._free_buffer) (oldbuf);
+ /* Make sure _IO_setb won't try to delete
+ _IO_buf_base. */
+ wd->_IO_buf_base = NULL;
+ }
+
+ INTUSE(_IO_wsetb) (fp, newbuf, newbuf + newsize, 1);
+
+ if (reading)
+ {
+ wd->_IO_write_base = newbuf + (wd->_IO_write_base - oldbuf);
+ wd->_IO_write_ptr = newbuf + (wd->_IO_write_ptr - oldbuf);
+ wd->_IO_write_end = newbuf + (wd->_IO_write_end - oldbuf);
+ wd->_IO_read_ptr = newbuf + (wd->_IO_read_ptr - oldbuf);
+
+ wd->_IO_read_base = newbuf;
+ wd->_IO_read_end = wd->_IO_buf_end;
+ }
+ else
+ {
+ wd->_IO_read_base = newbuf + (wd->_IO_read_base - oldbuf);
+ wd->_IO_read_ptr = newbuf + (wd->_IO_read_ptr - oldbuf);
+ wd->_IO_read_end = newbuf + (wd->_IO_read_end - oldbuf);
+ wd->_IO_write_ptr = newbuf + (wd->_IO_write_ptr - oldbuf);
+
+ wd->_IO_write_base = newbuf;
+ wd->_IO_write_end = wd->_IO_buf_end;
+ }
+
+ /* Clear the area between the last write position and th
+ new position. */
+ assert (offset >= oldend);
+ if (reading)
+ wmemset (wd->_IO_read_base + oldend, L'\0', offset - oldend);
+ else
+ wmemset (wd->_IO_write_base + oldend, L'\0', offset - oldend);
+
+ return 0;
+}
+
+
_IO_off64_t
_IO_wstr_seekoff (fp, offset, dir, mode)
_IO_FILE *fp;
@@ -234,7 +280,10 @@ _IO_wstr_seekoff (fp, offset, dir, mode)
default: /* case _IO_seek_set: */
break;
}
- if (offset < 0 || (_IO_ssize_t) offset > cur_size)
+ if (offset < 0)
+ return EOF;
+ if ((_IO_ssize_t) offset > cur_size
+ && enlarge_userbuf (fp, offset, 1) != 0)
return EOF;
fp->_wide_data->_IO_read_ptr = (fp->_wide_data->_IO_read_base
+ offset);
@@ -258,7 +307,10 @@ _IO_wstr_seekoff (fp, offset, dir, mode)
default: /* case _IO_seek_set: */
break;
}
- if (offset < 0 || (_IO_ssize_t) offset > cur_size)
+ if (offset < 0)
+ return EOF;
+ if ((_IO_ssize_t) offset > cur_size
+ && enlarge_userbuf (fp, offset, 0) != 0)
return EOF;
fp->_wide_data->_IO_write_ptr = (fp->_wide_data->_IO_write_base
+ offset);
@@ -283,7 +335,7 @@ _IO_wstr_finish (fp, dummy)
_IO_FILE *fp;
int dummy;
{
- if (fp->_wide_data->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
+ if (fp->_wide_data->_IO_buf_base && !(fp->_flags2 & _IO_FLAGS2_USER_WBUF))
(((_IO_strfile *) fp)->_s._free_buffer) (fp->_wide_data->_IO_buf_base);
fp->_wide_data->_IO_buf_base = NULL;