| 1 | # treec.tcl -- |
|---|
| 2 | # |
|---|
| 3 | # Implementation of a tree data structure for Tcl. |
|---|
| 4 | # This code based on critcl, API compatible to the PTI [x]. |
|---|
| 5 | # [x] Pure Tcl Implementation. |
|---|
| 6 | # |
|---|
| 7 | # Copyright (c) 2005 Andreas Kupries <andreas_kupries@users.sourceforge.net> |
|---|
| 8 | # |
|---|
| 9 | # See the file "license.terms" for information on usage and redistribution |
|---|
| 10 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
|---|
| 11 | # |
|---|
| 12 | # RCS: @(#) $Id: tree_c.tcl,v 1.4 2007/01/21 22:15:59 andreas_kupries Exp $ |
|---|
| 13 | |
|---|
| 14 | package require critcl |
|---|
| 15 | package provide struct_treec 2.1.1 |
|---|
| 16 | package require Tcl 8.2 |
|---|
| 17 | |
|---|
| 18 | namespace eval ::struct { |
|---|
| 19 | # Supporting code for the main command. |
|---|
| 20 | |
|---|
| 21 | catch { |
|---|
| 22 | critcl::cheaders -g |
|---|
| 23 | critcl::debug memory symbols |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | critcl::cheaders tree/*.h |
|---|
| 27 | critcl::csources tree/*.c |
|---|
| 28 | |
|---|
| 29 | critcl::ccode { |
|---|
| 30 | /* -*- c -*- */ |
|---|
| 31 | |
|---|
| 32 | #include <util.h> |
|---|
| 33 | #include <t.h> |
|---|
| 34 | #include <tn.h> |
|---|
| 35 | #include <ms.h> |
|---|
| 36 | #include <m.h> |
|---|
| 37 | |
|---|
| 38 | /* .................................................. */ |
|---|
| 39 | /* Global tree management, per interp |
|---|
| 40 | */ |
|---|
| 41 | |
|---|
| 42 | typedef struct TDg { |
|---|
| 43 | long int counter; |
|---|
| 44 | char buf [50]; |
|---|
| 45 | } TDg; |
|---|
| 46 | |
|---|
| 47 | static void |
|---|
| 48 | TDgrelease (ClientData cd, Tcl_Interp* interp) |
|---|
| 49 | { |
|---|
| 50 | ckfree((char*) cd); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | static CONST char* |
|---|
| 54 | TDnewName (Tcl_Interp* interp) |
|---|
| 55 | { |
|---|
| 56 | #define KEY "tcllib/struct::tree/critcl" |
|---|
| 57 | |
|---|
| 58 | Tcl_InterpDeleteProc* proc = TDgrelease; |
|---|
| 59 | TDg* tdg; |
|---|
| 60 | |
|---|
| 61 | tdg = Tcl_GetAssocData (interp, KEY, &proc); |
|---|
| 62 | if (tdg == NULL) { |
|---|
| 63 | tdg = (TDg*) ckalloc (sizeof (TDg)); |
|---|
| 64 | tdg->counter = 0; |
|---|
| 65 | |
|---|
| 66 | Tcl_SetAssocData (interp, KEY, proc, |
|---|
| 67 | (ClientData) tdg); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | tdg->counter ++; |
|---|
| 71 | sprintf (tdg->buf, "tree%d", tdg->counter); |
|---|
| 72 | return tdg->buf; |
|---|
| 73 | |
|---|
| 74 | #undef KEY |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | static void |
|---|
| 78 | TDdeleteCmd (ClientData clientData) |
|---|
| 79 | { |
|---|
| 80 | /* Release the whole tree. */ |
|---|
| 81 | t_delete ((T*) clientData); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | # Main command, tree creation. |
|---|
| 86 | |
|---|
| 87 | critcl::ccommand tree_critcl {dummy interp objc objv} { |
|---|
| 88 | /* Syntax |
|---|
| 89 | * - epsilon |1 |
|---|
| 90 | * - name |2 |
|---|
| 91 | * - name =|:=|as|deserialize source |4 |
|---|
| 92 | */ |
|---|
| 93 | |
|---|
| 94 | CONST char* name; |
|---|
| 95 | T* td; |
|---|
| 96 | Tcl_Obj* fqn; |
|---|
| 97 | Tcl_CmdInfo ci; |
|---|
| 98 | |
|---|
| 99 | #define USAGE "?name ?=|:=|as|deserialize source??" |
|---|
| 100 | |
|---|
| 101 | if ((objc != 4) && (objc != 2) && (objc != 1)) { |
|---|
| 102 | Tcl_WrongNumArgs (interp, 1, objv, USAGE); |
|---|
| 103 | return TCL_ERROR; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | if (objc < 2) { |
|---|
| 107 | name = TDnewName (interp); |
|---|
| 108 | } else { |
|---|
| 109 | name = Tcl_GetString (objv [1]); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | if (!Tcl_StringMatch (name, "::*")) { |
|---|
| 113 | /* Relative name. Prefix with current namespace */ |
|---|
| 114 | |
|---|
| 115 | Tcl_Eval (interp, "namespace current"); |
|---|
| 116 | fqn = Tcl_GetObjResult (interp); |
|---|
| 117 | fqn = Tcl_DuplicateObj (fqn); |
|---|
| 118 | Tcl_IncrRefCount (fqn); |
|---|
| 119 | |
|---|
| 120 | if (!Tcl_StringMatch (Tcl_GetString (fqn), "::")) { |
|---|
| 121 | Tcl_AppendToObj (fqn, "::", -1); |
|---|
| 122 | } |
|---|
| 123 | Tcl_AppendToObj (fqn, name, -1); |
|---|
| 124 | } else { |
|---|
| 125 | fqn = Tcl_NewStringObj (name, -1); |
|---|
| 126 | Tcl_IncrRefCount (fqn); |
|---|
| 127 | } |
|---|
| 128 | Tcl_ResetResult (interp); |
|---|
| 129 | |
|---|
| 130 | if (Tcl_GetCommandInfo (interp, |
|---|
| 131 | Tcl_GetString (fqn), |
|---|
| 132 | &ci)) { |
|---|
| 133 | Tcl_Obj* err; |
|---|
| 134 | |
|---|
| 135 | err = Tcl_NewObj (); |
|---|
| 136 | Tcl_AppendToObj (err, "command \"", -1); |
|---|
| 137 | Tcl_AppendObjToObj (err, fqn); |
|---|
| 138 | Tcl_AppendToObj (err, "\" already exists, unable to create tree", -1); |
|---|
| 139 | |
|---|
| 140 | Tcl_DecrRefCount (fqn); |
|---|
| 141 | Tcl_SetObjResult (interp, err); |
|---|
| 142 | return TCL_ERROR; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | if (objc == 4) { |
|---|
| 146 | Tcl_Obj* type = objv[2]; |
|---|
| 147 | Tcl_Obj* src = objv[3]; |
|---|
| 148 | int srctype; |
|---|
| 149 | |
|---|
| 150 | static CONST char* types [] = { |
|---|
| 151 | ":=", "=", "as", "deserialize", NULL |
|---|
| 152 | }; |
|---|
| 153 | enum types { |
|---|
| 154 | T_ASSIGN, T_IS, T_AS, T_DESER |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | if (Tcl_GetIndexFromObj (interp, type, types, "type", |
|---|
| 158 | 0, &srctype) != TCL_OK) { |
|---|
| 159 | Tcl_DecrRefCount (fqn); |
|---|
| 160 | Tcl_ResetResult (interp); |
|---|
| 161 | Tcl_WrongNumArgs (interp, 1, objv, USAGE); |
|---|
| 162 | return TCL_ERROR; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | td = t_new (); |
|---|
| 166 | |
|---|
| 167 | switch (srctype) { |
|---|
| 168 | case T_ASSIGN: |
|---|
| 169 | case T_AS: |
|---|
| 170 | case T_IS: |
|---|
| 171 | if (tms_assign (interp, td, src) != TCL_OK) { |
|---|
| 172 | t_delete (td); |
|---|
| 173 | Tcl_DecrRefCount (fqn); |
|---|
| 174 | return TCL_ERROR; |
|---|
| 175 | } |
|---|
| 176 | break; |
|---|
| 177 | |
|---|
| 178 | case T_DESER: |
|---|
| 179 | if (t_deserialize (td, interp, src) != TCL_OK) { |
|---|
| 180 | t_delete (td); |
|---|
| 181 | Tcl_DecrRefCount (fqn); |
|---|
| 182 | return TCL_ERROR; |
|---|
| 183 | } |
|---|
| 184 | break; |
|---|
| 185 | } |
|---|
| 186 | } else { |
|---|
| 187 | td = t_new (); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | td->cmd = Tcl_CreateObjCommand (interp, Tcl_GetString (fqn), |
|---|
| 191 | tms_objcmd, (ClientData) td, |
|---|
| 192 | TDdeleteCmd); |
|---|
| 193 | |
|---|
| 194 | Tcl_SetObjResult (interp, fqn); |
|---|
| 195 | Tcl_DecrRefCount (fqn); |
|---|
| 196 | return TCL_OK; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | namespace eval tree { |
|---|
| 200 | critcl::ccommand prune_critcl {dummy interp objc objv} { |
|---|
| 201 | return 5; |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | # ### ### ### ######### ######### ######### |
|---|
| 207 | ## Ready |
|---|