int main(void){ struct nbspqdb_st *nqdb; int status; status = qdb_open(&nqdb, "db.db", sizeof(struct nbspdb_prec_st)); if(status != 0) errx(1, "nbspdb_open: %d", status); print_all_records(nqdb); qdb_close(nqdb); return(0); }
static int qdbhandle_init(QdbHandle *self, PyObject *args) { char *vmname = NULL; if (!PyArg_ParseTuple(args, "|s", &vmname)) goto fail; self->qdb = qdb_open(vmname); if (!self->qdb) goto fail; return 0; fail: PyErr_SetFromErrno(qdb_error); return -1; }
nbspqtable_t *nbspqt_open(u_int8_t num_queues, int softlimit, int hardlimit, struct qdb_param_st *qdbparam, int *dberror){ int status = 0; int dberror_close = 0; struct nbsp_qtable_st *qt = NULL; char *dbfname = NULL; int dbfname_size = 0; struct qdb_param_st qdbparam_copy; int size; int i; int memory_based_db = 0; qt = malloc(sizeof(struct nbsp_qtable_st)); if(qt == NULL){ *dberror = errno; return(NULL); } size = num_queues * sizeof(struct nbspq_st*); qt->nbspq = malloc(size); if(qt->nbspq == NULL){ free(qt); *dberror = errno; return(NULL); } qt->n = 0; qt->softlimit = softlimit; qt->hardlimit = hardlimit; qt->reclen = qdbparam->reclen; qt->appdata = NULL; /* If the db file name is NULL or "" it is a memory based queue. */ if((qdbparam->dbname == NULL) || (qdbparam->dbname[0] == '\0')) memory_based_db = 1; if(memory_based_db == 0){ /* leave room to add one byte in hex + ".db" + '\0' */ dbfname_size = strlen(qdbparam->dbname) + 6; dbfname = malloc(dbfname_size); if(dbfname == NULL){ free(qt->nbspq); free(qt); *dberror = errno; return(NULL); } } memcpy(&qdbparam_copy, qdbparam, sizeof(qdbparam_copy)); for(i = 0; (i <= num_queues - 1) && (status == 0); ++i){ if(memory_based_db == 0){ snprintf(dbfname, dbfname_size, "%s%x.db", qdbparam->dbname, i); qdbparam_copy.dbname = dbfname; } status = qdb_open(&(qt->nbspq[i]), &qdbparam_copy, dberror); if(status == 0) ++qt->n; else break; } if(dbfname != NULL) free(dbfname); if(status != 0){ for(i = 0; i <= qt->n - 1; ++i){ qdb_close(qt->nbspq[i], &dberror_close); /* ignore any error here */ } free(qt->nbspq); free(qt); qt = NULL; } return(qt); }