From b41d53792c4c4e4abd6d810b2765d865775f5104 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Tue, 13 Apr 2021 16:58:15 +0100 Subject: jztool: New utility for installing a bootloader on FiiO M3K At present, this is just a command line tool for Linux only. Integrating this with the Rockbox utility and porting to other platforms should be straightforward; the README contains more information. Change-Id: Ie66fc837a02ab13c878925360cabc9805597548a --- rbutil/jztool/src/paramlist.c | 135 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 rbutil/jztool/src/paramlist.c (limited to 'rbutil/jztool/src/paramlist.c') diff --git a/rbutil/jztool/src/paramlist.c b/rbutil/jztool/src/paramlist.c new file mode 100644 index 0000000000..05bcf97a13 --- /dev/null +++ b/rbutil/jztool/src/paramlist.c @@ -0,0 +1,135 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2021 Aidan MacDonald + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "jztool.h" +#include +#include +#include + +struct jz_paramlist { + int size; + char** keys; + char** values; +}; + +static int jz_paramlist_extend(jz_paramlist* pl, int count) +{ + int nsize = pl->size + count; + + /* Reallocate key list */ + char** nkeys = realloc(pl->keys, nsize * sizeof(char*)); + if(!nkeys) + return JZ_ERR_OUT_OF_MEMORY; + + for(int i = pl->size; i < nsize; ++i) + nkeys[i] = NULL; + + pl->keys = nkeys; + + /* Reallocate value list */ + char** nvalues = realloc(pl->values, nsize * sizeof(char*)); + if(!nvalues) + return JZ_ERR_OUT_OF_MEMORY; + + for(int i = pl->size; i < nsize; ++i) + nvalues[i] = NULL; + + pl->values = nvalues; + + pl->size = nsize; + return JZ_SUCCESS; +} + +jz_paramlist* jz_paramlist_new(void) +{ + jz_paramlist* pl = malloc(sizeof(struct jz_paramlist)); + if(!pl) + return NULL; + + pl->size = 0; + pl->keys = NULL; + pl->values = NULL; + return pl; +} + +void jz_paramlist_free(jz_paramlist* pl) +{ + for(int i = 0; i < pl->size; ++i) { + free(pl->keys[i]); + free(pl->values[i]); + } + + if(pl->size > 0) { + free(pl->keys); + free(pl->values); + } + + free(pl); +} + +int jz_paramlist_set(jz_paramlist* pl, const char* param, const char* value) +{ + int pos = -1; + for(int i = 0; i < pl->size; ++i) { + if(!pl->keys[i] || !strcmp(pl->keys[i], param)) { + pos = i; + break; + } + } + + if(pos == -1) { + pos = pl->size; + int rc = jz_paramlist_extend(pl, 1); + if(rc < 0) + return rc; + } + + bool need_key = (pl->keys[pos] == NULL); + if(need_key) { + char* newparam = strdup(param); + if(!newparam) + return JZ_ERR_OUT_OF_MEMORY; + + pl->keys[pos] = newparam; + } + + char* newvalue = strdup(value); + if(!newvalue) { + if(need_key) { + free(pl->keys[pos]); + pl->keys[pos] = NULL; + } + + return JZ_ERR_OUT_OF_MEMORY; + } + + pl->values[pos] = newvalue; + return JZ_SUCCESS; +} + +const char* jz_paramlist_get(jz_paramlist* pl, const char* param) +{ + for(int i = 0; i < pl->size; ++i) + if(pl->keys[i] && !strcmp(pl->keys[i], param)) + return pl->values[i]; + + return NULL; +} -- cgit v1.2.3