sfxstdio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: sfxstdio.c,v 1.9 2004/08/05 17:02:36 stefan Exp $ */
  14. /* File stream implementation using stdio */
  15. #include "stdio_.h" /* includes std.h */
  16. #include "memory_.h"
  17. #include "gdebug.h"
  18. #include "gpcheck.h"
  19. #include "stream.h"
  20. #include "strimpl.h"
  21. /* Forward references for file stream procedures */
  22. private int
  23. s_file_available(stream *, long *),
  24. s_file_read_seek(stream *, long),
  25. s_file_read_close(stream *),
  26. s_file_read_process(stream_state *, stream_cursor_read *,
  27. stream_cursor_write *, bool);
  28. private int
  29. s_file_write_seek(stream *, long),
  30. s_file_write_flush(stream *),
  31. s_file_write_close(stream *),
  32. s_file_write_process(stream_state *, stream_cursor_read *,
  33. stream_cursor_write *, bool);
  34. private int
  35. s_file_switch(stream *, bool);
  36. /* ------ File reading ------ */
  37. /* Initialize a stream for reading an OS file. */
  38. void
  39. sread_file(register stream * s, FILE * file, byte * buf, uint len)
  40. {
  41. static const stream_procs p = {
  42. s_file_available, s_file_read_seek, s_std_read_reset,
  43. s_std_read_flush, s_file_read_close, s_file_read_process,
  44. s_file_switch
  45. };
  46. /*
  47. * There is no really portable way to test seekability, but this should
  48. * work on most systems. Note that if our probe sets the ferror bit for
  49. * the stream, we have to clear it again to avoid trouble later.
  50. */
  51. int had_error = ferror(file);
  52. long curpos = ftell(file);
  53. bool seekable = (curpos != -1L && fseek(file, curpos, SEEK_SET) == 0);
  54. if (!had_error)
  55. clearerr(file);
  56. s_std_init(s, buf, len, &p,
  57. (seekable ? s_mode_read + s_mode_seek : s_mode_read));
  58. if_debug1('s', "[s]read file=0x%lx\n", (ulong) file);
  59. s->file = file;
  60. s->file_modes = s->modes;
  61. s->file_offset = 0;
  62. s->file_limit = max_long;
  63. }
  64. /* Confine reading to a subfile. This is primarily for reusable streams. */
  65. int
  66. sread_subfile(stream *s, long start, long length)
  67. {
  68. if (s->file == 0 || s->modes != s_mode_read + s_mode_seek ||
  69. s->file_offset != 0 || s->file_limit != max_long ||
  70. ((s->position < start || s->position > start + length) &&
  71. sseek(s, start) < 0)
  72. )
  73. return ERRC;
  74. s->position -= start;
  75. s->file_offset = start;
  76. s->file_limit = length;
  77. return 0;
  78. }
  79. /* Procedures for reading from a file */
  80. private int
  81. s_file_available(register stream * s, long *pl)
  82. {
  83. long max_avail = s->file_limit - stell(s);
  84. long buf_avail = sbufavailable(s);
  85. *pl = min(max_avail, buf_avail);
  86. if (sseekable(s)) {
  87. long pos, end;
  88. pos = ftell(s->file);
  89. if (fseek(s->file, 0L, SEEK_END))
  90. return ERRC;
  91. end = ftell(s->file);
  92. if (fseek(s->file, pos, SEEK_SET))
  93. return ERRC;
  94. buf_avail += end - pos;
  95. *pl = min(max_avail, buf_avail);
  96. if (*pl == 0)
  97. *pl = -1; /* EOF */
  98. } else {
  99. if (*pl == 0 && feof(s->file))
  100. *pl = -1; /* EOF */
  101. }
  102. return 0;
  103. }
  104. private int
  105. s_file_read_seek(register stream * s, long pos)
  106. {
  107. uint end = s->srlimit - s->cbuf + 1;
  108. long offset = pos - s->position;
  109. if (offset >= 0 && offset <= end) { /* Staying within the same buffer */
  110. s->srptr = s->cbuf + offset - 1;
  111. return 0;
  112. }
  113. if (pos < 0 || pos > s->file_limit ||
  114. fseek(s->file, s->file_offset + pos, SEEK_SET) != 0
  115. )
  116. return ERRC;
  117. s->srptr = s->srlimit = s->cbuf - 1;
  118. s->end_status = 0;
  119. s->position = pos;
  120. return 0;
  121. }
  122. private int
  123. s_file_read_close(stream * s)
  124. {
  125. FILE *file = s->file;
  126. if (file != 0) {
  127. s->file = 0;
  128. return (fclose(file) ? ERRC : 0);
  129. }
  130. return 0;
  131. }
  132. /*
  133. * Process a buffer for a file reading stream.
  134. * This is the first stream in the pipeline, so pr is irrelevant.
  135. */
  136. private int
  137. s_file_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  138. stream_cursor_write * pw, bool last)
  139. {
  140. stream *s = (stream *)st; /* no separate state */
  141. FILE *file = s->file;
  142. uint max_count = pw->limit - pw->ptr;
  143. int status = 1;
  144. int count;
  145. if (s->file_limit < max_long) {
  146. long limit_count = s->file_offset + s->file_limit - ftell(file);
  147. if (max_count > limit_count)
  148. max_count = limit_count, status = EOFC;
  149. }
  150. count = fread(pw->ptr + 1, 1, max_count, file);
  151. if (count < 0)
  152. count = 0;
  153. pw->ptr += count;
  154. process_interrupts(s->memory);
  155. return (ferror(file) ? ERRC : feof(file) ? EOFC : status);
  156. }
  157. /* ------ File writing ------ */
  158. /* Initialize a stream for writing an OS file. */
  159. void
  160. swrite_file(register stream * s, FILE * file, byte * buf, uint len)
  161. {
  162. static const stream_procs p = {
  163. s_std_noavailable, s_file_write_seek, s_std_write_reset,
  164. s_file_write_flush, s_file_write_close, s_file_write_process,
  165. s_file_switch
  166. };
  167. s_std_init(s, buf, len, &p,
  168. (file == stdout ? s_mode_write : s_mode_write + s_mode_seek));
  169. if_debug1('s', "[s]write file=0x%lx\n", (ulong) file);
  170. s->file = file;
  171. s->file_modes = s->modes;
  172. s->file_offset = 0; /* in case we switch to reading later */
  173. s->file_limit = max_long; /* ibid. */
  174. }
  175. /* Initialize for appending to an OS file. */
  176. void
  177. sappend_file(register stream * s, FILE * file, byte * buf, uint len)
  178. {
  179. swrite_file(s, file, buf, len);
  180. s->modes = s_mode_write + s_mode_append; /* no seek */
  181. s->file_modes = s->modes;
  182. fseek(file, 0L, SEEK_END);
  183. s->position = ftell(file);
  184. }
  185. /* Procedures for writing on a file */
  186. private int
  187. s_file_write_seek(stream * s, long pos)
  188. {
  189. /* We must flush the buffer to reposition. */
  190. int code = sflush(s);
  191. if (code < 0)
  192. return code;
  193. if (fseek(s->file, pos, SEEK_SET) != 0)
  194. return ERRC;
  195. s->position = pos;
  196. return 0;
  197. }
  198. private int
  199. s_file_write_flush(register stream * s)
  200. {
  201. int result = s_process_write_buf(s, false);
  202. fflush(s->file);
  203. return result;
  204. }
  205. private int
  206. s_file_write_close(register stream * s)
  207. {
  208. s_process_write_buf(s, true);
  209. return s_file_read_close(s);
  210. }
  211. /*
  212. * Process a buffer for a file writing stream.
  213. * This is the last stream in the pipeline, so pw is irrelevant.
  214. */
  215. private int
  216. s_file_write_process(stream_state * st, stream_cursor_read * pr,
  217. stream_cursor_write * ignore_pw, bool last)
  218. {
  219. uint count = pr->limit - pr->ptr;
  220. /*
  221. * The DEC C library on AXP architectures gives an error on
  222. * fwrite if the count is zero!
  223. */
  224. if (count != 0) {
  225. FILE *file = ((stream *) st)->file;
  226. int written = fwrite(pr->ptr + 1, 1, count, file);
  227. if (written < 0)
  228. written = 0;
  229. pr->ptr += written;
  230. process_interrupts(NULL);
  231. return (ferror(file) ? ERRC : 0);
  232. } else {
  233. process_interrupts(NULL);
  234. return 0;
  235. }
  236. }
  237. /* ------ File switching ------ */
  238. /* Switch a file stream to reading or writing. */
  239. private int
  240. s_file_switch(stream * s, bool writing)
  241. {
  242. uint modes = s->file_modes;
  243. FILE *file = s->file;
  244. long pos;
  245. if (writing) {
  246. if (!(s->file_modes & s_mode_write))
  247. return ERRC;
  248. pos = stell(s);
  249. if_debug2('s', "[s]switch 0x%lx to write at %ld\n",
  250. (ulong) s, pos);
  251. fseek(file, pos, SEEK_SET);
  252. if (modes & s_mode_append) {
  253. sappend_file(s, file, s->cbuf, s->cbsize); /* sets position */
  254. } else {
  255. swrite_file(s, file, s->cbuf, s->cbsize);
  256. s->position = pos;
  257. }
  258. s->modes = modes;
  259. } else {
  260. if (!(s->file_modes & s_mode_read))
  261. return ERRC;
  262. pos = stell(s);
  263. if_debug2('s', "[s]switch 0x%lx to read at %ld\n",
  264. (ulong) s, pos);
  265. if (sflush(s) < 0)
  266. return ERRC;
  267. fseek(file, 0L, SEEK_CUR); /* pacify C library */
  268. sread_file(s, file, s->cbuf, s->cbsize);
  269. s->modes |= modes & s_mode_append; /* don't lose append info */
  270. s->position = pos;
  271. }
  272. s->file_modes = modes;
  273. return 0;
  274. }