summaryrefslogtreecommitdiff
path: root/gnu/lib/libg++/iostream/stream.C
blob: 5ee6bbeb5a55745e6b2772fca04f3f4bd8c24397 (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
#include <stdarg.h>
#include "ioprivate.h"
#include "stream.h"
#include "strstream.h"

static char Buffer[_G_BUFSIZ];
#define EndBuffer (Buffer+_G_BUFSIZ)
static char* next_chunk = Buffer; // Start of available part of Buffer.

char* form(const char* format, ...)
{
    int space_left = EndBuffer - next_chunk;
    // If less that 25% of the space is available start over.
    if (space_left < (_G_BUFSIZ>>2))
	next_chunk = Buffer;
    char* buf = next_chunk;

    strstreambuf stream(buf, EndBuffer-buf-1, buf);
    va_list ap;
    va_start(ap, format);
    int count = stream.vform(format, ap);
    va_end(ap);
    stream.sputc(0);
    next_chunk = buf + stream.pcount();
    return buf;
}

#define u_long unsigned long

static char* itoa(unsigned long i, int size, int neg, int base)
{
    // Conservative estimate: If base==2, might need 8 characters
    // for each input byte, but normally 3 is plenty.
    int needed = size ? size
	: (base >= 8 ? 3 : 8) * sizeof(unsigned long) + 2;
    int space_left = EndBuffer - next_chunk;
    if (space_left <= needed)
	next_chunk = Buffer; // start over.

    char* buf = next_chunk;

    register char* ptr = buf+needed+1;
    next_chunk = ptr;

    if (needed < (2+neg) || ptr > EndBuffer)
	return NULL;
    *--ptr = 0;
    
    if (i == 0)
	*--ptr = '0';
    while (i != 0 && ptr > buf) {
	int ch = i % base;
	i = i / base;
	if (ch >= 10)
	    ch += 'a' - 10;
	else
	    ch += '0';
	*--ptr = ch;
    }
    if (neg)
	*--ptr = '-';
    if (size == 0)
	return ptr;
    while (ptr > buf)
	*--ptr = ' ';
    return buf;
}

char* dec(long i, int len /* = 0 */)
{
    if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
    else return itoa((unsigned long)(-i), len, 1, 10);
}
char* dec(int i, int len /* = 0 */)
{
    if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
    else return itoa((unsigned long)(-i), len, 1, 10);
}
char* dec(unsigned long i, int len /* = 0 */)
{
    return itoa(i, len, 0, 10);
}
char* dec(unsigned int i, int len /* = 0 */)
{
    return itoa(i, len, 0, 10);
}

char* hex(long i, int len /* = 0 */)
{
    return itoa((unsigned long)i, len, 0, 16);
}
char* hex(int i, int len /* = 0 */)
{
    return itoa((unsigned long)i, len, 0, 16);
}
char* hex(unsigned long i, int len /* = 0 */)
{
    return itoa(i, len, 0, 16);
}
char* hex(unsigned int i, int len /* = 0 */)
{
    return itoa(i, len, 0, 16);
}

char* oct(long i, int len /* = 0 */)
{
    return itoa((unsigned long)i, len, 0, 8);
}
char* oct(int i, int len /* = 0 */)
{
    return itoa((unsigned long)i, len, 0, 8);
}
char* oct(unsigned long i, int len /* = 0 */)
{
    return itoa(i, len, 0, 8);
}
char* oct(unsigned int i, int len /* = 0 */)
{
    return itoa(i, len, 0, 8);
}