Debian Codegraph @

terps/tads/tads3/vmcset.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 vmcset.cpp - T3 CharacterSet metaclass
10 Function
11
12 Notes
13
14 Modified
15 06/06/01 MJRoberts - Creation
16 */
17
18 #include <stdlib.h>
19 #include "vmtype.h"
20 #include "vmobj.h"
21 #include "vmglob.h"
22 #include "vmcset.h"
23 #include "vmbif.h"
24 #include "vmfile.h"
25 #include "vmerrnum.h"
26 #include "vmerr.h"
27 #include "vmstack.h"
28 #include "vmmeta.h"
29 #include "vmrun.h"
30 #include "charmap.h"
31 #include "vmstr.h"
32 #include "vmpredef.h"
33 #include "vmrun.h"
34 #include "vmhost.h"
35
36
37 /* ------------------------------------------------------------------------ */
38 /*
39 * statics
40 */
41
42 /* metaclass registration object */
43 static CVmMetaclassCharSet metaclass_reg_obj;
44 CVmMetaclass *CVmObjCharSet::metaclass_reg_ = &metaclass_reg_obj;
45
46 /* function table */
47 int (CVmObjCharSet::
48 *CVmObjCharSet::func_table_[])(VMG_ vm_obj_id_t self,
49 vm_val_t *retval, uint *argc) =
50 {
51 &CVmObjCharSet::getp_undef,
52 &CVmObjCharSet::getp_get_name,
53 &CVmObjCharSet::getp_is_known,
54 &CVmObjCharSet::getp_is_mappable,
55 &CVmObjCharSet::getp_is_rt_mappable
56 };
57
58
59 /* ------------------------------------------------------------------------ */
60 /*
61 * Create from stack
62 */
63 vm_obj_id_t CVmObjCharSet::create_from_stack(VMG_ const uchar **pc_ptr,
64 uint argc)
65 {
66 vm_obj_id_t id;
67 vm_val_t *arg1;
68 const char *charset_name;
69
70 /* check our arguments */
71 if (argc != 1)
72 err_throw(VMERR_WRONG_NUM_OF_ARGS);
73
74 /* get the name of the character set */
75 arg1 = G_stk->get(0);
76 charset_name = arg1->get_as_string(vmg0_);
77 if (charset_name == 0)
78 err_throw(VMERR_BAD_TYPE_BIF);
79
80 /* create the character set object */
81 id = vm_new_id(vmg_ FALSE, FALSE, FALSE);
82 new (vmg_ id) CVmObjCharSet(vmg_ charset_name + VMB_LEN,
83 vmb_get_len(charset_name));
84
85 /* discard arguments */
86 G_stk->discard(argc);
87
88 /* return the new object */
89 return id;
90 }
91
92 /* ------------------------------------------------------------------------ */
93 /*
94 * Create with no contents
95 */
96 vm_obj_id_t CVmObjCharSet::create(VMG_ int in_root_set)
97 {
98 vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
99 new (vmg_ id) CVmObjCharSet();
100 return id;
101 }
102
103 /*
104 * Create with the given character set name
105 */
106 vm_obj_id_t CVmObjCharSet::create(VMG_ int in_root_set,
107 const char *charset_name,
108 size_t charset_name_len)
109 {
110 vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE);
111 new (vmg_ id) CVmObjCharSet(vmg_ charset_name, charset_name_len);
112 return id;
113 }
114
115 /* ------------------------------------------------------------------------ */
116 /*
117 * Instantiate
118 */
119 CVmObjCharSet::CVmObjCharSet(VMG_ const char *charset_name,
120 size_t charset_name_len)
121 {
122 /* allocate and initialize our extension */
123 ext_ = 0;
124 alloc_ext(vmg_ charset_name, charset_name_len);
125 }
126
127 /*
128 * Allocate and initialize our extension
129 */
130 void CVmObjCharSet::alloc_ext(VMG_ const char *charset_name,
131 size_t charset_name_len)
132 {
133 size_t alloc_size;
134 vmobj_charset_ext_t *extp;
135 CResLoader *res_ldr;
136
137 /* if we already have an extension, delete it */
138 if (ext_ != 0)
139 G_mem->get_var_heap()->free_mem(ext_);
140
141 /*
142 * compute the size we need - note that we use the one fixed byte of
143 * the structure's name element as the extra byte we need for null
144 * termination of the name
145 */
146 alloc_size = sizeof(vmobj_charset_ext_t) + charset_name_len;
147
148 /* allocate space for our extension structure */
149 ext_ = (char *)G_mem->get_var_heap()->alloc_mem(alloc_size, this);
150
151 /* cast the extension to our structure type */
152 extp = (vmobj_charset_ext_t *)ext_;
153
154 /* store the character set name and length, null-terminating the name */
155 extp->charset_name_len = charset_name_len;
156 memcpy(extp->charset_name, charset_name, charset_name_len);
157 extp->charset_name[charset_name_len] = '\0';
158
159 /* presume we won't be able to load the character sets */
160 extp->to_local = 0;
161 extp->to_uni = 0;
162
163 /* get the resource loader */
164 res_ldr = G_host_ifc->get_sys_res_loader();
165
166 /* if we have a resource loader, load the mappings */
167 if (res_ldr != 0)
168 {
169 /* load the unicode-to-local mapping */
170 extp->to_local = CCharmapToLocal::load(res_ldr, extp->charset_name);
171
172 /* load the local-to-unicode mapping */
173 extp->to_uni = CCharmapToUni::load(res_ldr, extp->charset_name);
174 }
175 }
176
177 /* ------------------------------------------------------------------------ */
178 /*
179 * Notify of deletion
180 */
181 void CVmObjCharSet::notify_delete(VMG_ int /*in_root_set*/)
182 {
183 /* release our mapper objects */
184 if (ext_ != 0)
185 {
186 /* release the to-local character mapper */
187 if (get_ext_ptr()->to_local != 0)
188 get_ext_ptr()->to_local->release_ref();
189
190 /* release the to-unicode character mapper */
191 if (get_ext_ptr()->to_uni != 0)
192 get_ext_ptr()->to_uni->release_ref();
193
194 /* free our extension */
195 G_mem->get_var_heap()->free_mem(ext_);
196 }
197 }
198
199 /* ------------------------------------------------------------------------ */
200 /*
201 * set a property
202 */
203 void CVmObjCharSet::set_prop(VMG_ class CVmUndo *,
204 vm_obj_id_t, vm_prop_id_t,
205 const vm_val_t *)
206 {
207 err_throw(VMERR_INVALID_SETPROP);
208 }
209
210 /* ------------------------------------------------------------------------ */
211 /*
212 * get a property
213 */
214 int CVmObjCharSet::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval,
215 vm_obj_id_t self, vm_obj_id_t *source_obj,
216 uint *argc)
217 {
218 uint func_idx;
219
220 /* translate the property index to an index into our function table */
221 func_idx = G_meta_table
222 ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop);
223
224 /* call the appropriate function */
225 if ((this->*func_table_[func_idx])(vmg_ self, retval, argc))
226 {
227 *source_obj = metaclass_reg_->get_class_obj(vmg0_);
228 return TRUE;
229 }
230
231 /* inherit default handling */
232 return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc);
233 }
234
235 /* ------------------------------------------------------------------------ */
236 /*
237 * load from an image file
238 */
239 void CVmObjCharSet::load_from_image(VMG_ vm_obj_id_t self,
240 const char *ptr, size_t siz)
241 {
242 /* initialize with the character set name from the image file */
243 alloc_ext(vmg_ ptr + VMB_LEN, vmb_get_len(ptr));
244 }
245
246 /* ------------------------------------------------------------------------ */
247 /*
248 * save to a file
249 */
250 void CVmObjCharSet::save_to_file(VMG_ class CVmFile *fp)
251 {
252 /* write the name length */
253 fp->write_uint2(get_ext_ptr()->charset_name_len);
254
255 /* write the bytes of the name */
256 fp->write_bytes(get_ext_ptr()->charset_name,
257 get_ext_ptr()->charset_name_len);
258 }
259
260 /*
261 * restore from a file
262 */
263 void CVmObjCharSet::restore_from_file(VMG_ vm_obj_id_t self,
264 CVmFile *fp, CVmObjFixup *)
265 {
266 char buf[128];
267 size_t len;
268 size_t read_len;
269
270 /* read the length of the character set name */
271 len = fp->read_uint2();
272
273 /* limit the reading to the length of the buffer */
274 read_len = len;
275 if (read_len > sizeof(buf))
276 read_len = sizeof(buf);
277
278 /* read the name, up to the buffer length */
279 fp->read_bytes(buf, read_len);
280
281 /* skip any bytes we couldn't fit in the buffer */
282 if (len > read_len)
283 fp->set_pos(fp->get_pos() + len - read_len);
284
285 /* initialize from the saved data */
286 alloc_ext(vmg_ buf, read_len);
287 }
288
289 /* ------------------------------------------------------------------------ */
290 /*
291 * Compare for equality
292 */
293 int CVmObjCharSet::equals(VMG_ vm_obj_id_t self, const vm_val_t *val,
294 int /*depth*/) const
295 {
296 CVmObjCharSet *other;
297 const vmobj_charset_ext_t *ext;
298 const vmobj_charset_ext_t *other_ext;
299
300 /* if it's a self-reference, it's certainly equal */
301 if (val->typ == VM_OBJ && val->val.obj == self)
302 return TRUE;
303
304 /* if it's not another character set, it's not equal */
305 if (val->typ != VM_OBJ || !is_charset(vmg_ val->val.obj))
306 return FALSE;
307
308 /* we know it's another character set - cast it */
309 other = (CVmObjCharSet *)vm_objp(vmg_ val->val.obj);
310
311 /* get my extension and the other extension */
312 ext = get_ext_ptr();
313 other_ext = other->get_ext_ptr();
314
315 /* it's equal if it has the same name (ignoring case) */
316 return (ext->charset_name_len == other_ext->charset_name_len
317 && memicmp(ext->charset_name, other_ext->charset_name,
318 ext->charset_name_len) == 0);
319 }
320
321 /*
322 * Calculate a hash value
323 */
324 uint CVmObjCharSet::calc_hash(VMG_ vm_obj_id_t self, int /*depth*/) const
325 {
326 uint hash;
327 size_t rem;
328 const char *p;
329
330 /* add up the bytes in the array */
331 for (hash = 0, rem = get_ext_ptr()->charset_name_len,
332 p = get_ext_ptr()->charset_name ;
333 rem != 0 ;
334 --rem, ++p)
335 {
336 /* add this character into the hash */
337 hash += *p;
338 }
339
340 /* return the result */
341 return hash;
342 }
343
344 /* ------------------------------------------------------------------------ */
345 /*
346 * property evaluator - get the character set name
347 */
348 int CVmObjCharSet::getp_get_name(VMG_ vm_obj_id_t self,
349 vm_val_t *retval, uint *argc)
350 {
351 static CVmNativeCodeDesc desc(0);
352
353 /* check arguments */
354 if (get_prop_check_argc(retval, argc, &desc))
355 return TRUE;
356
357 /* create a new string for the name */
358 retval->set_obj(CVmObjString::create(vmg_ FALSE,
359 get_ext_ptr()->charset_name,
360 get_ext_ptr()->charset_name_len));
361
362 /* handled */
363 return TRUE;
364 }
365
366 /*
367 * property evaluator - is known
368 */
369 int CVmObjCharSet::getp_is_known(VMG_ vm_obj_id_t self,
370 vm_val_t *retval, uint *argc)
371 {
372 static CVmNativeCodeDesc desc(0);
373
374 /* check arguments */
375 if (get_prop_check_argc(retval, argc, &desc))
376 return TRUE;
377
378 /*
379 * it's known if both of our character mappers are non-null; if either
380 * is null, the character set is not known on this platform
381 */
382 retval->set_logical(get_ext_ptr()->to_local != 0
383 && get_ext_ptr()->to_uni != 0);
384
385 /* handled */
386 return TRUE;
387 }
388
389 /*
390 * property evaluator - check a character or a string for mappability
391 */
392 int CVmObjCharSet::getp_is_mappable(VMG_ vm_obj_id_t self,
393 vm_val_t *retval, uint *argc)
394 {
395 static CVmNativeCodeDesc desc(1);
396 vm_val_t arg;
397 const char *str;
398 CCharmapToLocal *to_local;
399
400 /* check arguments */
401 if (get_prop_check_argc(retval, argc, &desc))
402 return TRUE;
403
404 /* get the local mapping */
405 to_local = get_to_local(vmg0_);
406
407 /* get the argument and check what type we have */
408 G_stk->pop(&arg);
409 if (to_local == 0)
410 {
411 /* no mapping is available, so it's not mappable */
412 retval->set_nil();
413 }
414 else if ((str = arg.get_as_string(vmg0_)) != 0)
415 {
416 size_t len;
417 utf8_ptr p;
418
419 /* get the length and skip the length prefix */
420 len = vmb_get_len(str);
421 str += VMB_LEN;
422
423 /* presume every character will be mappable */
424 retval->set_true();
425
426 /* check each character for mappability */
427 for (p.set((char *)str) ; len != 0 ; p.inc(&len))
428 {
429 /* check to see if this character is mappable */
430 if (!to_local->is_mappable(p.getch()))
431 {
432 /*
433 * The character isn't mappable - this is an
434 * all-or-nothing check, so if one isn't mappable we
435 * return false. Set the nil return and stop looking.
436 */
437 retval->set_nil();
438 break;
439 }
440 }
441 }
442 else if (arg.typ == VM_INT)
443 {
444 /*
445 * Check if the integer character value is mappable. If it's out
446 * of the 16-bit unicode range (0..0xffff), it's not mappable;
447 * otherwise, ask the character mapper.
448 */
449 if (arg.val.intval < 0 || arg.val.intval > 0xffff)
450 {
451 /* it's out of the valid unicode range, so it's not mappable */
452 retval->set_nil();
453 }
454 else
455 {
456 /* ask the character mapper */
457 retval->set_logical(to_local->is_mappable(
458 (wchar_t)arg.val.intval));
459 }
460 }
461
462 /* handled */
463 return TRUE;
464 }
465
466 /*
467 * property evaluator - check a character or a string to see if it has a
468 * round-trip mapping. A round-trip mapping is one where the unicode
469 * characters can be mapped to the local character set, then back to
470 * unicode, yielding the exact original unicode string.
471 */
472 int CVmObjCharSet::getp_is_rt_mappable(VMG_ vm_obj_id_t self,
473 vm_val_t *retval, uint *argc)
474 {
475 static CVmNativeCodeDesc desc(1);
476 vm_val_t arg;
477 const char *str;
478 CCharmapToLocal *to_local;
479 CCharmapToUni *to_uni;
480
481 /* check arguments */
482 if (get_prop_check_argc(retval, argc, &desc))
483 return TRUE;
484
485 /* get the local and unicode mappings */
486 to_local = get_to_local(vmg0_);
487 to_uni = get_to_uni(vmg0_);
488
489 /* get the argument and check what type we have */
490 G_stk->pop(&arg);
491 if (to_local == 0 || to_uni == 0)
492 {
493 /* no character sets, so it's not mappable */
494 retval->set_nil();
495 }
496 else if ((str = arg.get_as_string(vmg0_)) != 0)
497 {
498 size_t len;
499 utf8_ptr p;
500
501 /* get the length and skip the length prefix */
502 len = vmb_get_len(str);
503 str += VMB_LEN;
504
505 /* presume every character will be mappable */
506 retval->set_true();
507
508 /* check each character for mappability */
509 for (p.set((char *)str) ; len != 0 ; p.inc(&len))
510 {
511 /* check for round-trip mappability */
512 if (!is_rt_mappable(p.getch(), to_local, to_uni))
513 {
514 /* nope - return false */
515 retval->set_nil();
516 break;
517 }
518 }
519 }
520 else if (arg.typ == VM_INT)
521 {
522 /* check the integer character for mappability */
523 if (arg.val.intval < 0 || arg.val.intval > 0xffff)
524 {
525 /* it's out of the valid unicode range, so it's not mappable */
526 retval->set_nil();
527 }
528 else
529 {
530 /* ask the character mapper */
531 retval->set_logical(is_rt_mappable(
532 (wchar_t)arg.val.intval, to_local, to_uni));
533 }
534 }
535
536 /* handled */
537 return TRUE;
538 }
539
540 /*------------------------------------------------------------------------ */
541 /*
542 * Determine if a character has a round-trip mapping.
543 */
544 int CVmObjCharSet::is_rt_mappable(wchar_t c, CCharmapToLocal *to_local,
545 CCharmapToUni *to_uni)
546 {
547 char lclbuf[16];
548 char unibuf[16];
549 size_t lcllen;
550 size_t unilen;
551 char *p;
552
553 /* if there's no local mapping, it's obviously not mappable */
554 if (!to_local->is_mappable(c))
555 return FALSE;
556
557 /*
558 * If there's an expansion in the mapping to the local set, then there
559 * can't be a round-trip mapping. Expansions are inherently one-way
560 * because they produce multiple local characters for a single unicode
561 * character, and the reverse mapping has no way to group those
562 * multiple local characters back into a single unicode character.
563 */
564 if (to_local->get_expansion(c, &lcllen) != 0)
565 return FALSE;
566
567 /* get the local mapping */
568 lcllen = to_local->map_char(c, lclbuf, sizeof(lclbuf));
569
570 /* map it back to unicode */
571 p = unibuf;
572 unilen = sizeof(unibuf);
573 unilen = to_uni->map(&p, &unilen, lclbuf, lcllen);
574
575 /*
576 * if the unicode mapping is one character that exactly matches the
577 * original input character, then we have a valid round-trip mapping
578 */
579 return (unilen == utf8_ptr::s_wchar_size(c)
580 && utf8_ptr::s_getch(unibuf) == c);
581 }
582
583 /*------------------------------------------------------------------------ */
584 /*
585 * Get the unicode-to-local character set mapper
586 */
587 CCharmapToLocal *CVmObjCharSet::get_to_local(VMG0_) const
588 {
589 /* if there's no mapper, throw an exception */
590 if (get_ext_ptr()->to_local == 0)
591 {
592 /* throw an UnknownCharacterSetException */
593 G_interpreter->throw_new_class(vmg_ G_predef->charset_unknown_exc,
594 0, "unknown character set");
595 }
596
597 /* return the mapper */
598 return get_ext_ptr()->to_local;
599 }
600
601 /*
602 * Get the local-to-unicode character set mapper
603 */
604 CCharmapToUni *CVmObjCharSet::get_to_uni(VMG0_) const
605 {
606 /* if there's no mapper, throw an exception */
607 if (get_ext_ptr()->to_uni == 0)
608 {
609 /* throw an UnknownCharacterSetException */
610 G_interpreter->throw_new_class(vmg_ G_predef->charset_unknown_exc,
611 0, "unknown character set");
612 }
613
614 /* return the mapper */
615 return get_ext_ptr()->to_uni;
616 }