1 /* -*- Mode: C; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
2
3 /*
4 * GImageView
5 * Copyright (C) 2001 Takuro Ashie
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *
21 * $Id: gimv_io_file.c,v 1.1 2003/06/18 09:26:56 makeinu Exp $
22 */
23
24 #include <stdio.h>
25
26 #include "gimv_io.h"
27 #include "gimv_io_file.h"
28
29
30 static GimvIOStatus gimv_io_file_read (GimvIO *gio,
31 gchar *buf,
32 guint count,
33 guint *bytes_read);
34 static GimvIOStatus gimv_io_file_write (GimvIO *gio,
35 const gchar *buf,
36 guint count,
37 guint *bytes_written);
38 static GimvIOStatus gimv_io_file_seek (GimvIO *gio,
39 glong offset,
40 gint whence);
41 static GimvIOStatus gimv_io_file_tell (GimvIO *gio,
42 glong *offset);
43 static void gimv_io_file_close (GimvIO *gio);
44
45
46 typedef struct GimvIOFile_Tag GimvIOFile;
47
48 struct GimvIOFile_Tag
49 {
50 GimvIO parent;
51 FILE *fd;
52 };
53
54 GimvIOFuncs gimv_io_file_funcs =
55 {
56 gimv_io_file_read,
57 gimv_io_file_write,
58 gimv_io_file_seek,
59 gimv_io_file_tell,
60 gimv_io_file_close,
61 };
62
63
64 GimvIO *
gimv_io_file_new(const gchar * url,const gchar * mode)65 gimv_io_file_new (const gchar *url, const gchar *mode)
66 {
67 GimvIOFile *fio;
68 GimvIO *gio;
69 const gchar *filename = url;
70
71 g_return_val_if_fail (url, NULL);
72
73 fio = g_new0 (GimvIOFile, 1);
74 gio = (GimvIO *) fio;
75
76 gimv_io_init (gio, url);
77 gio->funcs = &gimv_io_file_funcs;
78
79 fio->fd = fopen (filename, (char *) mode);
80 if (!fio->fd) {
81 gimv_io_close (gio);
82 return NULL;
83 }
84
85 return gio;
86 }
87
88
89 static GimvIOStatus
gimv_io_file_read(GimvIO * gio,gchar * buf,guint count,guint * bytes_read)90 gimv_io_file_read (GimvIO *gio,
91 gchar *buf,
92 guint count,
93 guint *bytes_read)
94 {
95 GimvIOFile *fio = (GimvIOFile *) gio;
96
97 *bytes_read = fread (buf, 1, count, fio->fd);
98
99 return GIMV_IO_STATUS_NORMAL;
100 }
101
102
103 static GimvIOStatus
gimv_io_file_write(GimvIO * gio,const gchar * buf,guint count,guint * bytes_written)104 gimv_io_file_write (GimvIO *gio,
105 const gchar *buf,
106 guint count,
107 guint *bytes_written)
108 {
109 GimvIOFile *fio = (GimvIOFile *) gio;
110
111 *bytes_written = fwrite (buf, 1, count, fio->fd);
112
113 return GIMV_IO_STATUS_NORMAL;
114 }
115
116
117 static GimvIOStatus
gimv_io_file_seek(GimvIO * gio,glong offset,gint whence)118 gimv_io_file_seek (GimvIO *gio,
119 glong offset,
120 gint whence)
121 {
122 GimvIOFile *fio = (GimvIOFile *) gio;
123
124 fseek (fio->fd, offset, whence);
125
126 return GIMV_IO_STATUS_NORMAL;
127 }
128
129
130 static GimvIOStatus
gimv_io_file_tell(GimvIO * gio,glong * offset)131 gimv_io_file_tell (GimvIO *gio,
132 glong *offset)
133 {
134 GimvIOFile *fio = (GimvIOFile *) gio;
135
136 *offset = ftell (fio->fd);
137
138 return GIMV_IO_STATUS_NORMAL;
139 }
140
141
142 static void
gimv_io_file_close(GimvIO * gio)143 gimv_io_file_close (GimvIO *gio)
144 {
145 GimvIOFile *fio = (GimvIOFile *) gio;
146
147 if (fio->fd)
148 fclose (fio->fd);
149 }
150