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 / WtCollectionCmds.c

Lines Size Modified Created Owner MIME Types
287 7,824 2010/05/08 18:46:41 2011/06/13 15:35:15 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 "WtCollectionCmds.h"
0018
#include "WtUtil.h"
0019
#include "WtBasicCmds.h"
0020
0021
/* Helper to manage collection objects. Implements the "object
0022
   method args" command style. */
0023
0024
WtClnCmdStatus WtClnMethodHelper(ClientData clientData, Tcl_Interp *interp,
0025
    int objc, Tcl_Obj *const objv[], int *tclRet, Tcl_Obj **clnPtr,
0026
    Tcl_ObjType *type, int readOnly, int appendUsage)
0027
{
0028
  int ret = TCL_ERROR, status = WT_CLN_CMD_DONE, error = 0;
0029
  int size, listLen;
0030
  WtContext *w;
0031
  const char *subCmd;
0032
  Tcl_Obj *cln, *list, *item;
0033
0034
  w = (WtContext *)Tcl_GetAssocData(interp, "wt::context", NULL);
0035
0036
  if (objc < 2) {
0037
    if (appendUsage) {
0038
      WtClnMethodUsageError(interp, objv[0], readOnly);
0039
    }
0040
    return WT_CLN_CMD_MISSING;
0041
  }
0042
0043
  subCmd = WtToString(objv[1]);
0044
  cln = *clnPtr;
0045
0046
  if (!strcmp(subCmd, "count")) {
0047
0048
    /* count command */
0049
0050
    if (objc != 2) {
0051
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0052
        WtToString(objv[0]), " ", WtToString(objv[1]));
0053
    } else if (WtClnSize(cln, &size, interp)) {
0054
      Tcl_SetObjResult(interp, Tcl_NewIntObj(size));
0055
      ret = TCL_OK;
0056
    }
0057
0058
  } else if (!strcmp(subCmd, "has")) {
0059
0060
    /* has command */
0061
0062
    if (objc != 3) {
0063
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0064
        WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0065
        " name", NULL);
0066
    } else if (WtFindClnItem(cln, objv[2], &item, interp)) {
0067
      Tcl_SetObjResult(interp, Tcl_NewBooleanObj(item != NULL));
0068
      ret = TCL_OK;
0069
    }
0070
0071
  } else if (!strcmp(subCmd, "keys")) {
0072
0073
    /* keys command */
0074
0075
    if (objc != 2) {
0076
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0077
        WtToString(objv[0]), " ", WtToString(objv[1]), NULL);
0078
    } else if (WtGetClnKeys(cln, &list, interp)) {
0079
      Tcl_SetObjResult(interp, list);
0080
      ret = TCL_OK;
0081
    }
0082
0083
  } else if (!strcmp(subCmd, "values")) {
0084
0085
    /* values command */
0086
0087
    if (objc != 2) {
0088
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0089
        WtToString(objv[0]), " ", WtToString(objv[1]), NULL);
0090
    } else if (WtGetClnValues(cln, &list, interp)) {
0091
      Tcl_SetObjResult(interp, list);
0092
      ret = TCL_OK;
0093
    }
0094
0095
  } else if (!strcmp(subCmd, "get")) {
0096
0097
    /* get command */
0098
0099
    if (objc != 3 && objc != 4) {
0100
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0101
        WtToString(objv[0]), " ",
0102
        WtToString(objv[1]), " name ", "?defaultValue?",
0103
        NULL);
0104
    } else if (WtFindClnItem(cln, objv[2], &item, interp)) {
0105
      if (item) {
0106
        Tcl_SetObjResult(interp, item);
0107
        ret = TCL_OK;
0108
      } else if (objc > 3) {
0109
        Tcl_SetObjResult(interp, objv[3]);
0110
        ret = TCL_OK;
0111
      } else {
0112
        Tcl_AppendResult(interp, "No item with key \"",
0113
          WtToString(objv[2]),
0114
          "\" exists, and no default value was specified.",
0115
          NULL);
0116
      }
0117
    }
0118
0119
  } else if (!strcmp(subCmd, "getAll")) {
0120
0121
    /* getAll command */
0122
0123
    if (objc != 3 || objc != 4) {
0124
      Tcl_AppendResult(interp, "Incorrect usage. Should be:\n",
0125
        WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0126
        "name ", "?defaultValue?", NULL);
0127
    } else if (WtFindAllClnItems(cln, objv[2], &list, interp)) {
0128
      if (Tcl_ListObjLength(interp, objv[2], &listLen) == TCL_OK) {
0129
        if (listLen) {
0130
          Tcl_SetObjResult(interp, list);
0131
          ret = TCL_OK;
0132
        } else if (objc > 3) {
0133
          Tcl_SetObjResult(interp, objv[3]);
0134
          ret = TCL_OK;
0135
        } else {
0136
          Tcl_AppendResult(interp, "No entry with key \"",
0137
            WtToString(objv[2]),
0138
            "\" exists, and no default value was specified.",
0139
            NULL);
0140
        }
0141
      }
0142
    }
0143
0144
  } else if (!strcmp(subCmd, "add") && !readOnly) {
0145
0146
    /* add command */
0147
0148
    if (objc != 3) {
0149
      Tcl_AppendResult(interp, "incorrect usage: should be: \n",
0150
        WtToString(objv[0]), " ", WtToString(objv[1]), " item",
0151
        NULL);
0152
    } else if (WtAppendClnItem(cln, objv[2], interp)) {
0153
      Tcl_SetObjResult(interp, objv[2]);
0154
      ret = TCL_OK;
0155
    }
0156
0157
  } else if (!strcmp(subCmd, "addList") && !readOnly) {
0158
0159
    /* addList command */
0160
0161
    if (objc != 3) {
0162
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0163
        WtToString(objv[0]), " ", WtToString(objv[1]), " list",
0164
        NULL);
0165
    } else if (Tcl_ListObjLength(interp, objv[2], &listLen) ==
0166
        TCL_OK) {
0167
      if (WtAppendClnItemList(cln, objv[2], interp)) {
0168
        Tcl_SetObjResult(interp, WtGetClnItems(cln));
0169
        ret = TCL_OK;
0170
      }
0171
    }
