From 1e0e1f2f283ffdedb1c0d05c8e572074acf6baa8 Mon Sep 17 00:00:00 2001 From: graham.gower@gmail.com Date: Wed, 25 May 2011 20:51:50 -0400 Subject: Improve the opkg_{get,set}_option() C API. This breaks compatibility with the previous API, so bump the lib version. git-svn-id: http://opkg.googlecode.com/svn/trunk@622 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- diff --git a/libopkg/Makefile.am b/libopkg/Makefile.am index 373d09c..043c5c4 100644 --- a/libopkg/Makefile.am +++ b/libopkg/Makefile.am @@ -46,6 +46,8 @@ libopkg_la_SOURCES = \ libopkg_la_LIBADD = $(top_builddir)/libbb/libbb.la $(CURL_LIBS) $(GPGME_LIBS) $(OPENSSL_LIBS) $(PATHFINDER_LIBS) +libopkg_la_LDFLAGS = -version-info 1:0:0 + # make sure we only export symbols that are for public use #libopkg_la_LDFLAGS = -export-symbols-regex "^opkg_.*" diff --git a/libopkg/opkg.c b/libopkg/opkg.c index 7c3e18f..92f61f4 100644 --- a/libopkg/opkg.c +++ b/libopkg/opkg.c @@ -158,80 +158,77 @@ opkg_re_read_config_files(void) return opkg_new(); } -void -opkg_get_option(char *option, void **value) +int +opkg_get_option(char *option, void *value) { - int i = 0; + int i; extern opkg_option_t options[]; - /* look up the option - * TODO: this would be much better as a hash table - */ - while (options[i].name) { - if (strcmp(options[i].name, option) != 0) { - i++; - continue; - } + opkg_assert(option != NULL); + opkg_assert(value != NULL); + + *(char**)value = NULL; + + for (i=0; options[i].name; i++) { + if (strcmp(options[i].name, option) == 0) + break; } - /* get the option */ + if (options[i].name == NULL) + /* Not found. */ + return -1; + switch (options[i].type) { case OPKG_OPT_TYPE_BOOL: - *((int *) value) = *((int *) options[i].value); - return; + *(int *)value = *(int *)options[i].value; + break; case OPKG_OPT_TYPE_INT: - *((int *) value) = *((int *) options[i].value); - return; + *(int *)value = *(int *)options[i].value; + break; case OPKG_OPT_TYPE_STRING: - *((char **) value) = xstrdup(options[i].value); - return; + *(char **)value = xstrdup(*(char **)options[i].value); + break; } + return 0; } void opkg_set_option(char *option, void *value) { - int i = 0, found = 0; + int i; extern opkg_option_t options[]; opkg_assert(option != NULL); opkg_assert(value != NULL); - /* look up the option - * TODO: this would be much better as a hash table - */ - while (options[i].name) { - if (strcmp(options[i].name, option) == 0) { - found = 1; + for (i=0; options[i].name; i++) { + if (strcmp(options[i].name, option) == 0) break; - } - i++; } - if (!found) { + if (options[i].name == NULL) { opkg_msg(ERROR, "Invalid option: %s\n", option); return; } - /* set the option */ switch (options[i].type) { case OPKG_OPT_TYPE_BOOL: - if (*((int *) value) == 0) + if ((long)value == 0) *((int *) options[i].value) = 0; else *((int *) options[i].value) = 1; - return; + break; case OPKG_OPT_TYPE_INT: - *((int *) options[i].value) = *((int *) value); - return; + *((int *) options[i].value) = (long)value; + break; case OPKG_OPT_TYPE_STRING: - *((char **) options[i].value) = xstrdup(value); - return; + *((char **) options[i].value) = xstrdup((char *)value); + break; } } diff --git a/libopkg/opkg.h b/libopkg/opkg.h index b39f7fb..4fbd404 100644 --- a/libopkg/opkg.h +++ b/libopkg/opkg.h @@ -43,7 +43,7 @@ struct _opkg_progress_data_t int opkg_new (void); void opkg_free (void); int opkg_re_read_config_files (void); -void opkg_get_option (char *option, void **value); +int opkg_get_option (char *option, void *value); void opkg_set_option (char *option, void *value); int opkg_install_package (const char *package_name, opkg_progress_callback_t callback, void *user_data); diff --git a/tests/libopkg_test.c b/tests/libopkg_test.c index 31a22b0..21f100e 100644 --- a/tests/libopkg_test.c +++ b/tests/libopkg_test.c @@ -159,6 +159,18 @@ main (int argc, char **argv) return 1; } + char *cache; + opkg_set_option("cache", "|asdf|"); + if (opkg_get_option("cache", &cache) != -1) { + printf("cache=``%s''\n", cache); + } + + int verb; + opkg_set_option("verbosity", (void *)3); + if (opkg_get_option("verbosity", &verb) != -1) { + printf("verbosity=%d\n", verb); + } + switch (argv[1][0]) { case 'f': -- cgit v0.9.1