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