From 2cc3d161139bb6b2ebcddce97a3953a3b5c73b24 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Tue, 31 Oct 2023 23:04:17 +0800 Subject: [PATCH] dev 23.10.31 --- include/iini.h | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ test/ini/ini.c | 22 ++------- 2 files changed, 125 insertions(+), 18 deletions(-) diff --git a/include/iini.h b/include/iini.h index 1a207c7..2c99e3d 100644 --- a/include/iini.h +++ b/include/iini.h @@ -1,6 +1,127 @@ #ifndef _IINI_H_ #define _IINI_H_ +#include +#include +#include "sysenv.h" +#if _OS_WIN +#define strcasecmp _stricmp +#define strncasecmp _strnicmp +#elif _OS_LINUX +#include +#else +#error "Not Supported Operator System" +#endif + +#ifndef INICHAR +#define INICHAR char +#endif + +#if !defined INI_BUFFER_SIZE +#define INI_BUFFER_SIZE 512 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +static inline INICHAR* _skip_leading(const INICHAR* str) +{ + assert(str != NULL); + while ('\0' < *str && *str <= ' ') + str++; + return (INICHAR*)str; +} + +static inline INICHAR* _skip_trailing(const INICHAR* str, const INICHAR* base) +{ + assert(str != NULL); + assert(base != NULL); + while (str > base && '\0' < *(str - 1) && *(str - 1) <= ' ') + str--; + return (INICHAR*)str; +} + +//static TCHAR* striptrailing(TCHAR* str) +//{ +// TCHAR* ptr = skiptrailing(_tcschr(str, '\0'), str); +// assert(ptr != NULL); +// *ptr = '\0'; +// return str; +//} + +static int _get_key_string(FILE* fp, const INICHAR* section, const INICHAR* key, + int idx_section, int idx_key, INICHAR* buf, size_t szbuf) { + int len = 0, idx = 0; + INICHAR* start_ptr = NULL, * end_ptr = NULL; + INICHAR local_buf[INI_BUFFER_SIZE] = { 0 }; + + assert(fp != NULL); + len = (section != NULL) ? (int)strlen(section) : 0; + /* Move through file 1 line at a time until a section is matched or EOF. If + * parameter Section is NULL, only look at keys above the first section. If + * idxSection is positive, copy the relevant section name. + */ + do { + do { + if (fgets(local_buf, INI_BUFFER_SIZE, fp)) return 0; + start_ptr = _skip_leading(local_buf); + end_ptr = strrchr(local_buf, ']'); + } while (*start_ptr != '[' || end_ptr == NULL); + /* When arrived here, a section was found; now optionally skip leading and + * trailing whitespace. + */ + assert(start_ptr != NULL && *start_ptr == '['); + start_ptr = _skip_leading(start_ptr + 1); + assert(end_ptr != NULL && *end_ptr == ']'); + end_ptr = _skip_trailing(end_ptr, start_ptr); + //} while (1); + } while ((int)(end_ptr - start_ptr) == len); + //( ((int)(end_ptr - start_ptr) != len || section == NULL || strncasecmp(start_ptr, section, len) != 0) + //&& ++idx != idx_section) ); +} + +static int ini_get_str(const INICHAR* filename, const INICHAR* section, const INICHAR* key, + const INICHAR* def_vaule, INICHAR* buf, size_t szbuf) { + FILE* fp = NULL; + int res = 0; + + if (fp = fopen(filename, "rb")) { + res = getkeystring(); + } + + if (res == 0) { + res = strncpy(buf, (def_vaule == NULL) ? "" : def_vaule, szbuf); + } + + return res; +} + +#ifdef __cplusplus +} +#endif + +/* minIni - Multi-Platform INI file parser, suitable for embedded systems + * + * These routines are in part based on the article "Multiplatform .INI Files" + * by Joseph J. Graf in the March 1994 issue of Dr. Dobb's Journal. + * + * Copyright (c) CompuPhase, 2008-2021 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * Version: $Id: minIni.c 53 2015-01-18 13:35:11Z thiadmer.riemersma@gmail.com $ + */ #endif /* _IINI_H_ */ \ No newline at end of file diff --git a/test/ini/ini.c b/test/ini/ini.c index 4dc8845..259a813 100644 --- a/test/ini/ini.c +++ b/test/ini/ini.c @@ -1,22 +1,8 @@ -#include #include -int main() { - char log[128] = { 0 }; - size_t szlog = sizeof(log); - log_head_trace(log, szlog); - printf("%s\n", log); - log_head_debug(log, szlog); - printf("%s\n", log); - log_head_info(log, szlog); - printf("%s\n", log); - log_head_warn(log, szlog); - printf("%s\n", log); - log_head_error(log, szlog); - printf("%s\n", log); - log_head_fatal(log, szlog); - printf("%s\n", log); - getchar(); - //LOG_COLOR(23); +#include + +int main() { + return 0; } \ No newline at end of file