Beta
This commit is contained in:
parent
9745dcd8f4
commit
20fa833843
71
microconf.h
71
microconf.h
@ -33,6 +33,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef FILE* file_t;
|
||||
typedef struct {
|
||||
@ -45,6 +46,22 @@ typedef struct {
|
||||
char* value;
|
||||
} save_data_t;
|
||||
|
||||
void _remove_spaces_end(char* str) {
|
||||
if (str != NULL) {
|
||||
long pos = strlen(str);
|
||||
if (pos == 0) return;
|
||||
pos --;
|
||||
while (pos != 0) {
|
||||
if (is_space_test(str[pos])) str[pos] = '\0';
|
||||
else {
|
||||
if (str[pos + 1] != '\0') str[pos + 1] = '\0';
|
||||
return;
|
||||
}
|
||||
pos --;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int _microconf_read_file(file_t fp, save_data_t (*input_handler) (ulong), size_t max_len, size_t max_len_val, ulong max_cnt) {
|
||||
|
||||
int c;
|
||||
@ -52,6 +69,8 @@ int _microconf_read_file(file_t fp, save_data_t (*input_handler) (ulong), size_t
|
||||
bool is_space = true;
|
||||
bool read_key = false;
|
||||
bool read_val = false;
|
||||
bool read_val_spaces = true;
|
||||
bool print_warning = true;
|
||||
ulong num = 0;
|
||||
size_t key_addr = 0;
|
||||
size_t val_addr = 0;
|
||||
@ -68,20 +87,24 @@ int _microconf_read_file(file_t fp, save_data_t (*input_handler) (ulong), size_t
|
||||
read_val = false;
|
||||
key_addr = 0;
|
||||
val_addr = 0;
|
||||
read_val_spaces = true;
|
||||
print_warning = true;
|
||||
str_num ++;
|
||||
}
|
||||
/* Считать комментарием все до конца строки */
|
||||
if (c == '#') is_comment = true;
|
||||
/* Если символ не входит в комментарий, то: */
|
||||
if (!is_comment) {
|
||||
/* Пропустить пробелы в начале строки */
|
||||
/* Пропустить пробелы в начале и конце строки */
|
||||
if (is_space && !is_space_test(c)) {
|
||||
is_space = false;
|
||||
read_key = true;
|
||||
if (num == max_cnt) {
|
||||
puts("\033[1mmicroconf: \033[91mThe memory is full!\033[0m Code: READ_COUNT_ERR");
|
||||
puts("\033[1mmicroconf: \033[91mError: The memory is full!\033[0m Code: READ_COUNT_ERR");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
_remove_spaces_end(sv.key);
|
||||
_remove_spaces_end(sv.value);
|
||||
sv = input_handler(num);
|
||||
num ++;
|
||||
key_addr = 0;
|
||||
@ -90,19 +113,23 @@ int _microconf_read_file(file_t fp, save_data_t (*input_handler) (ulong), size_t
|
||||
}
|
||||
/* Чтение значения */
|
||||
if (read_val) {
|
||||
if (c == '\n' || c == '#' || c == EOF) {
|
||||
val_addr --;
|
||||
sv.value[val_addr] = '\0';
|
||||
read_val = false;
|
||||
}
|
||||
if (read_val) {
|
||||
if (sv.value != NULL) {
|
||||
sv.value[val_addr] = c;
|
||||
}
|
||||
val_addr ++;
|
||||
if (key_addr >= max_len) {
|
||||
puts("\033[1mmicroconf: \033[91mThe memory is full!\033[0m Code: READ_VAL_ERR");
|
||||
return EXIT_FAILURE;
|
||||
if (read_val_spaces && !is_space_test(c)) read_val_spaces = false;
|
||||
if (!read_val_spaces) {
|
||||
if (c == '\n' || c == '#' || c == EOF) {
|
||||
val_addr --;
|
||||
sv.value[val_addr] = '\0';
|
||||
read_val = false;
|
||||
}
|
||||
if (read_val) {
|
||||
if (sv.value != NULL) {
|
||||
sv.value[val_addr] = c;
|
||||
}
|
||||
val_addr ++;
|
||||
if (val_addr >= max_len_val && print_warning) {
|
||||
puts("\033[1mmicroconf: \033[35mWarning: The memory is full!\033[0m Code: READ_VAL_ERR");
|
||||
val_addr --;
|
||||
print_warning = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -120,26 +147,30 @@ int _microconf_read_file(file_t fp, save_data_t (*input_handler) (ulong), size_t
|
||||
sv.key[key_addr] = c;
|
||||
}
|
||||
key_addr ++;
|
||||
if (key_addr >= max_len) {
|
||||
puts("\033[1mmicroconf: \033[91mThe memory is full!\033[0m Code: READ_KEY_ERR");
|
||||
return EXIT_FAILURE;
|
||||
if (key_addr >= max_len && print_warning) {
|
||||
puts("\033[1mmicroconf: \033[35mWarning: The memory is full!\033[0m Code: READ_KEY_ERR");
|
||||
key_addr --;
|
||||
print_warning = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Проверка синтаксиса файла */
|
||||
if ((c == '\n' || c == '#' || c == EOF) && read_key) {
|
||||
printf("\033[1mmicroconf: \033[91mSyntax error on line %lu\033[0m\n", str_num);
|
||||
printf("\033[1mmicroconf: \033[91mError: Syntax error on line %lu\033[0m\n", str_num);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
/* Сохраняем старое значение в переменную */
|
||||
old = c;
|
||||
}
|
||||
/* Удаляем пробелы в конце строки */
|
||||
_remove_spaces_end(sv.key);
|
||||
_remove_spaces_end(sv.value);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int microconf_compile(file_t fp, save_data_t (*input_handler) (ulong), config_info_t st) {
|
||||
if (fp == NULL) {
|
||||
puts("\033[1mmicroconf: \033[91mUnable to open file!\033[0m");
|
||||
puts("\033[1mmicroconf: \033[91mError: Unable to open file!\033[0m");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
rewind(fp);
|
||||
|
||||
@ -20,6 +20,6 @@
|
||||
#
|
||||
#
|
||||
|
||||
key_1::value_1 # Value
|
||||
key_2::value_2 # Value
|
||||
key_3::value_3 # Value
|
||||
key_1 :: value_1 # Value
|
||||
key_2 :: value_2 # Value
|
||||
key_3 :: value_3 # Value
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user