diff options
author | Jaechul Lee <jcsing.lee@samsung.com> | 2024-07-23 14:57:38 +0900 |
---|---|---|
committer | Jaechul Lee <jcsing.lee@samsung.com> | 2024-07-25 10:10:37 +0900 |
commit | dc5fe3275987c662e2ef31997dd932097fddd4f7 (patch) | |
tree | 219ace9d21ff9a01261ecb5ee43c6cb49863e41e /tizen-audio-util.c | |
parent | 5e4b28101102d064f2e63fad3a83e4fc219176df (diff) | |
download | audio-alsa-dc5fe3275987c662e2ef31997dd932097fddd4f7.tar.gz audio-alsa-dc5fe3275987c662e2ef31997dd932097fddd4f7.tar.bz2 audio-alsa-dc5fe3275987c662e2ef31997dd932097fddd4f7.zip |
Support hal rootstrapaccepted/tizen/unified/x/asan/20240813.230740accepted/tizen/unified/x/20240729.014245accepted/tizen/unified/toolchain/20240812.132446accepted/tizen/unified/dev/20240730.010508accepted/tizen/unified/20240727.112739
* Added a dependency on halrootstrap
* Remove vconf dependency
* Remove iniparse dependency
[Version] 0.0.4
[Issue Type] Update
Change-Id: I0a11f526bb9591801755de244b6c7d80d90d8b43
Signed-off-by: Jaechul Lee <jcsing.lee@samsung.com>
Diffstat (limited to 'tizen-audio-util.c')
-rw-r--r-- | tizen-audio-util.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tizen-audio-util.c b/tizen-audio-util.c index 6ce8395..e8086c3 100644 --- a/tizen-audio-util.c +++ b/tizen-audio-util.c @@ -25,9 +25,26 @@ #include <stdlib.h> #include <string.h> #include <assert.h> +#include <ctype.h> #include "tizen-audio-internal.h" +#define MAX_LINE_LENGTH 256 +#define MAX_KEY_LENGTH 64 +#define MAX_VALUE_LENGTH 128 + +typedef struct { + char key[MAX_KEY_LENGTH]; + char value[MAX_VALUE_LENGTH]; +} key_value_pair_t; + +struct _dictionary { + key_value_pair_t *pairs; + int count; +}; + +typedef struct _dictionary dictionary_t; + /* ------ dump helper -------- */ #define MAX(a, b) ((a) > (b) ? (a) : (b)) @@ -163,3 +180,106 @@ void _audio_dump_free(dump_data_t *dump) } } /* ------ dump helper -------- */ + +/* ------ volume table parse -------- */ +char *rtrim(char *s) +{ + char* back = s + strlen(s); + while(isspace(*--back)); + *(back+1) = '\0'; + return s; +} + +dictionary_t *parser_load(const char *filename) +{ + FILE *file = NULL; + dictionary_t *dict = NULL; + char line[MAX_LINE_LENGTH]; + + file = fopen(filename, "r"); + if (!file) { + AUDIO_LOG_ERROR("Failed to open file: %s", filename); + return NULL; + } + + dict = (dictionary_t *)malloc(sizeof(dictionary_t)); + if (!dict) { + AUDIO_LOG_ERROR("failed to alloc memory"); + goto fail; + } + dict->pairs = NULL; + dict->count = 0; + + while (fgets(line, MAX_LINE_LENGTH, file)) { + char* saveptr; + char *key = strtok_r(line, "=", &saveptr); + char *value = strtok_r(NULL, "\n", &saveptr); + + if (key && value) { + key_value_pair_t pair; + strncpy(pair.key, key, MAX_KEY_LENGTH - 1); + rtrim(pair.key); + pair.key[MAX_KEY_LENGTH - 1] = '\0'; + strncpy(pair.value, value, MAX_VALUE_LENGTH - 1); + pair.value[MAX_VALUE_LENGTH - 1] = '\0'; + + dict->pairs = (key_value_pair_t *)realloc(dict->pairs, sizeof(key_value_pair_t) * (dict->count + 1)); + if (!dict->pairs) { + AUDIO_LOG_ERROR("failed to alloc memory"); + goto fail; + } + dict->pairs[dict->count++] = pair; + } + } + + fclose(file); + + return dict; + +fail: + if (file) + fclose(file); + if (dict) + parser_freedict(dict); + + return NULL; +} + +const char *parser_getstring(const dictionary_t *dict, const char *key, const char *default_value) +{ + if (!dict || !key) { + return default_value; + } + + for (int i = 0; i < dict->count; i++) { + if (strcmp(dict->pairs[i].key, key) == 0) { + return dict->pairs[i].value; + } + } + + return default_value; +} + +void parser_freedict(dictionary_t *dict) +{ + if (!dict) + return; + + if (dict->pairs) + free(dict->pairs); + + free(dict); +} + +void dump_dictionary(const dictionary_t *dict) +{ + if (!dict) { + AUDIO_LOG_ERROR("dictionary is NULL"); + return; + } + + AUDIO_LOG_INFO("dictionary contents:"); + for (int i = 0; i < dict->count; i++) + AUDIO_LOG_INFO("Key: %-16s Value: %s", dict->pairs[i].key, dict->pairs[i].value); +} + |