PageRenderTime 56ms CodeModel.GetById 13ms app.highlight 33ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/rel-1-3-26/SWIG/Source/DOH/file.c

#
C | 307 lines | 208 code | 36 blank | 63 comment | 32 complexity | 31dd88b35eaa2ec33e00b0d0f5722766 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1/* ----------------------------------------------------------------------------- 
  2 * file.c
  3 *
  4 *     This file implements a file-like object that can be built around an 
  5 *     ordinary FILE * or integer file descriptor.
  6 * 
  7 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  8 *
  9 * Copyright (C) 1999-2000.  The University of Chicago
 10 * See the file LICENSE for information on usage and redistribution.	
 11 * ----------------------------------------------------------------------------- */
 12
 13char cvsroot_file_c[] = "$Header$";
 14
 15#include "dohint.h"
 16
 17#ifdef DOH_INTFILE
 18#include <unistd.h>
 19#endif
 20#include <errno.h>
 21
 22typedef struct {
 23  FILE     *filep;       
 24  int       fd;
 25  int       closeondel;
 26} DohFile;
 27
 28/* -----------------------------------------------------------------------------
 29 * DelFile()
 30 * ----------------------------------------------------------------------------- */
 31
 32static void
 33DelFile(DOH *fo) {
 34  DohFile *f = (DohFile *) ObjData(fo);
 35  if (f->closeondel) {
 36    if (f->filep) {
 37      fclose(f->filep);
 38    }
 39#ifdef DOH_INTFILE
 40    if (f->fd) {
 41      close(f->fd);
 42    }
 43#endif
 44  }
 45  DohFree(f);
 46}
 47
 48/* -----------------------------------------------------------------------------
 49 * File_read()
 50 * ----------------------------------------------------------------------------- */
 51
 52static int
 53File_read(DOH *fo, void *buffer, int len) {
 54  DohFile *f = (DohFile *) ObjData(fo);
 55  
 56  if (f->filep) {
 57    return fread(buffer,1,len,f->filep);
 58  } else if (f->fd) {
 59#ifdef DOH_INTFILE
 60    return read(f->fd,buffer,len);
 61#endif
 62  }
 63  return -1;
 64}
 65
 66/* -----------------------------------------------------------------------------
 67 * File_write()
 68 * ----------------------------------------------------------------------------- */
 69
 70static int
 71File_write(DOH *fo, void *buffer, int len) {
 72  DohFile *f = (DohFile *) ObjData(fo);
 73  if (f->filep) {
 74    return fwrite(buffer,1,len,f->filep);
 75  } else if (f->fd) {
 76#ifdef DOH_INTFILE
 77    return write(f->fd,buffer,len);
 78#endif
 79  }
 80  return -1;
 81}
 82
 83/* -----------------------------------------------------------------------------
 84 * File_seek()
 85 * ----------------------------------------------------------------------------- */
 86
 87static int
 88File_seek(DOH *fo, long offset, int whence) {
 89  DohFile *f = (DohFile *) ObjData(fo);
 90  if (f->filep) {
 91    return fseek(f->filep,offset,whence);
 92  } else if (f->fd) {
 93#ifdef DOH_INTFILE
 94    return lseek(f->fd, offset, whence);
 95#endif
 96  }
 97  return -1;
 98}
 99
