home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / gs-2.6.1.4-src.lha / src / amiga / gs-2.6.1.4 / zfileio.c < prev    next >
C/C++ Source or Header  |  1994-01-27  |  11KB  |  487 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zfileio.c */
  20. /* File I/O operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "gp.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "stream.h"
  26. #include "estack.h"
  27. #include "files.h"
  28. #include "iscan.h"
  29. #include "store.h"
  30. #include "gsmatrix.h"            /* for gxdevice.h */
  31. #include "gxdevice.h"
  32. #include "gxdevmem.h"
  33.  
  34. /* Forward references */
  35. int zreadline_from(P4(byte *, uint, uint *, stream *));
  36. private es_ptr zget_current_file(P0());
  37. private int write_string(P2(os_ptr, stream *));
  38.  
  39. /* ------ Operators ------ */
  40.  
  41. /* <file> read <int> true */
  42. /* <file> read false */
  43. int
  44. zread(register os_ptr op)
  45. {    stream *s;
  46.     int ch;
  47.     check_read_file(s, op);
  48.     ch = sgetc(s);
  49.     if ( ch >= 0 )
  50.        {    make_int(op, ch);
  51.         push(1);
  52.         make_bool(op, 1);
  53.        }
  54.     else if ( ch == EOFC )
  55.         make_bool(op, 0);
  56.     else
  57.         return_error(e_ioerror);
  58.     return 0;
  59. }
  60.  
  61. /* <file> <int> write - */
  62. int
  63. zwrite(register os_ptr op)
  64. {    stream *s;
  65.     ulong ch;
  66.     check_write_file(s, op - 1);
  67.     check_type(*op, t_integer);
  68.     ch = op->value.intval;
  69.     if ( ch > 0xff )
  70.         return_error(e_rangecheck);
  71.     sputc(s, (byte)ch);
  72.     pop(2);
  73.     return 0;
  74. }
  75.  
  76. /* <file> <string> readhexstring <substring> <filled_bool> */
  77. int
  78. zreadhexstring(register os_ptr op)
  79. {    stream *s;
  80.     int code;
  81.     uint nread;
  82.     int odd = -1;
  83.     check_read_file(s, op - 1);
  84.     check_write_type(*op, t_string);
  85.     code = sreadhex(s, op->value.bytes, r_size(op), &nread, &odd, 1);
  86.     switch ( code )
  87.        {
  88.     case EOFC:
  89.         /* Reached end-of-file before filling the string. */
  90.         /* Return an appropriate substring. */
  91.         r_set_size(op, nread);
  92.         code = 1;
  93.         break;
  94.     case 0:
  95.         /* Filled the string. */
  96.         break;
  97.     default:            /* Error */
  98.         return_error(e_ioerror);
  99.        }
  100.     ref_assign(op - 1, op);
  101.     make_bool(op, 1 - code);
  102.     return 0;
  103. }
  104.  
  105. /* <file> <string> writehexstring - */
  106. int
  107. zwritehexstring(register os_ptr op)
  108. {    register stream *s;
  109.     register byte ch;
  110.     register const byte *p;
  111.     register const char _ds *hex_digits = "0123456789abcdef";
  112.     register uint len;
  113.     check_write_file(s, op - 1);
  114.     check_read_type(*op, t_string);
  115.     p = op->value.bytes;
  116.     len = r_size(op);
  117.     while ( len-- )
  118.        {    ch = *p++;
  119.         sputc(s, hex_digits[ch >> 4]);
  120.         sputc(s, hex_digits[ch & 0xf]);
  121.        }
  122.     pop(2);
  123.     return 0;
  124. }
  125.  
  126. /* <file> <string> readstring <substring> <filled_bool> */
  127. int
  128. zreadstring(register os_ptr op)
  129. {    stream *s;
  130.     uint len, rlen;
  131.     check_read_file(s, op - 1);
  132.     check_write_type(*op, t_string);
  133.     len = r_size(op);
  134.     rlen = sgets(s, op->value.bytes, len);
  135.     r_set_size(op, rlen);
  136.     op[-1] = *op;
  137.     make_bool(op, (rlen == len ? 1 : 0));
  138.     return 0;
  139. }
  140.  
  141. /* <file> <string> writestring - */
  142. int
  143. zwritestring(register os_ptr op)
  144. {    stream *s;
  145.     int code;
  146.     check_write_file(s, op - 1);
  147.     code = write_string(op, s);
  148.     if ( code >= 0 ) pop(2);
  149.     return code;
  150. }
  151.  
  152. /* <file> <string> readline <substring> <bool> */
  153. int
  154. zreadline(register os_ptr op)
  155. {    stream *s;
  156.     uint count;
  157.     int code;
  158.     check_read_file(s, op - 1);
  159.     check_write_type(*op, t_string);
  160.     code = zreadline_from(op->value.bytes, r_size(op), &count, s);
  161.     if ( code < 0 ) return code;
  162.     r_set_size(op, count);
  163.     op[-1] = *op;
  164.     make_bool(op, code);
  165.     return 0;
  166. }
  167.  
  168. /* Internal readline routine. */
  169. /* Returns 1 if OK, 0 if end of file, or an error code. */
  170. int
  171. zreadline_from(byte *ptr, uint size, uint *pcount, stream *s)
  172. {    uint count = 0;
  173.     int ch;
  174.     for ( ; ; count++ )
  175.        {
  176.         /* Most systems define \n as 0xa and \r as 0xd; however, */
  177.         /* OS-9 has \n == \r == 0xd and \l == 0xa.  The following */
  178.         /* code works properly regardless of environment. */
  179.  
  180.         switch ( ch = sgetc(s) )
  181.            {
  182.         case '\r':
  183. #if '\n' == '\r'            /* OS-9 or similar */
  184. #  define LF 0xa
  185.             if ( s != gs_stream_stdin )
  186.             {    ch = sgetc(s);
  187.                 if ( ch != LF && ch >= 0 )
  188.                     sputback(s);
  189.                         }
  190.             /* falls through */
  191.         case LF:
  192. #  undef LF
  193. #else                    /* '\n' != '\r' */
  194.             ch = sgetc(s);
  195.             if ( ch != '\n' && ch >= 0 ) sputback(s);
  196.             /* falls through */
  197.         case '\n':
  198. #endif
  199.             *pcount = count;
  200.             return 1;
  201.         case EOFC:
  202.             *pcount = count;
  203.             return 0;
  204.            }
  205.         if ( count >= size )    /* filled the string */
  206.            {    sputback(s);
  207.             return_error(e_rangecheck);
  208.            }
  209.         *ptr++ = ch;
  210.        }
  211.     /*return 0;*/        /* not reached */
  212. }
  213.  
  214. /* token - this is called from zstring.c */
  215. int
  216. ztoken_file(register os_ptr op)
  217. {    stream *s;
  218.     int code;
  219.     check_read_file(s, op);
  220.     switch ( code = scan_token(s, 0, (ref *)op) )
  221.        {
  222.     default:            /* possible error */
  223.         if ( code < 0 ) return code;
  224.                     /* read a token */
  225.         push(1);
  226.         make_true(op);
  227.         return 0;
  228.     case scan_EOF:            /* no tokens */
  229.         make_false(op);
  230.         return 0;
  231.        }
  232. }
  233.  
  234. /* <file> bytesavailable <int> */
  235. int
  236. zbytesavailable(register os_ptr op)
  237. {    stream *s;
  238.     long avail;
  239.     check_read_file(s, op);
  240.     if ( savailable(s, &avail) < 0 )
  241.         return_error(e_ioerror);
  242.     make_int(op, avail);
  243.     return 0;
  244. }
  245.  
  246. /* - flush - */
  247. int
  248. zflush(register os_ptr op)
  249. {    sflush(gs_stream_stdout);
  250.     return 0;
  251. }
  252.  
  253. /* <file> flushfile - */
  254. int
  255. zflushfile(register os_ptr op)
  256. {    stream *s;
  257.     check_file(s, op);
  258.     sflush(s);
  259.     pop(1);
  260.     return 0;
  261. }
  262.  
  263. /* <file> resetfile - */
  264. int
  265. zresetfile(register os_ptr op)
  266. {    NYI("resetfile");
  267.     pop(1);
  268.     return 0;
  269. }
  270.  
  271. /* - currentfile <file> */
  272. int
  273. zcurrentfile(register os_ptr op)
  274. {    es_ptr fp;
  275.     push(1);
  276.     /* Check the cache first */
  277.     if ( esfile != 0 )
  278.         ref_assign(op, esfile);
  279.     else if ( (fp = zget_current_file()) == 0 )
  280.        {    /* Return an invalid file object. */
  281.         /* This doesn't make a lot of sense to me, */
  282.         /* but it's what the PostScript manual specifies. */
  283.         make_file(op, 0, ~0, invalid_file_entry);
  284.        }
  285.     else
  286.        {    ref_assign(op, fp);
  287.         /* Load the cache */
  288.         esfile = fp;
  289.        }
  290.     /* Make the returned value literal. */
  291.     r_clear_attrs(op, a_executable);
  292.     return 0;
  293. }
  294.  
  295. /* <string> print - */
  296. int
  297. zprint(register os_ptr op)
  298. {    int code = write_string(op, gs_stream_stdout);
  299.     if ( code >= 0 ) pop(1);
  300.     return code;
  301. }
  302.  
  303. /* <bool> echo - */
  304. int
  305. zecho(register os_ptr op)
  306. {    check_type(*op, t_boolean);
  307.     /****** NOT IMPLEMENTED YET ******/
  308.     pop(1);
  309.     return 0;
  310. }
  311.  
  312. /* ------ Level 2 extensions ------ */
  313.  
  314. /* <file> fileposition <int> */
  315. int
  316. zfileposition(register os_ptr op)
  317. {    stream *s;
  318.     check_file(s, op);
  319.     if ( !sseekable(s) )
  320.         return_error(e_ioerror);
  321.     make_int(op, stell(s));
  322.     return 0;
  323. }
  324.  
  325. /* <file> <int> setfileposition - */
  326. int
  327. zsetfileposition(register os_ptr op)
  328. {    stream *s;
  329.     check_file(s, op - 1);
  330.     check_type(*op, t_integer);
  331.     if ( sseek(s, op->value.intval) < 0 )
  332.         return_error(e_ioerror);
  333.     pop(2);
  334.     return 0;
  335. }
  336.  
  337. /* ------ Ghostscript extensions ------ */
  338.  
  339. /* <file> <int> unread - */
  340. int
  341. zunread(register os_ptr op)
  342. {    stream *s;
  343.     ulong ch;
  344.     check_read_file(s, op - 1);
  345.     check_type(*op, t_integer);
  346.     ch = op->value.intval;
  347.     if ( ch > 0xff )
  348.         return_error(e_rangecheck);
  349.     if ( sungetc(s, (byte)ch) < 0 )
  350.         return_error(e_ioerror);
  351.     pop(2);
  352.     return 0;
  353. }
  354.  
  355. /* <file> <device> writeppmfile - */
  356. int
  357. zwriteppmfile(register os_ptr op)
  358. {    stream *s;
  359.     int code;
  360.     check_write_file(s, op - 1);
  361.     check_type(*op, t_device);
  362.     if ( !gs_device_is_memory(op->value.pdevice) )
  363.         return_error(e_typecheck);
  364.     sflush(s);
  365.     code = gs_writeppmfile((gx_device_memory *)(op->value.pdevice), s->file);
  366.     if ( code >= 0 ) pop(2);
  367.     return code;
  368. }
  369.  
  370. /* ------ Initialization procedure ------ */
  371.  
  372. op_def zfileio_op_defs[] = {
  373.     {"1bytesavailable", zbytesavailable},
  374.     {"0currentfile", zcurrentfile},
  375.     {"1echo", zecho},
  376.     {"1fileposition", zfileposition},
  377.     {"0flush", zflush},
  378.     {"1flushfile", zf