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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*
* bltArrayObj.c --
*
* Copyright 2000 Silicon Metrics, Inc.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and warranty
* disclaimer appear in supporting documentation, and that the names
* of Lucent Technologies or any of their entities not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission.
*
* Lucent Technologies disclaims all warranties with regard to this
* software, including all implied warranties of merchantability and
* fitness. In no event shall Lucent Technologies be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether in
* an action of contract, negligence or other tortuous action, arising
* out of or in connection with the use or performance of this
* software.
*
* The array Tcl object was created by George A. Howlett.
*/
#include "bltInt.h"
#ifndef NO_ARRAY
#include "bltHash.h"
static Tcl_DupInternalRepProc DupArrayInternalRep;
static Tcl_FreeInternalRepProc FreeArrayInternalRep;
static Tcl_UpdateStringProc UpdateStringOfArray;
static Tcl_SetFromAnyProc SetArrayFromAny;
static Tcl_ObjType arrayObjType = {
"array",
FreeArrayInternalRep, /* Called when an object is freed. */
DupArrayInternalRep, /* Copies an internal representation
* from one object to another. */
UpdateStringOfArray, /* Creates string representation from
* an object's internal representation. */
SetArrayFromAny, /* Creates valid internal representation
* from an object's string representation. */
};
#if 1
static int
SetArrayFromAny(interp, objPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
{
Blt_HashEntry *hPtr;
Blt_HashTable *tablePtr;
Tcl_Obj *elemObjPtr, **vobjv;
CONST Tcl_ObjType *oldTypePtr;
char *string;
int isNew;
int nElem;
register int i;
if (objPtr->typePtr == &arrayObjType) {
return TCL_OK;
}
if (Tcl_ListObjGetElements(interp, objPtr, &nElem, &vobjv) != TCL_OK) {
return TCL_ERROR;
}
if (nElem%2) {
if (interp != NULL) {
string = Tcl_GetString(objPtr);
Tcl_AppendResult(interp, "odd length: ", string, 0);
}
return TCL_ERROR;
}
tablePtr = Blt_Malloc(sizeof(Blt_HashTable));
assert(tablePtr);
Blt_InitHashTable(tablePtr, BLT_STRING_KEYS);
for (i = 0; i < nElem; i += 2) {
hPtr = Blt_CreateHashEntry(tablePtr, Tcl_GetString(vobjv[i]), &isNew);
elemObjPtr = vobjv[i + 1];
Blt_SetHashValue(hPtr, elemObjPtr);
/* Make sure we increment the reference count */
Tcl_IncrRefCount(elemObjPtr);
}
oldTypePtr = objPtr->typePtr;
if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
oldTypePtr->freeIntRepProc(objPtr);
}
objPtr->internalRep.otherValuePtr = (VOID *)tablePtr;
objPtr->typePtr = &arrayObjType;
return TCL_OK;
}
#else
static int
SetArrayFromAny(interp, objPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
{
Blt_HashEntry *hPtr;
Blt_HashTable *tablePtr;
Tcl_Obj *elemObjPtr;
Tcl_ObjType *oldTypePtr = objPtr->typePtr;
char **elemArr;
char *string;
int isNew;
int nElem;
register int i;
if (objPtr->typePtr == &arrayObjType) {
return TCL_OK;
}
/*
* Get the string representation. Make it up-to-date if necessary.
*/
string = Tcl_GetString(objPtr);
if (Tcl_SplitList(interp, string, &nElem, &elemArr) != TCL_OK) {
return TCL_ERROR;
}
if (nElem%2) {
if (interp != NULL) {
Tcl_AppendResult(interp, "odd length: ", string, 0);
}
Blt_Free(elemArr);
return TCL_ERROR;
}
tablePtr = Blt_Malloc(sizeof(Blt_HashTable));
assert(tablePtr);
Blt_InitHashTable(tablePtr, BLT_STRING_KEYS);
for (i = 0; i < nElem; i += 2) {
hPtr = Blt_CreateHashEntry(tablePtr, elemArr[i], &isNew);
elemObjPtr = Tcl_NewStringObj(elemArr[i + 1], -1);
Blt_SetHashValue(hPtr, elemObjPtr);
/* Make sure we increment the reference count */
Tcl_IncrRefCount(elemObjPtr);
}
if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
oldTypePtr->freeIntRepProc(objPtr);
}
objPtr->internalRep.otherValuePtr = (VOID *)tablePtr;
objPtr->typePtr = &arrayObjType;
Blt_Free(elemArr);
return TCL_OK;
}
#endif
static void
DupArrayInternalRep(srcPtr, destPtr)
Tcl_Obj *srcPtr; /* Object with internal rep to copy. */
Tcl_Obj *destPtr; /* Object with internal rep to set. */
{
Blt_HashEntry *hPtr, *h2Ptr;
Blt_HashSearch cursor;
Blt_HashTable *srcTablePtr, *destTablePtr;
Tcl_Obj *valueObjPtr;
char *key;
int isNew;
srcTablePtr = (Blt_HashTable *)srcPtr->internalRep.otherValuePtr;
destTablePtr = Blt_Malloc(sizeof(Blt_HashTable));
assert(destTablePtr);
Blt_InitHashTable(destTablePtr, BLT_STRING_KEYS);
for (hPtr = Blt_FirstHashEntry(srcTablePtr, &cursor); hPtr != NULL;
hPtr = Blt_NextHashEntry(&cursor)) {
key = Blt_GetHashKey(srcTablePtr, hPtr);
h2Ptr = Blt_CreateHashEntry(destTablePtr, key, &isNew);
valueObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
assert (valueObjPtr != NULL);
Blt_SetHashValue(h2Ptr, valueObjPtr );
/* Make sure we increment the reference count now that both
* array objects are using the same elements. */
Tcl_IncrRefCount(valueObjPtr);
}
Tcl_InvalidateStringRep(destPtr);
destPtr->internalRep.otherValuePtr = (VOID *)destTablePtr;
destPtr->typePtr = &arrayObjType;
}
static void
UpdateStringOfArray(objPtr)
Tcl_Obj *objPtr; /* Array object whose string rep to update. */
{
Tcl_DString dString;
Blt_HashTable *tablePtr;
Blt_HashEntry *hPtr;
Blt_HashSearch cursor;
Tcl_Obj *elemObjPtr;
tablePtr = (Blt_HashTable *)objPtr->internalRep.otherValuePtr;
Tcl_DStringInit(&dString);
for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); hPtr != NULL;
hPtr = Blt_NextHashEntry(&cursor)) {
elemObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
Tcl_DStringAppendElement(&dString, Blt_GetHashKey(tablePtr, hPtr));
Tcl_DStringAppendElement(&dString, elemObjPtr == NULL?"":Tcl_GetString(elemObjPtr));
}
objPtr->bytes = Blt_Strdup(Tcl_DStringValue(&dString));
objPtr->length = strlen(Tcl_DStringValue(&dString));
Tcl_DStringFree(&dString);
}
static void
FreeArrayInternalRep(objPtr)
Tcl_Obj *objPtr; /* Array object to release. */
{
Blt_HashEntry *hPtr;
Blt_HashSearch cursor;
Blt_HashTable *tablePtr;
Tcl_Obj *elemObjPtr;
Tcl_InvalidateStringRep(objPtr);
tablePtr = (Blt_HashTable *)objPtr->internalRep.otherValuePtr;
for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); hPtr != NULL;
hPtr = Blt_NextHashEntry(&cursor)) {
elemObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
Tcl_DecrRefCount(elemObjPtr);
}
Blt_DeleteHashTable(tablePtr);
Blt_Free(tablePtr);
}
int
Blt_GetArrayFromObj(interp, objPtr, tablePtrPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
Blt_HashTable **tablePtrPtr;
{
if (objPtr->typePtr == &arrayObjType) {
*tablePtrPtr = (Blt_HashTable *)objPtr->internalRep.otherValuePtr;
return TCL_OK;
}
if (SetArrayFromAny(interp, objPtr) == TCL_OK) {
*tablePtrPtr = (Blt_HashTable *)objPtr->internalRep.otherValuePtr;
return TCL_OK;
}
return TCL_ERROR;
}
Tcl_Obj *
Blt_NewArrayObj(objc, objv)
int objc;
Tcl_Obj *objv[];
{
Blt_HashEntry *hPtr;
Blt_HashTable *tablePtr;
Tcl_Obj *arrayObjPtr, *objPtr;
int isNew;
register int i;
if (objc % 2) {
return NULL;
}
tablePtr = Blt_Malloc(sizeof(Blt_HashTable));
assert(tablePtr);
Blt_InitHashTable(tablePtr, BLT_STRING_KEYS);
for (i = 0; i < objc; i += 2) {
hPtr = Blt_CreateHashEntry(tablePtr, Tcl_GetString(objv[i]), &isNew);
if ((i + 1) == objc) {
objPtr = Tcl_NewStringObj("",-1);
} else {
objPtr = objv[i+1];
}
Tcl_IncrRefCount(objPtr);
if (!isNew) {
Tcl_Obj *oldObjPtr;
oldObjPtr = Blt_GetHashValue(hPtr);
Tcl_DecrRefCount(oldObjPtr);
}
Blt_SetHashValue(hPtr, objPtr);
}
arrayObjPtr = Tcl_NewObj();
/*
* Reference counts for entry objects are initialized to 0. They
* are incremented as they are inserted into the tree via the
* Blt_TreeSetValue call.
*/
arrayObjPtr->refCount = 0;
arrayObjPtr->internalRep.otherValuePtr = (VOID *)tablePtr;
arrayObjPtr->bytes = NULL;
arrayObjPtr->length = 0;
arrayObjPtr->typePtr = &arrayObjType;
return arrayObjPtr;
}
int
Blt_IsArrayObj(objPtr)
Tcl_Obj *objPtr;
{
return (objPtr->typePtr == &arrayObjType);
}
/*ARGSUSED*/
void
Blt_RegisterArrayObj(interp)
Tcl_Interp *interp; /* Not used. */
{
Tcl_RegisterObjType(&arrayObjType);
}
#endif /* NO_ARRAY */
|