100/* -----------------------------------------------------------------------------
101 * File_tell()
102 * ----------------------------------------------------------------------------- */
103
104static long
105File_tell(DOH *fo) {
106  DohFile *f = (DohFile *) ObjData(fo);
107  if (f->filep) {
108    return ftell(f->filep);
109  } else if (f->fd) {
110#ifdef DOH_INTFILE
111    return lseek(f->fd, 0, SEEK_CUR);
112#endif
113  }
114  return -1;
115}
116
117/* -----------------------------------------------------------------------------
118 * File_putc()
119 * ----------------------------------------------------------------------------- */
120
121static int 
122File_putc(DOH *fo, int ch) {
123  DohFile *f = (DohFile *) ObjData(fo);
124  if (f->filep) {
125    return fputc(ch,f->filep);
126  } else if (f->fd) {
127#ifdef DOH_INTFILE
128    char c;
129    c = (char) ch;
130    return write(f->fd,&c,1);
131#endif
132  }
133  return -1;
134}
135
136/* -----------------------------------------------------------------------------
137 * File_getc()
138 * ----------------------------------------------------------------------------- */
139
140static int 
141File_getc(DOH *fo) {
142  DohFile *f = (DohFile *) ObjData(fo);
143  if (f->filep) {
144    return fgetc(f->filep);
145  } else if (f->fd) {
146#ifdef DOH_INTFILE
147    char c;
148    if (read(f->fd,&c,1) < 0) return EOF;
149    return c;
150#endif
151  }
152  return EOF;
153}
154
155/* -----------------------------------------------------------------------------
156 * File_ungetc()
157 *
158 * Put a character back onto the input
159 * ----------------------------------------------------------------------------- */
160
161static int 
162File_ungetc(DOH *fo, int ch) {
163  DohFile *f = (DohFile *) ObjData(fo);
164  if (f->filep) {
165    return ungetc(ch, f->filep);
166  } else if (f->fd) {
167#ifdef DOH_INTFILE
168    /* Not implemented yet */
169#endif
170  }
171  return -1;
172}
173
174/* -----------------------------------------------------------------------------
175 * File_close()
176 *
177 * Close the file
178 * ----------------------------------------------------------------------------- */
179
180static int
181File_close(DOH *fo) {
182  int ret = 0;
183  DohFile *f = (DohFile *) ObjData(fo);
184  if (f->filep) {
185    ret = fclose(f->filep);
186    f->filep = 0;
187  } else if (f->fd) {
188#ifdef DOH_INTFILE
189    ret = close(f->fd);
190    f->fd = 0;
191#endif
192  }
193  return ret;
194}
195
196static DohFileMethods FileFileMethods = {
197  File_read, 
198  File_write,
199  File_putc,
200  File_getc,
201  File_ungetc,
202  File_seek,
203  File_tell,
204  File_close,           /* close */
205};
206
207static DohObjInfo DohFileType = {
208    "DohFile",          /* objname      */
209    DelFile,         /* doh_del      */
210    0,               /* doh_copy     */
211    0,               /* doh_clear    */
212    0,               /* doh_str      */
213    0,               /* doh_data     */
214    0,               /* doh_dump     */
215    0,               /* doh_len      */
216    0,               /* doh_hash     */
217    0,               /* doh_cmp      */
218    0,               /* doh_first    */
219    0,               /* doh_next     */
220    0,               /* doh_setfile  */
221    0,               /* doh_getfile  */
222    0,               /* doh_setline  */
223    0,               /* doh_getline  */
224    0,               /* doh_mapping  */
225    0,               /* doh_sequence */
226    &FileFileMethods,/* doh_file     */
227    0,               /* doh_string   */
228    0,               /* doh_callable */
229    0,               /* doh_position */
230};
231
232/* -----------------------------------------------------------------------------
233 * NewFile()
234 *
235 * Create a new file from a given filename and mode.
236 * ----------------------------------------------------------------------------- */
237
238DOH *
239DohNewFile(DOH *fn, const char *mode)
240{
241  DohFile *f;
242  FILE *file;
243  char *filename;
244
245  filename = Char(fn);
246  file = fopen(filename,mode);
247  if (!file) return 0;
248
249  f = (DohFile *) DohMalloc(sizeof(DohFile));
250  if (!f) {
251    fclose(file);
252    return 0;
253  }
254  f->filep = file;
255  f->fd = 0;
256  f->closeondel = 1;
257  return DohObjMalloc(&DohFileType,f);
258}
259
260/* -----------------------------------------------------------------------------
261 * NewFileFromFile()
262 *
263 * Create a file object from an already open FILE *.
264 * ----------------------------------------------------------------------------- */
265
266DOH *
267DohNewFileFromFile(FILE *file) 
268{
269  DohFile *f;
270  f = (DohFile *) DohMalloc(sizeof(DohFile));
271  if (!f) return 0;
272  f->filep = file;
273  f->fd = 0;
274  f->closeondel = 0;
275  return DohObjMalloc(&DohFileType,f);
276}
277
278/* -----------------------------------------------------------------------------
279 * NewFileFromFd()
280 *
281 * Create a file object from an already open FILE *.
282 * ----------------------------------------------------------------------------- */
283
284DOH *
285DohNewFileFromFd(int fd)
286{
287  DohFile *f;
288  f = (DohFile *) DohMalloc(sizeof(DohFile));
289  if (!f) return 0;
290  f->filep = 0;
291  f->fd = fd;
292  f->closeondel = 0;
293  return DohObjMalloc(&DohFileType,f);
294}
295
296/* -----------------------------------------------------------------------------
297 * FileErrorDisplay()
298 *
299 * Display cause of one of the NewFile functions failing.
300 * ----------------------------------------------------------------------------- */
301
302void
303DohFileErrorDisplay(DOHString *filename)
304{
305  Printf(stderr, "Unable to open file %s: %s\n", filename, strerror(errno));
306}
307