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 / ZDICT.C < prev    next >
C/C++ Source or Header  |  1992-09-20  |  6KB  |  283 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. /* zdict.c */
  21. /* Dictionary operators for GhostScript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "dict.h"
  26. #include "dstack.h"
  27. #include "name.h"            /* for dict_find_name */
  28. #include "store.h"
  29.  
  30. /* dict */
  31. int
  32. zdict(register os_ptr op)
  33. {    check_type(*op, t_integer);
  34.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  35.         return e_rangecheck;
  36.     return dict_create((uint)op->value.intval, op);
  37. }
  38.  
  39. /* maxlength */
  40. int
  41. zmaxlength(register os_ptr op)
  42. {    check_type(*op, t_dictionary);
  43.     check_dict_read(*op);
  44.     make_int(op, dict_maxlength(op));
  45.     return 0;
  46. }
  47.  
  48. /* setmaxlength */
  49. int
  50. zsetmaxlength(register os_ptr op)
  51. {    uint new_size;
  52.     int code;
  53.     os_ptr op1 = op - 1;
  54.     check_type(*op1, t_dictionary);
  55.     check_dict_write(*op1);
  56.     check_type(*op, t_integer);
  57.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  58.         return e_rangecheck;
  59.     new_size = (uint)op->value.intval;
  60.     if ( dict_length(op - 1) > new_size )
  61.         return e_dictfull;
  62.     code = dict_resize(op - 1, new_size);
  63.     if ( code >= 0 ) pop(2);
  64.     return code;
  65. }
  66.  
  67. /* begin */
  68. int
  69. zbegin(register os_ptr op)
  70. {    check_type(*op, t_dictionary);
  71.     check_dict_read(*op);
  72.     if ( dsp == dstop ) return e_dictstackoverflow;
  73.     ++dsp;
  74.     ref_assign(dsp, op);
  75.     pop(1);
  76.     return 0;
  77. }
  78.  
  79. /* end */
  80. int
  81. zend(register os_ptr op)
  82. {    if ( dsp == dstack + (min_dstack_size - 1) )
  83.         return e_dictstackunderflow;
  84.     dsp--;
  85.     return 0;
  86. }
  87.  
  88. /* def */
  89. /* We make this into a separate procedure because */
  90. /* the interpreter will almost always call it directly. */
  91. int
  92. zop_def(register os_ptr op)
  93. {    check_op(2);
  94.     if ( r_has_type(op - 1, t_null) ) return e_typecheck;
  95.     check_dict_write(*dsp);
  96.     return dict_put(dsp, op - 1, op);
  97. }
  98. int
  99. zdef(os_ptr op)
  100. {    int code = zop_def(op);
  101.     if ( !code ) { pop(2); }
  102.     return code;
  103. }
  104.  
  105. /* load */
  106. int
  107. zload(register os_ptr op)
  108. {    ref *pvalue;
  109.     check_op(1);
  110.     if ( r_has_type(op, t_null) ) return e_typecheck;
  111.     if ( r_has_type(op, t_name) )
  112.        {    /* Use the fast lookup */
  113.         if ( (pvalue = dict_find_name(op)) == 0 )
  114.             return e_undefined;
  115.        }
  116.     else
  117.        {    if ( dict_lookup(dstack, dsp, op, &pvalue) <= 0 )
  118.             return e_undefined;
  119.        }
  120.     ref_assign(op, pvalue);
  121.     return 0;
  122. }
  123.  
  124. /* store */
  125. int
  126. zstore(register os_ptr op)
  127. {    ref *pvalue;
  128.     int code;
  129.     check_op(2);
  130.     if ( r_has_type(op - 1, t_null) ) return e_typecheck;
  131.     if ( dict_lookup(dstack, dsp, op - 1, &pvalue) <= 0 )
  132.        {    code = dict_put(dsp, op - 1, op);
  133.         if ( code ) return code;
  134.        }
  135.     else
  136.         ref_assign_old(pvalue, op, "store");
  137.     pop(2);
  138.     return 0;
  139. }
  140.  
  141. /* get - implemented in zgeneric.c */
  142.  
  143. /* put - implemented in zgeneric.c */
  144.  
  145. /* undef */
  146. int
  147. zundef(register os_ptr op)
  148. {    check_type(op[-1], t_dictionary);
  149.     check_dict_write(op[-1]);
  150.     if ( !r_has_type(op, t_null) )
  151.         dict_undef(op - 1, op);    /* ignore undefined error */
  152.     pop(2);
  153.     return 0;
  154. }
  155.  
  156. /* known */
  157. int
  158. zknown(register os_ptr op)
  159. {    os_ptr op1 = op - 1;
  160.     ref *pvalue;
  161.     check_type(*op1, t_dictionary);
  162.     check_dict_read(*op1);
  163.     make_bool(op1,
  164.           (r_has_type(op, t_null) ? 0 :
  165.            dict_find(op1, op, &pvalue) > 0 ? 1 : 0));
  166.     pop(1);
  167.     return 0;
  168. }
  169.  
  170. /* .knownget */
  171. int
  172. zknownget(register os_ptr op)
  173. {    register os_ptr op1 = op - 1;
  174.     ref *pvalue;
  175.     check_type(*op1, t_dictionary);
  176.     check_dict_read(*op1);
  177.     if ( r_has_type(op, t_null) || dict_find(op1, op, &pvalue) <= 0 )
  178.     {    make_false(op1);
  179.         pop(1);
  180.     }
  181.     else
  182.     {    ref_assign(op1, pvalue);
  183.         make_true(op);
  184.     }
  185.     return 0;
  186. }
  187.  
  188. /* where */
  189. int
  190. zwhere(register os_ptr op)
  191. {    const ref *pdref = dsp;
  192.     ref *pvalue;
  193.     check_op(1);
  194.     if ( r_has_type(op, t_null) )
  195.        {    make_bool(op, 0);
  196.         return 0;
  197.        }
  198.     while ( 1 )
  199.        {    check_dict_read(*pdref);
  200.         if ( dict_find(pdref, op, &pvalue) > 0 ) break;
  201.         if ( --pdref < dstack )
  202.            {    make_bool(op, 0);
  203.             return 0;
  204.            }
  205.        }
  206.     ref_assign(op, pdref);
  207.     push(1);
  208.     make_bool(op, 1);
  209.     return 0;
  210. }
  211.  
  212. /* copy for dictionaries -- called from zcopy in zgeneric.c. */
  213. /* Only the type of *op has been checked. */
  214. int
  215. zcopy_dict(register os_ptr op)
  216. {    os_ptr op1 = op - 1;
  217.     check_type(*op1, t_dictionary);
  218.     check_dict_read(*op1);
  219.     check_dict_write(*op);
  220.     if ( !dict_auto_expand && (dict_length(op) != 0 || dict_maxlength(op) < dict_length(op1)) )
  221.         return e_rangecheck;
  222.     dict_copy(op1, op);
  223.     ref_assign(op - 1, op);
  224.     pop(1);
  225.     return 0;
  226. }
  227.  
  228. /* currentdict */
  229. int
  230. zcurrentdict(register os_ptr op)
  231. {    push(1);
  232.     ref_assign(op, dsp);
  233.     return 0;
  234. }
  235.  
  236. /* countdictstack */
  237. int
  238. zcountdictstack(register os_ptr op)
  239. {    push(1);
  240.     make_int(op, dsp - dstack + 1);
  241.     return 0;
  242. }
  243.  
  244. /* dictstack */
  245. int
  246. zdictstack(register os_ptr op)
  247. {    int depth = dsp - dstack + 1;
  248.     check_write_type(*op, t_array);
  249.     if ( depth > r_size(op) ) return e_rangecheck;
  250.     r_set_size(op, depth);
  251.     refcpy_to_old(op->value.refs, dstack, depth, "dictstack");
  252.     return 0;
  253. }
  254.  
  255. /* cleardictstack */
  256. int
  257. zcleardictstack(os_ptr op)
  258. {    dsp = dstack + (min_dstack_size - 1);
  259.     return 0;
  260. }
  261.  
  262. /* ------ Initialization procedure ------ */
  263.  
  264. op_def zdict_op_defs[] = {
  265.     {"0cleardictstack", zcleardictstack},
  266.     {"1begin", zbegin},
  267.     {"0countdictstack", zcountdictstack},
  268.     {"0currentdict", zcurrentdict},
  269.     {"2def", zdef},
  270.     {"1dict", zdict},
  271.     {"0dictstack", zdictstack},
  272.     {"0end", zend},
  273.     {"2known", zknown},
  274.     {"2.knownget", zknownget},
  275.     {"1load", zload},
  276.     {"1maxlength", zmaxlength},
  277.     {"2setmaxlength", zsetmaxlength},
  278.     {"2store", zstore},
  279.     {"2undef", zundef},
  280.     {"1where", zwhere},
  281.     op_def_end(0)
  282. };
  283.