summaryrefslogtreecommitdiffstats
path: root/libio/ioextend.cc
blob: a31ff36e6e5802a83db7fb5007265cb24c5938d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* 
Copyright (C) 1993 Free Software Foundation

This file is part of the GNU IO Library.  This library is free
software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option)
any later version.

This 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

As a special exception, if you link this library with files
compiled with a GNU compiler to produce an executable, this does not cause
the resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why
the executable file might be covered by the GNU General Public License. */

#include <iostream.h>

static int __xalloc = 0;

int ios::xalloc()
{
  return __xalloc++;
}

static ios::fmtflags __used_fmt_flags
= ios::skipws | ios::left | ios::right | ios::internal
| ios::dec | ios::oct | ios::hex | ios::showbase | ios::showpoint
| ios::uppercase | ios::showpos | ios::scientific | ios::fixed
#ifndef _IO_NEW_STREAMS
| ios::dont_close
#endif
| ios::unitbuf | ios::stdio;

ios::fmtflags ios::bitalloc()
{
  fmtflags bit_to_try = (fmtflags)1;
  for (; bit_to_try; bit_to_try <<= 1)
    {
      if ((__used_fmt_flags & bit_to_try) == 0)
	{
	  __used_fmt_flags |= bit_to_try;
	  return bit_to_try;
	}
    }
  return 0;
}

// NOTE:  This implementation of ios::iword and ios::pword assumes
// that these methods are seldom used, so we want to minimize
// the space and time overhead when NOT using these methods.
//
// ANSI specifies two conceptually-infinite arrays, one whose
// elements are longs, and one whose elements are (void*)s.
// We implement this as a single array, each of whose element is
// a (struct ptr_and_long), which has space for both a long and a void*.
// We also specify that (the i field of) the 0'th element of the array
// contains the allocated length of the array (not counting that
// initial element).

struct ptr_and_long {
  void *p;
  long i;
};

static struct ptr_and_long&
get_array_element(ios& io, int index)
{
  if (index < 0)
    io._throw_failure();
  register struct ptr_and_long *array = (ptr_and_long*)io._arrays;
  int old_length = array == NULL ? 0 : array[0].i;
  if (index >= old_length)
    {
      register int i;
      int new_length = index + 10;
      register struct ptr_and_long *new_array
	= new ptr_and_long[1 + new_length];
      if (array != NULL)
	{
	  for (i = 1; i <= old_length; i++)
	    new_array[i] = array[i];
	  delete [] array;
	}
      for (i = old_length + 1; i <= new_length; i++)
	{
	  new_array[i].i = 0;
	  new_array[i].p = NULL;
	}
      new_array[0].i = new_length;
      new_array[0].p = NULL;
      io._arrays = (void*)new_array;
      array = new_array;
    }
  return array[index+1];
}

long& ios::iword(int index)
{
  return get_array_element(*this, index).i;
}

void*& ios::pword(int index)
{
  return get_array_element(*this, index).p;
}

long ios::iword(int index) const
{
  if (index < 0)
    _throw_failure();
  register struct ptr_and_long *pl_array = (ptr_and_long*)_arrays;
  int len = pl_array == NULL ? 0 : pl_array[0].i;
  return index >= len ? 0 : pl_array[index+1].i;
}

void* ios::pword(int index) const
{
  if (index < 0)
    _throw_failure();
  register struct ptr_and_long *pl_array = (ptr_and_long*)_arrays;
  int len = pl_array == NULL ? 0 : pl_array[0].i;
  return index >= len ? 0 : pl_array[index+1].p;
}
OpenPOWER on IntegriCloud