diff options
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); +} + |