From 5db799efdd114e7dbd42a56d8ebb69853e29f7cb Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Tue, 9 Oct 2018 13:37:32 +0200 Subject: [PATCH] remove default-parameter from dc_get_config() and return the correct default value automatically --- cmdline/cmdline.c | 13 ++++-------- cmdline/stress.c | 3 +++ src/dc_context.c | 51 ++++++++++++++++++++++++++++++----------------- src/deltachat.h | 2 +- 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/cmdline/cmdline.c b/cmdline/cmdline.c index b4f9a55b..9ba9eb78 100644 --- a/cmdline/cmdline.c +++ b/cmdline/cmdline.c @@ -487,7 +487,7 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline) else if (!s_is_auth) { if (strcmp(cmd, "auth")==0) { - char* is_pw = dc_get_config(context, "mail_pw", ""); + char* is_pw = dc_get_config(context, "mail_pw"); if (strcmp(arg1, is_pw)==0) { s_is_auth = 1; ret = COMMAND_SUCCEEDED; @@ -646,14 +646,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline) else if (strcmp(cmd, "get")==0) { if (arg1) { - char* val = dc_get_config(context, arg1, ""); - if (val) { - ret = dc_mprintf("%s=%s", arg1, val); - free(val); - } - else { - ret = COMMAND_FAILED; - } + char* val = dc_get_config(context, arg1); + ret = dc_mprintf("%s=%s", arg1, val); + free(val); } else { ret = dc_strdup("ERROR: Argument missing."); diff --git a/cmdline/stress.c b/cmdline/stress.c index 900c63f0..d2582680 100644 --- a/cmdline/stress.c +++ b/cmdline/stress.c @@ -360,6 +360,9 @@ void stress_functions(dc_context_t* context) **************************************************************************/ { + assert( atol("")==0 ); /* we rely on this eg. in dc_sqlite3_get_config() */ + assert( atoi("")==0 ); + assert( !dc_may_be_valid_addr(NULL) ); assert( !dc_may_be_valid_addr("") ); assert( dc_may_be_valid_addr("user@domain.tld") ); diff --git a/src/dc_context.c b/src/dc_context.c index 5f69b5d3..cba5a0f3 100644 --- a/src/dc_context.c +++ b/src/dc_context.c @@ -420,9 +420,11 @@ cleanup: /** - * Get a configuration option. The configuration option is typically set by dc_set_config() or by the library itself. + * Get a configuration option. + * The configuration option is set by dc_set_config() or by the library itself. * - * Moreover, this function can be used to query some global system values: + * Beside the options shown at dc_set_config(), + * this function can be used to query some global system values: * * - `sys.version` = get the version string eg. as `1.2.3` or as `1.2.3special4` * - `sys.msgsize_max_recommended` = maximal recommended attachment size in bytes. @@ -434,35 +436,48 @@ cleanup: * * @memberof dc_context_t * @param context The context object as created by dc_context_new(). For querying system values, this can be NULL. - * @param key The key to query - * @param def Default value to return if "key" is unset. - * @return Returns current value of "key", if "key" is unset, "def" is returned (which may be NULL) - * If the returned values is not NULL, the return value must be free()'d, + * @param key The key to query. + * @return Returns current value of "key", if "key" is unset, the default value is returned. + * The returned value must be free()'d, NULL is never returned. */ -char* dc_get_config(dc_context_t* context, const char* key, const char* def) +char* dc_get_config(dc_context_t* context, const char* key) { + char* value = NULL; + if (key && key[0]=='s' && key[1]=='y' && key[2]=='s' && key[3]=='.') { return get_sys_config_str(key); } - if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || key==NULL) { /* "def" may be NULL */ - return dc_strdup_keep_null(def); + if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || key==NULL) { + return dc_strdup(""); } - if (strcmp(key, "selfavatar")==0) - { + if (strcmp(key, "selfavatar")==0) { char* rel_path = dc_sqlite3_get_config(context->sql, key, NULL); - if (rel_path==NULL) { - return dc_strdup_keep_null(def); + if (rel_path) { + value = dc_get_abs_path(context, rel_path); + free(rel_path); } - char* abs_path = dc_get_abs_path(context, rel_path); - free(rel_path); - return abs_path; } - else + else { + value = dc_sqlite3_get_config(context->sql, key, NULL); + } + + if (value==NULL) { - return dc_sqlite3_get_config(context->sql, key, def); + // no value yet, use default value + if (strcmp(key, "e2ee_enabled")==0) { + value = dc_mprintf("%i", DC_E2EE_DEFAULT_ENABLED); + } + else if (strcmp(key, "mdns_enabled")==0) { + value = dc_mprintf("%i", DC_MDNS_DEFAULT_ENABLED); + } + else { + value = dc_mprintf(""); + } } + + return value; } diff --git a/src/deltachat.h b/src/deltachat.h index 973196ca..746b7820 100644 --- a/src/deltachat.h +++ b/src/deltachat.h @@ -230,7 +230,7 @@ int dc_is_open (const dc_context_t*); char* dc_get_blobdir (const dc_context_t*); int dc_set_config (dc_context_t*, const char* key, const char* value); -char* dc_get_config (dc_context_t*, const char* key, const char* def); +char* dc_get_config (dc_context_t*, const char* key); char* dc_get_info (dc_context_t*); char* dc_get_version_str (void); void dc_openssl_init_not_required (void);