示例#1
0
struct t_hashtable *
weechat_tcl_dict_to_hashtable (Tcl_Interp *interp, Tcl_Obj *dict,
                               int hashtable_size)
{
    struct t_hashtable *hashtable;
    Tcl_DictSearch search;
    Tcl_Obj *key, *value;
    int done;

    hashtable = weechat_hashtable_new (hashtable_size,
                                       WEECHAT_HASHTABLE_STRING,
                                       WEECHAT_HASHTABLE_STRING,
                                       NULL,
                                       NULL);
    if (!hashtable)
        return NULL;

    if (Tcl_DictObjFirst (interp, dict, &search, &key, &value, &done) == TCL_OK)
    {
        for (; !done ; Tcl_DictObjNext(&search, &key, &value, &done))
        {
            weechat_hashtable_set (hashtable,
                                   Tcl_GetString (key),
                                   Tcl_GetString (value));
        }
    }
    Tcl_DictObjDone(&search);

    return hashtable;
}
示例#2
0
/*
 * Process worker keyword arguments.
 * Only modifies arguments if the keyword arg was encountered, i.e.
 * caller should initialise the arguments to their default values.
 *
 * This will validate any values received.
 */
static int
worker_keyword_args(Tcl_Interp *interp, Tcl_Obj *const objv[],
                  Tcl_Obj *dict, int *buffer_count, int *buffer_size) {
  int rc;
  Tcl_DictSearch search;
  Tcl_Obj *key_obj, *val_obj;
  int done;

  rc = Tcl_DictObjFirst(interp, dict, &search, &key_obj, &val_obj,
                        &done);
  TCL_CHECK_MSG(rc, "Error iterating over dict: %s",
                Tcl_GetString(dict));

  for (; !done; Tcl_DictObjNext(&search, &key_obj, &val_obj, &done))
  {
    const char *key = Tcl_GetString(key_obj);
    if (strcmp(key, "buffer_count") == 0)
    {
      rc = Tcl_GetIntFromObj(interp, val_obj, buffer_count);
      TCL_CHECK_MSG(rc, "Expected integer value for buffer_count");

      TCL_CONDITION(*buffer_count >= 0, "Positive value for "
            "buffer_count expected, but got %i", *buffer_count);
    }
    else if (strcmp(key, "buffer_size") == 0)
    {
      rc = Tcl_GetIntFromObj(interp, val_obj, buffer_size);
      TCL_CHECK_MSG(rc, "Expected integer value for buffer_size");

      TCL_CONDITION(*buffer_size >= 0, "Positive value for buffer_size "
                                  "expected, but got %i", *buffer_size);
    }
    else
    {
      TCL_RETURN_ERROR("Invalid key for key-value argument: %s\n", key);
      return TCL_ERROR;
    }
  }

  Tcl_DictObjDone(&search);

  return TCL_OK;
}
示例#3
0
struct t_hashtable *
weechat_tcl_dict_to_hashtable (Tcl_Interp *interp, Tcl_Obj *dict,
                               int size, const char *type_keys,
                               const char *type_values)
{
    struct t_hashtable *hashtable;
    Tcl_DictSearch search;
    Tcl_Obj *key, *value;
    int done;

    hashtable = weechat_hashtable_new (size,
                                       type_keys,
                                       type_values,
                                       NULL,
                                       NULL);
    if (!hashtable)
        return NULL;

    if (Tcl_DictObjFirst (interp, dict, &search, &key, &value, &done) == TCL_OK)
    {
        for (; !done ; Tcl_DictObjNext(&search, &key, &value, &done))
        {
            if (strcmp (type_values, WEECHAT_HASHTABLE_STRING) == 0)
            {
                weechat_hashtable_set (hashtable,
                                       Tcl_GetString (key),
                                       Tcl_GetString (value));
            }
            else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0)
            {
                weechat_hashtable_set (hashtable,
                                       Tcl_GetString (key),
                                       plugin_script_str2ptr (weechat_tcl_plugin,
                                                              NULL, NULL,
                                                              Tcl_GetString (value)));
            }
        }
    }
    Tcl_DictObjDone(&search);

    return hashtable;
}