void lookup(const char* str) { //ignore unchanged str static char* old = 0; if(old) { if(!strcmp(str,old)) { return; } } free(old); old = calloc(strlen(str)+1,sizeof(char)); strcpy(old,str); //end ignore unchanged str if(db_search(str)){ return; } else if(search_for_stems(str) == -1) { return; } else { char *temp; temp = (char*) calloc(strlen(str) + strlen("…") + 1,sizeof(char)); strcpy(temp,str); strcpy(&temp[strlen(temp)],"…"); if(!db_search(temp)) { strcpy(temp,"…"); strcpy(&temp[strlen("…")],str); db_search(temp); } free(temp); } }
int search_for_stems(const char* str) { int i=0; for(i;i<TRANSFORMATION_ENTRYS_COUNT;i++) { if(strlen(str)>strlen(stem_transformation[i*3]) ) { char* last= (char*) &str[ strlen(str)- strlen(stem_transformation[i*3]) ]; if(strcmp(last,stem_transformation[i*3]) == 0) { int stem_lenght = strlen(str) - strlen(last); int new_suffix_lenght = strlen(stem_transformation[i*3+1]); char* temp = (char*) calloc( (stem_lenght + new_suffix_lenght + 1),sizeof(char) ); strncpy(temp,str,stem_lenght); strncpy((char*)&temp[stem_lenght], stem_transformation[i*3+1], new_suffix_lenght); if(db_search(temp)) { free(temp); return -1; } free(temp); } } } return -2; }
static PyObject * PongoDict_search(PongoDict *self, PyObject *args) { PyObject *path, *value, *ret=NULL; dbtype_t dbpath, dbvalue, dbrslt; char *rel; char *sep = "."; int decpath = 0; relop_t relop; if (!PyArg_ParseTuple(args, "OsO:search", &path, &rel, &value)) return NULL; if (!strcmp(rel, "==") || !strcmp(rel, "eq")) { relop = db_EQ; } else if (!strcmp(rel, "!=") || !strcmp(rel, "ne")) { relop = db_NE; } else if (!strcmp(rel, "<") || !strcmp(rel, "lt")) { relop = db_LT; } else if (!strcmp(rel, "<=") || !strcmp(rel, "le")) { relop = db_LE; } else if (!strcmp(rel, ">") || !strcmp(rel, "gt")) { relop = db_GT; } else if (!strcmp(rel, ">=") || !strcmp(rel, "ge")) { relop = db_GE; } else { PyErr_Format(PyExc_ValueError, "Unknown relop '%s'", rel); return NULL; } if (PyString_Check(path)) { path = PyObject_CallMethod(path, "split", "s", sep); decpath = 1; } if (!PySequence_Check(path)) { PyErr_Format(PyExc_TypeError, "path must be a sequence"); return NULL; } dblock(self->ctx); dbpath = from_python(self->ctx, path); if (decpath) Py_DECREF(path); if (dbtype(self->ctx, dbpath) == List) { dbvalue = from_python(self->ctx, value); if (!PyErr_Occurred()) { dbrslt = dbcollection_new(self->ctx, 0); db_search(SELF_CTX_AND_DBPTR, dbpath, -1, relop, dbvalue, dbrslt); // PROXYCHLD means turn the root object into a real dict, but // create proxy objects for all children. ret = to_python(self->ctx, dbrslt, TP_PROXYCHLD); } } else { PyErr_Format(PyExc_Exception, "path type isn't List (%d)", dbtype(self->ctx, dbpath)); } dbunlock(self->ctx); return ret; }
int main() { //struct DBC dbc = {128*1024*1024, 2046, 16*1024*1024}; //struct DB *dbn = dbcreate("mydb", &dbc); //struct DB db = *dbn; struct DB db; db_init(&db, "mydb", 2048, 128*1024*1024, 16*1024*1024); cache_print(db.pool->cache); db_print(&db); db_insert(&db, "1234", "Hello, world1", 13); db_insert(&db, "1235", "Hello, world2", 13); db_insert(&db, "1236", "Hello, world3", 13); db_insert(&db, "1237", "Hello, world4", 13); db_insert(&db, "1238", "Hello, world5", 13); cache_print(db.pool->cache); db_insert(&db, "1232", "Hello, world6", 13); db_insert(&db, "1231", "Hello, world7", 13); db_insert(&db, "1240", "Hello, world8", 13); db_insert(&db, "1232", "Hello, world6", 13); db_insert(&db, "1251", "Hello, world7", 13); db_insert(&db, "1252", "Hello, world8", 13); db_insert(&db, "1253", "Hello, world6", 13); db_insert(&db, "1254", "Hello, world7", 13); cache_print(db.pool->cache); db_insert(&db, "1255", "Hello, world8", 13); db_insert(&db, "1253", "Hello, world6", 13); db_insert(&db, "1254", "Hello, world7", 13); db_insert(&db, "1255", "Hello, world8", 13); db_insert(&db, "1256", "Hello, world8", 13); db_insert(&db, "1257", "Hello, world7", 13); db_insert(&db, "1258", "Hello, world8", 13); db_insert(&db, "1259", "Hello, world6", 13); cache_print(db.pool->cache); db_insert(&db, "1260", "Hello, world8", 13); db_insert(&db, "1210", "Hello, world7", 13); db_insert(&db, "1209", "Hello, world8", 13); db_insert(&db, "1208", "Hello, world6", 13); db_insert(&db, "1207", "Hello, world8", 13); db_insert(&db, "1206", "Hello, world6", 13); db_insert(&db, "1205", "Hello, world8", 13); cache_print(db.pool->cache); db_insert(&db, "1204", "Hello, world7", 13); db_insert(&db, "1203", "Hello, world8", 13); db_insert(&db, "1202", "Hello, world6", 13); db_insert(&db, "1208", "Hello, world8", 13); db_insert(&db, "1209", "Hello, world6", 13); db_insert(&db, "121000", "Hello, world8", 13); /*db_delete(&db, "121000");*/ db_print(&db); cache_print(db.pool->cache); char *data = NULL; size_t val = 0; db_search(&db, "1231", (void **)&data, &val); printf("\n%13s\n", data); db_free(&db); return 0; }
static void _search_paths (GList **paths, const char *path) { GList *children, *iter; children = db_search (path); for (iter = children; iter; iter = g_list_next (iter)) { _search_paths (paths, (const char *) iter->data); } *paths = g_list_concat (children, *paths); }
static GList * search_path (const char *path) { GList *results = NULL; GList *iter = NULL; /* Proxy first */ results = proxy_search (path); if (!results) { /* Indexers second */ if (index_get (path, &results) == true) { DEBUG (" (index result:)\n"); } else { /* Search database next */ results = db_search (path); /* Append any provided paths */ GList *providers = NULL; providers = cb_match (&provide_list, path, CB_MATCH_PART); for (iter = providers; iter; iter = g_list_next (iter)) { cb_info_t *provider = iter->data; int len = strlen (path); /* If there is a provider for a single node below here it may * show as a "*" entry in this list, which is not desirable */ if (strlen (provider->path) > strlen (path) && provider->path[strlen (path)] == '*') { continue; } char *ptr, *provider_path = g_strdup (provider->path); if ((ptr = strchr (&provider_path[len ? len : len+1], '/')) != 0) *ptr = '\0'; if (!g_list_find_custom (results, provider_path, (GCompareFunc) strcmp)) results = g_list_prepend (results, provider_path); else g_free (provider_path); } g_list_free_full (providers, (GDestroyNotify) cb_release); } } return results; }
/* this finds references to 'obj' that are not within the 'topobj' * combination hierarchy, elsewhere in the database. this is so we * can make sure we don't kill objects that are referenced somewhere * else in the database. */ HIDDEN int find_reference(struct db_i *dbip, const char *topobj, const char *obj) { int ret; struct bu_vls str = BU_VLS_INIT_ZERO; if (!dbip || !topobj || !obj) return 0; /* FIXME: these should be wrapped in quotes, but need to dewrap * after bu_argv_from_string(). */ bu_vls_printf(&str, "-depth >0 -not -below -name %s -name %s", topobj, obj); ret = db_search(NULL, DB_SEARCH_TREE, bu_vls_cstr(&str), 0, NULL, dbip); bu_vls_free(&str); return ret; }
/*ARGSUSED*/ void db_search_cmd(db_expr_t daddr, int have_addr, db_expr_t dcount, char *modif) { int t; db_addr_t addr; int size; db_expr_t value; db_expr_t mask; db_expr_t count; t = db_read_token(); if (t == tSLASH) { t = db_read_token(); if (t != tIDENT) { bad_modifier: db_printf("Bad modifier\n"); db_flush_lex(); return; } if (!strcmp(db_tok_string, "b")) size = 1; else if (!strcmp(db_tok_string, "h")) size = 2; else if (!strcmp(db_tok_string, "l")) size = 4; else goto bad_modifier; } else { db_unread_token(t); size = 4; } if (!db_expression(&value)) { db_printf("Address missing\n"); db_flush_lex(); return; } addr = (db_addr_t) value; if (!db_expression(&value)) { db_printf("Value missing\n"); db_flush_lex(); return; } if (!db_expression(&mask)) mask = (int) ~0; t = db_read_token(); if (t == tCOMMA) { if (!db_expression(&count)) { db_printf("Count missing\n"); db_flush_lex(); return; } } else { db_unread_token(t); count = -1; /* forever */ } db_skip_to_eol(); db_search(addr, size, value, mask, count); }
int ged_lc(struct ged *gedp, int argc, const char *argv[]) { char *file_name = NULL; int file_name_flag_cnt = 0; int sort_column = 1; int sort_column_flag_cnt = 0; int find_duplicates_flag = 0; int skip_special_duplicates_flag = 0; int skip_subtracted_regions_flag = 0; int descending_sort_flag = 0; int unrecognized_flag_cnt = 0; int orig_argc; const char **orig_argv; static const char *usage = "[-d|-s] [-r] [-z] [-0|-1|-2|-3|-4|-5] [-f {FileName}] {GroupName}"; int c; int error_cnt = 0; FILE *outfile = NULL; struct bu_vls *output; const char *group_name; size_t i,j; struct bu_ptbl results1 = BU_PTBL_INIT_ZERO; struct bu_ptbl results2 = BU_PTBL_INIT_ZERO; char *path; char *plan; struct db_full_path root; int matches; struct region_record *regions; size_t ignored_cnt = 0; /* The defaults are the minimum widths to accommodate column headers */ size_t region_id_len_max = 2; size_t material_id_len_max = 3; size_t los_len_max = 3; size_t obj_len_max = 6; /* For the output at the end */ size_t start, end, incr; /* initialize result */ bu_vls_trunc(gedp->ged_result_str, 0); if (argc == 1) { bu_vls_printf(gedp->ged_result_str, "Usage: %s\n", usage); return GED_HELP; } GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR); bu_optind = 1; /* re-init bu_getopt() */ while ((c = bu_getopt(argc, (char * const *)argv, "dsrz012345f:")) != -1) { switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': sort_column_flag_cnt++; sort_column = c - '0'; break; case 'f': file_name_flag_cnt++; file_name = bu_optarg; break; case 's': skip_special_duplicates_flag = 1; /* FALLTHROUGH */ case 'd': find_duplicates_flag = 1; break; case 'r': skip_subtracted_regions_flag = 1; break; case 'z': descending_sort_flag = 1; break; default: unrecognized_flag_cnt++; } } orig_argc = argc; orig_argv = argv; argc -= (bu_optind - 1); argv += (bu_optind - 1); /* Attempt to recreate the exact error messages from the original lc.tcl */ if (file_name_flag_cnt > 1) { bu_vls_printf(gedp->ged_result_str, "Error: '-f' used more than once.\n"); error_cnt++; } if (sort_column_flag_cnt > 1) { bu_vls_printf(gedp->ged_result_str, "Error: Sort column defined more than once.\n"); error_cnt++; } if (file_name_flag_cnt + argc + unrecognized_flag_cnt > 3) { bu_vls_printf(gedp->ged_result_str, "Error: More than one group name and/or file name was specified.\n"); error_cnt++; } else if (argc < 2) { if (file_name_flag_cnt && !file_name) { bu_vls_printf(gedp->ged_result_str, "Error: Group name and file name not specified\n"); } else { bu_vls_printf(gedp->ged_result_str, "Error: Group name not specified.\n"); } error_cnt++; } else if (argc + unrecognized_flag_cnt > 2) { bu_vls_printf(gedp->ged_result_str, "Error: More than one group name was specified.\n"); error_cnt++; } else if (file_name_flag_cnt && !file_name) { bu_vls_printf(gedp->ged_result_str, "Error: File name not specified.\n"); error_cnt++; } if (file_name) { char *norm_name; norm_name = bu_realpath(file_name, NULL); if (file_name[0] == '-') { bu_vls_printf(gedp->ged_result_str, "Error: File name can not start with '-'.\n"); error_cnt++; } else if (bu_file_exists(file_name, NULL)) { bu_vls_printf(gedp->ged_result_str, "Error: Output file %s already exists.\n",norm_name); error_cnt++; } else { outfile = fopen(file_name, "w"); if (!outfile) { bu_vls_printf(gedp->ged_result_str, "Error: %d\n", errno); error_cnt++; } } bu_vls_printf(gedp->ged_result_str, "Output filename: %s\n", norm_name); bu_free(norm_name, "ged_lc"); output = bu_vls_vlsinit(); } else { output = gedp->ged_result_str; } if (error_cnt > 0) { return GED_ERROR; } group_name = argv[1]; /* The 7 is for the "-name" and '\0' */ plan = (char *) bu_malloc(sizeof(char) * (strlen(group_name) + 7), "ged_lc"); sprintf(plan, "-name %s", group_name); matches = db_search(&results1, DB_SEARCH_TREE, plan, 0, NULL, gedp->ged_wdbp->dbip); if (matches < 1) { bu_vls_printf(gedp->ged_result_str, "Error: Group '%s' does not exist.\n", group_name); return GED_ERROR; } bu_free(plan, "ged_lc"); db_search_free(&results1); if (skip_subtracted_regions_flag) { plan = "-type region ! -bool -"; } else { plan = "-type region"; } path = (char *) bu_malloc(sizeof(char) * (strlen(group_name) + 2), "ged_lc"); sprintf(path, "/%s", group_name); db_string_to_path(&root, gedp->ged_wdbp->dbip, path); matches = db_search(&results2, DB_SEARCH_TREE, plan, root.fp_len, root.fp_names, gedp->ged_wdbp->dbip); bu_free(path, "ged_lc"); if (matches < 1) { return GED_ERROR; } regions = (struct region_record *) bu_malloc(sizeof(struct region_record) * BU_PTBL_LEN(&results2), "ged_lc"); for (i = 0; i < BU_PTBL_LEN(&results2); i++) { struct db_full_path *entry = (struct db_full_path *)BU_PTBL_GET(&results2, i); struct directory *dp_curr_dir = DB_FULL_PATH_CUR_DIR(entry); struct bu_attribute_value_set avs; j = BU_PTBL_LEN(&results2) - i - 1 ; regions[j].ignore = 0; bu_avs_init_empty(&avs); db5_get_attributes(gedp->ged_wdbp->dbip, &avs, dp_curr_dir); regions[j].region_id = get_attr(&avs, "region_id"); V_MAX(region_id_len_max, strlen(regions[j].region_id)); regions[j].material_id = get_attr(&avs, "material_id"); V_MAX(material_id_len_max, strlen(regions[j].material_id)); regions[j].los = get_attr(&avs, "los"); V_MAX(los_len_max, strlen(regions[j].los)); regions[j].obj_name = dp_curr_dir->d_namep; V_MAX(obj_len_max, strlen(regions[j].obj_name)); if (entry->fp_len > 1) { struct directory *dp_parent = DB_FULL_PATH_GET(entry, entry->fp_len - 2); regions[j].obj_parent = dp_parent->d_namep; } else { regions[j].obj_parent = "--"; } } if (find_duplicates_flag) { bu_sort((void *) regions, BU_PTBL_LEN(&results2), sizeof(struct region_record), cmp_regions, NULL); for (i = 1; i < BU_PTBL_LEN(&results2); i++) { int same; if (skip_special_duplicates_flag) { same = !cmp_regions((void *)&(regions[i - 1]), (void *)&(regions[i]), NULL); } else { same = !bu_strcmp(regions[i - 1].region_id, regions[i].region_id); } if (same) { regions[i].ignore = 1; ignored_cnt++; } } if (ignored_cnt == BU_PTBL_LEN(&results2)) { if (file_name) { print_cmd_args(output, orig_argc, orig_argv); bu_vls_printf(output, "No duplicate region_id\n"); bu_vls_fwrite(outfile, output); fclose(outfile); } bu_vls_printf(gedp->ged_result_str, "No duplicate region_id\n"); bu_vls_printf(gedp->ged_result_str, "Done.\n"); bu_free(regions, "ged_lc"); return GED_ERROR; } } if (sort_column > 0) { bu_sort((void *) regions, BU_PTBL_LEN(&results2), sizeof(struct region_record), sort_regions, (void *)&sort_column); } if (file_name) { print_cmd_args(output, orig_argc, orig_argv); } bu_vls_printf(output, "List length: %d\n", BU_PTBL_LEN(&results2) - ignored_cnt); bu_vls_printf(output, "%-*s %-*s %-*s %-*s %s\n", region_id_len_max + 1, "ID", material_id_len_max + 1, "MAT", los_len_max, "LOS", obj_len_max, "REGION", "PARENT"); end = BU_PTBL_LEN(&results2); if (descending_sort_flag) { start = end - 1; end = -1; incr = -1; } else { start = 0; incr = 1; } for (i = start; i != end; i += incr) { if (regions[i].ignore) { continue; } bu_vls_printf(output, "%-*s %-*s %-*s %-*s %s\n", region_id_len_max + 1, regions[i].region_id, material_id_len_max + 1, regions[i].material_id, los_len_max, regions[i].los, obj_len_max, regions[i].obj_name, regions[i].obj_parent); } bu_vls_printf(gedp->ged_result_str, "Done.\n"); if (file_name) { bu_vls_fwrite(outfile, output); fclose(outfile); } bu_free(regions, "ged_lc"); return GED_OK; }
static void _traverse_paths (GList **pvlist, const char *path) { GList *children, *iter; char *value = NULL; size_t vsize; /* Look for a value - db first */ if (!db_get (path, (unsigned char**)&value, &vsize)) { /* Provide next */ value = provide_get (path); } if (value) { Apteryx__PathValue *pv = NULL; /* Allocate a new pv */ pv = g_malloc0 (sizeof (Apteryx__PathValue)); pv->path = g_strdup (path); pv->value = value; /* Add to the list */ *pvlist = g_list_prepend (*pvlist, pv); } /* Check for children - index first */ char *path_s = g_strdup_printf ("%s/", path); if (!index_get (path_s, &children)) { /* Search database next */ children = db_search (path_s); /* Append any provided paths */ GList *providers = NULL; providers = cb_match (&provide_list, path_s, CB_MATCH_PART); for (iter = providers; iter; iter = g_list_next (iter)) { cb_info_t *provider = iter->data; char *ptr, *ppath; int len = strlen (path_s); if (strcmp (provider->path, path_s) == 0) continue; ppath = g_strdup (provider->path); if ((ptr = strchr (&ppath[len+1], '/')) != 0) *ptr = '\0'; if (!g_list_find_custom (children, ppath, (GCompareFunc) strcmp)) children = g_list_prepend (children, ppath); else g_free (ppath); } g_list_free_full (providers, (GDestroyNotify) cb_release); } for (iter = children; iter; iter = g_list_next (iter)) { _traverse_paths (pvlist, (const char *) iter->data); } g_list_free_full (children, g_free); g_free (path_s); }
/* * Search for a value in memory. * Syntax: search [/bhl] addr value [mask] [,count] [thread] */ void db_search_cmd(void) { int t; db_addr_t addr; int size = 0; db_expr_t value; db_expr_t mask; db_addr_t count; thread_act_t thr_act; boolean_t thread_flag = FALSE; register char *p; t = db_read_token(); if (t == tSLASH) { t = db_read_token(); if (t != tIDENT) { bad_modifier: db_printf("Bad modifier \"/%s\"\n", db_tok_string); db_flush_lex(); return; } for (p = db_tok_string; *p; p++) { switch(*p) { case 'b': size = sizeof(char); break; case 'h': size = sizeof(short); break; case 'l': size = sizeof(long); break; case 't': thread_flag = TRUE; break; default: goto bad_modifier; } } } else { db_unread_token(t); size = sizeof(int); } if (!db_expression((db_expr_t *) &addr)) { db_printf("Address missing\n"); db_flush_lex(); return; } if (!db_expression(&value)) { db_printf("Value missing\n"); db_flush_lex(); return; } if (!db_expression(&mask)) mask = ~0; t = db_read_token(); if (t == tCOMMA) { if (!db_expression((db_expr_t *) &count)) { db_printf("Count missing\n"); db_flush_lex(); return; } } else { db_unread_token(t); count = -1; /* effectively forever */ } if (thread_flag) { if (!db_get_next_act(&thr_act, 0)) return; } else thr_act = THR_ACT_NULL; db_search(addr, size, value, mask, count, db_act_to_task(thr_act)); }
int db_get(struct DB *db, void *key, size_t key_len, void **val, size_t *val_len) { return db_search(db, key, val, val_len); }