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