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