aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/src/read.c
blob: 7f0b9dafaa1c94e6cf85688618db75fd6918807f (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
/* $Id$ */

/*
    libg3d - 3D object loading library

    Copyright (C) 2005-2009  Markus Dahms <mad@automagically.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>
#include <g3d/types.h>

gint32 g3d_read_int8(FILE *f)
{
	int c = fgetc(f);
	if(c == EOF)
		return 0;
	else
		return c;
}

gint32 g3d_read_int16_be(FILE *f)
{
	return (g3d_read_int8(f)<<8) | g3d_read_int8(f);
}

gint32 g3d_read_int16_le(FILE *f)
{
	return g3d_read_int8(f) | (g3d_read_int8(f)<<8);
}

gint32 g3d_read_int32_be(FILE *f)
{
	return (g3d_read_int8(f) << 24) | (g3d_read_int8(f) << 16) |
		(g3d_read_int8(f) << 8) | g3d_read_int8(f);
}

gint32 g3d_read_int32_le(FILE *f)
{
	return  g3d_read_int8(f) | (g3d_read_int8(f) << 8) |
		(g3d_read_int8(f) << 16) | (g3d_read_int8(f) << 24);
}

static void g3d_read_bytes(FILE *f, guint8 *buf, gsize n)
{
	gint32 i;
	for(i = 0; i < n; i ++)
		buf[i] = g3d_read_int8(f);
}

static void g3d_read_bytes_swap(FILE *f, guint8 *buf, gsize n)
{
	gint32 i;
	for(i = (n - 1); i >= 0; i --)
		buf[i] = g3d_read_int8(f);
}

G3DFloat g3d_read_float_be(FILE *f)
{
	union {
		G3DFloat f;
		guint8 u[4];
	} u;

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
	g3d_read_bytes_swap(f, u.u, 4);
#elif G_BYTE_ORDER == G_BIG_ENDIAN
	g3d_read_bytes(f, u.u, 4);
#endif
	return u.f;
}

G3DFloat g3d_read_float_le(FILE *f)
{
	union {
		G3DFloat f;
		guint8 u[4];
	} u;

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
	g3d_read_bytes(f, u.u, 4);
#elif G_BYTE_ORDER == G_BIG_ENDIAN
	g3d_read_bytes_swap(f, u.u, 4);
#endif
	return u.f;
}

G3DDouble g3d_read_double_be(FILE *f)
{
	union {
		G3DDouble d;
		guint8 u[8];
	} u;

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
	g3d_read_bytes_swap(f, u.u, 8);
#elif G_BYTE_ORDER == G_BIG_ENDIAN
	g3d_read_bytes(f, u.u, 8);
#endif
	return u.d;
}

G3DDouble g3d_read_double_le(FILE *f)
{
	union {
		G3DDouble d;
		guint8 u[8];
	} u;

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
	g3d_read_bytes(f, u.u, 8);
#elif G_BYTE_ORDER == G_BIG_ENDIAN
	g3d_read_bytes_swap(f, u.u, 8);
#endif
	return u.d;
}

gint32 g3d_read_cstr(FILE *f, gchar *buffer, gint32 max_len)
{
	gint32 n = 0;
	gchar c;

	do
	{
		c = g3d_read_int8(f);
		buffer[n] = c;
		n ++;
	}
	while((c != 0) && (n < max_len));

	buffer[max_len - 1] = '\0';

	return n;
}