aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/src/read.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/src/read.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/src/read.c147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/src/read.c b/src/others/mimesh/libg3d-0.0.8/src/read.c
new file mode 100644
index 0000000..7f0b9da
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/src/read.c
@@ -0,0 +1,147 @@
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 <stdio.h>
24#include <g3d/types.h>
25
26gint32 g3d_read_int8(FILE *f)
27{
28 int c = fgetc(f);
29 if(c == EOF)
30 return 0;
31 else
32 return c;
33}
34
35gint32 g3d_read_int16_be(FILE *f)
36{
37 return (g3d_read_int8(f)<<8) | g3d_read_int8(f);
38}
39
40gint32 g3d_read_int16_le(FILE *f)
41{
42 return g3d_read_int8(f) | (g3d_read_int8(f)<<8);
43}
44
45gint32 g3d_read_int32_be(FILE *f)
46{
47 return (g3d_read_int8(f) << 24) | (g3d_read_int8(f) << 16) |
48 (g3d_read_int8(f) << 8) | g3d_read_int8(f);
49}
50
51gint32 g3d_read_int32_le(FILE *f)
52{
53 return g3d_read_int8(f) | (g3d_read_int8(f) << 8) |
54 (g3d_read_int8(f) << 16) | (g3d_read_int8(f) << 24);
55}
56
57static void g3d_read_bytes(FILE *f, guint8 *buf, gsize n)
58{
59 gint32 i;
60 for(i = 0; i < n; i ++)
61 buf[i] = g3d_read_int8(f);
62}
63
64static void g3d_read_bytes_swap(FILE *f, guint8 *buf, gsize n)
65{
66 gint32 i;
67 for(i = (n - 1); i >= 0; i --)
68 buf[i] = g3d_read_int8(f);
69}
70
71G3DFloat g3d_read_float_be(FILE *f)
72{
73 union {
74 G3DFloat f;
75 guint8 u[4];
76 } u;
77
78#if G_BYTE_ORDER == G_LITTLE_ENDIAN
79 g3d_read_bytes_swap(f, u.u, 4);
80#elif G_BYTE_ORDER == G_BIG_ENDIAN
81 g3d_read_bytes(f, u.u, 4);
82#endif
83 return u.f;
84}
85
86G3DFloat g3d_read_float_le(FILE *f)
87{
88 union {
89 G3DFloat f;
90 guint8 u[4];
91 } u;
92
93#if G_BYTE_ORDER == G_LITTLE_ENDIAN
94 g3d_read_bytes(f, u.u, 4);
95#elif G_BYTE_ORDER == G_BIG_ENDIAN
96 g3d_read_bytes_swap(f, u.u, 4);
97#endif
98 return u.f;
99}
100
101G3DDouble g3d_read_double_be(FILE *f)
102{
103 union {
104 G3DDouble d;
105 guint8 u[8];
106 } u;
107
108#if G_BYTE_ORDER == G_LITTLE_ENDIAN
109 g3d_read_bytes_swap(f, u.u, 8);
110#elif G_BYTE_ORDER == G_BIG_ENDIAN
111 g3d_read_bytes(f, u.u, 8);
112#endif
113 return u.d;
114}
115
116G3DDouble g3d_read_double_le(FILE *f)
117{
118 union {
119 G3DDouble d;
120 guint8 u[8];
121 } u;
122
123#if G_BYTE_ORDER == G_LITTLE_ENDIAN
124 g3d_read_bytes(f, u.u, 8);
125#elif G_BYTE_ORDER == G_BIG_ENDIAN
126 g3d_read_bytes_swap(f, u.u, 8);
127#endif
128 return u.d;
129}
130
131gint32 g3d_read_cstr(FILE *f, gchar *buffer, gint32 max_len)
132{
133 gint32 n = 0;
134 gchar c;
135
136 do
137 {
138 c = g3d_read_int8(f);
139 buffer[n] = c;
140 n ++;
141 }
142 while((c != 0) && (n < max_len));
143
144 buffer[max_len - 1] = '\0';
145
146 return n;
147}