2018-05-01 06:15:48 +01:00
|
|
|
/*
|
2018-08-05 12:40:32 +01:00
|
|
|
* Copyright (c) 2018 naehrwert
|
|
|
|
* Copyright (C) 2018 CTCaer
|
|
|
|
*
|
|
|
|
* 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 06:15:48 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ini.h"
|
2018-08-13 09:58:24 +01:00
|
|
|
#include "../libs/fatfs/ff.h"
|
|
|
|
#include "../mem/heap.h"
|
2018-08-21 02:37:40 +01:00
|
|
|
#include "../utils/dirlist.h"
|
2018-05-01 06:15:48 +01:00
|
|
|
|
|
|
|
static char *_strdup(char *str)
|
|
|
|
{
|
2018-08-21 02:37:40 +01:00
|
|
|
char *res = (char *)malloc(strlen(str) + 1);
|
2018-05-01 06:15:48 +01:00
|
|
|
strcpy(res, str);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
int ini_parse(link_t *dst, char *ini_path, bool is_dir)
|
2018-05-01 06:15:48 +01:00
|
|
|
{
|
|
|
|
u32 lblen;
|
2018-08-21 02:37:40 +01:00
|
|
|
u32 pathlen = strlen(ini_path);
|
|
|
|
u32 k = 0;
|
2018-05-01 06:15:48 +01:00
|
|
|
char lbuf[512];
|
2018-08-21 02:37:40 +01:00
|
|
|
char *filelist = NULL;
|
2018-05-01 06:15:48 +01:00
|
|
|
FIL fp;
|
|
|
|
ini_sec_t *csec = NULL;
|
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
char *filename = (char *)malloc(256);
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
memcpy(filename, ini_path, pathlen + 1);
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
if (is_dir)
|
|
|
|
{
|
|
|
|
filelist = dirlist(filename);
|
|
|
|
if (!filelist)
|
|
|
|
{
|
|
|
|
free(filename);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(filename + pathlen, "/", 2);
|
|
|
|
pathlen++;
|
|
|
|
}
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (is_dir)
|
2018-05-01 06:15:48 +01:00
|
|
|
{
|
2018-08-21 02:37:40 +01:00
|
|
|
if (filelist[k * 256])
|
2018-05-01 06:15:48 +01:00
|
|
|
{
|
2018-08-21 02:37:40 +01:00
|
|
|
memcpy(filename + pathlen, &filelist[k * 256], strlen(&filelist[k * 256]) + 1);
|
|
|
|
k++;
|
2018-05-01 06:15:48 +01:00
|
|
|
}
|
2018-08-21 02:37:40 +01:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
if (f_open(&fp, filename, FA_READ) != FR_OK)
|
|
|
|
{
|
|
|
|
free(filelist);
|
|
|
|
free(filename);
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
return 0;
|
2018-05-01 06:15:48 +01:00
|
|
|
}
|
2018-08-21 02:37:40 +01:00
|
|
|
|
|
|
|
do
|
2018-07-01 18:29:30 +01:00
|
|
|
{
|
2018-08-21 02:37:40 +01:00
|
|
|
// Fetch one line.
|
|
|
|
lbuf[0] = 0;
|
|
|
|
f_gets(lbuf, 512, &fp);
|
|
|
|
lblen = strlen(lbuf);
|
2018-07-01 18:29:30 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
// Remove trailing newline.
|
|
|
|
if (lbuf[lblen - 1] == '\n')
|
|
|
|
lbuf[lblen - 1] = 0;
|
2018-07-01 18:29:30 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
if (lblen > 2 && lbuf[0] == '[') // Create new section.
|
2018-07-01 18:29:30 +01:00
|
|
|
{
|
2018-08-21 02:37:40 +01:00
|
|
|
if (csec)
|
|
|
|
{
|
|
|
|
list_append(dst, &csec->link);
|
|
|
|
csec = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 i;
|
|
|
|
for (i = 0; i < lblen && lbuf[i] != '\n' && lbuf[i] != ']'; i++)
|
|
|
|
;
|
|
|
|
lbuf[i] = 0;
|
|
|
|
|
|
|
|
csec = (ini_sec_t *)malloc(sizeof(ini_sec_t));
|
|
|
|
csec->name = _strdup(&lbuf[1]);
|
|
|
|
csec->type = INI_CHOICE;
|
|
|
|
list_init(&csec->kvs);
|
2018-07-01 18:29:30 +01:00
|
|
|
}
|
2018-08-21 02:37:40 +01:00
|
|
|
else if (lblen > 2 && lbuf[0] == '{') //Create new caption.
|
|
|
|
{
|
|
|
|
if (csec)
|
|
|
|
{
|
|
|
|
list_append(dst, &csec->link);
|
|
|
|
csec = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 i;
|
|
|
|
for (i = 0; i < lblen && lbuf[i] != '\n' && lbuf[i] != '}'; i++)
|
|
|
|
;
|
|
|
|
lbuf[i] = 0;
|
|
|
|
|
|
|
|
csec = (ini_sec_t *)malloc(sizeof(ini_sec_t));
|
|
|
|
csec->name = _strdup(&lbuf[1]);
|
|
|
|
csec->type = INI_CAPTION;
|
|
|
|
csec->color = 0xFF0AB9E6;
|
|
|
|
}
|
|
|
|
else if (lblen > 2 && lbuf[0] == '#') //Create empty lines and comments.
|
|
|
|
{
|
|
|
|
if (csec)
|
|
|
|
{
|
|
|
|
list_append(dst, &csec->link);
|
|
|
|
csec = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 i;
|
|
|
|
for (i = 0; i < lblen && lbuf[i] != '\n'; i++)
|
|
|
|
;
|
|
|
|
lbuf[i] = 0;
|
|
|
|
|
|
|
|
csec = (ini_sec_t *)malloc(sizeof(ini_sec_t));
|
|
|
|
csec->name = _strdup(&lbuf[1]);
|
|
|
|
csec->type = INI_COMMENT;
|
|
|
|
}
|
|
|
|
else if (lblen < 2)
|
|
|
|
{
|
|
|
|
if (csec)
|
|
|
|
{
|
|
|
|
list_append(dst, &csec->link);
|
|
|
|
csec = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
csec = (ini_sec_t *)malloc(sizeof(ini_sec_t));
|
|
|
|
csec->name = NULL;
|
|
|
|
csec->type = INI_NEWLINE;
|
|
|
|
}
|
2018-08-23 02:37:02 +01:00
|
|
|
else if (csec && csec->type == INI_CHOICE) //Extract key/value.
|
2018-07-01 18:29:30 +01:00
|
|
|
{
|
2018-08-21 02:37:40 +01:00
|
|
|
u32 i;
|
|
|
|
for (i = 0; i < lblen && lbuf[i] != '\n' && lbuf[i] != '='; i++)
|
|
|
|
;
|
|
|
|
lbuf[i] = 0;
|
|
|
|
|
|
|
|
ini_kv_t *kv = (ini_kv_t *)malloc(sizeof(ini_kv_t));
|
|
|
|
kv->key = _strdup(&lbuf[0]);
|
|
|
|
kv->val = _strdup(&lbuf[i + 1]);
|
|
|
|
list_append(&csec->kvs, &kv->link);
|
2018-07-01 18:29:30 +01:00
|
|
|
}
|
2018-08-21 02:37:40 +01:00
|
|
|
} while (!f_eof(&fp));
|
2018-07-01 18:29:30 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
f_close(&fp);
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-08-22 01:38:25 +01:00
|
|
|
if (csec)
|
|
|
|
list_append(dst, &csec->link);
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-08-22 01:38:25 +01:00
|
|
|
} while (is_dir);
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
free(filename);
|
|
|
|
free(filelist);
|
|
|
|
|
2018-05-01 06:15:48 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2018-06-13 00:34:32 +01:00
|
|
|
|
|
|
|
void ini_free(link_t *dst)
|
|
|
|
{
|
2018-08-23 02:37:02 +01:00
|
|
|
if (!dst->prev || !dst->next)
|
2018-07-01 18:29:30 +01:00
|
|
|
return;
|
|
|
|
|
2018-06-13 00:34:32 +01:00
|
|
|
LIST_FOREACH_ENTRY(ini_sec_t, ini_sec, dst, link)
|
|
|
|
{
|
2018-07-01 18:29:30 +01:00
|
|
|
if (ini_sec->type == INI_CHOICE)
|
2018-06-13 00:34:32 +01:00
|
|
|
{
|
2018-07-01 18:29:30 +01:00
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &ini_sec->kvs, link)
|
|
|
|
{
|
|
|
|
free(kv->key);
|
|
|
|
free(kv->val);
|
2018-09-18 21:38:54 +01:00
|
|
|
//free(kv);
|
2018-07-01 18:29:30 +01:00
|
|
|
}
|
2018-06-13 00:34:32 +01:00
|
|
|
}
|
|
|
|
free(ini_sec->name);
|
2018-08-23 02:37:02 +01:00
|
|
|
//free(ini_sec);
|
2018-06-13 00:34:32 +01:00
|
|
|
}
|
2018-07-22 13:18:30 +01:00
|
|
|
|
2018-08-23 02:37:02 +01:00
|
|
|
list_init(dst);
|
2018-06-13 00:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ini_sec_t *ini_clone_section(ini_sec_t *cfg)
|
|
|
|
{
|
|
|
|
if (cfg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ini_sec_t *csec = (ini_sec_t *)malloc(sizeof(ini_sec_t));
|
|
|
|
list_init(&csec->kvs);
|
|
|
|
|
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &cfg->kvs, link)
|
|
|
|
{
|
|
|
|
ini_kv_t *kvcfg = (ini_kv_t *)malloc(sizeof(ini_kv_t));
|
|
|
|
kvcfg->key = _strdup(kv->key);
|
|
|
|
kvcfg->val = _strdup(kv->val);
|
|
|
|
list_append(&csec->kvs, &kvcfg->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
return csec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ini_free_section(ini_sec_t *cfg)
|
|
|
|
{
|
|
|
|
if (cfg == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &cfg->kvs, link)
|
|
|
|
{
|
|
|
|
free(kv->key);
|
|
|
|
free(kv->val);
|
|
|
|
free(kv);
|
|
|
|
}
|
|
|
|
free(cfg);
|
2018-07-22 13:18:30 +01:00
|
|
|
|
|
|
|
cfg = NULL;
|
2018-06-13 00:34:32 +01:00
|
|
|
}
|
2018-08-21 02:45:19 +01:00
|
|
|
|
|
|
|
char *ini_check_payload_section(ini_sec_t *cfg)
|
|
|
|
{
|
|
|
|
char *path = NULL;
|
|
|
|
|
|
|
|
if (cfg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &cfg->kvs, link)
|
|
|
|
{
|
|
|
|
if (!strcmp("payload", kv->key))
|
|
|
|
{
|
|
|
|
if (!path)
|
|
|
|
path = _strdup(kv->val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path)
|
2018-09-18 21:47:37 +01:00
|
|
|
return path;
|
2018-08-21 02:45:19 +01:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|