home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZDICT.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  10KB  |  379 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zdict.c */
  20. /* Dictionary operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "idict.h"
  25. #include "dstack.h"
  26. #include "ilevel.h"            /* for [count]dictstack */
  27. #include "iname.h"            /* for dict_find_name */
  28. #include "ipacked.h"            /* for inline dict lookup */
  29. #include "ivmspace.h"            /* for check_store_space */
  30. #include "store.h"
  31.  
  32. /* <int> dict <dict> */
  33. int
  34. zdict(register os_ptr op)
  35. {    check_type(*op, t_integer);
  36.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  37.         return_error(e_rangecheck);
  38.     return dict_create((uint)op->value.intval, op);
  39. }
  40.  
  41. /* <dict> maxlength <int> */
  42. int
  43. zmaxlength(register os_ptr op)
  44. {    check_type(*op, t_dictionary);
  45.     check_dict_read(*op);
  46.     make_int(op, dict_maxlength(op));
  47.     return 0;
  48. }
  49.  
  50. /* <dict> <int> .setmaxlength - */
  51. int
  52. zsetmaxlength(register os_ptr op)
  53. {    uint new_size;
  54.     int code;
  55.     os_ptr op1 = op - 1;
  56.     check_type(*op1, t_dictionary);
  57.     check_dict_write(*op1);
  58.     check_type(*op, t_integer);
  59.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  60.         return_error(e_rangecheck);
  61.     new_size = (uint)op->value.intval;
  62.     if ( dict_length(op - 1) > new_size )
  63.         return_error(e_dictfull);
  64.     code = dict_resize(op - 1, new_size);
  65.     if ( code >= 0 ) pop(2);
  66.     return code;
  67. }
  68.  
  69. /* <dict> begin - */
  70. int
  71. zbegin(register os_ptr op)
  72. {    check_type(*op, t_dictionary);
  73.     check_dict_read(*op);
  74.     if ( dsp == dstop )
  75.         return_error(e_dictstackoverflow);
  76.     ++dsp;
  77.     ref_assign(dsp, op);
  78.     dict_set_top();
  79.     pop(1);
  80.     return 0;
  81. }
  82.  
  83. /* - end - */
  84. int
  85. zend(register os_ptr op)
  86. {    if ( ref_stack_count_inline(&d_stack) == min_dstack_size )
  87.       {    /* We would underflow the d-stack. */
  88.         return_error(e_dictstackunderflow);
  89.       }
  90.     if ( dsp == dsbot )
  91.       {    /* We're about to underflow the current block. */
  92.           return_error(e_dictstackunderflow);
  93.       }
  94.     dsp--;
  95.     dict_set_top();
  96.     return 0;
  97. }
  98.  
  99. /* <key> <value> def - */
  100. /* We make this into a separate procedure because */
  101. /* the interpreter will almost always call it directly. */
  102. int
  103. zop_def(register os_ptr op)
  104. {    register os_ptr op1 = op - 1;
  105.     ref *pvslot;
  106.     /* The following combines a check_op(2) with a type check. */
  107.     switch ( r_type(op1) )
  108.     {
  109.     case t_name:
  110.     {    /* We can use the fast single-probe lookup here. */
  111.         uint nidx = name_index(op1);
  112.         uint htemp;
  113.         if_dict_find_name_by_index_top(nidx, htemp, pvslot)
  114.         {    if ( (r_type_attrs(op) & dsmask) == 0 )
  115.                 goto ra;
  116.         }
  117.         break;            /* handle all slower cases */
  118.     }
  119.     case t_null:
  120.         return_error(e_typecheck);
  121.     case t__invalid:
  122.         return_error(e_stackunderflow);
  123.     }
  124.     /* Combine the check for a writable top dictionary with */
  125.     /* the global/local store check.  See dstack.h for details. */
  126.     if ( (r_type_attrs(op) & dsmask) != 0 )
  127.     {    int code;
  128.         check_dict_write(*dsp);
  129.         /*
  130.          * If the dictionary is writable, the problem must be
  131.          * an invalid store.  We need a special check to allow
  132.          * storing references to local objects in systemdict,
  133.          * or in dictionaries known in systemdict,
  134.          * during initialization (see ivmspace.h).
  135.          */
  136.         if ( ialloc_is_in_save() )
  137.           return_error(e_invalidaccess);
  138.         if ( dsp->value.pdict != systemdict->value.pdict )
  139.           {    /* See if systemdict is still writable, */
  140.             /* i.e., we are still doing initialization. */
  141.             int index;
  142.             ref elt[2];        /* key, value */
  143.             check_dict_write(*systemdict);
  144.             /* See if this dictionary is known in systemdict. */
  145.             for ( index = dict_first(systemdict);
  146.                   (index = dict_next(systemdict, index, &elt[0])) >= 0;
  147.                 )
  148.               if ( r_has_type(&elt[1], t_dictionary) &&
  149.                    elt[1].value.pdict == dsp->value.pdict
  150.                  )
  151.                 break;
  152.             if ( index < 0 )
  153.               return_error(e_invalidaccess);
  154.           }
  155.         switch ( code = dict_find(dsp, op1, &pvslot) )
  156.         {
  157.         case 1:                /* found */
  158.             goto ra;
  159.         default:            /* some other error */
  160.             return code;
  161.         /*
  162.          * If we have to grow the dictionary, do it now, so that
  163.          * the allocator will allocate the copy in the correct space.
  164.          */
  165.         case e_dictfull:
  166.             if ( !dict_auto_expand )
  167.                 return_error(e_dictfull);
  168.             code = dict_grow(dsp);
  169.             if ( code < 0 )
  170.                 return code;
  171.         case 0:
  172.             ;
  173.         }
  174.         /* Temporarily identify the dictionary as local, */
  175.         /* so the store check in dict_put won't fail. */
  176.         r_set_attrs(dsp, a_local);
  177.         code = dict_put(dsp, op1, op);
  178.         r_clear_attrs(dsp, a_local);
  179.     }
  180.     /* Save a level of procedure call in the common (redefinition) */
  181.     /* case.  With the current interfaces, we pay a double lookup */
  182.     /* in the uncommon case. */
  183.     if ( dict_find(dsp, op1, &pvslot) <= 0 )
  184.         return dict_put(dsp, op1, op);
  185. ra:    ref_assign_old_inline(dsp, pvslot, op, "dict_put(value)");
  186.     return 0;
  187. }
  188. int
  189. zdef(os_ptr op)
  190. {    int code = zop_def(op);
  191.     if ( code >= 0 ) { pop(2); }
  192.     return code;
  193. }
  194.  
  195. /* <key> load <value> */
  196. int
  197. zload(register os_ptr op)
  198. {    ref *pvalue;
  199.     check_op(1);
  200.     switch ( r_type(op) )
  201.     {
  202.     case t_name:
  203.         /* Use the fast lookup. */
  204.         if ( (pvalue = dict_find_name(op)) == 0 )
  205.             return_error(e_undefined);
  206.         ref_assign(op, pvalue);
  207.         return 0;
  208.     case t_null:
  209.         return_error(e_typecheck);
  210.     default:
  211.     {    /* Use an explicit loop. */
  212.         uint size = ref_stack_count(&d_stack);
  213.         uint i;
  214.         for ( i = 0; i < size; i++ )
  215.           {    ref *dp = ref_stack_index(&d_stack, i);
  216.             check_dict_read(*dp);
  217.             if ( dict_find(dp, op, &pvalue) > 0 )
  218.               {    ref_assign(op, pvalue);
  219.                 return 0;
  220.               }
  221.           }
  222.         return_error(e_undefined);
  223.     }
  224.     }
  225. }
  226.  
  227. /* get - implemented in zgeneric.c */
  228.  
  229. /* put - implemented in zgeneric.c */
  230.  
  231. /* <dict> <key> undef - */
  232. int
  233. zundef(register os_ptr op)
  234. {    check_type(op[-1], t_dictionary);
  235.     check_dict_write(op[-1]);
  236.     if ( !r_has_type(op, t_null) )
  237.         dict_undef(op - 1, op);    /* ignore undefined error */
  238.     pop(2);
  239.     return 0;
  240. }
  241.  
  242. /* <dict> <key> known <bool> */
  243. int
  244. zknown(register os_ptr op)
  245. {    register os_ptr op1 = op - 1;
  246.     ref *pvalue;
  247.     check_type(*op1, t_dictionary);
  248.     check_dict_read(*op1);
  249.     make_bool(op1,
  250.           (r_has_type(op, t_null) ? 0 :
  251.            dict_find(op1, op, &pvalue) > 0 ? 1 : 0));
  252.     pop(1);
  253.     return 0;
  254. }
  255.  
  256. /* <dict> <key> .knownget <value> true */
  257. /* <dict> <key> .knownget false */
  258. int
  259. zknownget(register os_ptr op)
  260. {    register os_ptr op1 = op - 1;
  261.     ref *pvalue;
  262.     check_type(*op1, t_dictionary);
  263.     check_dict_read(*op1);
  264.     if ( r_has_type(op, t_null) || dict_find(op1, op, &pvalue) <= 0 )
  265.     {    make_false(op1);
  266.         pop(1);
  267.     }
  268.     else
  269.     {    ref_assign(op1, pvalue);
  270.         make_true(op);
  271.     }
  272.     return 0;
  273. }
  274.  
  275. /* <key> where <dict> true */
  276. /* <key> where false */
  277. int
  278. zwhere(register os_ptr op)
  279. {    check_op(1);
  280.     if ( r_has_type(op, t_null) )
  281.       {    make_false(op);
  282.         return 0;
  283.       }
  284.     STACK_LOOP_BEGIN(&d_stack, bot, size)
  285.       {    const ref *pdref = bot + size;
  286.         ref *pvalue;
  287.         while ( pdref-- > bot )
  288.           {    check_dict_read(*pdref);
  289.             if ( dict_find(pdref, op, &pvalue) > 0 )
  290.               {    push(1);
  291.                 ref_assign(op - 1, pdref);
  292.                 make_true(op);
  293.                 return 0;
  294.               }
  295.           }
  296.        }
  297.     STACK_LOOP_END(bot, size)
  298.     make_false(op);
  299.     return 0;
  300. }
  301.  
  302. /* copy for dictionaries -- called from zcopy in zgeneric.c. */
  303. /* Only the type of *op has been checked. */
  304. int
  305. zcopy_dict(register os_ptr op)
  306. {    os_ptr op1 = op - 1;
  307.     int code;
  308.     check_type(*op1, t_dictionary);
  309.     check_dict_read(*op1);
  310.     check_dict_write(*op);
  311.     if ( !dict_auto_expand &&
  312.          (dict_length(op) != 0 || dict_maxlength(op) < dict_length(op1))
  313.        )
  314.       return_error(e_rangecheck);
  315.     code = dict_copy(op1, op);
  316.     if ( code < 0 )
  317.       return code;
  318.     ref_assign(op1, op);
  319.     pop(1);
  320.     return 0;
  321. }
  322.  
  323. /* - currentdict <dict> */
  324. int
  325. zcurrentdict(register os_ptr op)
  326. {    push(1);
  327.     ref_assign(op, dsp);
  328.     return 0;
  329. }
  330.  
  331. /* - countdictstack <int> */
  332. int
  333. zcountdictstack(register os_ptr op)
  334. {    uint count = ref_stack_count(&d_stack);
  335.     push(1);
  336.     if ( !level2_enabled )
  337.       count--;        /* see dstack.h */
  338.     make_int(op, count);
  339.     return 0;
  340. }
  341.  
  342. /* <array> dictstack <subarray> */
  343. int
  344. zdictstack(register os_ptr op)
  345. {    uint count = ref_stack_count(&d_stack);
  346.     check_write_type(*op, t_array);
  347.     if ( !level2_enabled )
  348.       count--;        /* see dstack.h */
  349.     return ref_stack_store(&d_stack, op, count, 0, 0, true, "dictstack");
  350. }
  351.  
  352. /* - cleardictstack - */
  353. int
  354. zcleardictstack(os_ptr op)
  355. {    while ( zend(op) >= 0 ) ;
  356.     return 0;
  357. }
  358.  
  359. /* ------ Initialization procedure ------ */
  360.  
  361. op_def zdict_op_defs[] = {
  362.     {"0cleardictstack", zcleardictstack},
  363.     {"1begin", zbegin},
  364.     {"0countdictstack", zcountdictstack},
  365.     {"0currentdict", zcurrentdict},
  366.     {"2def", zdef},
  367.     {"1dict", zdict},
  368.     {"0dictstack", zdictstack},
  369.     {"0end", zend},
  370.     {"2known", zknown},
  371.     {"2.knownget", zknownget},
  372.     {"1load", zload},
  373.     {"1maxlength", zmaxlength},
  374.     {"2.setmaxlength", zsetmaxlength},
  375.     {"2undef", zundef},
  376.     {"1where", zwhere},
  377.     op_def_end(0)
  378. };
  379.