/****************************************** * dbm example with apr * Sylvain Marechal * 25/10/2005 *****************************************/ #include "apr_general.h" #include "apr_getopt.h" #include "apr_dbm.h" /* args */ static apr_getopt_option_t s_opts[] = { { "help", 'h', 0, "display this help" }, { 0, 0, 0, 0 } }; /********************************************* * usage ********************************************/ static void usage() { int i; printf( "Usage:\n" ); for( i = 0; i < sizeof(s_opts)/sizeof(apr_getopt_option_t) -1; i ++ ) { printf( "\t--%s\t-%c\t%s\n", s_opts[i].name, s_opts[i].optch, s_opts[i].description ); } exit(1); } /********************************************* * get_args ********************************************/ static int get_args( apr_pool_t *pool, int argc, char * argv[] ) { apr_getopt_t *os; apr_status_t ret; apr_getopt_init( &os, pool, argc, (const char * const *)argv ); do { int opt_ch; const char * opt_arg; ret = apr_getopt_long( os, s_opts, &opt_ch, &opt_arg ); if( ret == APR_SUCCESS ) { switch(opt_ch) { case 'h': { usage(); break; } } } } while( ret == APR_SUCCESS ); return 0; } typedef struct key_val { char * key; char * val; } key_val; /****************************************** * string2datum() *****************************************/ static apr_datum_t string2datum( const char * psz ) { apr_datum_t dt; dt.dptr = (char *)psz; dt.dsize = strlen(psz) + 1; return dt; } /****************************************** * test_dbm() *****************************************/ static void test_dbm( apr_pool_t *pool ) { apr_dbm_t * dbm; apr_status_t ret; apr_datum_t dtkey, dtval; int i, exist; const char * used1, * used2; key_val akeyvals[] = { { "toto", " est un rigolo" }, { "tutu", " a un chapeau pointu" }, { "titi", " ne fait pas un pli" }, { "tata", " est vraiment flagada" } }; /* open/create the db */ ret = apr_dbm_open( &dbm, "./dbm.bin", APR_DBM_RWCREATE, APR_OS_DEFAULT, pool ); if( ret != APR_SUCCESS ) { printf( "apr_dbm_open() error %d\n", ret ); exit(1); } /* Fill it with values */ for( i = 0; i < sizeof(akeyvals)/sizeof(key_val); i ++ ) { ret = apr_dbm_store( dbm, string2datum(akeyvals[i].key), string2datum(akeyvals[i].val) ); if( ret != APR_SUCCESS ) { printf( "apr_dbm_store() error %d\n", ret ); exit(1); } } /* Close it */ apr_dbm_close( dbm ); /* Re open it */ ret = apr_dbm_open( &dbm, "./dbm.bin", APR_DBM_READWRITE, APR_OS_DEFAULT, pool ); if( ret != APR_SUCCESS ) { printf( "apr_dbm_open() error %d\n", ret ); exit(1); } /* Search a key */ exist = apr_dbm_exists( dbm, string2datum("tutu") ); if( exist == 0 ) { printf( "apr_dbm_exists() error: tutu exists\n" ); } /* Delete a record */ ret = apr_dbm_delete( dbm, string2datum( "tata" ) ); if( ret != APR_SUCCESS ) { printf( "apr_dbm_delete() error %d\n", ret ); } /* Search 'tata' */ exist = apr_dbm_exists( dbm, string2datum("tata") ); if( exist ) { printf( "apr_dbm_exists() error: tata exists\n" ); } /* Enumerates all keys */ ret = apr_dbm_firstkey( dbm, &dtkey ); while( ret == APR_SUCCESS && dtkey.dptr != NULL ) { ret = apr_dbm_fetch( dbm, dtkey, &dtval ); printf( "key: '%s', val: '%s'\n", dtkey.dptr, dtval.dptr ); ret = apr_dbm_nextkey( dbm, &dtkey ); } /* Close db */ apr_dbm_close( dbm ); /* delete db */ apr_dbm_get_usednames( pool, "./dbm.bin", &used1, &used2 ); if( used1 ) apr_file_remove( used1, pool ); if( used2 ) apr_file_remove( used2, pool ); } /****************************************** * main() *****************************************/ int main( int argc, char * argv[] ) { apr_pool_t *pool; apr_status_t ret; /* Init apr */ ret = apr_initialize(); if( ret != APR_SUCCESS) { printf( "apr_initialize() failed (%d)\n", ret ); exit(-1); } /* Create pool */ ret = apr_pool_create(&pool, NULL); if( ret != APR_SUCCESS) { printf( "apr_pool_create() failed %d\n", ret ); exit( -1); } /* Parse args */ get_args( pool, argc, argv ); /* dbm tests */ test_dbm( pool ); /* Destroy pool */ apr_pool_destroy( pool ); return 0; }