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