2018-05-01 05:15:48 +00:00
|
|
|
/*
|
2018-08-05 11:40:32 +00:00
|
|
|
* Copyright (c) 2018 naehrwert
|
2022-01-20 10:41:20 +00:00
|
|
|
* Copyright (c) 2018-2022 CTCaer
|
2018-08-05 11:40:32 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-05-01 05:15:48 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ini.h"
|
2020-06-14 13:45:45 +00:00
|
|
|
#include <libs/fatfs/ff.h>
|
|
|
|
#include <mem/heap.h>
|
|
|
|
#include <utils/dirlist.h>
|
2022-01-20 10:41:20 +00:00
|
|
|
#include <utils/util.h>
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2019-02-23 22:35:24 +00:00
|
|
|
u32 _find_section_name(char *lbuf, u32 lblen, char schar)
|
|
|
|
{
|
|
|
|
u32 i;
|
2020-01-17 07:18:31 +00:00
|
|
|
// Depends on 'FF_USE_STRFUNC 2' that removes \r.
|
|
|
|
for (i = 0; i < lblen && lbuf[i] != schar && lbuf[i] != '\n'; i++)
|
2019-02-23 22:35:24 +00:00
|
|
|
;
|
|
|
|
lbuf[i] = 0;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ini_sec_t *_ini_create_section(link_t *dst, ini_sec_t *csec, char *name, u8 type)
|
|
|
|
{
|
|
|
|
if (csec)
|
|
|
|
list_append(dst, &csec->link);
|
|
|
|
|
2022-01-20 10:41:20 +00:00
|
|
|
// Calculate total allocation size.
|
|
|
|
u32 len = name ? strlen(name) + 1 : 0;
|
|
|
|
char *buf = calloc(sizeof(ini_sec_t) + len, 1);
|
|
|
|
|
|
|
|
csec = (ini_sec_t *)buf;
|
|
|
|
csec->name = strcpy_ns(buf + sizeof(ini_sec_t), name);
|
2019-02-23 22:35:24 +00:00
|
|
|
csec->type = type;
|
|
|
|
|
2022-05-19 12:03:00 +00:00
|
|
|
// Initialize list.
|
|
|
|
list_init(&csec->kvs);
|
|
|
|
|
2019-02-23 22:35:24 +00:00
|
|
|
return csec;
|
|
|
|
}
|
|
|
|
|
2018-08-21 01:37:40 +00:00
|
|
|
int ini_parse(link_t *dst, char *ini_path, bool is_dir)
|
2018-05-01 05:15:48 +00:00
|
|
|
{
|
2021-03-17 07:08:34 +00:00
|
|
|
FIL fp;
|
2018-05-01 05:15:48 +00:00
|
|
|
u32 lblen;
|
2018-08-21 01:37:40 +00:00
|
|
|
u32 k = 0;
|
2022-07-11 19:10:11 +00:00
|
|
|
u32 pathlen = strlen(ini_path);
|
2018-05-01 05:15:48 +00:00
|
|
|
ini_sec_t *csec = NULL;
|
|
|
|
|
2022-07-11 19:10:11 +00:00
|
|
|
char *lbuf = NULL;
|
2021-03-17 07:08:34 +00:00
|
|
|
char *filelist = NULL;
|
2018-08-21 01:37:40 +00:00
|
|
|
char *filename = (char *)malloc(256);
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2019-12-04 13:56:53 +00:00
|
|
|
strcpy(filename, ini_path);
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2019-02-23 22:35:24 +00:00
|
|
|
// Get all ini filenames.
|
2018-08-21 01:37:40 +00:00
|
|
|
if (is_dir)
|
|
|
|
{
|
2020-06-13 15:16:29 +00:00
|
|
|
filelist = dirlist(filename, "*.ini", false, false);
|
2018-08-21 01:37:40 +00:00
|
|
|
if (!filelist)
|
|
|
|
{
|
|
|
|
free(filename);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-04 13:56:53 +00:00
|
|
|
strcpy(filename + pathlen, "/");
|
2018-08-21 01:37:40 +00:00
|
|
|
pathlen++;
|
|
|
|
}
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-08-21 01:37:40 +00:00
|
|
|
do
|
|
|
|
{
|
2019-02-23 22:35:24 +00:00
|
|
|
// Copy ini filename in path string.
|
2018-08-21 01:37:40 +00:00
|
|
|
if (is_dir)
|
2018-05-01 05:15:48 +00:00
|
|
|
{
|
2018-08-21 01:37:40 +00:00
|
|
|
if (filelist[k * 256])
|
2018-05-01 05:15:48 +00:00
|
|
|
{
|
2019-12-04 13:56:53 +00:00
|
|
|
strcpy(filename + pathlen, &filelist[k * 256]);
|
2018-08-21 01:37:40 +00:00
|
|
|
k++;
|
2018-05-01 05:15:48 +00:00
|
|
|
}
|
2018-08-21 01:37:40 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2019-02-23 22:35:24 +00:00
|
|
|
// Open ini.
|
2018-08-21 01:37:40 +00:00
|
|
|
if (f_open(&fp, filename, FA_READ) != FR_OK)
|
|
|
|
{
|
|
|
|
free(filelist);
|
|
|
|
free(filename);
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-08-21 01:37:40 +00:00
|
|
|
return 0;
|
2018-05-01 05:15:48 +00:00
|
|
|
}
|
2018-08-21 01:37:40 +00:00
|
|
|
|
2021-03-17 07:08:34 +00:00
|
|
|
lbuf = malloc(512);
|
|
|
|
|
2018-08-21 01:37:40 +00:00
|
|
|
do
|
2018-07-01 17:29:30 +00:00
|
|
|
{
|
2018-08-21 01:37:40 +00:00
|
|
|
// Fetch one line.
|
|
|
|
lbuf[0] = 0;
|
|
|
|
f_gets(lbuf, 512, &fp);
|
|
|
|
lblen = strlen(lbuf);
|
2018-07-01 17:29:30 +00:00
|
|
|
|
2020-01-17 07:18:31 +00:00
|
|
|
// Remove trailing newline. Depends on 'FF_USE_STRFUNC 2' that removes \r.
|
|
|
|
if (lblen && lbuf[lblen - 1] == '\n')
|
2018-08-21 01:37:40 +00:00
|
|
|
lbuf[lblen - 1] = 0;
|
2018-07-01 17:29:30 +00:00
|
|
|
|
2018-08-21 01:37:40 +00:00
|
|
|
if (lblen > 2 && lbuf[0] == '[') // Create new section.
|
2018-07-01 17:29:30 +00:00
|
|
|
{
|
2019-02-23 22:35:24 +00:00
|
|
|
_find_section_name(lbuf, lblen, ']');
|
|
|
|
|
|
|
|
csec = _ini_create_section(dst, csec, &lbuf[1], INI_CHOICE);
|
2018-07-01 17:29:30 +00:00
|
|
|
}
|
2020-01-17 07:18:31 +00:00
|
|
|
else if (lblen > 1 && lbuf[0] == '{') // Create new caption. Support empty caption '{}'.
|
2018-08-21 01:37:40 +00:00
|
|
|
{
|
2019-02-23 22:35:24 +00:00
|
|
|
_find_section_name(lbuf, lblen, '}');
|
|
|
|
|
|
|
|
csec = _ini_create_section(dst, csec, &lbuf[1], INI_CAPTION);
|
2018-08-21 01:37:40 +00:00
|
|
|
csec->color = 0xFF0AB9E6;
|
|
|
|
}
|
2020-01-17 07:18:31 +00:00
|
|
|
else if (lblen > 2 && lbuf[0] == '#') // Create comment.
|
2018-08-21 01:37:40 +00:00
|
|
|
{
|
2019-02-23 22:35:24 +00:00
|
|
|
csec = _ini_create_section(dst, csec, &lbuf[1], INI_COMMENT);
|
2018-08-21 01:37:40 +00:00
|
|
|
}
|
2020-01-17 07:18:31 +00:00
|
|
|
else if (lblen < 2) // Create empty line.
|
2018-08-21 01:37:40 +00:00
|
|
|
{
|
2019-02-23 22:35:24 +00:00
|
|
|
csec = _ini_create_section(dst, csec, NULL, INI_NEWLINE);
|
2018-08-21 01:37:40 +00:00
|
|
|
}
|
2020-01-17 07:18:31 +00:00
|
|
|
else if (csec && csec->type == INI_CHOICE) // Extract key/value.
|
2018-07-01 17:29:30 +00:00
|
|
|
{
|
2019-02-23 22:35:24 +00:00
|
|
|
u32 i = _find_section_name(lbuf, lblen, '=');
|
2018-08-21 01:37:40 +00:00
|
|
|
|
2022-01-20 10:41:20 +00:00
|
|
|
// Calculate total allocation size.
|
2022-07-11 19:10:11 +00:00
|
|
|
u32 klen = strlen(&lbuf[0]) + 1;
|
|
|
|
u32 vlen = strlen(&lbuf[i + 1]) + 1;
|
2022-01-20 10:41:20 +00:00
|
|
|
char *buf = calloc(sizeof(ini_kv_t) + klen + vlen, 1);
|
|
|
|
|
|
|
|
ini_kv_t *kv = (ini_kv_t *)buf;
|
|
|
|
buf += sizeof(ini_kv_t);
|
|
|
|
kv->key = strcpy_ns(buf, &lbuf[0]);
|
|
|
|
buf += klen;
|
|
|
|
kv->val = strcpy_ns(buf, &lbuf[i + 1]);
|
2018-08-21 01:37:40 +00:00
|
|
|
list_append(&csec->kvs, &kv->link);
|
2018-07-01 17:29:30 +00:00
|
|
|
}
|
2018-08-21 01:37:40 +00:00
|
|
|
} while (!f_eof(&fp));
|
2018-07-01 17:29:30 +00:00
|
|
|
|
2022-05-19 12:03:00 +00:00
|
|
|
free(lbuf);
|
|
|
|
|
2018-08-21 01:37:40 +00:00
|
|
|
f_close(&fp);
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-08-22 00:38:25 +00:00
|
|
|
if (csec)
|
2019-02-23 22:35:24 +00:00
|
|
|
{
|
2018-08-22 00:38:25 +00:00
|
|
|
list_append(dst, &csec->link);
|
2019-02-23 22:35:24 +00:00
|
|
|
if (is_dir)
|
|
|
|
csec = NULL;
|
|
|
|
}
|
2018-08-22 00:38:25 +00:00
|
|
|
} while (is_dir);
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-08-21 01:37:40 +00:00
|
|
|
free(filename);
|
|
|
|
free(filelist);
|
|
|
|
|
2018-05-01 05:15:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-06-12 23:34:32 +00:00
|
|
|
|
2022-12-19 03:38:03 +00:00
|
|
|
char *ini_check_special_section(ini_sec_t *cfg)
|
2018-08-21 01:45:19 +00:00
|
|
|
{
|
|
|
|
if (cfg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &cfg->kvs, link)
|
|
|
|
{
|
2022-12-19 03:38:03 +00:00
|
|
|
if (!strcmp("l4t", kv->key))
|
|
|
|
return ((kv->val[0] == '1') ? (char *)-1 : NULL);
|
|
|
|
else if (!strcmp("payload", kv->key))
|
2019-08-27 22:08:57 +00:00
|
|
|
return kv->val;
|
2018-08-21 01:45:19 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 22:08:57 +00:00
|
|
|
return NULL;
|
2018-08-21 01:45:19 +00:00
|
|
|
}
|
2022-05-19 12:03:00 +00:00
|
|
|
|
|
|
|
void ini_free(link_t *src)
|
|
|
|
{
|
|
|
|
ini_sec_t *prev_sec = NULL;
|
|
|
|
|
|
|
|
// Parse and free all ini sections.
|
|
|
|
LIST_FOREACH_ENTRY(ini_sec_t, ini_sec, src, link)
|
|
|
|
{
|
2022-06-25 02:56:11 +00:00
|
|
|
ini_kv_t *prev_kv = NULL;
|
|
|
|
|
2022-05-19 12:03:00 +00:00
|
|
|
// Free all ini key allocations if they exist.
|
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &ini_sec->kvs, link)
|
|
|
|
{
|
|
|
|
// Free previous key.
|
2022-06-25 02:56:11 +00:00
|
|
|
if (prev_kv)
|
|
|
|
free(prev_kv);
|
2022-05-19 12:03:00 +00:00
|
|
|
|
|
|
|
// Set next key to free.
|
2022-06-25 02:56:11 +00:00
|
|
|
prev_kv = kv;
|
2022-05-19 12:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free last key.
|
2022-06-25 02:56:11 +00:00
|
|
|
if (prev_kv)
|
|
|
|
free(prev_kv);
|
2022-05-19 12:03:00 +00:00
|
|
|
|
|
|
|
// Free previous section.
|
|
|
|
if (prev_sec)
|
|
|
|
free(prev_sec);
|
|
|
|
|
|
|
|
// Set next section to free.
|
|
|
|
prev_sec = ini_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free last section.
|
|
|
|
if (prev_sec)
|
|
|
|
free(prev_sec);
|
|
|
|
}
|