| | 1 | #ifdef RCSID |
| | 2 | static char RCSid[] = |
| | 3 | "$Header$"; |
| | 4 | #endif |
| | 5 | |
| | 6 | /* |
| | 7 | * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved. |
| | 8 | * |
| | 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| | 10 | * on using and copying this software. |
| | 11 | */ |
| | 12 | /* |
| | 13 | Name |
| | 14 | vmrunsym.cpp - runtime symbol table |
| | 15 | Function |
| | 16 | |
| | 17 | Notes |
| | 18 | |
| | 19 | Modified |
| | 20 | 02/17/01 MJRoberts - Creation |
| | 21 | */ |
| | 22 | |
| | 23 | #include "t3std.h" |
| | 24 | #include "vmtype.h" |
| | 25 | #include "vmrunsym.h" |
| | 26 | |
| | 27 | /* |
| | 28 | * delete - deletes all of our symbol list entries |
| | 29 | */ |
| | 30 | CVmRuntimeSymbols::~CVmRuntimeSymbols() |
| | 31 | { |
| | 32 | vm_runtime_sym *sym; |
| | 33 | vm_runtime_sym *nxt; |
| | 34 | |
| | 35 | /* delete each symbol */ |
| | 36 | for (sym = head_ ; sym != 0 ; sym = nxt) |
| | 37 | { |
| | 38 | /* |
| | 39 | * remember the next one, since we're deleting our link pointer |
| | 40 | * along with the structure |
| | 41 | */ |
| | 42 | nxt = sym->nxt; |
| | 43 | |
| | 44 | /* delete it */ |
| | 45 | t3free(sym); |
| | 46 | } |
| | 47 | } |
| | 48 | |
| | 49 | /* |
| | 50 | * add a symbol |
| | 51 | */ |
| | 52 | void CVmRuntimeSymbols::add_sym(const char *sym, size_t len, |
| | 53 | const vm_val_t *val) |
| | 54 | { |
| | 55 | vm_runtime_sym *new_sym; |
| | 56 | |
| | 57 | /* allocate a new structure */ |
| | 58 | new_sym = (vm_runtime_sym *)t3malloc(sizeof(vm_runtime_sym) + len - 1); |
| | 59 | |
| | 60 | /* copy the data */ |
| | 61 | new_sym->val = *val; |
| | 62 | new_sym->len = len; |
| | 63 | memcpy(new_sym->sym, sym, len); |
| | 64 | |
| | 65 | /* link it into our list */ |
| | 66 | new_sym->nxt = 0; |
| | 67 | if (tail_ == 0) |
| | 68 | head_ = new_sym; |
| | 69 | else |
| | 70 | tail_->nxt = new_sym; |
| | 71 | |
| | 72 | /* it's the new tail */ |
| | 73 | tail_ = new_sym; |
| | 74 | |
| | 75 | /* count it */ |
| | 76 | ++cnt_; |
| | 77 | } |
| | 78 | |
| | 79 | /* |
| | 80 | * Find the name for a value |
| | 81 | */ |
| | 82 | const char *CVmRuntimeSymbols::find_val_name(VMG_ const vm_val_t *val, |
| | 83 | size_t *name_len) const |
| | 84 | { |
| | 85 | vm_runtime_sym *sym; |
| | 86 | |
| | 87 | /* search the table */ |
| | 88 | for (sym = head_ ; sym != 0 ; sym = sym->nxt) |
| | 89 | { |
| | 90 | /* check for a match to the value */ |
| | 91 | if (val->equals(vmg_ &sym->val)) |
| | 92 | { |
| | 93 | /* it's a match - return the name */ |
| | 94 | *name_len = sym->len; |
| | 95 | return sym->sym; |
| | 96 | } |
| | 97 | } |
| | 98 | |
| | 99 | /* didn't find it - return null */ |
| | 100 | *name_len = 0; |
| | 101 | return 0; |
| | 102 | } |