microconflib/example.c
2025-12-16 15:29:59 +04:00

95 lines
2.6 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* example.c
*
* Copyright 2025
*
* 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 program is distributed in the hope that 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include "microconf.h"
/* Настройки */
#define TEST_FILE "microconf_test.conf"
#define LEN_MAX_VAL 20
#define LEN_MAX_KEY 10
#define VALS_COUNT 3
/* Массивы */
char vals[VALS_COUNT][LEN_MAX_VAL] = {""};
char keys[VALS_COUNT][LEN_MAX_KEY] = {""};
/* Готовый обработчик, требует только имена массивов */
#define INPUT_KEYS_NUM keys
#define INPUT_VALS_NUM vals
#include "handler.h"
/*
*
* Можно определить свой обработчик, например:
*
* save_data_t my_handler(unsigned long num) {
* save_data_t t;
* t.key = keys[num];
* t.value = vals[num];
* return t;
* }
*
*/
int main(void) {
/* Отрыть файл */
file_t fp = fopen(TEST_FILE, "r");
/*
* Структура данных:
*
* {
* максимальная_длинна_ключа,
* максимальная_длинна_значения,
* максимальноеоличество_ключей
* };
*
*/
config_info_t st = {LEN_MAX_KEY, LEN_MAX_VAL, VALS_COUNT};
/* Функция:
* int microconf_compile (
* файловая_структура,
* обработчик_ввода,
* структура,
* );
*/
int status = microconf_compile(fp, microconf_std_handler, st);
/* При ошибке - аварийно завершить */
if (status == EXIT_FAILURE) return status;
/* Вывести в консоль */
unsigned char num_read = 0;
while (num_read < 3) {
char* key = keys[num_read];
char* val = vals[num_read];
printf("Key=%s Value=%s\n", key, val);
num_read ++;
}
/* Закрытие файла и успешное завершение программы */
fclose(fp);
return EXIT_SUCCESS;
}