uint8_t * db_js_lookup(const char * model, const char * jstag) { struct db_dev_model * mdl; struct cmd_list * lst; struct db_info * inf; int tagid; int nmid; int idx; int j; if ((inf = db_info_get()) == NULL) return NULL; DCC_LOG(LOG_TRACE, "1. str_lookup()"); if ((tagid = str_lookup(jstag, strlen(jstag))) < 0) { DCC_LOG(LOG_WARNING, "tag not found!"); return NULL; } DCC_LOG(LOG_TRACE, "2. str_lookup()"); if ((nmid = str_lookup(model, strlen(model))) < 0) { DCC_LOG(LOG_WARNING, "model name not found!"); return NULL; } DCC_LOG(LOG_TRACE, "3. db_dev_model_index_by_name()"); idx = db_dev_model_index_by_name(inf, nmid); if ((mdl = db_dev_model_by_index(inf, idx)) == NULL) { DCC_LOG(LOG_WARNING, "device not found!"); return NULL; } if ((lst = mdl->cmd) == NULL) { DCC_LOG(LOG_WARNING, "command list empty!"); return NULL; } DCC_LOG(LOG_TRACE, "4. lookup..."); for (j = 0; j < lst->cnt; ++j) { struct cmd_entry * cmd = &lst->cmd[j]; if (cmd->tag == tagid) return cmd->code; } DCC_LOG(LOG_WARNING, "command not found!"); return NULL; }
void * str_dict_get(struct dict * h, char * key) { struct dict_entry * e = str_lookup(h, key); if (e) { return (e -> value).ptr; } else { return NULL; } }
void str_dict_set(struct dict * h, char * key, void * value) { struct dict_entry * entry ; unsigned int hashval; hashval = str_hash(h, key); // associate key with value in hash if (!(entry = str_lookup(h, key))) { // not in the hash entry = (struct dict_entry *) malloc(sizeof(struct dict_entry)); assert(entry); // Ensure that entry has been allocated. entry -> next = (h -> hashtab)[hashval]; // Make the head of the entry linked list (h -> hashtab)[hashval] = entry; // Make this the first element seen when looked up in the hashtab array. } (entry -> value).ptr = value; entry -> key = malloc((strlen(key) + 1) * sizeof(char)); assert(entry -> key); strcpy(entry -> key, key); }
void str_dict_delete(struct dict * h, char * key) { if (str_lookup(h, key)) { // Key has to exist to be deleted struct dict_entry * entry = NULL, * temp = NULL; if ((entry = h -> hashtab[str_hash(h, key)]) && (h -> hashtab[str_hash(h, key)]) -> key == key) { // First element in the hashtab h -> hashtab[str_hash(h, key)] = entry -> next; return; } for (entry = (h -> hashtab[str_hash(h, key)]); entry -> next != NULL; entry = entry -> next) { if (entry -> next -> key == key) { temp = entry -> next; // Found the entry: delete it entry -> next = temp -> next; } } } }
static int strtoLocal(struct chain *table, char str[]) { struct local_var *lv = (struct local_var *)str_lookup(_lvar_cmp, table, str); return (lv != NULL)? lv->index : _NO_SUCH_LOCAL; }
static struct expr_var *strtoVar(struct chain *table, char str[]) { return (struct expr_var *)str_lookup(_var_cmp, table, str); }
static struct expr_const *strtoConst(struct expr_environ *env, char str[]) { return (struct expr_const*)str_lookup(_ct_cmp, &env->ctes, str); }
static struct expr_func *strtoFun(struct expr_environ *env, char str[]) { return (struct expr_func*)str_lookup(_fn_cmp, &env->funcs, str); }
/// Like `str_lookup` but assuming value is a string. char *str_gets(char** substs, const char *name) { return (char*)str_lookup(substs,name); }