0172
0173
  } else if (!strcmp(subCmd, "remove") && !readOnly) {
0174
0175
    /* remove command */
0176
0177
    if (objc != 3) {
0178
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0179
        WtToString(objv[0]), " ", WtToString(objv[1]),
0180
        " name", NULL);
0181
    } else if (WtRemoveFromClnByKey(cln, objv[2], -1, interp)) {
0182
      ret = TCL_OK;
0183
    }
0184
0185
  } else if (!strcmp(subCmd, "removeList") && !readOnly) {
0186
0187
    /* removeList command */
0188
0189
    if (objc != 3) {
0190
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0191
        WtToString(objv[0]), " ", WtToString(objv[1]), " list",
0192
        NULL);
0193
    } else if (Tcl_ListObjLength(interp, objv[2], &listLen) ==
0194
        TCL_OK) {
0195
      if (WtRemoveFromClnByKeyList(cln, objv[2], -1, interp)) {
0196
        ret = TCL_OK;
0197
      }
0198
    }
0199
0200
  } else if (!strcmp(subCmd, "clear") && !readOnly) {
0201
0202
    /* clear command */
0203
0204
    WtClearCollection(cln);
0205
    ret = TCL_OK;
0206
0207
  } else if (!strcmp(subCmd, "list")) {
0208
0209
    /* list command */
0210
0211
    if (objc != 2) {
0212
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0213
        WtToString(objv[0]), " ", WtToString(objv[1]), NULL);
0214
    } else {
0215
      Tcl_SetObjResult(interp, WtGetClnItems(cln));
0216
      ret = TCL_OK;
0217
    }
0218
0219
  } else if (!strcmp(subCmd, "array")) {
0220
0221
    /* array command */
0222
0223
    if (objc < 3) {
0224
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0225
        WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0226
        "arrayName", NULL);
0227
    } else if (WtClnToArray(interp, cln, objv[2], 1)) {
0228
      ret = TCL_OK;
0229
    }
0230
0231
  } else {
0232
0233
    /* unknown command */
0234
0235
    status = WT_CLN_CMD_NOT_FOUND;
0236
0237
    if (appendUsage) {
0238
      WtClnMethodUsageError(interp, objv[0], readOnly);
0239
    }
0240
0241
  }
0242
0243
  *tclRet = ret;
0244
0245
  return status;
0246
}
0247
0248
void WtClnMethodUsageError(Tcl_Interp *interp, Tcl_Obj *cmd, int readOnly)
0249
{
0250
  Tcl_AppendResult(interp, wtBadUsagePrefix, "\n", NULL);
0251
  WtClnMethodAppendUsage(interp, cmd, readOnly);
0252
}
0253
0254
void WtClnMethodAppendUsage(Tcl_Interp *interp, Tcl_Obj *cmd, int readOnly)
0255
{
0256
  char *cmdStr = WtToString(cmd);
0257
0258
  if (readOnly) {
0259
    Tcl_AppendResult(interp,
0260
      cmdStr, " count\n",
0261
      cmdStr, " has key\n",
0262
      cmdStr, " keys\n",
0263
      cmdStr, " values\n",
0264
      cmdStr, " get key ?defaultValue?\n",
0265
      cmdStr, " getAll key ?defaultValue?\n",
0266
      cmdStr, " items\n",
0267
      cmdStr, " list\n",
0268
      cmdStr, " array arrayName\n",
0269
      NULL);
0270
  } else {
0271
    Tcl_AppendResult(interp,
0272
      cmdStr, " count\n",
0273
      cmdStr, " has key\n",
0274
      cmdStr, " keys\n",
0275
      cmdStr, " values\n",
0276
      cmdStr, " get key ?defaultValue?\n",
0277
      cmdStr, " getAll key ?defaultValue?\n",
0278
      cmdStr, " add item\n",
0279
      cmdStr, " addList list\n",
0280
      cmdStr, " remove key\n",
0281
      cmdStr, " removeList list\n",
0282
      cmdStr, " clear\n",
0283
      cmdStr, " list\n",
0284
      cmdStr, " array arrayName\n",
0285
      NULL);
0286
  }
0287
}