diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/src/stream_read.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/src/stream_read.c | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/src/stream_read.c b/src/others/mimesh/libg3d-0.0.8/src/stream_read.c new file mode 100644 index 0000000..e222378 --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/src/stream_read.c | |||
@@ -0,0 +1,152 @@ | |||
1 | /* $Id:$ */ | ||
2 | |||
3 | /* | ||
4 | libg3d - 3D object loading library | ||
5 | |||
6 | Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de> | ||
7 | |||
8 | This library is free software; you can redistribute it and/or | ||
9 | modify it under the terms of the GNU Lesser General Public | ||
10 | License as published by the Free Software Foundation; either | ||
11 | version 2.1 of the License, or (at your option) any later version. | ||
12 | |||
13 | This library is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | Lesser General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Lesser General Public | ||
19 | License along with this library; if not, write to the Free Software | ||
20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | #include <g3d/types.h> | ||
24 | #include <g3d/stream.h> | ||
25 | |||
26 | EAPI | ||
27 | gint32 g3d_stream_read_int8(G3DStream *stream) | ||
28 | { | ||
29 | guint8 c; | ||
30 | if(g3d_stream_read(stream, &c, 1) != 1) | ||
31 | return 0; | ||
32 | return c; | ||
33 | } | ||
34 | |||
35 | EAPI | ||
36 | gint32 g3d_stream_read_int16_be(G3DStream *stream) | ||
37 | { | ||
38 | return (g3d_stream_read_int8(stream) << 8) | g3d_stream_read_int8(stream); | ||
39 | } | ||
40 | |||
41 | EAPI | ||
42 | gint32 g3d_stream_read_int16_le(G3DStream *stream) | ||
43 | { | ||
44 | return g3d_stream_read_int8(stream) | (g3d_stream_read_int8(stream) << 8); | ||
45 | } | ||
46 | |||
47 | EAPI | ||
48 | gint32 g3d_stream_read_int32_be(G3DStream *stream) | ||
49 | { | ||
50 | return (g3d_stream_read_int8(stream) << 24) | | ||
51 | (g3d_stream_read_int8(stream) << 16) | | ||
52 | (g3d_stream_read_int8(stream) << 8) | g3d_stream_read_int8(stream); | ||
53 | } | ||
54 | |||
55 | EAPI | ||
56 | gint32 g3d_stream_read_int32_le(G3DStream *stream) | ||
57 | { | ||
58 | return g3d_stream_read_int8(stream) | (g3d_stream_read_int8(stream) << 8) | | ||
59 | (g3d_stream_read_int8(stream) << 16) | | ||
60 | (g3d_stream_read_int8(stream) << 24); | ||
61 | } | ||
62 | |||
63 | static void g3d_stream_read_bytes(G3DStream *stream, guint8 *buf, gsize n) | ||
64 | { | ||
65 | gint32 i; | ||
66 | for(i = 0; i < n; i ++) | ||
67 | buf[i] = g3d_stream_read_int8(stream); | ||
68 | } | ||
69 | |||
70 | static void g3d_stream_read_bytes_swap(G3DStream *stream, guint8 *buf, gsize n) | ||
71 | { | ||
72 | gint32 i; | ||
73 | for(i = (n - 1); i >= 0; i --) | ||
74 | buf[i] = g3d_stream_read_int8(stream); | ||
75 | } | ||
76 | |||
77 | EAPI | ||
78 | G3DFloat g3d_stream_read_float_be(G3DStream *stream) | ||
79 | { | ||
80 | union { | ||
81 | G3DFloat f; | ||
82 | guint8 u[4]; | ||
83 | } u; | ||
84 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN | ||
85 | g3d_stream_read_bytes_swap(stream, u.u, 4); | ||
86 | #elif G_BYTE_ORDER == G_BIG_ENDIAN | ||
87 | g3d_stream_read_bytes(stream, u.u, 4); | ||
88 | #endif | ||
89 | return u.f; | ||
90 | } | ||
91 | |||
92 | EAPI | ||
93 | G3DFloat g3d_stream_read_float_le(G3DStream *stream) | ||
94 | { | ||
95 | union { | ||
96 | G3DFloat f; | ||
97 | guint8 u[4]; | ||
98 | } u; | ||
99 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN | ||
100 | g3d_stream_read_bytes(stream, u.u, 4); | ||
101 | #elif G_BYTE_ORDER == G_BIG_ENDIAN | ||
102 | g3d_stream_read_bytes_swap(stream, u.u, 4); | ||
103 | #endif | ||
104 | return u.f; | ||
105 | } | ||
106 | |||
107 | |||
108 | EAPI | ||
109 | G3DDouble g3d_stream_read_double_be(G3DStream *stream) | ||
110 | { | ||
111 | union { | ||
112 | G3DDouble f; | ||
113 | guint8 u[8]; | ||
114 | } u; | ||
115 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN | ||
116 | g3d_stream_read_bytes_swap(stream, u.u, 8); | ||
117 | #elif G_BYTE_ORDER == G_BIG_ENDIAN | ||
118 | g3d_stream_read_bytes(stream, u.u, 8); | ||
119 | #endif | ||
120 | return u.f; | ||
121 | } | ||
122 | |||
123 | EAPI | ||
124 | G3DDouble g3d_stream_read_double_le(G3DStream *stream) | ||
125 | { | ||
126 | union { | ||
127 | G3DDouble f; | ||
128 | guint8 u[8]; | ||
129 | } u; | ||
130 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN | ||
131 | g3d_stream_read_bytes(stream, u.u, 8); | ||
132 | #elif G_BYTE_ORDER == G_BIG_ENDIAN | ||
133 | g3d_stream_read_bytes_swap(stream, u.u, 8); | ||
134 | #endif | ||
135 | return u.f; | ||
136 | } | ||
137 | |||
138 | EAPI | ||
139 | gint32 g3d_stream_read_cstr(G3DStream *stream, gchar *buffer, gint32 max_len) | ||
140 | { | ||
141 | gint32 n = 0; | ||
142 | gchar c; | ||
143 | |||
144 | do { | ||
145 | c = g3d_stream_read_int8(stream); | ||
146 | buffer[n] = c; | ||
147 | n ++; | ||
148 | } while((c != 0) && (n < max_len)); | ||
149 | buffer[max_len - 1] = '\0'; | ||
150 | return n; | ||
151 | } | ||
152 | |||