1
0

sfxstdio.c 8.2 KB

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