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 / ZGENERIC.C < prev    next >
C/C++ Source or Header  |  1992-08-15  |  11KB  |  426 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. /* zgeneric.c */
  21. /* Array/string/dictionary generic operators for PostScript */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "dict.h"
  27. #include "estack.h"    /* for forall */
  28. #include "name.h"
  29. #include "packed.h"
  30. #include "store.h"
  31.  
  32. /* This file implements copy, get, put, getinterval, putinterval, */
  33. /* length, and forall, which apply generically to */
  34. /* arrays, strings, and dictionaries.  (Copy also has a special */
  35. /* meaning for copying the top N elements of the stack.) */
  36.  
  37. /* Imported operators */
  38. extern int zcopy_device(P1(os_ptr));
  39. extern int zcopy_dict(P1(os_ptr));
  40.  
  41. /* Forward references */
  42. private int copy_interval(P4(os_ptr, uint, os_ptr, const char *));
  43.  
  44. /* copy */
  45. /* Note that this implements copy for arrays and strings, */
  46. /* but not for dictionaries (see zcopy_dict in zdict.c) */
  47. /* or devices (see zcopy_device in zdevice.c). */
  48. int
  49. zcopy(register os_ptr op)
  50. {    os_ptr op1 = op - 1;
  51.     int code;
  52.     switch ( r_type(op) )
  53.        {
  54.     case t_integer:
  55.        {    int count;
  56.         if ( (ulong)op->value.intval > op - osbot )
  57.             return e_rangecheck;
  58.         count = op->value.intval;
  59.         if ( op1 + count > ostop )
  60.             return e_stackoverflow;
  61.         memcpy((char *)op, (char *)(op - count), count * sizeof(ref));
  62.         push(count - 1);
  63.         return 0;
  64.        }
  65.     case t_array:
  66.     case t_string:
  67.         code = copy_interval(op, 0, op1, "copy");
  68.         break;
  69.     case t_dictionary:
  70.         return zcopy_dict(op);
  71.     default:
  72.         return e_typecheck;
  73.        }
  74.     if ( code ) return code;    /* error */
  75.     r_set_size(op, r_size(op1));
  76.     *op1 = *op;
  77.     pop(1);
  78.     return 0;
  79. }
  80.  
  81. /* length */
  82. int
  83. zlength(register os_ptr op)
  84. {    switch ( r_type(op) )
  85.        {
  86.     case t_array:
  87.     case t_string:
  88.     case t_mixedarray:
  89.     case t_shortarray:
  90.         check_read(*op);
  91.         make_int(op, r_size(op));
  92.         return 0;
  93.     case t_dictionary:
  94.         check_dict_read(*op);
  95.         make_int(op, dict_length(op));
  96.         return 0;
  97.     case t_name:
  98.        {    ref str;
  99.         name_string_ref(op, &str);
  100.         make_int(op, r_size(&str));
  101.        }
  102.         return 0;
  103.     default:
  104.         return e_typecheck;
  105.        }
  106. }
  107.  
  108. /* get */
  109. int
  110. zget(register os_ptr op)
  111. {    os_ptr op1 = op - 1;
  112.     ref *pvalue;
  113.     switch ( r_type(op1) )
  114.        {
  115.     case t_dictionary:
  116.         check_dict_read(*op1);
  117.         if ( dict_find(op1, op, &pvalue) <= 0 ) return e_undefined;
  118.         op[-1] = *pvalue;
  119.         break;
  120.     case t_string:
  121.         check_type(*op, t_integer);
  122.         check_read(*op1);
  123.         if ( (ulong)(op->value.intval) >= r_size(op1) )
  124.             return e_rangecheck;
  125.        {    int element = op1->value.bytes[(uint)op->value.intval];
  126.         make_int(op1, element);
  127.        }
  128.         pop(1);
  129.         return 0;
  130.     default:
  131.        {    int code;
  132.         check_type(*op, t_integer);
  133.         check_read(*op1);
  134.         code = array_get(op1, op->value.intval, op1);
  135.         if ( code < 0 ) return code;
  136.        }
  137.        }
  138.     pop(1);
  139.     return 0;
  140. }
  141.  
  142. /* put */
  143. int
  144. zput(register os_ptr op)
  145. {    os_ptr op1 = op - 1;
  146.     os_ptr op2 = op1 - 1;
  147.     switch ( r_type(op2) )
  148.        {
  149.     case t_dictionary:
  150.         check_dict_write(*op2);
  151.        {    int code = dict_put(op2, op1, op);
  152.         if ( code ) return code;    /* error */
  153.        }
  154.         break;
  155.     case t_array:
  156.         check_type(*op1, t_integer);
  157.         check_write(*op2);
  158.         if ( (ulong)(op1->value.intval) >= r_size(op2) )
  159.             return e_rangecheck;
  160.         ref_assign_old(op2->value.refs + (uint)op1->value.intval, op, "put");
  161.         break;
  162.     case t_mixedarray:        /* packed arrays are read-only */
  163.     case t_shortarray:
  164.         return e_invalidaccess;
  165.     case t_string:
  166.         check_type(*op1, t_integer);
  167.         check_write(*op2);
  168.         if ( (ulong)(op1->value.intval) >= r_size(op2) )
  169.             return e_rangecheck;
  170.         check_type(*op, t_integer);
  171.         if ( (ulong)op->value.intval > 0xff )
  172.             return e_rangecheck;
  173.         op2->value.bytes[(uint)op1->value.intval] = (byte)op->value.intval;
  174.         break;
  175.     default:
  176.         return e_typecheck;
  177.        }
  178.     pop(3);
  179.     return 0;
  180. }
  181.  
  182. /* getinterval */
  183. int
  184. zgetinterval(register os_ptr op)
  185. {    os_ptr op1 = op - 1;
  186.     os_ptr op2 = op1 - 1;
  187.     uint index;
  188.     uint count;
  189.     check_type(*op1, t_integer);
  190.     check_type(*op, t_integer);
  191.     switch ( r_type(op2) )
  192.        {
  193.     default:
  194.         return e_typecheck;
  195.     case t_array: case t_string:
  196.     case t_mixedarray:
  197.     case t_shortarray: ;
  198.        }
  199.     check_read(*op2);
  200.     if ( (ulong)op1->value.intval > r_size(op2) ) return e_rangecheck;
  201.     index = op1->value.intval;
  202.     if ( (ulong)op->value.intval > r_size(op2) - index ) return e_rangecheck;
  203.     count = op->value.intval;
  204.     switch ( r_type(op2) )
  205.        {
  206.     case t_array: op2->value.refs += index; break;
  207.     case t_string: op2->value.bytes += index; break;
  208.     case t_mixedarray:
  209.        {    const ref_packed *packed = op2->value.packed;
  210.         for ( ; index--; ) packed = packed_next(packed);
  211.         op2->value.packed = packed;
  212.        }    break;
  213.     case t_shortarray: op2->value.packed += index; break;
  214.        }
  215.     r_set_size(op2, count);
  216.     pop(2);
  217.     return 0;
  218. }
  219.  
  220. /* putinterval */
  221. int
  222. zputinterval(register os_ptr op)
  223. {    os_ptr opindex = op - 1;
  224.     os_ptr opto = opindex - 1;
  225.     int code;
  226.     check_type(*opindex, t_integer);
  227.     switch ( r_type(opto) )
  228.        {
  229.     default: return e_typecheck;
  230.     case t_mixedarray: case t_shortarray: return e_invalidaccess;
  231.     case t_array: case t_string: ;
  232.        }
  233.     check_write(*opto);
  234.     if ( (ulong)opindex->value.intval > r_size(opto) )
  235.         return e_rangecheck;
  236.     code = copy_interval(opto, (uint)(opindex->value.intval), op, "putinterval");
  237.     if ( code >= 0 ) pop(3);
  238.     return code;
  239. }
  240.  
  241. /* forall */
  242. private int
  243.   array_continue(P1(os_ptr)),
  244.   i_array_continue,
  245.   dict_continue(P1(os_ptr)),
  246.   i_dict_continue,
  247.   string_continue(P1(os_ptr)),
  248.   i_string_continue,
  249.   packedarray_continue(P1(os_ptr)),
  250.   i_packedarray_continue;
  251. int
  252. zforall(register os_ptr op)
  253. {    int (*cproc)(P1(os_ptr));
  254.     os_ptr obj = op - 1;
  255.     uint index = 0;            /* only used for dictionaries */
  256.     switch ( r_type(obj) )
  257.        {
  258.     default:
  259.         return e_typecheck;
  260.     case t_array:
  261.         check_read(*obj);
  262.         cproc = array_continue;
  263.         break;
  264.     case t_dictionary:
  265.         check_dict_read(*obj);
  266.         cproc = dict_continue;
  267.         index = dict_first(obj);
  268.         break;
  269.     case t_string:
  270.         check_read(*obj);
  271.         cproc = string_continue;
  272.         break;
  273.     case t_mixedarray:
  274.     case t_shortarray:
  275.         check_read(*obj);
  276.         cproc = packedarray_continue;
  277.         break;
  278.        }
  279.     check_proc(*op);
  280.     /* Push a mark, the composite object, the iteration index, */
  281.     /* and the procedure, and invoke the continuation operator. */
  282.     check_estack(6);
  283.     mark_estack(es_for);
  284.     *++esp = *obj;
  285.     ++esp;
  286.     make_int(esp, index);
  287.     *++esp = *op;
  288.     pop(2);  op -= 2;
  289.     return (*cproc)(op);
  290. }
  291. /* Continuation operator for arrays */
  292. private int
  293. array_continue(register os_ptr op)
  294. {    es_ptr obj = esp - 2;
  295.     if ( r_size(obj) )        /* continue */
  296.        {    r_inc_size(obj, -1);
  297.         push(1);
  298.         *op = *obj->value.refs;
  299.         obj->value.refs++;
  300.         push_op_estack(array_continue, i_array_continue);    /* push continuation */
  301.         *++esp = obj[2];
  302.         return o_push_estack;
  303.        }
  304.     else                /* done */
  305.        {    esp -= 4;        /* pop mark, object, index, proc */
  306.         return o_pop_estack;
  307.        }
  308. }
  309. /* Continuation operator for dictionaries */
  310. private int
  311. dict_continue(register os_ptr op)
  312. {    es_ptr obj = esp - 2;
  313.     int index = (int)esp[-1].value.intval;
  314.     push(2);            /* make room for key and value */
  315.     if ( (index = dict_next(obj, index, op - 1)) >= 0 )    /* continue */
  316.        {    esp[-1].value.intval = index;
  317.         push_op_estack(dict_continue, i_dict_continue);    /* push continuation */
  318.         *++esp = obj[2];
  319.         return o_push_estack;
  320.        }
  321.     else                /* done */
  322.        {    pop(2);            /* undo push */
  323.         esp -= 4;        /* pop mark, object, index, proc */
  324.         return o_pop_estack;
  325.        }
  326. }
  327. /* Continuation operator for strings */
  328. private int
  329. string_continue(register os_ptr op)
  330. {    es_ptr obj = esp - 2;
  331.     if ( r_size(obj) )        /* continue */
  332.        {    r_inc_size(obj, -1);
  333.         push(1);
  334.         make_int(op, *obj->value.bytes);
  335.         obj->value.bytes++;
  336.         push_op_estack(string_continue, i_string_continue);    /* push continuation */
  337.         *++esp = obj[2];
  338.         return o_push_estack;
  339.        }
  340.     else                /* done */
  341.        {    esp -= 4;        /* pop mark, object, index, proc */
  342.         return o_pop_estack;
  343.        }
  344. }
  345. /* Continuation operator for packed arrays */
  346. private int
  347. packedarray_continue(register os_ptr op)
  348. {    es_ptr obj = esp - 2;
  349.     if ( r_size(obj) )        /* continue */
  350.        {    const ref_packed *packed = obj->value.packed;
  351.         r_inc_size(obj, -1);
  352.         push(1);
  353.         packed_get(packed, op);
  354.         obj->value.packed = packed_next(packed);
  355.         push_op_estack(packedarray_continue, i_packedarray_continue);    /* push continuation */
  356.         *++esp = obj[2];
  357.         return o_push_estack;
  358.        }
  359.     else                /* done */
  360.        {    esp -= 4;        /* pop mark, object, index, proc */
  361.         return o_pop_estack;
  362.        }
  363. }
  364.  
  365. /* ------ Initialization procedure ------ */
  366.  
  367. op_def zgeneric_op_defs[] = {
  368.     {"1copy", zcopy},
  369.     {"2forall", zforall},
  370.     {"2get", zget},
  371.     {"3getinterval", zgetinterval},
  372.     {"1length", zlength},
  373.     {"3put", zput},
  374.     {"3putinterval", zputinterval},
  375.         /* Internal operators */
  376.     {"0%array_continue", array_continue, &i_array_continue},
  377.     {"0%dict_continue", dict_continue, &i_dict_continue},
  378.     {"0%packedarray_continue", packedarray_continue, &i_packedarray_continue},
  379.     {"0%string_continue", string_continue, &i_string_continue},
  380.     op_def_end(0)
  381. };
  382.  
  383. /* ------ Shared routines ------ */
  384.  
  385. /* Copy an interval from one operand to another. */
  386. /* This is used by both putinterval and string/array copy. */
  387. /* One operand is known to be an array or string, */
  388. /* and the starting index is known to be less than or equal to */
  389. /* its length; nothing else has been checked. */
  390. private int
  391. copy_interval(os_ptr prto, uint index, os_ptr prfrom, const char *cname)
  392. {    int fromtype = r_type(prfrom);
  393.     uint fromsize = r_size(prfrom);
  394.     if ( !(fromtype == r_type(prto) || (fromtype == t_shortarray
  395.         || fromtype == t_mixedarray) && r_type(prto) == t_array)
  396.        )
  397.         return e_typecheck;
  398.     check_read(*prfrom);
  399.     check_write(*prto);
  400.     if ( fromsize > r_size(prto) - index ) return e_rangecheck;
  401.     switch ( fromtype )
  402.        {
  403.     case t_array:
  404.         refcpy_to_old(prto->value.refs + index, prfrom->value.refs,
  405.                    fromsize, cname);
  406.         break;
  407.     case t_string:
  408.         memcpy(prto->value.bytes + index, prfrom->value.bytes,
  409.                fromsize);
  410.         break;
  411.     case t_mixedarray:
  412.     case t_shortarray:
  413.        {    int i;
  414.         const ref_packed *packed = prfrom->value.packed;
  415.         ref *pdest = prto->value.refs + index;
  416.         ref elt;
  417.         for ( i = 0; i < fromsize; i++, pdest++ )
  418.          { packed_get(packed, &elt);
  419.            ref_assign_old(pdest, &elt, cname);
  420.            packed = packed_next(packed);
  421.          }
  422.        }    break;
  423.        }
  424.     return 0;
  425. }
  426.