Debian Codegraph @

terps/tads/tads3/vmdict.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmdict.h - VM 'Dictionary' metaclass
12 Function
13
14 Notes
15
16 Modified
17 01/24/00 MJRoberts - Creation
18 */
19
20 #ifndef VMDICT_H
21 #define VMDICT_H
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "t3std.h"
27 #include "vmtype.h"
28 #include "vmglob.h"
29 #include "vmobj.h"
30 #include "vmundo.h"
31 #include "vmhash.h"
32 #include "utf8.h"
33
34
35 /* forward-declare the class */
36 class CVmObjDict;
37
38 /* ------------------------------------------------------------------------ */
39 /*
40 * undo action codes
41 */
42 enum dict_undo_action
43 {
44 /* we added this word to the dictionary (undo by deleting it) */
45 DICT_UNDO_ADD,
46
47 /* we deleted this word from the dictionary (undo by adding it back) */
48 DICT_UNDO_DEL,
49
50 /* we change the comparator object */
51 DICT_UNDO_COMPARATOR
52 };
53
54
55 /* ------------------------------------------------------------------------ */
56 /*
57 * The image file data block is arranged as follows:
58 *
59 *. UINT4 comparator_object_id
60 *. UINT2 load_image_entry_count
61 *. entry 1
62 *. entry 2
63 *. ...
64 *. entry N
65 *
66 * Each entry has the following structure:
67 *
68 *. UCHAR key_string_byte_length
69 *. key_string (UTF-8 characters, not null terminated, XOR'ed with 0xBD)
70 *. UINT2 number of sub-entries
71 *. sub-entry 1
72 *. sub-entry 2
73 *. etc
74 *
75 * Each sub-entry is structured like this:
76 *
77 * UINT4 associated_object_id
78 *. UINT2 defining_property_id
79 *
80 * Note that each byte of the key string is XOR'ed with the arbitrary
81 * byte value 0xBD. This is simply to provide a minimal level of
82 * obfuscation in the image file to prevent casual browsing of the image
83 * contents.
84 */
85 /*
86 * Separately, we maintain a hash table and entries in the hash table. We
87 * don't attempt to keep the hash table in a portable format, which means
88 * that we have to rebuild the hash table on restoring a saved state file.
89 */
90
91 /* comparator object types */
92 enum vm_dict_comp_type
93 {
94 VMDICT_COMP_NONE, /* no comparator object */
95 VMDICT_COMP_STRCOMP, /* StringComparator */
96 VMDICT_COMP_GENERIC /* generic comparator object */
97 };
98
99 /*
100 * Dictionary object extension - we use this memory (in the variable
101 * heap) to keep track of our image data and our hash table.
102 */
103 struct vm_dict_ext
104 {
105 /* pointer to load image data, if any */
106 const char *image_data_;
107 size_t image_data_size_;
108
109 /* pointer to our hash table (in the system heap) */
110 class CVmHashTable *hashtab_;
111
112 /* flag: the table has been changed since image load */
113 ulong modified_;
114
115 /* our comparator object, if any */
116 vm_obj_id_t comparator_;
117 CVmObject *comparator_obj_;
118
119 /* type of comparator */
120 vm_dict_comp_type comparator_type_;
121
122 /* Trie of our entries, for spelling correction */
123 struct vmdict_TrieNode *trie_;
124 };
125
126
127 /* ------------------------------------------------------------------------ */
128 /*
129 * For spelling correction, we maintain a Trie on the dictionary in
130 * parallel to the hash table.
131 */
132
133 /*
134 * Trie Node - a node in the trie tree.
135 */
136 struct vmdict_TrieNode
137 {
138 vmdict_TrieNode(vmdict_TrieNode *nxt, wchar_t c)
139 {
140 /* remember the transition character from our parent to us */
141 ch = c;
142
143 /* we don't have any words in this node yet */
144 word_cnt = 0;
145
146 /* remember our next sibling link */
147 this->nxt = nxt;
148
149 /* we don't have any children yet */
150 chi = 0;
151 }
152
153 ~vmdict_TrieNode()
154 {
155 /* delete the subtree */
156 while (chi != 0)
157 {
158 /* unlink this child */
159 vmdict_TrieNode *n = chi;
160 chi = chi->nxt;
161
162 /* delete it */
163 delete n;
164 }
165 }
166
167 /* add a word */
168 void add_word(const char *str, size_t len);
169
170 /* find a word */
171 vmdict_TrieNode *find_word(const char *str, size_t len);
172
173 /* delete a word */
174 void del_word(const char *str, size_t len);
175
176 /*
177 * Transition character from parent to this node. Append this
178 * character to the string that reached the parent node to get the
179 * string that reaches the child node.
180 */
181 wchar_t ch;
182
183 /*
184 * Number of words at this node. Since we use the Trie only for
185 * spelling correction, we don't care about any of the other data
186 * stored per word; we can look that up through the hash table. All we
187 * care about is whether or not a given node has any words associated
188 * with it. Since the same word can be entered in the dictionary
189 * several times, we need to keep a count - that way, we can track
190 * dynamic removal of words by matching deletions against insertions.
191 */
192 int word_cnt;
193
194 /* the head of the list of child nodes */
195 vmdict_TrieNode *chi;
196
197 /* our next sibling */
198 vmdict_TrieNode *nxt;
199 };
200
201
202 /* ------------------------------------------------------------------------ */
203 /*
204 * Dictionary object interface
205 */
206 class CVmObjDict: public CVmObject
207 {
208 friend class CVmMetaclassDict;
209
210 public:
211 /* metaclass registration object */
212 static class CVmMetaclass *metaclass_reg_;
213 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
214
215 /* am I of the given metaclass? */
216 virtual int is_of_metaclass(class CVmMetaclass *meta) const
217 {
218 /* try my own metaclass and my base class */
219 return (meta == metaclass_reg_
220 || CVmObject::is_of_metaclass(meta));
221 }
222
223 /* create dynamically using stack arguments */
224 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
225 uint argc);
226
227 /*
228 * call a static property - we don't have any of our own, so simply
229 * "inherit" the base class handling
230 */
231 static int call_stat_prop(VMG_ vm_val_t *result,
232 const uchar **pc_ptr, uint *argc,
233 vm_prop_id_t prop)
234 { return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); }
235
236 /* determine if an object is a Dictionary object */
237 static int is_dictionary_obj(VMG_ vm_obj_id_t obj)
238 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
239
240 /* notify of deletion */
241 void notify_delete(VMG_ int in_root_set);
242
243 /* get a property */
244 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
245 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
246
247 /* set a property */
248 void set_prop(VMG_ class CVmUndo *undo,
249 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
250
251 /* receive notification of a new undo savepoint */
252 void notify_new_savept() { }
253
254 /* apply undo */
255 void apply_undo(VMG_ struct CVmUndoRecord *rec);
256
257 /* discard additional information associated with an undo record */
258 void discard_undo(VMG_ struct CVmUndoRecord *rec);
259
260 /* mark a reference in undo */
261 void mark_undo_ref(VMG_ struct CVmUndoRecord *rec);
262
263 /* remove stale weak references from an undo record */
264 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *rec);
265
266 /* mark references */
267 void mark_refs(VMG_ uint);
268
269 /* remove weak references */
270 void remove_stale_weak_refs(VMG0_);
271
272 /* load from an image file */
273 void load_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t siz);
274
275 /* perform post-load initialization */
276 void post_load_init(VMG_ vm_obj_id_t self);
277
278 /* restore to image file state */
279 void reset_to_image(VMG_ vm_obj_id_t /*self*/);
280
281 /* determine if the object has been changed since it was loaded */
282 int is_changed_since_load() const;
283
284 /* save to a file */
285 void save_to_file(VMG_ class CVmFile *fp);
286
287 /* restore from a file */
288 void restore_from_file(VMG_ vm_obj_id_t self,
289 class CVmFile *fp, class CVmObjFixup *);
290
291 /* rebuild for image file */
292 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
293
294 /* convert to constant data */
295 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
296 vm_obj_id_t self);
297
298 /* enumerate the properties for which a word is defined */
299 void enum_word_props(VMG_ void (*cb_func)(VMG_ void *, vm_prop_id_t,
300 const vm_val_t *),
301 void *cb_ctx, const vm_val_t *strval,
302 const char *strp, size_t strl);
303
304 /* get/set my comparator object */
305 vm_obj_id_t get_comparator() const { return get_ext()->comparator_; }
306 void set_comparator(VMG_ vm_obj_id_t obj);
307
308 /*
309 * Match a pair of strings.
310 *
311 * 'valstrval' is the vm_val_t with the string value, if one is
312 * available; if not, this can be given as null and we'll synthesize a
313 * new string object from 'valstr' and 'vallen' if one is needed. We
314 * don't require the caller to synthesize a string object because we
315 * might not need one; we'll create one only if one is actually needed.
316 *
317 * 'valstr' and 'vallen' directly give the text of the value string,
318 * and 'refstr' and 'reflen' likewise directly give the text of the
319 * reference string.
320 */
321 int match_strings(VMG_ const vm_val_t *valstrval,
322 const char *valstr, size_t vallen,
323 const char *refstr, size_t reflen,
324 vm_val_t *result_val);
325
326 /*
327 * Calculate the hash value for a string. As in match_strings(),
328 * 'valstrval' can be passed as null if no vm_val_t for the string text
329 * is available, in which case we'll synthesize a new string object
330 * from 'valstr' and 'vallen' if one is needed.
331 */
332 unsigned int calc_str_hash(VMG_ const vm_val_t *valstrval,
333 const char *valstr, size_t vallen);
334
335 /* add a string/obj/prop association from a VMB_LEN+buffer string */
336 void add_word(VMG_ vm_obj_id_t self, const char *str,
337 vm_obj_id_t obj, vm_prop_id_t prop)
338 { add_word(vmg_ self, str + VMB_LEN, vmb_get_len(str), obj, prop); }
339
340 /* add a string/obj/prop association */
341 void add_word(VMG_ vm_obj_id_t self, const char *str, size_t len,
342 vm_obj_id_t obj, vm_prop_id_t voc_prop);
343
344 /* remove a string/obj/prop association, given a VMB_LEN+buffer string */
345 void del_word(VMG_ vm_obj_id_t self, const char *str,
346 vm_obj_id_t obj, vm_prop_id_t prop)
347 { del_word(vmg_ self, str + VMB_LEN, vmb_get_len(str), obj, prop); }
348
349 /* remove a string/obj/prop association */
350 void del_word(VMG_ vm_obj_id_t self, const char *str, size_t len,
351 vm_obj_id_t obj, vm_prop_id_t voc_prop);
352
353 protected:
354 CVmObjDict(VMG0_);
355
356 /* set the comparator type */
357 void set_comparator_type(VMG_ vm_obj_id_t obj);
358
359 /* create or re-create the hash table */
360 void create_hash_table(VMG0_);
361
362 /* fill the hash table with entries from the image data */
363 void build_hash_from_image(VMG0_);
364
365 /* build the Trie from the hash table */
366 void build_trie(VMG0_);
367
368 /* property evaluation - undefined property */
369 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
370
371 /* enumeration callback for getp_find */
372 static void find_cb(void *ctx, class CVmHashEntry *entry);
373
374 /* enumeration callback for getp_isdef */
375 static void isdef_cb(void *ctx, class CVmHashEntry *entry);
376
377 /* enumeration callback for enum_word_props */
378 static void enum_word_props_cb(void *ctx, class CVmHashEntry *entry);
379
380 /* property evaluation - set the comparator object */
381 int getp_set_comparator(VMG_ vm_obj_id_t, vm_val_t *val, uint *argc);
382
383 /* property evaluation - findWord */
384 int getp_find(VMG_ vm_obj_id_t, vm_val_t *val, uint *argc);
385
386 /* property evaluation - addWord */
387 int getp_add(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
388
389 /* property evaluation - delWord */
390 int getp_del(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
391
392 /* property evaluation - isWordDefined */
393 int getp_is_defined(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
394
395 /* property evaluation - forEachWord */
396 int getp_for_each_word(VMG_ vm_obj_id_t self, vm_val_t *retval,
397 uint *argc);
398
399 /* property evaluation - find corrections for a misspelled word */
400 int getp_correct(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
401
402 /* get my extension, properly cast */
403 vm_dict_ext *get_ext() const { return (vm_dict_ext *)ext_; }
404
405 /*
406 * add an entry; returns true if successful, false if the entry
407 * already exists, in which case no new entry is made
408 */
409 int add_hash_entry(VMG_ const char *p, size_t len, int copy,
410 vm_obj_id_t obj, vm_prop_id_t prop, int from_image);
411
412 /* delete an entry */
413 int del_hash_entry(VMG_ const char *p, size_t len,
414 vm_obj_id_t obj, vm_prop_id_t prop);
415
416 /* callback for hash table enumeration - delete stale weak refs */
417 static void remove_weak_ref_cb(void *ctx, class CVmHashEntry *entry);
418
419 /* callback for hash table enumeration - save file */
420 static void save_file_cb(void *ctx, class CVmHashEntry *entry);
421
422 /* callback for hash table enumeration - convert to constant data */
423 static void cvt_const_cb(void *ctx, class CVmHashEntry *entry);
424
425 /* callback for hash table enumeration - rebuild image, phase 1 */
426 static void rebuild_cb_1(void *ctx, class CVmHashEntry *entry);
427
428 /* callback for hash table enumeration - rebuild image, phase 2 */
429 static void rebuild_cb_2(void *ctx, class CVmHashEntry *entry);
430
431 /* allocate an undo record */
432 struct dict_undo_rec *alloc_undo_rec(enum dict_undo_action action,
433 const char *txt, size_t len);
434
435 /* add a record to the global undo stream */
436 void add_undo_rec(VMG_ vm_obj_id_t self, struct dict_undo_rec *rec);
437
438 /* function table */
439 static int (CVmObjDict::*func_table_[])(VMG_ vm_obj_id_t self,
440 vm_val_t *retval, uint *argc);
441 };
442
443
444 /* ------------------------------------------------------------------------ */
445 /*
446 * hash table list entry - each hash entry has a list of associated
447 * objects; this structure represents an entry in one of these lists
448 */
449 struct vm_dict_entry
450 {
451 vm_dict_entry(vm_obj_id_t obj, vm_prop_id_t prop, int from_image)
452 {
453 obj_ = obj;
454 prop_ = prop;
455 from_image_ = from_image;
456 nxt_ = 0;
457 }
458
459 /* object ID of this entry */
460 vm_obj_id_t obj_;
461
462 /* defining property ID */
463 vm_prop_id_t prop_;
464
465 /* next entry in the list */
466 vm_dict_entry *nxt_;
467
468 /* flag: entry is from the image file */
469 int from_image_;
470 };
471
472 /*
473 * Dictionary object hash table entry
474 */
475 class CVmHashEntryDict: public CVmHashEntryCS
476 {
477 public:
478 CVmHashEntryDict(const char *str, size_t len, int copy, int from_image)
479 : CVmHashEntryCS(str, len, copy || from_image)
480 {
481 /* nothing in our item list yet */
482 list_ = 0;
483 }
484
485 ~CVmHashEntryDict()
486 {
487 /* delete all of our entries */
488 while (list_ != 0)
489 {
490 vm_dict_entry *nxt;
491
492 /* note the next entry */
493 nxt = list_->nxt_;
494
495 /* delete the current entry */
496 delete list_;
497
498 /* move on to the next entry */
499 list_ = nxt;
500 }
501 }
502
503 /* get the first entry in our list */
504 vm_dict_entry *get_head() const { return list_; }
505
506 /*
507 * add an entry to our list - returns true if we added the entry,
508 * false if an entry with the same object ID was already present, in
509 * which case we will ignore the addition
510 */
511 int add_entry(vm_obj_id_t obj, vm_prop_id_t prop, int from_image)
512 {
513 vm_dict_entry *entry;
514 vm_dict_entry *prv;
515
516 /*
517 * check to see if this entry is already in our list - if it is,
518 * don't bother adding the redundant definition
519 */
520 for (prv = 0, entry = list_ ; entry != 0 ; entry = entry->nxt_)
521 {
522 /*
523 * if this entry matches the object ID and property ID,
524 * ignore the addition
525 */
526 if (entry->obj_ == obj && entry->prop_ == prop)
527 return FALSE;
528
529 /*
530 * if this entry matches the property, remember it as the
531 * insertion point, so that we keep all definitions for the
532 * same word with the same property together in the list
533 */
534 if (entry->prop_ == prop)
535 prv = entry;
536 }
537
538 /* create a list entry */
539 entry = new vm_dict_entry(obj, prop, from_image);
540
541 /*
542 * link it in after the insertion point, or at the head of our
543 * list if we didn't find any other insertion point
544 */
545 if (prv != 0)
546 {
547 /* we found an insertion point - link it in */
548 entry->nxt_ = prv->nxt_;
549 prv->nxt_ = entry;
550 }
551 else
552 {
553 /* no insertion point - link it at the head of our list */
554 entry->nxt_ = list_;
555 list_ = entry;
556 }
557
558 /* we added the entry */
559 return TRUE;
560 }
561
562 /*
563 * Delete all entries matching a given object ID from our list.
564 * Returns true if any entries were deleted, false if not.
565 */
566 int del_entry(CVmHashTable *table, vm_obj_id_t obj, vm_prop_id_t prop)
567 {
568 vm_dict_entry *cur;
569 vm_dict_entry *nxt;
570 vm_dict_entry *prv;
571 int found;
572
573 /* find the entry in our list */
574 for (found = FALSE, prv = 0, cur = list_ ; cur != 0 ;
575 prv = cur, cur = nxt)
576 {
577 /* remember the next entry */
578 nxt = cur->nxt_;
579
580 /* if this is our entry, delete it */
581 if (cur->obj_ == obj && cur->prop_ == prop)
582 {
583 /* unlink this entry */
584 if (prv != 0)
585 prv->nxt_ = nxt;
586 else
587 list_ = nxt;
588
589 /* delete this entry */
590 delete cur;
591
592 /* note that we found at least one entry to delete */
593 found = TRUE;
594 }
595 }
596
597 /* if our list is now empty, delete myself from the table */
598 if (list_ == 0)
599 {
600 /* remove myself from the table */
601 table->remove(this);
602
603 /* delete myself */
604 delete this;
605 }
606
607 /* tell the caller whether we found anything to delete */
608 return found;
609 }
610
611 protected:
612 /* list of associated objects */
613 vm_dict_entry *list_;
614 };
615
616
617 /* ------------------------------------------------------------------------ */
618 /*
619 * Registration table object
620 */
621 class CVmMetaclassDict: public CVmMetaclass
622 {
623 public:
624 /* get the global name */
625 const char *get_meta_name() const { return "dictionary2/030001"; }
626
627 /* create from image file */
628 void create_for_image_load(VMG_ vm_obj_id_t id)
629 { new (vmg_ id) CVmObjDict(vmg0_); }
630
631 /* create from restoring from saved state */
632 void create_for_restore(VMG_ vm_obj_id_t id)
633 { new (vmg_ id) CVmObjDict(vmg0_); }
634
635 /* create dynamically using stack arguments */
636 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
637 { return CVmObjDict::create_from_stack(vmg_ pc_ptr, argc); }
638
639 /* call a static property */
640 int call_stat_prop(VMG_ vm_val_t *result,
641 const uchar **pc_ptr, uint *argc,
642 vm_prop_id_t prop)
643 {
644 return CVmObjDict::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
645 }
646 };
647
648 #endif /* VMDICT_H */
649
650 /*
651 * Register the class
652 */
653 VM_REGISTER_METACLASS(CVmObjDict)