1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-05 19:42:04 +02:00

remove default-parameter from dc_get_config() and return the correct default value automatically

This commit is contained in:
B. Petersen 2018-10-09 13:37:32 +02:00
parent 987dc80033
commit 5db799efdd
4 changed files with 41 additions and 28 deletions

View file

@ -487,7 +487,7 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
else if (!s_is_auth) else if (!s_is_auth)
{ {
if (strcmp(cmd, "auth")==0) { 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) { if (strcmp(arg1, is_pw)==0) {
s_is_auth = 1; s_is_auth = 1;
ret = COMMAND_SUCCEEDED; ret = COMMAND_SUCCEEDED;
@ -646,14 +646,9 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
else if (strcmp(cmd, "get")==0) else if (strcmp(cmd, "get")==0)
{ {
if (arg1) { if (arg1) {
char* val = dc_get_config(context, arg1, "<unset>"); char* val = dc_get_config(context, arg1);
if (val) { ret = dc_mprintf("%s=%s", arg1, val);
ret = dc_mprintf("%s=%s", arg1, val); free(val);
free(val);
}
else {
ret = COMMAND_FAILED;
}
} }
else { else {
ret = dc_strdup("ERROR: Argument <key> missing."); ret = dc_strdup("ERROR: Argument <key> missing.");

View file

@ -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(NULL) );
assert( !dc_may_be_valid_addr("") ); assert( !dc_may_be_valid_addr("") );
assert( dc_may_be_valid_addr("user@domain.tld") ); assert( dc_may_be_valid_addr("user@domain.tld") );

View file

@ -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.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. * - `sys.msgsize_max_recommended` = maximal recommended attachment size in bytes.
@ -434,35 +436,48 @@ cleanup:
* *
* @memberof dc_context_t * @memberof dc_context_t
* @param context The context object as created by dc_context_new(). For querying system values, this can be NULL. * @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 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, the default value is returned.
* @return Returns current value of "key", if "key" is unset, "def" is returned (which may be NULL) * The returned value must be free()'d, NULL is never returned.
* If the returned values is not NULL, the return value must be free()'d,
*/ */
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]=='.') { if (key && key[0]=='s' && key[1]=='y' && key[2]=='s' && key[3]=='.') {
return get_sys_config_str(key); return get_sys_config_str(key);
} }
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || key==NULL) { /* "def" may be NULL */ if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || key==NULL) {
return dc_strdup_keep_null(def); return dc_strdup("");
} }
if (strcmp(key, "selfavatar")==0) if (strcmp(key, "selfavatar")==0) {
{
char* rel_path = dc_sqlite3_get_config(context->sql, key, NULL); char* rel_path = dc_sqlite3_get_config(context->sql, key, NULL);
if (rel_path==NULL) { if (rel_path) {
return dc_strdup_keep_null(def); 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;
} }

View file

@ -230,7 +230,7 @@ int dc_is_open (const dc_context_t*);
char* dc_get_blobdir (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); 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_info (dc_context_t*);
char* dc_get_version_str (void); char* dc_get_version_str (void);
void dc_openssl_init_not_required (void); void dc_openssl_init_not_required (void);