Debian Codegraph @

terps/tads/tads3/vmfilnam.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2012 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 vmfilnam.h - CVmObjFileName object
12 Function
13 A FileName represents a local filename string. This is essentially just
14 a string containing the filename, but provides methods to manipulate the
15 name portably, using the various osifc path manipulation routines.
16 Notes
17
18 Modified
19 03/03/12 MJRoberts - Creation
20 */
21
22 #ifndef VMFILNAM_H
23 #define VMFILNAM_H
24
25 #include "t3std.h"
26 #include "vmtype.h"
27 #include "vmglob.h"
28 #include "vmobj.h"
29 #include "vmundo.h"
30
31
32 /* ------------------------------------------------------------------------ */
33 /*
34 * The image file data block is arranged as follows:
35 *
36 *. UINT2 special_id = special file ID (0 for regular named files)
37 *. UINT2 str_len = length of the string in byts
38 *. UTF8 str = the filename string in universal notation
39 *
40 * If the special file ID is non-zero, the str_len and str fields aren't
41 * stored.
42 */
43
44
45 /* ------------------------------------------------------------------------ */
46 /*
47 * Our in-memory extension data structure. For most instances, this is
48 * simply the filename string in our usual VMB_LEN prefix format.
49 */
50 struct vm_filnam_ext
51 {
52 /* allocate the structure */
53 static vm_filnam_ext *alloc_ext(VMG_ class CVmObjFileName *self,
54 int32_t sfid, const char *str, size_t len);
55
56 /* get the length of the string, and a pointer to the string buffer */
57 size_t get_len() { return vmb_get_len(str); }
58 const char *get_str() { return str + VMB_LEN; }
59
60 /* special file ID */
61 int32_t sfid;
62
63 /* flag: is the sfid valid? */
64 char sfid_valid;
65
66 /*
67 * Flag: the filename was manually selected by the user through a file
68 * dialog. If this is non-zero, it's an OS_AFP_xxx value giving the
69 * type of dialog that was presented to the user to obtain this
70 * filename. Files selected by the user override the file safety
71 * settings, since the user implicitly grants us permission to use a
72 * file by manually selecting it.
73 */
74 char from_ui;
75
76 /*
77 * the actual data is in the same format as an ordinary internal string
78 * - a two-byte length prefix in little-endian order, followed by the
79 * UTF8 bytes of the string
80 */
81 char str[1];
82 };
83
84
85 /* ------------------------------------------------------------------------ */
86 /*
87 * CVmObjFileName intrinsic class definition
88 */
89
90 class CVmObjFileName: public CVmObject
91 {
92 friend class CVmMetaclassFileName;
93
94 public:
95 /* metaclass registration object */
96 static class CVmMetaclass *metaclass_reg_;
97 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
98
99 /* am I of the given metaclass? */
100 virtual int is_of_metaclass(class CVmMetaclass *meta) const
101 {
102 /* try my own metaclass and my base class */
103 return (meta == metaclass_reg_
104 || CVmObject::is_of_metaclass(meta));
105 }
106
107 /* is this a CVmObjFileName object? */
108 static int is_vmfilnam_obj(VMG_ vm_obj_id_t obj)
109 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
110
111 /* create dynamically using stack arguments */
112 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
113 uint argc);
114
115 /* create from URL notation */
116 static vm_obj_id_t create_from_url(VMG_ const char *str, size_t len);
117
118 /* create from a local path */
119 static vm_obj_id_t create_from_local(VMG_ const char *str, size_t len);
120
121 /* create from a special file ID (and its resolved local path) */
122 static vm_obj_id_t create_from_sfid(
123 VMG_ int32_t sfid, const char *path, size_t len);
124
125 /*
126 * Combine two path local paths into a new FileName object. If
127 * 'literal' is true, it means that we build the literal combined path,
128 * otherwise we build the canonical path (e.g., resolving '.' and '..'
129 * relative links).
130 */
131 static vm_obj_id_t combine_path(
132 VMG_ const char *path, size_t pathl, const char *file, size_t filel,
133 int literal);
134
135 /*
136 * call a static property - we don't have any of our own, so simply
137 * "inherit" the base class handling
138 */
139 static int call_stat_prop(VMG_ vm_val_t *result,
140 const uchar **pc_ptr, uint *argc,
141 vm_prop_id_t prop);
142
143 /* notify of deletion */
144 void notify_delete(VMG_ int in_root_set);
145
146 /* compare filenames by filename string */
147 virtual int equals(VMG_ vm_obj_id_t self, const vm_val_t *val,
148 int depth) const;
149
150 /* add - this appends a path element */
151 virtual int add_val(VMG_ vm_val_t *result,
152 vm_obj_id_t self, const vm_val_t *val);
153
154 /* cast to string */
155 virtual const char *cast_to_string(VMG_ vm_obj_id_t /*self*/,
156 vm_val_t * /*new_str*/) const
157 { return get_path_string(); }
158
159 /* get my path string */
160 const char *get_path_string() const;
161
162 /* get my special file information */
163 int is_special_file() const { return get_ext()->sfid != 0; }
164 int32_t get_sfid() const { return get_ext()->sfid; }
165
166 /*
167 * Get/set the "from UI" flag. If the user manually selected the file
168 * through a file dialog, this is an OS_AFP_xxx value specifying the
169 * type of dialog that was presented.
170 */
171 void set_from_ui(int typ) { get_ext()->from_ui = (char)typ; }
172 int get_from_ui() const { return get_ext()->from_ui; }
173
174 /* set a property */
175 void set_prop(VMG_ class CVmUndo *undo,
176 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
177
178 /* get a property */
179 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
180 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
181
182 /*
183 * receive savepoint notification - we don't keep any
184 * savepoint-relative records, so we don't need to do anything here
185 */
186 void notify_new_savept() { }
187
188 /* apply an undo record (we're immutable -> no undo) */
189 void apply_undo(VMG_ struct CVmUndoRecord *) { }
190
191 /* discard an undo record */
192 void discard_undo(VMG_ struct CVmUndoRecord *) { }
193
194 /* mark our undo record references */
195 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
196
197 /* mark our references (we have none) */
198 void mark_refs(VMG_ uint) { }
199
200 /* remove weak references */
201 void remove_stale_weak_refs(VMG0_) { }
202 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
203
204 /* load from an image file */
205 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
206
207 /* reload from an image file */
208 void reload_from_image(VMG_ vm_obj_id_t self,
209 const char *ptr, size_t siz);
210
211 /* rebuild for image file */
212 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
213
214 /* save to a file */
215 void save_to_file(VMG_ class CVmFile *fp);
216
217 /* restore from a file */
218 void restore_from_file(VMG_ vm_obj_id_t self,
219 class CVmFile *fp, class CVmObjFixup *fixups);
220
221 /* determine if we've changed since loading; we're immutable, so no */
222 int is_changed_since_load() const { return FALSE; }
223
224 protected:
225 /* get my extension data */
226 vm_filnam_ext *get_ext() const { return (vm_filnam_ext *)ext_; }
227
228 /* get the local path for a given string or FileName value */
229 static const char *get_local_path(VMG_ const vm_val_t *val);
230
231 /* load or reload image data */
232 void load_image_data(VMG_ const char *ptr, size_t siz);
233
234 /* create a with no initial contents */
235 CVmObjFileName() { ext_ = 0; }
236
237 /* create with contents */
238 CVmObjFileName(VMG_ int32_t sfid, const char *str, size_t len);
239
240 /* convert a URL string to a local path string */
241 static char *url_to_local(VMG_ const char *str, size_t len, int nullterm);
242
243 /*
244 * convert my filename to universal format, returning an allocated
245 * buffer (which the caller must free with lib_free_str() when done)
246 */
247 char *to_universal(VMG0_) const;
248
249 /*
250 * get the special file type flags - this returns FileTypeSelfLink or
251 * FileTypeParentLink, if applicable, based on the root name
252 */
253 int32_t special_filetype_flags(VMG0_);
254
255 /* property evaluator - undefined function */
256 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
257
258 /* getName method */
259 int getp_getName(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
260
261 /* getRootName method */
262 int getp_getRootName(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
263
264 /* getPath method */
265 int getp_getPath(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
266
267 /* fromUniversal static method */
268 int getp_fromUniversal(VMG_ vm_obj_id_t, vm_val_t *retval, uint *argc)
269 { return s_getp_fromUniversal(vmg_ retval, argc); }
270
271 /* fromUniversal static handler */
272 static int s_getp_fromUniversal(VMG_ vm_val_t *retval, uint *argc);
273
274 /* toUniversal method */
275 int getp_toUniversal(
276 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
277
278 /* addToPath method */
279 int getp_addToPath(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
280
281 /* isAbsolute method */
282 int getp_isAbsolute(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
283
284 /* getAbsolutePath method */
285 int getp_getAbsolutePath(
286 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
287
288 /* getRootDirs method */
289 int getp_getRootDirs(VMG_ vm_obj_id_t, vm_val_t *retval, uint *argc)
290 { return s_getp_getRootDirs(vmg_ retval, argc); }
291
292 /* static getRootDirs method */
293 static int s_getp_getRootDirs(VMG_ vm_val_t *retval, uint *argc);
294
295 /* getFileType method */
296 int getp_getFileType(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
297
298 /* getFileInfo method */
299 int getp_getFileInfo(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
300
301 /* deleteFile method */
302 int getp_deleteFile(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
303
304 /* renameFile method */
305 int getp_renameFile(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
306
307 /* getFilesInDir method */
308 int getp_listDir(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
309
310 /* forEachFile method */
311 int getp_forEachFile(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
312
313 /* createDirectory method */
314 int getp_createDirectory(
315 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
316
317 /* removeDirectory method */
318 int getp_removeDirectory(
319 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
320
321 /* property evaluation function table */
322 static int (CVmObjFileName::*func_table_[])(VMG_ vm_obj_id_t self,
323 vm_val_t *retval, uint *argc);
324 };
325
326
327 /* ------------------------------------------------------------------------ */
328 /*
329 * CVmObjFileName metaclass registration table object
330 */
331 class CVmMetaclassFileName: public CVmMetaclass
332 {
333 public:
334 /* get the global name */
335 const char *get_meta_name() const { return "filename/030000"; }
336
337 /* create from image file */
338 void create_for_image_load(VMG_ vm_obj_id_t id)
339 {
340 new (vmg_ id) CVmObjFileName();
341 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
342 }
343
344 /* create from restoring from saved state */
345 void create_for_restore(VMG_ vm_obj_id_t id)
346 {
347 new (vmg_ id) CVmObjFileName();
348 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
349 }
350
351 /* create dynamically using stack arguments */
352 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
353 { return CVmObjFileName::create_from_stack(vmg_ pc_ptr, argc); }
354
355 /* call a static property */
356 int call_stat_prop(VMG_ vm_val_t *result,
357 const uchar **pc_ptr, uint *argc,
358 vm_prop_id_t prop)
359 {
360 return CVmObjFileName::
361 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
362 }
363 };
364
365 #endif /* VMFILNAM_H */
366
367 /*
368 * Register the class
369 */
370 VM_REGISTER_METACLASS(CVmObjFileName)