Debian Codegraph @

terps/tads/tads3/vmstrbuf.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2010 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 vmstrbuf.h - StringBuffer object
12 Function
13
14 Notes
15
16 Modified
17 12/13/09 MJRoberts - Creation
18 */
19
20 #ifndef VMSTRBUF_H
21 #define VMSTRBUF_H
22
23 #include "t3std.h"
24 #include "vmtype.h"
25 #include "vmglob.h"
26 #include "vmobj.h"
27 #include "vmundo.h"
28
29
30 /* ------------------------------------------------------------------------ */
31 /*
32 * The image file data block is arranged as follows:
33 *
34 *. UINT4 buffer_length
35 *. UINT4 buffer_increment
36 *. UINT4 string_length
37 *. UINT2 string[string_length]
38 *
39 * buffer_length is the allocated size of the buffer.
40 *
41 * buffer_increment is the incremental allocation size; when we need to
42 * expand the buffer, we'll allocate in multiples of this size.
43 *
44 * string_length is the length of the string data; this is the amount of
45 * buffer space actually used.
46 *
47 * string is the string data, as 16-bit Unicode character values. (Note
48 * that this is not stored in UTF-8 encoding as most other VM strings are.
49 * This is instead an array of 16-bit character values.)
50 */
51
52
53 /* ------------------------------------------------------------------------ */
54 /*
55 * Our in-memory extension data structure, which mimics the image file
56 * structure but uses native types.
57 */
58 struct vm_strbuf_ext
59 {
60 /* allocate the structure */
61 static vm_strbuf_ext *alloc_ext(VMG_ class CVmObjStringBuffer *self,
62 int32_t alo, int32_t inc);
63
64 /* expand an existing extension */
65 static vm_strbuf_ext *expand_ext(VMG_ class CVmObjStringBuffer *self,
66 vm_strbuf_ext *old_ext,
67 int32_t new_len);
68
69 /* the length of the string data (the part of the buffer in use) */
70 int32_t len;
71
72 /* the allocated buffer size */
73 int32_t alo;
74
75 /* the incremental allocation size */
76 int32_t inc;
77
78 /*
79 * The string data, as 16-bit unicode character values. We
80 * overallocate the structure to make room for an actual array length
81 * of [alo] elements here.
82 */
83 wchar_t buf[1];
84 };
85
86
87 /* ------------------------------------------------------------------------ */
88 /*
89 * undo action codes
90 */
91 enum strbuf_undo_action
92 {
93 /* we inserted text into the buffer */
94 STRBUF_UNDO_INS,
95
96 /* we deleted text from the buffer */
97 STRBUF_UNDO_DEL,
98
99 /* we replaced text in the buffer */
100 STRBUF_UNDO_REPL
101 };
102
103
104 /* ------------------------------------------------------------------------ */
105 /*
106 * StringBuffer metaclass
107 */
108
109 class CVmObjStringBuffer: public CVmObject
110 {
111 friend class CVmMetaclassStringBuffer;
112
113 public:
114 /* metaclass registration object */
115 static class CVmMetaclass *metaclass_reg_;
116 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
117
118 /* am I of the given metaclass? */
119 virtual int is_of_metaclass(class CVmMetaclass *meta) const
120 {
121 /* try my own metaclass and my base class */
122 return (meta == metaclass_reg_
123 || CVmObject::is_of_metaclass(meta));
124 }
125
126 /* is this a StringBuffer object? */
127 static int is_string_buffer_obj(VMG_ vm_obj_id_t obj)
128 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
129
130 /* create */
131 static vm_obj_id_t create(VMG_ int in_root_set, int32_t alo, int32_t inc);
132
133 /* create dynamically using stack arguments */
134 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
135 uint argc);
136
137 /*
138 * call a static property - we don't have any of our own, so simply
139 * "inherit" the base class handling
140 */
141 static int call_stat_prop(VMG_ vm_val_t *result,
142 const uchar **pc_ptr, uint *argc,
143 vm_prop_id_t prop)
144 {
145 return CVmObject::
146 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
147 }
148
149 /* reserve constant data */
150 virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper,
151 vm_obj_id_t self)
152 {
153 /* we cannot be converted to constant data */
154 }
155
156 /* convert to constant data */
157 virtual void convert_to_const_data(VMG_ class CVmConstMapper *,
158 vm_obj_id_t)
159 {
160 /* we don't reference anything */
161 }
162
163 /* notify of deletion */
164 void notify_delete(VMG_ int in_root_set);
165
166 /* set a property */
167 void set_prop(VMG_ class CVmUndo *undo,
168 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
169
170 /* get a property */
171 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
172 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
173
174 /*
175 * receive savepoint notification - we don't keep any
176 * savepoint-relative records, so we don't need to do anything here
177 */
178 void notify_new_savept() { }
179
180 /* apply an undo record */
181 void apply_undo(VMG_ struct CVmUndoRecord *rec);
182
183 /* discard an undo record */
184 void discard_undo(VMG_ struct CVmUndoRecord *);
185
186 /* we don't reference anything */
187 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
188
189 /* we don't reference anything */
190 void mark_refs(VMG_ uint) { }
191
192 /* we don't keep any references */
193 void remove_stale_weak_refs(VMG0_) { }
194 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
195
196 /* load from an image file */
197 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
198
199 /* reload from an image file */
200 void reload_from_image(VMG_ vm_obj_id_t self,
201 const char *ptr, size_t siz);
202
203 /* rebuild for image file */
204 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
205
206 /* save to a file */
207 void save_to_file(VMG_ class CVmFile *fp);
208
209 /* restore from a file */
210 void restore_from_file(VMG_ vm_obj_id_t self,
211 class CVmFile *fp, class CVmObjFixup *fixups);
212
213 /*
214 * determine if we've been changed since loading - assume we have (if
215 * we haven't, the only harm is the cost of unnecessarily reloading or
216 * saving)
217 */
218 int is_changed_since_load() const { return TRUE; }
219
220 /* get a value by index */
221 int index_val_q(VMG_ vm_val_t *result,
222 vm_obj_id_t self,
223 const vm_val_t *index_val);
224
225 /* set a value by index */
226 int set_index_val_q(VMG_ vm_val_t *new_container,
227 vm_obj_id_t self,
228 const vm_val_t *index_val,
229 const vm_val_t *new_val);
230
231 /* add an entry - does not generate undo */
232 void add_entry(VMG_ const vm_val_t *key, const vm_val_t *val);
233
234 /* set or add an entry - does not generate undo */
235 void set_or_add_entry(VMG_ const vm_val_t *key, const vm_val_t *val);
236
237 /* cast to string */
238 virtual const char *cast_to_string(VMG_ vm_obj_id_t self,
239 vm_val_t *new_str) const;
240
241 /* equals - we equal a string or string buffer with the same text */
242 virtual int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, int) const;
243
244 /* test for equality to a constant string */
245 int equals_str(const char *str) const;
246
247 /* compare - we can compare to a string or string buffer */
248 virtual int compare_to(VMG_ vm_obj_id_t self, const vm_val_t *val) const;
249
250 /* compare to a constant string - strcmp semantics */
251 int compare_str(const char *str) const;
252
253 /* calculate my hash value */
254 virtual uint calc_hash(VMG_ vm_obj_id_t self, int /*depth*/) const;
255
256 /* calculate the length in bytes of the contents in UTF-8 format */
257 int32_t utf8_length() const;
258
259 /*
260 * Copy a portion of the string to a buffer as UTF-8 bytes.
261 *
262 * 'idx' is an in-out variable. On input, it's the starting character
263 * index requested for the copy. On output, it's updated to the index
264 * of the next character after the last character copied. This can be
265 * used for piecewise copies, since it's updated to point to the start
266 * of the next piece after copying each piece.
267 *
268 * 'bytelen' is an in-out variable. On input, this is the number of
269 * bytes requested to copy to the buffer. On output, it's the actual
270 * number of bytes copied. This might be smaller than the request
271 * size, because (a) we won't copy past the end of the string, and (b)
272 * we'll only copy whole, well-formed character sequences, so if the
273 * requested number of bytes would copy a fractional character, we'll
274 * omit that fractional character and stop at the previous whole
275 * character.
276 */
277 void to_utf8(char *buf, int32_t &idx, int32_t &bytelen);
278
279 /* ensure there's enough space for the given buffer length */
280 void ensure_space(VMG_ int32_t len);
281
282 /* ensure there's enough space for the given ADDED buffer length */
283 void ensure_added_space(VMG_ int32_t len)
284 { ensure_space(vmg_ get_ext()->len + len); }
285
286 /*
287 * Get my string buffer. Use with caution; the underlying buffer
288 * pointer can change if we modify the contents.
289 */
290 const wchar_t *get_buf() const { return get_ext()->buf; }
291
292 /* get the current character length */
293 size_t get_len() const { return get_ext()->len; }
294
295 protected:
296 /* get my extension data */
297 vm_strbuf_ext *get_ext() const { return (vm_strbuf_ext *)ext_; }
298
299 /* create a string object from a substring of the buffer */
300 const char *substr_to_string(
301 VMG_ vm_val_t *new_str, int32_t idx, int32_t len) const;
302
303 /* adjust arguments to make sure they're within the buffer */
304 void adjust_args(int32_t *idx, int32_t *len, int32_t *ins) const;
305
306 /* move the tail of the buffer for a splice operation */
307 void splice_move(VMG_ int32_t idx, int32_t del, int32_t ins);
308
309 /* load or reload image data */
310 void load_image_data(VMG_ const char *ptr, size_t siz);
311
312 /* create a with no initial contents */
313 CVmObjStringBuffer() { ext_ = 0; }
314
315 /* create a string buffer with the given buffer size */
316 CVmObjStringBuffer(VMG_ int32_t alo, int32_t inc);
317
318 /* insert text into the buffer */
319 void insert_text(VMG_ vm_obj_id_t self,
320 int32_t idx, const wchar_t *src, int32_t chars, int undo)
321 { splice_text(vmg_ self, idx, 0, src, chars, undo); }
322
323 /* insert a character */
324 void insert_char(VMG_ vm_obj_id_t self,
325 int32_t idx, wchar_t ch, int undo)
326 { splice_text(vmg_ self, idx, 0, &ch, 1, undo); }
327
328 /* insert UTF-8 text into the buffer */
329 void insert_text(VMG_ vm_obj_id_t self,
330 int32_t idx, const char *src, int32_t bytes, int undo)
331 { splice_text(vmg_ self, idx, 0, src, bytes, undo); }
332
333 /* delete text from the buffer */
334 void delete_text(VMG_ vm_obj_id_t self,
335 int32_t idx, int32_t chars, int undo)
336 { splice_text(vmg_ self, idx, chars, (wchar_t *)0, 0, undo); }
337
338 /* splice text into the buffer */
339 void splice_text(VMG_ vm_obj_id_t self,
340 int32_t idx, int32_t del_chars,
341 const wchar_t *src, int32_t ins_chars, int undo);
342 void splice_text(VMG_ vm_obj_id_t self,
343 int32_t idx, int32_t del_chars,
344 const char *src, int32_t ins_bytes, int undo);
345
346 /* property evaluator - undefined function */
347 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
348
349 /* property evaluator - get length */
350 int getp_len(VMG_ vm_obj_id_t, vm_val_t *, uint *);
351
352 /* property evaluator - get a character at an index position */
353 int getp_char_at(VMG_ vm_obj_id_t, vm_val_t *, uint *);
354
355 /* property evaluator - append */
356 int getp_append(VMG_ vm_obj_id_t, vm_val_t *, uint *);
357
358 /* property evaluator - copyChars */
359 int getp_copyChars(VMG_ vm_obj_id_t, vm_val_t *, uint *);
360
361 /* property evaluator - insert */
362 int getp_insert(VMG_ vm_obj_id_t, vm_val_t *, uint *);
363
364 /* property evaluator - delete */
365 int getp_delete(VMG_ vm_obj_id_t, vm_val_t *, uint *);
366
367 /* property evaluator - splice */
368 int getp_splice(VMG_ vm_obj_id_t, vm_val_t *, uint *);
369
370 /* property evaluator - substr */
371 int getp_substr(VMG_ vm_obj_id_t, vm_val_t *, uint *);
372
373 /* add a record to the global undo stream */
374 void add_undo_rec(VMG_ vm_obj_id_t self,
375 enum strbuf_undo_action action,
376 int32_t idx, int32_t old_len, int32_t new_len);
377
378 /* property evaluation function table */
379 static int (CVmObjStringBuffer::*func_table_[])(VMG_ vm_obj_id_t self,
380 vm_val_t *retval, uint *argc);
381 };
382
383
384 /* ------------------------------------------------------------------------ */
385 /*
386 * StringBuffer registration table object
387 */
388 class CVmMetaclassStringBuffer: public CVmMetaclass
389 {
390 public:
391 /* get the global name */
392 const char *get_meta_name() const { return "stringbuffer/030000"; }
393
394 /* create from image file */
395 void create_for_image_load(VMG_ vm_obj_id_t id)
396 {
397 new (vmg_ id) CVmObjStringBuffer();
398 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
399 }
400
401 /* create from restoring from saved state */
402 void create_for_restore(VMG_ vm_obj_id_t id)
403 {
404 new (vmg_ id) CVmObjStringBuffer();
405 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
406 }
407
408 /* create dynamically using stack arguments */
409 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
410 { return CVmObjStringBuffer::create_from_stack(vmg_ pc_ptr, argc); }
411
412 /* call a static property */
413 int call_stat_prop(VMG_ vm_val_t *result,
414 const uchar **pc_ptr, uint *argc,
415 vm_prop_id_t prop)
416 {
417 return CVmObjStringBuffer::
418 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
419 }
420 };
421
422 #endif /* VMSTRBUF_H */
423
424 /*
425 * Register the class
426 */
427 VM_REGISTER_METACLASS(CVmObjStringBuffer)