home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-base.tgz / libg++-2.7.1-src.tar / fsf / libg++ / libio / ioextend.cc < prev    next >
C/C++ Source or Header  |  1995-06-15  |  4KB  |  133 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  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.
  9.  
  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.
  14.  
  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.
  18.  
  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. */
  24.  
  25. #include <iostream.h>
  26.  
  27. static int __xalloc = 0;
  28.  
  29. int ios::xalloc()
  30. {
  31.   return __xalloc++;
  32. }
  33.  
  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;
  42.  
  43. ios::fmtflags ios::bitalloc()
  44. {
  45.   fmtflags bit_to_try = (fmtflags)1;
  46.   for (; bit_to_try; bit_to_try <<= 1)
  47.     {
  48.       if ((__used_fmt_flags & bit_to_try) == 0)
  49.     {
  50.       __used_fmt_flags |= bit_to_try;
  51.       return bit_to_try;
  52.     }
  53.     }
  54.   return 0;
  55. }
  56.  
  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.
  60. //
  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).
  68.  
  69. struct ptr_and_long {
  70.   void *p;
  71.   long i;
  72. };
  73.  
  74. static struct ptr_and_long&
  75. get_array_element(ios& io, int index)
  76. {
  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)
  82.     {
  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)
  88.     {
  89.       for (i = 1; i <= old_length; i++)
  90.         new_array[i] = array[i];
  91.       delete [] array;
  92.     }
  93.       for (i = old_length + 1; i <= new_length; i++)
  94.     {
  95.       new_array[i].i = 0;
  96.       new_array[i].p = (void*)0;
  97.     }
  98.       new_array[0].i = new_length;
  99.       new_array[0].p = (void*)0;
  100.       io._arrays = (void*)new_array;
  101.       array = new_array;
  102.     }
  103.   return array[index+1];
  104. }
  105.  
  106. long& ios::iword(int index)
  107. {
  108.   return get_array_element(*this, index).i;
  109. }
  110.  
  111. void*& ios::pword(int index)
  112. {
  113.   return get_array_element(*this, index).p;
  114. }
  115.  
  116. long ios::iword(int index) const
  117. {
  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;
  123. }
  124.  
  125. void* ios::pword(int index) const
  126. {
  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;
  132. }
  133.