ia64/xen-unstable

view tools/libxutil/file_stream.c @ 2422:2274a0386cc9

bitkeeper revision 1.1159.69.5 (4138e882jA1YaR_OfTfNHe_uT4PDIg)

trivial
author iap10@labyrinth.cl.cam.ac.uk
date Fri Sep 03 21:56:18 2004 +0000 (2004-09-03)
parents 236a9f2698a3
children 94b7d61e3dfe 78dff7880271 fbad927100bc a01199a95070
line source
1 /*
2 * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
19 /** @file
20 * An IOStream implementation using FILE*.
21 */
22 #ifndef __KERNEL__
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "allocate.h"
26 #include "file_stream.h"
28 static int file_read(IOStream *s, void *buf, size_t n);
29 static int file_write(IOStream *s, const void *buf, size_t n);
30 static int file_error(IOStream *s);
31 static int file_close(IOStream *s);
32 static void file_free(IOStream *s);
33 static int file_flush(IOStream *s);
35 /** Methods used by a FILE* IOStream. */
36 static const IOMethods file_methods = {
37 read: file_read,
38 write: file_write,
39 error: file_error,
40 close: file_close,
41 free: file_free,
42 flush: file_flush,
43 };
45 /** IOStream for stdin. */
46 static IOStream _iostdin = {
47 methods: &file_methods,
48 data: (void*)1,
49 };
51 /** IOStream for stdout. */
52 static IOStream _iostdout = {
53 methods: &file_methods,
54 data: (void*)2,
55 };
57 /** IOStream for stderr. */
58 static IOStream _iostderr = {
59 methods: &file_methods,
60 data: (void*)3,
61 };
63 /** IOStream for stdin. */
64 IOStream *iostdin = &_iostdin;
66 /** IOStream for stdout. */
67 IOStream *iostdout = &_iostdout;
69 /** IOStream for stderr. */
70 IOStream *iostderr = &_iostderr;
72 /** Get the underlying FILE*.
73 *
74 * @param s file stream
75 * @return the stream s wraps
76 */
77 static inline FILE *get_file(IOStream *s){
78 switch((long)s->data){
79 case 1: s->data = stdin; break;
80 case 2: s->data = stdout; break;
81 case 3: s->data = stderr; break;
82 }
83 return (FILE*)s->data;
84 }
86 /** Control buffering on the underlying stream, like setvbuf().
87 *
88 * @param io file stream
89 * @param buf buffer
90 * @param mode buffering mode (see man setvbuf())
91 * @param size buffer size
92 * @return 0 on success, non-zero otherwise
93 */
94 int file_stream_setvbuf(IOStream *io, char *buf, int mode, size_t size){
95 return setvbuf(get_file(io), buf, mode, size);
96 }
98 /** Write to the underlying stream using fwrite();
99 *
100 * @param stream input
101 * @param buf where to put input
102 * @param n number of bytes to write
103 * @return number of bytes written
104 */
105 static int file_write(IOStream *s, const void *buf, size_t n){
106 return fwrite(buf, 1, n, get_file(s));
107 }
109 /** Read from the underlying stream using fread();
110 *
111 * @param stream input
112 * @param buf where to put input
113 * @param n number of bytes to read
114 * @return number of bytes read
115 */
116 static int file_read(IOStream *s, void *buf, size_t n){
117 return fread(buf, 1, n, get_file(s));
118 }
120 /** Fush the underlying stream using fflush().
121 *
122 * @param s file stream
123 * @return 0 on success, error code otherwise
124 */
125 static int file_flush(IOStream *s){
126 return fflush(get_file(s));
127 }
129 /** Check if a stream has an error.
130 *
131 * @param s file stream
132 * @return 1 if has an error, 0 otherwise
133 */
134 static int file_error(IOStream *s){
135 return ferror(get_file(s));
136 }
138 /** Close a file stream.
139 *
140 * @param s file stream to close
141 * @return result of the close
142 */
143 static int file_close(IOStream *s){
144 return fclose(get_file(s));
145 }
147 /** Free a file stream.
148 *
149 * @param s file stream
150 */
151 static void file_free(IOStream *s){
152 // Do nothing - fclose does it all?
153 }
155 /** Create an IOStream for a stream.
156 *
157 * @param f stream to wrap
158 * @return new IOStream using f for i/o
159 */
160 IOStream *file_stream_new(FILE *f){
161 IOStream *io = ALLOCATE(IOStream);
162 if(io){
163 io->methods = &file_methods;
164 io->data = (void*)f;
165 }
166 return io;
167 }
169 /** IOStream version of fopen().
170 *
171 * @param file name of the file to open
172 * @param flags giving the mode to open in (as for fopen())
173 * @return new stream for the open file, or 0 if failed
174 */
175 IOStream *file_stream_fopen(const char *file, const char *flags){
176 IOStream *io = 0;
177 FILE *fin = fopen(file, flags);
178 if(fin){
179 io = file_stream_new(fin);
180 if(!io){
181 fclose(fin);
182 //free(fin); // fclose frees ?
183 }
184 }
185 return io;
186 }
188 /** IOStream version of fdopen().
189 *
190 * @param fd file descriptor
191 * @param flags giving the mode to open in (as for fdopen())
192 * @return new stream for the open file, or 0 if failed
193 */
194 IOStream *file_stream_fdopen(int fd, const char *flags){
195 IOStream *io = 0;
196 FILE *fin = fdopen(fd, flags);
197 if(fin){
198 io = file_stream_new(fin);
199 }
200 return io;
201 }
202 #endif