PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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