View Source

[modwtcl]
apreq.h
dist/
libapreq1/
mod_wtcl.c
mod_wtcl.h
setupExclude.txt
util/
win.def
win.mak
wt1.1/
WtAppTable.c
WtAppTable.h
WtBasicCmds.c
WtBasicCmds.h
WtClientRequest.c
WtClientRequest.h
WtCollection.c
WtCollection.h
WtCollectionCmds.c
WtCollectionCmds.h
WtContext.c
WtContext.h
WtContextEvents.c
WtContextEvents.h
WtCookie.c
WtCookie.h
WtDbSession.c
WtDbSession.h
WtExecute.c
WtExecute.h
WtHtmlEntities.c
WtHtmlEntities.h
WtInitCmds.c
WtInitCmds.h
WtMtTable.c
WtMtTable.h
WtMultiTable.c
WtMultiTable.h
WtOS.h
WtProcSession.c
WtProcSession.h
WtResponse.c
WtResponse.h
WtServerCmds.c
WtServerCmds.h
WtSession.c
WtSession.h
WtSettings.c
WtSettings.h
WtTable.c
WtTable.h
WtTableCmds.c
WtTableCmds.h
WtTableUtil.c
WtTableUtil.h
WtUpload.c
WtUpload.h
WtUtil.c
WtUtil.h
WtWebErrors.c
WtWebErrors.h
WtWindows.h
File: / archive / modwtcl / WtTableUtil.c

Lines Size Modified Created Owner MIME Types
397 8,427 2010/05/08 18:46:41 2011/06/13 15:35:16 BUILTIN\Administrators text/x-csrc

0001
/*
0002
 * Copyright 2001 Alexander Boverman and the original authors.
0003
 * 
0004
 * Licensed under the Apache License, Version 2.0 (the "License");
0005
 * you may not use this file except in compliance with the License.
0006
 * You may obtain a copy of the License at
0007
 * 
0008
 *      http://www.apache.org/licenses/LICENSE-2.0
0009
 * 
0010
 * Unless required by applicable law or agreed to in writing, software
0011
 * distributed under the License is distributed on an "AS IS" BASIS,
0012
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013
 * See the License for the specific language governing permissions and
0014
 * limitations under the License.
0015
 */
0016
0017
#include "WtTableUtil.h"
0018
#include "WtTable.h"
0019
#include "WtUtil.h"
0020
0021
/* Convert a table to a list */
0022
0023
Tcl_Obj *WtTableToList(Tcl_Obj *table, int takeFirstValue, Tcl_Interp *interp)
0024
{
0025
  int ok = 1;
0026
  Tcl_HashTable *hashTable = WtGetTableMap(table);
0027
  Tcl_HashEntry *ent;
0028
  Tcl_HashSearch search;
0029
  Tcl_Obj *list, *key, *val, *first;
0030
0031
  list = Tcl_NewListObj(0, NULL);
0032
  ent = Tcl_FirstHashEntry(hashTable, &search);
0033
0034
  while (ent) {    
0035
    key = (Tcl_Obj *)Tcl_GetHashKey(hashTable, ent);
0036
    if (Tcl_ListObjAppendElement(interp, list, key) != TCL_OK) {
0037
      ok = 0;
0038
      break;
0039
    } else {
0040
      val = (Tcl_Obj *)Tcl_GetHashValue(ent);
0041
      if (takeFirstValue) {
0042
        if (Tcl_ListObjIndex(NULL, val, 0, &first) == TCL_OK) {
0043
          val = first;
0044
        } else {
0045
          ok = 0;
0046
          break;
0047
        }
0048
      }
0049
      if (Tcl_ListObjAppendElement(interp, list, val) != TCL_OK) {
0050
        ok = 0;
0051
        break;
0052
      }
0053
    }
0054
    ent = Tcl_NextHashEntry(&search);
0055
  }
0056
0057
  if (!ok) {
0058
    Tcl_DecrRefCount(list);
0059
    list = NULL;
0060
  }
0061
0062
  return list;
0063
}
0064
0065
/* Convert a table to a Tcl array */
0066
0067
int WtTableToArray(Tcl_Interp *interp, Tcl_Obj *table,
0068
    Tcl_Obj *arrName, int takeFirstValue)
