diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/src/stream.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/src/stream.c | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/src/stream.c b/src/others/mimesh/libg3d-0.0.8/src/stream.c new file mode 100644 index 0000000..5f3930d --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/src/stream.c | |||
@@ -0,0 +1,171 @@ | |||
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/stream.h> | ||
24 | |||
25 | EAPI | ||
26 | gboolean g3d_stream_is_seekable(G3DStream *stream) | ||
27 | { | ||
28 | return (stream->flags & (1 << G3D_STREAM_SEEKABLE)); | ||
29 | } | ||
30 | |||
31 | EAPI | ||
32 | gchar *g3d_stream_get_uri(G3DStream *stream) | ||
33 | { | ||
34 | return stream->uri; | ||
35 | } | ||
36 | |||
37 | EAPI | ||
38 | gsize g3d_stream_read(G3DStream *stream, gpointer ptr, gsize size) | ||
39 | { | ||
40 | if(stream->read) | ||
41 | return stream->read(ptr, size, stream->data); | ||
42 | return -1; | ||
43 | } | ||
44 | |||
45 | static gchar *generic_readline(G3DStream *stream, gchar *buf, gsize size) | ||
46 | { | ||
47 | gint32 i; | ||
48 | |||
49 | for(i = 0; i < (size - 1); i ++) { | ||
50 | if(g3d_stream_eof(stream)) | ||
51 | return NULL; | ||
52 | if(g3d_stream_read(stream, buf + i, 1) <= 0) | ||
53 | return NULL; | ||
54 | if(buf[i] == '\n') { | ||
55 | buf[i + 1] = '\0'; | ||
56 | return buf; | ||
57 | } | ||
58 | } | ||
59 | buf[size - 1] = '\0'; | ||
60 | return buf; | ||
61 | } | ||
62 | |||
63 | EAPI | ||
64 | gchar *g3d_stream_read_line(G3DStream *stream, gchar *buf, gsize size) | ||
65 | { | ||
66 | stream->linecount ++; | ||
67 | |||
68 | if(stream->readline) | ||
69 | return stream->readline(buf, size, stream->data); | ||
70 | else | ||
71 | return generic_readline(stream, buf, size); | ||
72 | } | ||
73 | |||
74 | EAPI | ||
75 | gint g3d_stream_skip(G3DStream *stream, goffset offset) | ||
76 | { | ||
77 | /* g_return_val_if_fail(offset >= 0, -1); */ | ||
78 | if(stream->seek) | ||
79 | return stream->seek(stream->data, offset, G_SEEK_CUR); | ||
80 | else { | ||
81 | goffset todo = offset; | ||
82 | gsize s; | ||
83 | guint8 *buffer = g_new0(guint8, 1024); | ||
84 | while(todo > 0) { | ||
85 | s = MIN(todo, 1024); | ||
86 | if(stream->read(buffer, s, stream->data) < s) { | ||
87 | g_free(buffer); | ||
88 | return -1; | ||
89 | } | ||
90 | offset -= s; | ||
91 | } | ||
92 | g_free(buffer); | ||
93 | return 0; | ||
94 | } | ||
95 | } | ||
96 | |||
97 | EAPI | ||
98 | gint g3d_stream_seek(G3DStream *stream, goffset offset, GSeekType whence) | ||
99 | { | ||
100 | if(stream->seek) | ||
101 | return stream->seek(stream->data, offset, whence); | ||
102 | return -1; | ||
103 | } | ||
104 | |||
105 | EAPI | ||
106 | goffset g3d_stream_tell(G3DStream *stream) | ||
107 | { | ||
108 | if(stream->tell) | ||
109 | return stream->tell(stream->data); | ||
110 | return 0; | ||
111 | } | ||
112 | |||
113 | EAPI | ||
114 | guint32 g3d_stream_line(G3DStream *stream) | ||
115 | { | ||
116 | return stream->linecount; | ||
117 | } | ||
118 | |||
119 | EAPI | ||
120 | goffset g3d_stream_size(G3DStream *stream) | ||
121 | { | ||
122 | if(stream->size) | ||
123 | return stream->size(stream->data); | ||
124 | return -1; | ||
125 | } | ||
126 | |||
127 | EAPI | ||
128 | gboolean g3d_stream_eof(G3DStream *stream) | ||
129 | { | ||
130 | if(stream->eof) | ||
131 | return stream->eof(stream->data); | ||
132 | return FALSE; | ||
133 | } | ||
134 | |||
135 | EAPI | ||
136 | gint g3d_stream_close(G3DStream *stream) | ||
137 | { | ||
138 | gint retval = 0; | ||
139 | if(stream->close) | ||
140 | retval = stream->close(stream->data); | ||
141 | g_free(stream->uri); | ||
142 | g_free(stream); | ||
143 | return retval; | ||
144 | } | ||
145 | |||
146 | EAPI | ||
147 | G3DStream *g3d_stream_new_custom(guint32 flags, const gchar *uri, | ||
148 | G3DStreamReadFunc readfunc, G3DStreamReadLineFunc readlinefunc, | ||
149 | G3DStreamSeekFunc seekfunc, G3DStreamTellFunc tellfunc, | ||
150 | G3DStreamSizeFunc sizefunc, | ||
151 | G3DStreamEofFunc eoffunc, G3DStreamCloseFunc closefunc, gpointer data, | ||
152 | G3DStream *zip_container) | ||
153 | { | ||
154 | G3DStream *stream; | ||
155 | |||
156 | stream = g_new0(G3DStream, 1); | ||
157 | stream->flags = flags; | ||
158 | stream->uri = g_strdup(uri); | ||
159 | stream->read = readfunc; | ||
160 | stream->readline = readlinefunc; | ||
161 | stream->seek = seekfunc; | ||
162 | stream->tell = tellfunc; | ||
163 | stream->size = sizefunc; | ||
164 | stream->eof = eoffunc; | ||
165 | stream->close = closefunc; | ||
166 | stream->data = data; | ||
167 | stream->zip_container = zip_container; | ||
168 | |||
169 | return stream; | ||
170 | } | ||
171 | |||