summaryrefslogtreecommitdiff
path: root/src/gd_io.c
blob: 94032771efc4fd7b02610f38d0414c66f87e52fd (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * io.c
 *
 * Implements the simple I/O 'helper' routines.
 *
 * Not really essential, but these routines were used extensively in GD,
 * so they were moved here. They also make IOCtx calls look better...
 *
 * Written (or, at least, moved) 1999, Philip Warner.
 */

#ifdef HAVE_CONFIG_H
#	include "config.h"
#endif

#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "gd.h"

/* Use this for commenting out debug-print statements. */
/* Just use the first '#define' to allow all the prints... */
/*#define IO_DBG(s) (s) */
#define IO_DBG(s)

#define GD_IO_EOF_CHK(r) \
	if(r == EOF) { \
		return 0; \
	}

void gdPutC(const unsigned char c, gdIOCtx *ctx)
{
	(ctx->putC)(ctx, c);
}

void gdPutWord (int w, gdIOCtx *ctx)
{
	IO_DBG(printf("Putting word...\n"));
	(ctx->putC)(ctx, (unsigned char)(w >> 8));
	(ctx->putC)(ctx, (unsigned char)(w & 0xFF));
	IO_DBG(printf("put.\n"));
}

void gdPutInt (int w, gdIOCtx *ctx)
{
	IO_DBG(printf("Putting int...\n"));
	(ctx->putC)(ctx, (unsigned char) (w >> 24));
	(ctx->putC)(ctx, (unsigned char) ((w >> 16) & 0xFF));
	(ctx->putC)(ctx, (unsigned char) ((w >> 8) & 0xFF));
	(ctx->putC)(ctx, (unsigned char) (w & 0xFF));
	IO_DBG(printf("put.\n"));
}

int gdGetC(gdIOCtx *ctx)
{
	return ((ctx->getC)(ctx));
}

int gdGetByte(int *result, gdIOCtx *ctx)
{
	int r;

	r = (ctx->getC)(ctx);
	if(r == EOF) {
		return 0;
	}

	*result = r;

	return 1;
}

int gdGetWord(int *result, gdIOCtx *ctx)
{
	int r;

	r = (ctx->getC)(ctx);
	if(r == EOF) {
		return 0;
	}

	*result = r << 8;

	r = (ctx->getC)(ctx);
	if(r == EOF) {
		return 0;
	}

	*result += r;

	return 1;
}

int gdGetWordLSB(signed short int *result, gdIOCtx *ctx)
{
	int high = 0, low = 0;
	low = (ctx->getC) (ctx);
	if (low == EOF) {
		return 0;
	}

	high = (ctx->getC) (ctx);
	if (high == EOF) {
		return 0;
	}

	if (result) {
		*result = (high << 8) | low;
	}

	return 1;
}

int gdGetInt(int *result, gdIOCtx *ctx)
{
	unsigned int r;

	r = (ctx->getC)(ctx);
	if(r == EOF) {
		return 0;
	}

	*result = r << 24;

	r = (ctx->getC)(ctx);
	if(r == EOF) {
		return 0;
	}

	*result += r << 16;

	r = (ctx->getC)(ctx);
	if(r == EOF) {
		return 0;
	}

	*result += r << 8;

	r = (ctx->getC)(ctx);
	if(r == EOF) {
		return 0;
	}

	*result += r;

	return 1;
}

int gdGetIntLSB(signed int *result, gdIOCtx *ctx)
{
	unsigned int c;
	unsigned int r = 0;

	c = (ctx->getC) (ctx);
	if (c == EOF) {
		return 0;
	}
	r |= (c << 24);
	r >>= 8;

	c = (ctx->getC) (ctx);
	if (c == EOF) {
		return 0;
	}
	r |= (c << 24);
	r >>= 8;

	c = (ctx->getC) (ctx);
	if (c == EOF) {
		return 0;
	}
	r |= (c << 24);
	r >>= 8;

	c = (ctx->getC) (ctx);
	if (c == EOF) {
		return 0;
	}
	r |= (c << 24);

	if (result) {
		*result = (signed int)r;
	}

	return 1;
}

int gdPutBuf(const void *buf, int size, gdIOCtx *ctx)
{
	IO_DBG(printf("Putting buf...\n"));
	return (ctx->putBuf)(ctx, buf, size);
	IO_DBG(printf("put.\n"));
}

int gdGetBuf(void *buf, int size, gdIOCtx *ctx)
{
	return (ctx->getBuf)(ctx, buf, size);
}

int gdSeek(gdIOCtx *ctx, const int pos)
{
	IO_DBG(printf("Seeking...\n"));
	return ((ctx->seek)(ctx, pos));
	IO_DBG(printf("Done.\n"));
}

long gdTell(gdIOCtx *ctx)
{
	IO_DBG(printf("Telling...\n"));
	return ((ctx->tell)(ctx));
	IO_DBG(printf("told.\n"));
}