0069
{
0070
  int ok = 1;
0071
  Tcl_HashTable *hashTable = WtGetTableMap(table);
0072
  Tcl_HashEntry *ent;
0073
  Tcl_HashSearch search;
0074
  Tcl_Obj *key, *val, *first;
0075
0076
  ent = Tcl_FirstHashEntry(hashTable, &search);
0077
0078
  while (ent) {    
0079
    key = (Tcl_Obj *)Tcl_GetHashKey(hashTable, ent);
0080
    val = (Tcl_Obj *)Tcl_GetHashValue(ent);
0081
    if (takeFirstValue) {
0082
      if (Tcl_ListObjIndex(NULL, val, 0, &first) == TCL_OK) {
0083
        val = first;
0084
      } else {
0085
        ok = 0;
0086
        break;
0087
      }
0088
    }
0089
    if (!Tcl_ObjSetVar2(interp, arrName, key, val, TCL_LEAVE_ERR_MSG)) {
0090
      ok = 0;
0091
      break;
0092
    }
0093
    ent = Tcl_NextHashEntry(&search);
0094
  }
0095
0096
  return ok;
0097
}
0098
0099
int WtTableHasStr(Tcl_Obj *tbl, const char *key)
0100
{
0101
  Tcl_Obj *keyObj, *valObj;
0102
  keyObj = WtNewString(key);
0103
  Tcl_IncrRefCount(keyObj);
0104
  valObj = WtTableGet(tbl, keyObj);
0105
  Tcl_DecrRefCount(keyObj);
0106
  return valObj != NULL;
0107
}
0108
0109
void WtTableSetStrToStr(Tcl_Obj *tbl, const char *key, const char *val)
0110
{
0111
  Tcl_Obj *keyObj, *valObj;
0112
0113
  keyObj = WtNewString(key);
0114
  Tcl_IncrRefCount(keyObj);
0115
  valObj = WtNewString(val);
0116
  Tcl_IncrRefCount(valObj);
0117
0118
  WtTableSet(tbl, keyObj, valObj);
0119
0120
  Tcl_DecrRefCount(keyObj);
0121
  Tcl_DecrRefCount(valObj);
0122
}
0123
0124
void WtTableSetStrToInt(Tcl_Obj *tbl, const char *key, int val)
0125
{
0126
  Tcl_Obj *keyObj, *valObj;
0127
0128
  keyObj = WtNewString(key);
0129
  Tcl_IncrRefCount(keyObj);
0130
0131
  valObj = Tcl_NewIntObj(val);
0132
  Tcl_IncrRefCount(valObj);
0133
0134
  WtTableSet(tbl, keyObj, valObj);
0135
0136
  Tcl_DecrRefCount(keyObj);
0137
  Tcl_DecrRefCount(valObj);
0138
}
0139
0140
void WtTableSetStrToBool(Tcl_Obj *tbl, const char *key, int val)
0141
{
0142
  Tcl_Obj *keyObj, *valObj;
0143
0144
  keyObj = WtNewString(key);
0145
  Tcl_IncrRefCount(keyObj);
0146
0147
  valObj = Tcl_NewBooleanObj(val);
0148
  Tcl_IncrRefCount(valObj);
0149
0150
  WtTableSet(tbl, keyObj, valObj);
0151
0152
  Tcl_DecrRefCount(keyObj);
0153
  Tcl_DecrRefCount(valObj);
0154
}
0155
0156
void WtTableSetStrToLong(Tcl_Obj *tbl, const char *key, long val)
0157
{
0158
  Tcl_Obj *keyObj, *valObj;
0159
0160
  keyObj = WtNewString(key);
0161
  Tcl_IncrRefCount(keyObj);
0162
0163
  valObj = Tcl_NewLongObj(val);
0164
  Tcl_IncrRefCount(valObj);
0165
0166
  WtTableSet(tbl, keyObj, valObj);
0167
0168
  Tcl_DecrRefCount(keyObj);
0169
  Tcl_DecrRefCount(valObj);
0170
}
0171
0172
void WtTableSetStrToDouble(Tcl_Obj *tbl, const char *key, double val)
0173
{
0174
  Tcl_Obj *keyObj, *valObj;
0175
0176
  keyObj = WtNewString(key);
0177
  Tcl_IncrRefCount(keyObj);
0178
0179
  valObj = Tcl_NewDoubleObj(val);
0180
  Tcl_IncrRefCount(valObj);
0181
0182
  WtTableSet(tbl, keyObj, valObj);
0183
0184
  Tcl_DecrRefCount(keyObj);
0185
  Tcl_DecrRefCount(valObj);
0186
}
0187
0188
void WtTableSetStrToObj(Tcl_Obj *tbl, const char *key, Tcl_Obj *val)
0189
{
0190
  Tcl_Obj *keyObj;
0191
0192
  keyObj = WtNewString(key);
0193
  Tcl_IncrRefCount(keyObj);
0194
0195
  WtTableSet(tbl, keyObj, val);
0196
0197
  Tcl_DecrRefCount(keyObj);
0198
}
0199
0200
void WtTableSetoIntToObj(Tcl_Obj *tbl, int key, Tcl_Obj *val)
0201
{
0202
  Tcl_Obj *keyObj;
0203
0204
  keyObj = Tcl_NewIntObj(key);
0205
  Tcl_IncrRefCount(keyObj);
0206
0207
  WtTableSet(tbl, keyObj, val);
0208
0209
  Tcl_DecrRefCount(keyObj);
0210
}
0211
0212
int WtTableAppendStrToList(Tcl_Obj *tbl, const char *key, const char *val,
0213
    Tcl_Interp *interp)
