int iatom(char *conf_name, char *atom_name) { datum pkey, pvalue; char key[MAXCHAR_LINE]; char sbuff[MAXCHAR_LINE]; int i_atom; /* construct the atom key */ strcpy(key, "IATOM"); strip(sbuff, conf_name); strcat(key, sbuff); strip(sbuff, atom_name); strcat(key, sbuff); pkey.dptr = key; pkey.dsize = strlen(key); /* find the atom in the database */ if (gdbm_exists(param_db, pkey)) { /* existing key */ pvalue = gdbm_fetch(param_db, pkey); /* cast generic pointer pvalue.dptr to integer pointer by (int *), then * dereference the pointer to an integer by *, so the return value is * an integer */ i_atom = * (int *) pvalue.dptr; free(pvalue.dptr); return i_atom; } else return -1; /* not in the database */ }
static int dbm_contains(PyObject *self, PyObject *arg) { dbmobject *dp = (dbmobject *)self; datum key; Py_ssize_t size; if ((dp)->di_dbm == NULL) { PyErr_SetString(DbmError, "GDBM object has already been closed"); return -1; } if (PyUnicode_Check(arg)) { key.dptr = PyUnicode_AsUTF8AndSize(arg, &size); key.dsize = size; if (key.dptr == NULL) return -1; } else if (!PyBytes_Check(arg)) { PyErr_Format(PyExc_TypeError, "gdbm key must be bytes or string, not %.100s", arg->ob_type->tp_name); return -1; } else { key.dptr = PyBytes_AS_STRING(arg); key.dsize = PyBytes_GET_SIZE(arg); } return gdbm_exists(dp->di_dbm, key); }
int param_get(char *key1, char *key2, char *key3, void *value) /* WARNING: if the expected value is a string, it has to be long enough * (>=MAXCHAR_LINE) to accept the stored value to avoid over * boundary writing. */ { datum pkey, pvalue; char key[MAXCHAR_LINE]; char sbuff[MAXCHAR_LINE]; /* convert 3 key strings to one key, leading and ending spaces stripped */ strip(key, key1); strip(sbuff, key2); strcat(key, sbuff); strip(sbuff, key3); strcat(key, sbuff); /* make the key string to be the database search key */ pkey.dptr = key; pkey.dsize = strlen(key); /* get the value */ if (!gdbm_exists(param_db, pkey)) return -1; /* failure */ else { /* success */ pvalue = gdbm_fetch(param_db, pkey); memcpy(value, pvalue.dptr, pvalue.dsize); free(pvalue.dptr); return 0; } }
static PyObject * dbm_has_key(register dbmobject *dp, PyObject *args) { datum key; if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &key.dsize)) return NULL; check_dbmobject_open(dp); return PyInt_FromLong((long) gdbm_exists(dp->di_dbm, key)); }
int main(int argc,char **argv) { static GDBM_FILE gdbm_stotest = NULL; datum key,data; int i; for(i = 1;i<argc;i++) { key.dptr = "store test1!"; key.dsize = strlen("store test1")+1; data = key; gdbm_stotest = db_open(argv[i]); printf("\n--------open dbm id:%d-----------\n",gdbm_stotest); if(db_store(gdbm_stotest,key,data) < 0) { printf("\n---------store err-----------\n"); db_close(gdbm_stotest); break; } else { printf("\n---------store successfully!-----------\n"); db_close(gdbm_stotest); } printf("\n--------close dbm id:%d-----------\n",gdbm_stotest); key.dptr = "store test2!"; key.dsize = strlen("store test1")+1; data = key; gdbm_stotest = db_open(argv[i]); printf("\n--------open dbm id:%d-----------\n",gdbm_stotest); if(db_store(gdbm_stotest,key,data) < 0) { printf("\n---------store err-----------\n"); db_close(gdbm_stotest); break; } else { printf("\n---------store successfully!-----------\n"); db_close(gdbm_stotest); } printf("\n--------close dbm id:%d-----------\n",gdbm_stotest); gdbm_stotest = db_open(argv[i]); key.dptr = "store test1!"; key.dsize = strlen("store test1!")+1; data = key; if(gdbm_exists(gdbm_stotest,key) != 0){ printf("-------really exist here!-----"); } else { printf("-------not exist here!-------"); } } }
void index_file(char *path, void* db_) { GDBM_FILE db = db_; datum key = {path, strlen(path)+1}; int skip; #pragma omp critical skip = gdbm_exists(db, key); if(skip) { free(path); return; } struct feature *features; int num_features; struct kd_node *kd_tree; char *data; size_t data_size; if(verbose) tprintf("%s\n", path); if(!sift(path, &features, &num_features)) { free(path); return; } if(verbose) tprintf(" %d features detected\n", num_features); kd_tree = kdtree_build(features, num_features); pack(features, num_features, kd_tree, &data, &data_size); free(features); kdtree_release(kd_tree); datum value = {data, data_size}; #pragma omp critical gdbm_store(db, key, value, GDBM_REPLACE); free(data); free(path); }
static char * gdbmgetfn(Param pm) { datum key, content; int ret; GDBM_FILE dbf; key.dptr = pm->node.nam; key.dsize = strlen(key.dptr) + 1; dbf = (GDBM_FILE)(pm->u.hash->tmpdata); ret = gdbm_exists(dbf, key); if(ret) { content = gdbm_fetch(dbf, key); } else { content.dptr = dupstring(""); } return content.dptr; }
bool authenticate_author(Request req) { GDBM_FILE list; datum key; int rc; assert(req != NULL); assert(req->author != NULL); if (c_authorfile == NULL) return strcmp(req->author,c_author) == 0; list = gdbm_open(c_authorfile,DB_BLOCK,GDBM_READER,0,dbcritical); if (list == NULL) return false; key.dptr = req->author; key.dsize = strlen(req->author) + 1; rc = gdbm_exists(list,key); gdbm_close(list); return rc; }
int param_exist(char *key1, char *key2, char *key3) { datum pkey; char key[MAXCHAR_LINE]; char sbuff[MAXCHAR_LINE]; /* convert 3 key strings to one key, leading and ending spaces stripped */ strip(key, key1); strip(sbuff, key2); strcat(key, sbuff); strip(sbuff, key3); strcat(key, sbuff); /* make the key string to be the database search key */ pkey.dptr = key; pkey.dsize = strlen(key); /* get the value */ if (!gdbm_exists(param_db, pkey)) { return 0; /* failure */ } else { return 1; /* success */ } }
static int vt_gdbm_exists(apr_dbm_t *dbm, apr_datum_t key) { datum *ckey = (datum *)&key; return gdbm_exists(dbm->file, *ckey) != 0; }
/* * execute the 'index' function */ int sblib_func_exec(int index, int param_count, slib_par_t *params, var_t *retval) { int success = 0; switch ( index ) { case 0: // handle <- GDBM_OPEN(file[, block_size, flags, mode]) { char *file; int bsize, flags, mode; success = mod_parstr_ptr(0, params, param_count, &file); success = mod_opt_parint(1, params, param_count, &bsize, 0); success = mod_opt_parint(2, params, param_count, &flags, GDBM_WRCREAT); success = mod_opt_parint(3, params, param_count, &mode, 0666); if ( success ) { int handle; handle = get_free_handle(); if ( handle >= 0 ) { table[handle].dbf = gdbm_open(file, bsize, flags, mode, NULL); success = (table[handle].dbf != NULL); if ( success ) v_setint(retval, handle); else v_setstr(retval, gdbm_strerror(gdbm_errno)); } else { success = 0; v_setstr(retval, "GDBM_OPEN: NO FREE HANDLES"); } } else v_setstr(retval, "GDBM_OPEN: argument error"); } break; case 1: // handle <- GDBM_STORE(handle, key, data [, flags]) { int handle, flags; char *key, *data; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); success = mod_parstr_ptr(2, params, param_count, &data); success = mod_opt_parint(3, params, param_count, &flags, GDBM_REPLACE); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key, dt_data; int r; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; dt_data.dptr = data; dt_data.dsize = strlen(data) + 1; r = gdbm_store(table[handle].dbf, dt_key, dt_data, flags); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_STORE: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_STORE: argument error"); } break; case 2: // data <- GDBM_FETCH(handle, key) { int handle; char *key; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key, dt_data; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; dt_data = gdbm_fetch(table[handle].dbf, dt_key); v_setstr(retval, (char *) dt_data.dptr); success = 1; } else { success = 0; v_setstr(retval, "GDBM_FETCH: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_FETCH: argument error"); } break; case 3: // status <- GDBM_DELETE(handle, key) { int handle; char *key; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key; int r; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; r = gdbm_delete(table[handle].dbf, dt_key); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_DELETE: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_DELETE: argument error"); } break; case 4: // key <- GDBM_FIRSTKEY(handle) { int handle; success = mod_parint (0, params, param_count, &handle); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key; dt_key = gdbm_firstkey(table[handle].dbf); v_setstr(retval, (char *) dt_key.dptr); success = 1; } else { success = 0; v_setstr(retval, "GDBM_FIRSTKEY: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_FIRSTKEY: argument error"); } break; case 5: // key <- GDBM_NEXTKEY(handle, key) { int handle; char *key; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; dt_key = gdbm_nextkey(table[handle].dbf, dt_key); v_setstr(retval, (char *) dt_key.dptr); success = 1; } else { success = 0; v_setstr(retval, "GDBM_NEXTKEY: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_NEXTKEY: argument error"); } break; case 6: // status <- GDBM_REORGANIZE(handle) { int handle; success = mod_parint (0, params, param_count, &handle); if ( success ) { if ( is_valid_handle(handle) ) { int r; r = gdbm_reorganize(table[handle].dbf); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_REORGANIZE: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_REORGANIZE: argument error"); } break; case 7: // status <- GDBM_EXISTS(handle, key) { int handle; char *key; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key; int r; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; r = gdbm_exists(table[handle].dbf, dt_key); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_EXISTS: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_EXISTS: argument error"); } break; case 8: // str <- GDBM_STRERROR() v_setstr(retval, gdbm_strerror(gdbm_errno)); break; case 9: // status <- GDBM_SETOPT(handle, option, value, size) { int handle, option, value, size; success = mod_parint (0, params, param_count, &handle); success = mod_parint (1, params, param_count, &option); success = mod_parint (2, params, param_count, &value); success = mod_parint (3, params, param_count, &size); if ( success ) { if ( is_valid_handle(handle) ) { int r; r = gdbm_setopt(table[handle].dbf, option, &value, size); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_SETOPT: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_SETOPT: argument error"); } break; case 10: // status <- GDBM_FDESC(handle) { int handle; success = mod_parint (0, params, param_count, &handle); if ( success ) { if ( is_valid_handle(handle) ) { int r; r = gdbm_fdesc(table[handle].dbf); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_FDESC: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_FDESC: argument error"); } break; default: v_setstr(retval, "GDBM: function does not exist!"); } return success; }