home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZARRAY.C < prev    next >
C/C++ Source or Header  |  1992-04-24  |  3KB  |  119 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zarray.c */
  21. /* Array operators for GhostScript */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "alloc.h"
  25. #include "errors.h"
  26. #include "oper.h"
  27. #include "packed.h"
  28. #include "store.h"
  29.  
  30. /* The generic operators (copy, get, put, getinterval, putinterval, */
  31. /* length, and forall) are implemented in zgeneric.c. */
  32.  
  33. /* Forward references */
  34. private int make_array(P4(os_ptr, int, int, const char *));
  35.  
  36. /* array */
  37. int
  38. zarray(register os_ptr op)
  39. {    int code = make_array(op, t_array, a_all, "array");
  40.     if ( code < 0 ) return code;
  41.     refset_null(op->value.refs, r_size(op));
  42.     return 0;
  43. }
  44.  
  45. /* aload */
  46. int
  47. zaload(register os_ptr op)
  48. {    ref aref;
  49.     int type;
  50.     ref_assign(&aref, op);
  51.     switch ( (type = r_type(&aref)) )
  52.        {
  53.     default: return e_typecheck;
  54.     case t_array: case t_mixedarray: case t_shortarray: ;
  55.        }
  56.     check_read(aref);
  57. #define asize r_size(&aref)
  58.     if ( asize > ostop - op ) return e_rangecheck;
  59.     if ( type == t_array )
  60.         memcpy((char *)op, (const char *)aref.value.refs,
  61.                asize * sizeof(ref));
  62.     else
  63.        {    register ushort i;
  64.         const ref_packed *packed =
  65.             (const ref_packed *)aref.value.packed;
  66.         os_ptr pdest = op;
  67.         for ( i = 0; i < asize; i++, pdest++ )
  68.             packed_get(packed, pdest),
  69.             packed = packed_next(packed);
  70.        }
  71.     push(asize);
  72. #undef asize
  73.     ref_assign(op, &aref);
  74.     return 0;
  75. }
  76.  
  77. /* astore */
  78. int
  79. zastore(register os_ptr op)
  80. {    uint size;
  81.     check_type(*op, t_array);
  82.     check_write(*op);
  83.     size = r_size(op);
  84.     if ( size > op - osbot ) return e_stackunderflow;
  85.     refcpy_to_old(op->value.refs, op - size, size, "astore");
  86.     op[-(int)size] = *op;
  87.     pop(size);
  88.     return 0;
  89. }
  90.  
  91. /* ------ Initialization procedure ------ */
  92.  
  93. op_def zarray_op_defs[] = {
  94.     {"1aload", zaload},
  95.     {"1array", zarray},
  96.     {"1astore", zastore},
  97.     op_def_end(0)
  98. };
  99.  
  100. /* ------ Internal procedures ------ */
  101.  
  102. /* Make an array from the operand stack. */
  103. /* Don't fill it in. */
  104. private int
  105. make_array(register os_ptr op, int type, int attrs, const char *client_name)
  106. {    ref *abody;
  107.     uint size;
  108.     check_type(*op, t_integer);
  109.     if ( op->value.intval < 0 ||
  110.          op->value.intval > max_uint / sizeof(ref) - 1
  111.        )
  112.         return e_rangecheck;
  113.     size = op->value.intval;
  114.     abody = alloc_refs(size, client_name);
  115.     if ( abody == 0 ) return e_VMerror;
  116.     make_tasv(op, type, attrs, size, refs, abody);
  117.     return 0;
  118. }
  119.