0214
{
0215
  int ok;
0216
  Tcl_Obj *keyObj, *valObj;
0217
0218
  keyObj = WtNewString(key);
0219
  Tcl_IncrRefCount(keyObj);
0220
  valObj = WtNewString(val);
0221
  Tcl_IncrRefCount(valObj);
0222
0223
  ok = WtTableAppendToList(tbl, keyObj, valObj, NULL, interp);
0224
0225
  Tcl_DecrRefCount(keyObj);
0226
  Tcl_DecrRefCount(valObj);
0227
0228
  return ok;
0229
}
0230
0231
int WtTableAppendObjToList(Tcl_Obj *tbl, const char *key, Tcl_Obj *valObj,
0232
    Tcl_Interp *interp)
0233
{
0234
  int ok;
0235
  Tcl_Obj *keyObj;
0236
0237
  keyObj = WtNewString(key);
0238
  Tcl_IncrRefCount(keyObj);
0239
  ok = WtTableAppendToList(tbl, keyObj, valObj, NULL, interp);
0240
  Tcl_DecrRefCount(keyObj);
0241
0242
  return ok;
0243
}
0244
0245
char *WtTableGetStrFromStr(Tcl_Obj *tbl, const char *key, char *fallback)
0246
{
0247
  Tcl_Obj *keyObj, *valObj;
0248
0249
  keyObj = WtNewString(key);
0250
  Tcl_IncrRefCount(keyObj);
0251
0252
  valObj = WtTableGet(tbl, keyObj);
0253
0254
  Tcl_DecrRefCount(keyObj);
0255
0256
  return valObj ? WtToString(valObj) : fallback;
0257
}
0258
0259
int WtTableGetIntFromStr(Tcl_Obj *tbl, const char *key, int fallback)
0260
{
0261
  int ret = fallback, intVal;
0262
  Tcl_Obj *keyObj, *valObj;
0263
0264
  keyObj = WtNewString(key);
0265
  Tcl_IncrRefCount(keyObj);
0266
0267
  valObj = WtTableGet(tbl, keyObj);
0268
0269
  Tcl_DecrRefCount(keyObj);
0270
0271
  if (valObj) {
0272
    if (Tcl_GetIntFromObj(NULL, valObj, &intVal) == TCL_OK) {
0273
      ret = intVal;
0274
    }
0275
  }
0276
0277
  return ret;
0278
}
0279
0280
int WtTableGetBoolFromStr(Tcl_Obj *tbl, const char *key, int fallback)
0281
{
0282
  int ret = fallback, intVal;
0283
  Tcl_Obj *keyObj, *valObj;
0284
0285
  keyObj = WtNewString(key);
0286
  Tcl_IncrRefCount(keyObj);
0287
0288
  valObj = WtTableGet(tbl, keyObj);
0289
0290
  Tcl_DecrRefCount(keyObj);
0291
0292
  if (valObj) {
0293
    if (Tcl_GetBooleanFromObj(NULL, valObj, &intVal) == TCL_OK) {
0294
      ret = intVal;
0295
    }
0296
  }
0297
0298
  return ret;
0299
}
0300
0301
Tcl_Obj *WtTableGetObjFromStr(Tcl_Obj *tbl, const char *key, Tcl_Obj *fallback)
0302
{
0303
  Tcl_Obj *keyObj, *valObj;
0304
0305
  keyObj = WtNewString(key);
0306
  Tcl_IncrRefCount(keyObj);
0307
0308
  valObj = WtTableGet(tbl, keyObj);
0309
0310
  Tcl_DecrRefCount(keyObj);
0311
0312
  return valObj ? valObj : fallback;
0313
}
0314
0315
Tcl_Obj *WtTableGetObjFromInt(Tcl_Obj *tbl, int key, Tcl_Obj *fallback)
0316
{
0317
  Tcl_Obj *keyObj, *valObj;
0318
0319
  keyObj = Tcl_NewIntObj(key);
0320
  Tcl_IncrRefCount(keyObj);
0321
0322
  valObj = WtTableGet(tbl, keyObj);
0323
0324
  Tcl_DecrRefCount(keyObj);
0325
0326
  return valObj ? valObj : fallback;
0327
}
0328
0329
int WtTableGetListFromStr(Tcl_Obj *tbl, const char *keyStr, Tcl_Obj **val,
0330
    Tcl_Obj *fallback, Tcl_Interp *interp)
