2011-03-24 19:11:06 +01:00
|
|
|
/*
|
2015-04-02 22:36:54 +02:00
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
2011-03-24 19:11:06 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-11-10 19:22:07 +01:00
|
|
|
#include <cutils/str_parms.h>
|
|
|
|
|
2011-03-24 19:11:06 +01:00
|
|
|
#define LOG_TAG "str_params"
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <cutils/hashmap.h>
|
|
|
|
#include <cutils/memory.h>
|
2017-01-10 22:19:54 +01:00
|
|
|
#include <log/log.h>
|
2011-03-24 19:11:06 +01:00
|
|
|
|
2016-02-03 19:40:45 +01:00
|
|
|
/* When an object is allocated but not freed in a function,
|
|
|
|
* because its ownership is released to other object like a hashmap,
|
|
|
|
* call RELEASE_OWNERSHIP to tell the clang analyzer and avoid
|
|
|
|
* false warnings about potential memory leak.
|
|
|
|
* For now, a "temporary" assignment to global variables
|
|
|
|
* is enough to confuse the clang static analyzer.
|
|
|
|
*/
|
|
|
|
#ifdef __clang_analyzer__
|
|
|
|
static void *released_pointer;
|
|
|
|
#define RELEASE_OWNERSHIP(x) { released_pointer = x; released_pointer = 0; }
|
|
|
|
#else
|
|
|
|
#define RELEASE_OWNERSHIP(x)
|
|
|
|
#endif
|
|
|
|
|
2011-03-24 19:11:06 +01:00
|
|
|
struct str_parms {
|
|
|
|
Hashmap *map;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static bool str_eq(void *key_a, void *key_b)
|
|
|
|
{
|
|
|
|
return !strcmp((const char *)key_a, (const char *)key_b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use djb hash unless we find it inadequate */
|
2015-08-26 19:40:00 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
__attribute__((no_sanitize("integer")))
|
|
|
|
#endif
|
2011-03-24 19:11:06 +01:00
|
|
|
static int str_hash_fn(void *str)
|
|
|
|
{
|
|
|
|
uint32_t hash = 5381;
|
|
|
|
|
2017-11-10 19:22:07 +01:00
|
|
|
for (char* p = static_cast<char*>(str); p && *p; p++)
|
2011-03-24 19:11:06 +01:00
|
|
|
hash = ((hash << 5) + hash) + *p;
|
|
|
|
return (int)hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct str_parms *str_parms_create(void)
|
|
|
|
{
|
2017-11-10 19:22:07 +01:00
|
|
|
str_parms* s = static_cast<str_parms*>(calloc(1, sizeof(str_parms)));
|
|
|
|
if (!s) return NULL;
|
2011-03-24 19:11:06 +01:00
|
|
|
|
2017-11-10 19:22:07 +01:00
|
|
|
s->map = hashmapCreate(5, str_hash_fn, str_eq);
|
|
|
|
if (!s->map) {
|
|
|
|
free(s);
|
2011-03-24 19:11:06 +01:00
|
|
|
return NULL;
|
2017-11-10 19:22:07 +01:00
|
|
|
}
|
2011-03-24 19:11:06 +01:00
|
|
|
|
2017-11-10 19:22:07 +01:00
|
|
|
return s;
|
2011-03-24 19:11:06 +01:00
|
|
|
}
|
|
|
|
|
2012-03-15 07:12:40 +01:00
|
|
|
struct remove_ctxt {
|
|
|
|
struct str_parms *str_parms;
|
|
|
|
const char *key;
|
|
|
|
};
|
|
|
|
|
2011-03-24 19:11:06 +01:00
|
|
|
static bool remove_pair(void *key, void *value, void *context)
|
|
|
|
{
|
2017-11-10 19:22:07 +01:00
|
|
|
remove_ctxt* ctxt = static_cast<remove_ctxt*>(context);
|
2012-03-15 07:12:40 +01:00
|
|
|
bool should_continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* - if key is not supplied, then we are removing all entries,
|
|
|
|
* so remove key and continue (i.e. return true)
|
|
|
|
* - if key is supplied and matches, then remove it and don't
|
|
|
|
* continue (return false). Otherwise, return true and keep searching
|
|
|
|
* for key.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if (!ctxt->key) {
|
|
|
|
should_continue = true;
|
|
|
|
goto do_remove;
|
2017-11-10 19:22:07 +01:00
|
|
|
} else if (!strcmp(ctxt->key, static_cast<const char*>(key))) {
|
2012-03-15 07:12:40 +01:00
|
|
|
should_continue = false;
|
|
|
|
goto do_remove;
|
|
|
|
}
|
2011-03-24 19:11:06 +01:00
|
|
|
|
2012-03-15 07:12:40 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
do_remove:
|
|
|
|
hashmapRemove(ctxt->str_parms->map, key);
|
2011-03-24 19:11:06 +01:00
|
|
|
free(key);
|
|
|
|
free(value);
|
2012-03-15 07:12:40 +01:00
|
|
|
return should_continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void str_parms_del(struct str_parms *str_parms, const char *key)
|
|
|
|
{
|
|
|
|
struct remove_ctxt ctxt = {
|
|
|
|
.str_parms = str_parms,
|
|
|
|
.key = key,
|
|
|
|
};
|
|
|
|
hashmapForEach(str_parms->map, remove_pair, &ctxt);
|
2011-03-24 19:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void str_parms_destroy(struct str_parms *str_parms)
|
|
|
|
{
|
2012-03-15 07:12:40 +01:00
|
|
|
struct remove_ctxt ctxt = {
|
|
|
|
.str_parms = str_parms,
|
|
|
|
};
|
|
|
|
|
|
|
|
hashmapForEach(str_parms->map, remove_pair, &ctxt);
|
2011-03-24 19:11:06 +01:00
|
|
|
hashmapFree(str_parms->map);
|
|
|
|
free(str_parms);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct str_parms *str_parms_create_str(const char *_string)
|
|
|
|
{
|
|
|
|
struct str_parms *str_parms;
|
|
|
|
char *str;
|
|
|
|
char *kvpair;
|
|
|
|
char *tmpstr;
|
|
|
|
int items = 0;
|
|
|
|
|
|
|
|
str_parms = str_parms_create();
|
|
|
|
if (!str_parms)
|
|
|
|
goto err_create_str_parms;
|
|
|
|
|
|
|
|
str = strdup(_string);
|
|
|
|
if (!str)
|
|
|
|
goto err_strdup;
|
|
|
|
|
2011-10-20 12:54:09 +02:00
|
|
|
ALOGV("%s: source string == '%s'\n", __func__, _string);
|
2011-03-24 19:11:06 +01:00
|
|
|
|
|
|
|
kvpair = strtok_r(str, ";", &tmpstr);
|
|
|
|
while (kvpair && *kvpair) {
|
|
|
|
char *eq = strchr(kvpair, '='); /* would love strchrnul */
|
|
|
|
char *value;
|
|
|
|
char *key;
|
|
|
|
void *old_val;
|
|
|
|
|
|
|
|
if (eq == kvpair)
|
|
|
|
goto next_pair;
|
|
|
|
|
|
|
|
if (eq) {
|
|
|
|
key = strndup(kvpair, eq - kvpair);
|
|
|
|
if (*(++eq))
|
|
|
|
value = strdup(eq);
|
|
|
|
else
|
|
|
|
value = strdup("");
|
|
|
|
} else {
|
|
|
|
key = strdup(kvpair);
|
|
|
|
value = strdup("");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we replaced a value, free it */
|
|
|
|
old_val = hashmapPut(str_parms->map, key, value);
|
2016-02-03 19:40:45 +01:00
|
|
|
RELEASE_OWNERSHIP(value);
|
2012-03-15 07:10:06 +01:00
|
|
|
if (old_val) {
|
2011-03-24 19:11:06 +01:00
|
|
|
free(old_val);
|
2012-03-15 07:10:06 +01:00
|
|
|
free(key);
|
2016-02-03 19:40:45 +01:00
|
|
|
} else {
|
|
|
|
RELEASE_OWNERSHIP(key);
|
2012-03-15 07:10:06 +01:00
|
|
|
}
|
2011-03-24 19:11:06 +01:00
|
|
|
|
|
|
|
items++;
|
|
|
|
next_pair:
|
|
|
|
kvpair = strtok_r(NULL, ";", &tmpstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!items)
|
2011-10-20 12:54:09 +02:00
|
|
|
ALOGV("%s: no items found in string\n", __func__);
|
2011-03-24 19:11:06 +01:00
|
|
|
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return str_parms;
|
|
|
|
|
|
|
|
err_strdup:
|
|
|
|
str_parms_destroy(str_parms);
|
|
|
|
err_create_str_parms:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_parms_add_str(struct str_parms *str_parms, const char *key,
|
|
|
|
const char *value)
|
|
|
|
{
|
2014-03-06 18:15:43 +01:00
|
|
|
void *tmp_key = NULL;
|
|
|
|
void *tmp_val = NULL;
|
|
|
|
void *old_val = NULL;
|
|
|
|
|
|
|
|
// strdup and hashmapPut both set errno on failure.
|
|
|
|
// Set errno to 0 so we can recognize whether anything went wrong.
|
|
|
|
int saved_errno = errno;
|
|
|
|
errno = 0;
|
2011-03-24 19:11:06 +01:00
|
|
|
|
2012-03-12 19:01:16 +01:00
|
|
|
tmp_key = strdup(key);
|
2014-03-06 18:15:43 +01:00
|
|
|
if (tmp_key == NULL) {
|
|
|
|
goto clean_up;
|
|
|
|
}
|
|
|
|
|
2012-03-12 19:01:16 +01:00
|
|
|
tmp_val = strdup(value);
|
2014-03-06 18:15:43 +01:00
|
|
|
if (tmp_val == NULL) {
|
|
|
|
goto clean_up;
|
|
|
|
}
|
2011-03-24 19:11:06 +01:00
|
|
|
|
2014-03-06 18:15:43 +01:00
|
|
|
old_val = hashmapPut(str_parms->map, tmp_key, tmp_val);
|
|
|
|
if (old_val == NULL) {
|
|
|
|
// Did hashmapPut fail?
|
|
|
|
if (errno == ENOMEM) {
|
|
|
|
goto clean_up;
|
|
|
|
}
|
|
|
|
// For new keys, hashmap takes ownership of tmp_key and tmp_val.
|
2016-02-03 19:40:45 +01:00
|
|
|
RELEASE_OWNERSHIP(tmp_key);
|
|
|
|
RELEASE_OWNERSHIP(tmp_val);
|
2014-03-06 18:15:43 +01:00
|
|
|
tmp_key = tmp_val = NULL;
|
|
|
|
} else {
|
|
|
|
// For existing keys, hashmap takes ownership of tmp_val.
|
|
|
|
// (It also gives up ownership of old_val.)
|
2016-02-03 19:40:45 +01:00
|
|
|
RELEASE_OWNERSHIP(tmp_val);
|
2014-03-06 18:15:43 +01:00
|
|
|
tmp_val = NULL;
|
2011-03-24 19:11:06 +01:00
|
|
|
}
|
2014-03-06 18:15:43 +01:00
|
|
|
|
|
|
|
clean_up:
|
|
|
|
free(tmp_key);
|
|
|
|
free(tmp_val);
|
|
|
|
free(old_val);
|
|
|
|
int result = -errno;
|
|
|
|
errno = saved_errno;
|
|
|
|
return result;
|
2011-03-24 19:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int str_parms_add_int(struct str_parms *str_parms, const char *key, int value)
|
|
|
|
{
|
|
|
|
char val_str[12];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = snprintf(val_str, sizeof(val_str), "%d", value);
|
|
|
|
if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = str_parms_add_str(str_parms, key, val_str);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_parms_add_float(struct str_parms *str_parms, const char *key,
|
|
|
|
float value)
|
|
|
|
{
|
|
|
|
char val_str[23];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = snprintf(val_str, sizeof(val_str), "%.10f", value);
|
|
|
|
if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = str_parms_add_str(str_parms, key, val_str);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:47:29 +01:00
|
|
|
int str_parms_has_key(struct str_parms *str_parms, const char *key) {
|
|
|
|
return hashmapGet(str_parms->map, (void *)key) != NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-24 19:11:06 +01:00
|
|
|
int str_parms_get_str(struct str_parms *str_parms, const char *key, char *val,
|
|
|
|
int len)
|
|
|
|
{
|
2017-11-10 19:22:07 +01:00
|
|
|
// TODO: hashmapGet should take a const* key.
|
|
|
|
char* value = static_cast<char*>(hashmapGet(str_parms->map, (void*)key));
|
2011-03-24 19:11:06 +01:00
|
|
|
if (value)
|
|
|
|
return strlcpy(val, value, len);
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_parms_get_int(struct str_parms *str_parms, const char *key, int *val)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
2017-11-10 19:22:07 +01:00
|
|
|
// TODO: hashmapGet should take a const* key.
|
|
|
|
char* value = static_cast<char*>(hashmapGet(str_parms->map, (void*)key));
|
2011-03-24 19:11:06 +01:00
|
|
|
if (!value)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
*val = (int)strtol(value, &end, 0);
|
|
|
|
if (*value != '\0' && *end == '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_parms_get_float(struct str_parms *str_parms, const char *key,
|
|
|
|
float *val)
|
|
|
|
{
|
|
|
|
float out;
|
|
|
|
char *end;
|
|
|
|
|
2017-11-10 19:22:07 +01:00
|
|
|
// TODO: hashmapGet should take a const* key.
|
|
|
|
char* value = static_cast<char*>(hashmapGet(str_parms->map, (void*)(key)));
|
2011-03-24 19:11:06 +01:00
|
|
|
if (!value)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
out = strtof(value, &end);
|
2013-11-22 16:38:46 +01:00
|
|
|
if (*value == '\0' || *end != '\0')
|
|
|
|
return -EINVAL;
|
2011-03-24 19:11:06 +01:00
|
|
|
|
2013-11-22 16:38:46 +01:00
|
|
|
*val = out;
|
|
|
|
return 0;
|
2011-03-24 19:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool combine_strings(void *key, void *value, void *context)
|
|
|
|
{
|
2017-11-10 19:22:07 +01:00
|
|
|
char** old_str = static_cast<char**>(context);
|
2011-03-24 19:11:06 +01:00
|
|
|
char *new_str;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = asprintf(&new_str, "%s%s%s=%s",
|
|
|
|
*old_str ? *old_str : "",
|
|
|
|
*old_str ? ";" : "",
|
|
|
|
(char *)key,
|
|
|
|
(char *)value);
|
|
|
|
if (*old_str)
|
|
|
|
free(*old_str);
|
|
|
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
*old_str = new_str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*old_str = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *str_parms_to_str(struct str_parms *str_parms)
|
|
|
|
{
|
|
|
|
char *str = NULL;
|
2018-07-11 23:26:40 +02:00
|
|
|
hashmapForEach(str_parms->map, combine_strings, &str);
|
|
|
|
return (str != NULL) ? str : strdup("");
|
2011-03-24 19:11:06 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 01:23:51 +01:00
|
|
|
static bool dump_entry(void* key, void* value, void* /*context*/) {
|
2012-01-04 20:19:03 +01:00
|
|
|
ALOGI("key: '%s' value: '%s'\n", (char *)key, (char *)value);
|
2011-03-24 19:11:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void str_parms_dump(struct str_parms *str_parms)
|
|
|
|
{
|
|
|
|
hashmapForEach(str_parms->map, dump_entry, str_parms);
|
|
|
|
}
|