home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / gpplib22.zoo / iosrc / stream.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  3.8 KB  |  150 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 GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 <stdarg.h>
  26. #include "libioP.h"
  27. #include "stream.h"
  28. #include "strstream.h"
  29.  
  30. #if defined(atarist)
  31. #undef  _IO_BUFSIZ
  32. #define _IO_BUFSIZ 1024
  33. #endif
  34.  
  35. static char Buffer[_IO_BUFSIZ];
  36. #define EndBuffer (Buffer+_IO_BUFSIZ)
  37. static char* next_chunk = Buffer; // Start of available part of Buffer.
  38.  
  39. char* form(const char* format, ...)
  40. {
  41.     int space_left = EndBuffer - next_chunk;
  42.     // If less that 25% of the space is available start over.
  43.     if (space_left < (_IO_BUFSIZ>>2))
  44.     next_chunk = Buffer;
  45.     char* buf = next_chunk;
  46.  
  47.     strstreambuf stream(buf, EndBuffer-buf-1, buf);
  48.     va_list ap;
  49.     va_start(ap, format);
  50.     int count = stream.vform(format, ap);
  51.     va_end(ap);
  52.     stream.sputc(0);
  53.     next_chunk = buf + stream.pcount();
  54.     return buf;
  55. }
  56.  
  57. #define u_long unsigned long
  58.  
  59. static char* itoa(unsigned long i, int size, int neg, int base)
  60. {
  61.     // Conservative estimate: If base==2, might need 8 characters
  62.     // for each input byte, but normally 3 is plenty.
  63.     int needed = size ? size
  64.     : (base >= 8 ? 3 : 8) * sizeof(unsigned long) + 2;
  65.     int space_left = EndBuffer - next_chunk;
  66.     if (space_left <= needed)
  67.     next_chunk = Buffer; // start over.
  68.  
  69.     char* buf = next_chunk;
  70.  
  71.     register char* ptr = buf+needed+1;
  72.     next_chunk = ptr;
  73.  
  74.     if (needed < (2+neg) || ptr > EndBuffer)
  75.     return NULL;
  76.     *--ptr = 0;
  77.     
  78.     if (i == 0)
  79.     *--ptr = '0';
  80.     while (i != 0 && ptr > buf) {
  81.     int ch = i % base;
  82.     i = i / base;
  83.     if (ch >= 10)
  84.         ch += 'a' - 10;
  85.     else
  86.         ch += '0';
  87.     *--ptr = ch;
  88.     }
  89.     if (neg)
  90.     *--ptr = '-';
  91.     if (size == 0)
  92.     return ptr;
  93.     while (ptr > buf)
  94.     *--ptr = ' ';
  95.     return buf;
  96. }
  97.  
  98. char* dec(long i, int len /* = 0 */)
  99. {
  100.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  101.     else return itoa((unsigned long)(-i), len, 1, 10);
  102. }
  103. char* dec(int i, int len /* = 0 */)
  104. {
  105.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  106.     else return itoa((unsigned long)(-i), len, 1, 10);
  107. }
  108. char* dec(unsigned long i, int len /* = 0 */)
  109. {
  110.     return itoa(i, len, 0, 10);
  111. }
  112. char* dec(unsigned int i, int len /* = 0 */)
  113. {
  114.     return itoa(i, len, 0, 10);
  115. }
  116.  
  117. char* hex(long i, int len /* = 0 */)
  118. {
  119.     return itoa((unsigned long)i, len, 0, 16);
  120. }
  121. char* hex(int i, int len /* = 0 */)
  122. {
  123.     return itoa((unsigned long)i, len, 0, 16);
  124. }
  125. char* hex(unsigned long i, int len /* = 0 */)
  126. {
  127.     return itoa(i, len, 0, 16);
  128. }
  129. char* hex(unsigned int i, int len /* = 0 */)
  130. {
  131.     return itoa(i, len, 0, 16);
  132. }
  133.  
  134. char* oct(long i, int len /* = 0 */)
  135. {
  136.     return itoa((unsigned long)i, len, 0, 8);
  137. }
  138. char* oct(int i, int len /* = 0 */)
  139. {
  140.     return itoa((unsigned long)i, len, 0, 8);
  141. }
  142. char* oct(unsigned long i, int len /* = 0 */)
  143. {
  144.     return itoa(i, len, 0, 8);
  145. }
  146. char* oct(unsigned int i, int len /* = 0 */)
  147. {
  148.     return itoa(i, len, 0, 8);
  149. }
  150.