aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/src/stream_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/src/stream_file.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/src/stream_file.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/src/stream_file.c b/src/others/mimesh/libg3d-0.0.8/src/stream_file.c
new file mode 100644
index 0000000..37c5f35
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/src/stream_file.c
@@ -0,0 +1,117 @@
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 <glib/gstdio.h>
25#include <g3d/stream.h>
26
27typedef struct {
28 FILE *f;
29 goffset size;
30} G3DStreamFile;
31
32static gsize g3d_stream_file_read(gpointer ptr, gsize size, gpointer data)
33{
34 G3DStreamFile *sf = (G3DStreamFile *)data;
35 return fread((void *)ptr, 1, size, sf->f);
36}
37
38static gchar *g3d_stream_file_read_line(gchar *buf, gsize size, gpointer data)
39{
40 G3DStreamFile *sf = (G3DStreamFile *)data;
41 return fgets(buf, size, sf->f);
42}
43
44static gint g3d_stream_file_seek(gpointer data, goffset offset,
45 GSeekType whence)
46{
47 G3DStreamFile *sf = (G3DStreamFile *)data;
48 int w = SEEK_SET;
49 switch(whence) {
50 case G_SEEK_SET: w = SEEK_SET; break;
51 case G_SEEK_CUR: w = SEEK_CUR; break;
52 case G_SEEK_END: w = SEEK_END; break;
53 }
54 return fseek(sf->f, offset, w);
55}
56
57static goffset g3d_stream_file_tell(gpointer data)
58{
59 G3DStreamFile *sf = (G3DStreamFile *)data;
60 return ftell(sf->f);
61}
62
63static goffset g3d_stream_file_size(gpointer data)
64{
65 G3DStreamFile *sf = (G3DStreamFile *)data;
66 return sf->size;
67}
68
69static gboolean g3d_stream_file_eof(gpointer data)
70{
71 G3DStreamFile *sf = (G3DStreamFile *)data;
72 return feof(sf->f);
73}
74
75static gint g3d_stream_file_close(gpointer data)
76{
77 G3DStreamFile *sf = (G3DStreamFile *)data;
78 FILE *f = sf->f;
79 g_free(sf);
80 return fclose(f);
81}
82
83EAPI
84G3DStream *g3d_stream_open_file(const gchar *filename, const gchar *mode)
85{
86 G3DStreamFile *sf;
87 struct stat stats;
88 guint32 flags = 0;
89
90 sf = g_new0(G3DStreamFile, 1);
91
92 /* open file; may be moved to some GLib interface */
93 sf->f = fopen(filename, mode);
94 if(sf->f == NULL) {
95 g_free(sf);
96 return NULL;
97 }
98
99 /* get file size */
100 if(g_stat(filename, &stats) != 0)
101 sf->size = -1;
102 sf->size = stats.st_size;
103
104 if(mode[0] == 'r')
105 flags |= (1 << G3D_STREAM_READABLE);
106 if(mode[0] == 'w')
107 flags |= (1 << G3D_STREAM_WRITABLE);
108
109 flags |= (1 << G3D_STREAM_SEEKABLE);
110
111 return g3d_stream_new_custom(flags, filename,
112 g3d_stream_file_read, g3d_stream_file_read_line,
113 g3d_stream_file_seek, g3d_stream_file_tell,
114 g3d_stream_file_size, g3d_stream_file_eof,
115 g3d_stream_file_close, sf, NULL);
116}
117