diff options
author | Steve Bennett <steveb@workware.net.au> | 2010-10-05 13:12:27 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2010-10-20 10:14:42 +1000 |
commit | 2d499dd0fd27436b64777396e1ebd9ae7774d674 (patch) | |
tree | f159d968791a49b14e8276cf4e6ea107b8b11454 /jim.c | |
parent | 1d2f53210dda3c9826c3ce2e499b46994c1bf0a8 (diff) | |
download | jimtcl-2d499dd0fd27436b64777396e1ebd9ae7774d674.zip jimtcl-2d499dd0fd27436b64777396e1ebd9ae7774d674.tar.gz jimtcl-2d499dd0fd27436b64777396e1ebd9ae7774d674.tar.bz2 |
Add support for 'dict keys'
And implement 'array names' in terms of it
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jim.c')
-rw-r--r-- | jim.c | 41 |
1 files changed, 39 insertions, 2 deletions
@@ -12672,17 +12672,47 @@ static int Jim_RenameCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *a return Jim_RenameCommand(interp, oldName, newName); } +int Jim_DictKeys(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj) +{ + int i; + int len; + Jim_Obj *resultObj; + Jim_Obj *dictObj; + Jim_Obj **dictValuesObj; + + if (Jim_DictKeysVector(interp, objPtr, NULL, 0, &dictObj, JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } + + if (Jim_DictPairs(interp, dictObj, &dictValuesObj, &len) != JIM_OK) { + return JIM_ERR; + } + + /* Only return the matching values */ + resultObj = Jim_NewListObj(interp, NULL, 0); + + for (i = 0; i < len; i += 2) { + if (patternObj == NULL || Jim_StringMatchObj(patternObj, dictValuesObj[i], 0)) { + Jim_ListAppendElement(interp, resultObj, dictValuesObj[i]); + } + } + Jim_Free(dictValuesObj); + + Jim_SetResult(interp, resultObj); + return JIM_OK; +} + /* [dict] */ static int Jim_DictCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { Jim_Obj *objPtr; int option; const char *options[] = { - "create", "get", "set", "unset", "exists", NULL + "create", "get", "set", "unset", "exists", "keys", NULL }; enum { - OPT_CREATE, OPT_GET, OPT_SET, OPT_UNSET, OPT_EXIST + OPT_CREATE, OPT_GET, OPT_SET, OPT_UNSET, OPT_EXIST, OPT_KEYS }; if (argc < 2) { @@ -12722,6 +12752,13 @@ static int Jim_DictCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *arg } return Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 3, NULL); + case OPT_KEYS: + if (argc != 3 && argc != 4) { + Jim_WrongNumArgs(interp, 2, argv, "dictVar ?pattern?"); + return JIM_ERR; + } + return Jim_DictKeys(interp, argv[2], argc == 4 ? argv[3] : NULL); + case OPT_CREATE: if (argc % 2) { Jim_WrongNumArgs(interp, 2, argv, "?key value ...?"); |