home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs20.zoo / stream.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-13  |  2.8 KB  |  129 lines

  1. #include <stdarg.h>
  2. #include <ioprivat.h>
  3. #include <stream.h>
  4. #include <strstrea.h>
  5.  
  6. // WARNING: dont do this for the atari because _G_BUFSIZ is a variable
  7. // not a constant.
  8. #ifndef atarist
  9. #undef BUFSIZ
  10. #define BUFSIZ _G_BUFSIZ
  11. #endif
  12.  
  13.  
  14. static char Buffer[BUFSIZ];
  15. #define EndBuffer (Buffer+BUFSIZ)
  16. static char* next_chunk = Buffer; // Start of available part of Buffer.
  17.  
  18. char* form(const char* format, ...)
  19. {
  20.     _G_size_t space_left = EndBuffer - next_chunk;
  21.     // If less that 25% of the space is available start over.
  22.     if (space_left < (BUFSIZ>>2))
  23.     next_chunk = Buffer;
  24.     char* buf = next_chunk;
  25.  
  26.     strstreambuf stream(buf, EndBuffer-buf-1, buf);
  27.     va_list ap;
  28.     va_start(ap, format);
  29.     int count = stream.vform(format, ap);
  30.     va_end(ap);
  31.     stream.sputc(0);
  32.     next_chunk = buf + stream.pcount();
  33.     return buf;
  34. }
  35.  
  36. #define u_long unsigned long
  37.  
  38. static char* itoa(unsigned long i, int size, int neg, int base)
  39. {
  40.     // Conservative estimate: If base==2, might need 8 characters
  41.     // for each input byte, but normally 3 is plenty.
  42.     int needed = size ? size
  43.     : (base >= 8 ? 3 : 8) * sizeof(unsigned long) + 2;
  44.     _G_size_t space_left = EndBuffer - next_chunk;
  45.     if (space_left <= needed)
  46.     next_chunk = Buffer; // start over.
  47.  
  48.     char* buf = next_chunk;
  49.  
  50.     register char* ptr = buf+needed+1;
  51.     next_chunk = ptr;
  52.  
  53.     if (needed < (2+neg) || ptr > EndBuffer)
  54.     return NULL;
  55.     *--ptr = 0;
  56.     
  57.     if (i == 0)
  58.     *--ptr = '0';
  59.     while (i != 0 && ptr > buf) {
  60.     int ch = i % base;
  61.     i = i / base;
  62.     if (ch >= 10)
  63.         ch += 'a' - 10;
  64.     else
  65.         ch += '0';
  66.     *--ptr = ch;
  67.     }
  68.     if (neg)
  69.     *--ptr = '-';
  70.     if (size == 0)
  71.     return ptr;
  72.     while (ptr > buf)
  73.     *--ptr = ' ';
  74.     return buf;
  75. }
  76.  
  77. char* dec(long i, int len /* = 0 */)
  78. {
  79.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  80.     else return itoa((unsigned long)(-i), len, 1, 10);
  81. }
  82. char* dec(int i, int len /* = 0 */)
  83. {
  84.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  85.     else return itoa((unsigned long)(-i), len, 1, 10);
  86. }
  87. char* dec(unsigned long i, int len /* = 0 */)
  88. {
  89.     return itoa(i, len, 0, 10);
  90. }
  91. char* dec(unsigned int i, int len /* = 0 */)
  92. {
  93.     return itoa(i, len, 0, 10);
  94. }
  95.  
  96. char* hex(long i, int len /* = 0 */)
  97. {
  98.     return itoa((unsigned long)i, len, 0, 16);
  99. }
  100. char* hex(int i, int len /* = 0 */)
  101. {
  102.     return itoa((unsigned long)i, len, 0, 16);
  103. }
  104. char* hex(unsigned long i, int len /* = 0 */)
  105. {
  106.     return itoa(i, len, 0, 16);
  107. }
  108. char* hex(unsigned int i, int len /* = 0 */)
  109. {
  110.     return itoa(i, len, 0, 16);
  111. }
  112.  
  113. char* oct(long i, int len /* = 0 */)
  114. {
  115.     return itoa((unsigned long)i, len, 0, 8);
  116. }
  117. char* oct(int i, int len /* = 0 */)
  118. {
  119.     return itoa((unsigned long)i, len, 0, 8);
  120. }
  121. char* oct(unsigned long i, int len /* = 0 */)
  122. {
  123.     return itoa(i, len, 0, 8);
  124. }
  125. char* oct(unsigned int i, int len /* = 0 */)
  126. {
  127.     return itoa(i, len, 0, 8);
  128. }
  129.