void hash_table_print(HashTable* ht) { printf("size: %zu\n", ht->num_elements); printf("buckets: %zu\n", ht->size); printf("load factor: %.2f\n", ((double)((double)ht->num_elements / (double)ht->size))); printf("data:\n"); size_t i; for(i = 0; i < ht->size; i++) { printf("[%zu]: ", i); if(ht->buckets[i] != NULL) { Bucket *b = ht->buckets[i]; while(b != NULL) { printf("["); printf("%s, ", b->key); var_print(b->value); printf("] "); //printf("index=%lu, hash=%lu, type=%s, key=%s, value=", (unsigned long)(b->hash % ht->size), (unsigned long)b->hash, type, b->key); //var_print(b->value); //printf(", "); b = b->next; } } else { printf("%s", "null"); } printf("\n"); } }
/* * Add given variable with given value to file, overwriting any * previous occurrence. */ int var_set(const char *fname, const char *variable, const char *value) { FILE *fp; FILE *fout; char *tmpname; int fd; char *line; size_t len; size_t varlen; Boolean done; struct stat st; varlen = strlen(variable); if (varlen == 0) return 0; fp = fopen(fname, "r"); if (fp == NULL) { if (errno != ENOENT) { warn("var_set: can't open '%s' for reading", fname); return -1; } if (value == NULL) return 0; /* Nothing to do */ } tmpname = xasprintf("%s.XXXXXX", fname); if ((fd = mkstemp(tmpname)) < 0) { free(tmpname); if (fp != NULL) fclose(fp); warn("var_set: can't open temp file for '%s' for writing", fname); return -1; } if (chmod(tmpname, 0644) < 0) { close(fd); if (fp != NULL) fclose(fp); free(tmpname); warn("var_set: can't set permissions for temp file for '%s'", fname); return -1; } if ((fout=fdopen(fd, "w")) == NULL) { close(fd); remove(tmpname); free(tmpname); if (fp != NULL) fclose(fp); warn("var_set: can't open temp file for '%s' for writing", fname); return -1; } done = FALSE; if (fp) { while ((line = fgetln(fp, &len)) != (char *) NULL) { if (var_cmp(line, len, variable, varlen) == NULL) fprintf(fout, "%.*s", (int)len, line); else { if (!done && value) { var_print(fout, variable, value); done = TRUE; } } } (void) fclose(fp); } if (!done && value) var_print(fout, variable, value); if (fclose(fout) < 0) { free(tmpname); warn("var_set: write error for '%s'", fname); return -1; } if (stat(tmpname, &st) < 0) { free(tmpname); warn("var_set: cannot stat tempfile for '%s'", fname); return -1; } if (st.st_size == 0) { if (remove(tmpname) < 0) { free(tmpname); warn("var_set: cannot remove tempfile for '%s'", fname); return -1; } free(tmpname); if (remove(fname) < 0) { warn("var_set: cannot remove '%s'", fname); return -1; } return 0; } if (rename(tmpname, fname) < 0) { free(tmpname); warn("var_set: cannot move tempfile to '%s'", fname); return -1; } free(tmpname); return 0; }
void hash_table_print_element(HashTable* ht, Bucket *b) { printf("index=%lu, hash=%lu, key=%s, value=", (unsigned long)(b->hash % ht->size), (unsigned long)b->hash, b->key); var_print(b->value); printf("\n"); }
int wam_run_query(wam_t *wam, char *query_str) { if (!strcmp(query_str, "quit.") || !strcmp(query_str, "exit.")) { return 0; } else if (!strcmp(query_str, "labels.")) { int i; for (i = 0; i < wam->prog->nstmt; i++) if (wam->prog->stmts[i]->label[0]) printf("%s\n", wam->prog->stmts[i]->label); return 1; } else if (!strcmp(query_str, "procedures.")) { int i; for (i = 0; i < wam->prog->nstmt; i++) { char *label = wam->prog->stmts[i]->label; if (strlen(label) > 0 && indexof(label, '~') < 0) printf("%s\n", label); } return 1; } else if (!strcmp(query_str, "list.")) { prog_info(wam->prog); return 1; } else if (!strcmp(query_str, "reset.")) { wam_reset(wam); return 1; } else if (!strcmp(query_str, "help.")) { printf("sorry there is no help.\n"); return 1; } if (strlen(query_str) == 0) return 1; wam_reset(wam); prog_del_from_label(wam->prog, "query$"); prog_t *prog = compile_query(query_str); prog_add_prog(wam->prog, prog); prog_update_label(wam->prog); wam->pc = prog_locate_label(wam->prog, "query$"); while (1) { wam_run(wam); if (wam->failed) { printf("NO.\n"); } else { printf("YES.\n"); int i; for (i = 0; i < MAX_VAR_CNT; i++) { if (wam->qvars[i] != NULL) { if (wam->qvars[i]->display) { var_print(wam->qvars[i]); } } } } if (wam->cp != NULL) { char ans[MAX_WORD_LEN]; printf("more? [Y]es, [N]o\n"); scanf("%s", ans); if (ans[0] == 'Y' || ans[0] == 'y') wam_backtrack(wam); else break; } else break; } wam_reset(wam); return 1; }