Mon Nov 17 23:42:03 1997 Bruno Haible <haible@ilog.fr>
[official-gcc.git] / libio / ioextend.cc
bloba31ff36e6e5802a83db7fb5007265cb24c5938d6
1 /*
2 Copyright (C) 1993 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
25 #include <iostream.h>
27 static int __xalloc = 0;
29 int ios::xalloc()
31 return __xalloc++;
34 static ios::fmtflags __used_fmt_flags
35 = ios::skipws | ios::left | ios::right | ios::internal
36 | ios::dec | ios::oct | ios::hex | ios::showbase | ios::showpoint
37 | ios::uppercase | ios::showpos | ios::scientific | ios::fixed
38 #ifndef _IO_NEW_STREAMS
39 | ios::dont_close
40 #endif
41 | ios::unitbuf | ios::stdio;
43 ios::fmtflags ios::bitalloc()
45 fmtflags bit_to_try = (fmtflags)1;
46 for (; bit_to_try; bit_to_try <<= 1)
48 if ((__used_fmt_flags & bit_to_try) == 0)
50 __used_fmt_flags |= bit_to_try;
51 return bit_to_try;
54 return 0;
57 // NOTE: This implementation of ios::iword and ios::pword assumes
58 // that these methods are seldom used, so we want to minimize
59 // the space and time overhead when NOT using these methods.
61 // ANSI specifies two conceptually-infinite arrays, one whose
62 // elements are longs, and one whose elements are (void*)s.
63 // We implement this as a single array, each of whose element is
64 // a (struct ptr_and_long), which has space for both a long and a void*.
65 // We also specify that (the i field of) the 0'th element of the array
66 // contains the allocated length of the array (not counting that
67 // initial element).
69 struct ptr_and_long {
70 void *p;
71 long i;
74 static struct ptr_and_long&
75 get_array_element(ios& io, int index)
77 if (index < 0)
78 io._throw_failure();
79 register struct ptr_and_long *array = (ptr_and_long*)io._arrays;
80 int old_length = array == NULL ? 0 : array[0].i;
81 if (index >= old_length)
83 register int i;
84 int new_length = index + 10;
85 register struct ptr_and_long *new_array
86 = new ptr_and_long[1 + new_length];
87 if (array != NULL)
89 for (i = 1; i <= old_length; i++)
90 new_array[i] = array[i];
91 delete [] array;
93 for (i = old_length + 1; i <= new_length; i++)
95 new_array[i].i = 0;
96 new_array[i].p = NULL;
98 new_array[0].i = new_length;
99 new_array[0].p = NULL;
100 io._arrays = (void*)new_array;
101 array = new_array;
103 return array[index+1];
106 long& ios::iword(int index)
108 return get_array_element(*this, index).i;
111 void*& ios::pword(int index)
113 return get_array_element(*this, index).p;
116 long ios::iword(int index) const
118 if (index < 0)
119 _throw_failure();
120 register struct ptr_and_long *pl_array = (ptr_and_long*)_arrays;
121 int len = pl_array == NULL ? 0 : pl_array[0].i;
122 return index >= len ? 0 : pl_array[index+1].i;
125 void* ios::pword(int index) const
127 if (index < 0)
128 _throw_failure();
129 register struct ptr_and_long *pl_array = (ptr_and_long*)_arrays;
130 int len = pl_array == NULL ? 0 : pl_array[0].i;
131 return index >= len ? 0 : pl_array[index+1].p;