0331
{
0332
  int ret = 0;
0333
  Tcl_Obj *key;
0334
0335
  key = WtNewString(keyStr);
0336
  Tcl_IncrRefCount(key);
0337
0338
  ret = WtTableGetListDefault(tbl, key, val, fallback, interp);
0339
0340
  Tcl_DecrRefCount(key);
0341
0342
  return ret;
0343
}
0344
0345
void WtTableRemoveStr(Tcl_Obj *tbl, const char *key)
0346
{
0347
  Tcl_Obj *keyObj;
0348
0349
  keyObj = WtNewString(key);
0350
  Tcl_IncrRefCount(keyObj);
0351
0352
  WtTableRemove(tbl, keyObj);
0353
0354
  Tcl_DecrRefCount(keyObj);
0355
}
0356
0357
int WtOwnTable(Tcl_Obj **objPtr, Tcl_Interp *interp)
0358
{
0359
  WtCopyOnWrite(objPtr);
0360
  return WtConvertToTable(*objPtr, interp);
0361
}
0362
0363
int WtOwnCaselessTable(Tcl_Obj **objPtr, Tcl_Interp *interp)
0364
{
0365
  WtCopyOnWrite(objPtr);
0366
  return WtConvertToCaselessTable(*objPtr, interp);
0367
}
0368
0369
int WtOwnTableObj(Tcl_Obj **objPtr, Tcl_ObjType *type, Tcl_Interp *interp)
0370
{
0371
  WtCopyOnWrite(objPtr);
0372
  return WtConvertToTableObj(*objPtr, type, interp);
0373
}
0374
0375
int WtImportTableVars(Tcl_Obj *tbl, Tcl_Interp *interp)
0376
{
0377
  int ok = 1;
0378
  Tcl_HashTable *hashTable = WtGetTableMap(tbl);
0379
  Tcl_HashEntry *ent;
0380
  Tcl_HashSearch search;
0381
  Tcl_Obj *key, *val;
0382
0383
  ent = Tcl_FirstHashEntry(hashTable, &search);
0384
0385
  while (ent) {    
0386
    key = (Tcl_Obj *)Tcl_GetHashKey(hashTable, ent);
0387
    val = (Tcl_Obj *)Tcl_GetHashValue(ent);
0388
    if (!Tcl_ObjSetVar2(interp, key, NULL, val,
0389
        TCL_LEAVE_ERR_MSG)) {
0390
      ok = 0;
0391
      break;
0392
    }
0393
    ent = Tcl_NextHashEntry(&search);
0394
  }
0395
0396
  return ok;
0397
}