dev ini base
This commit is contained in:
parent
2cc3d16113
commit
20f69d5af8
113
include/iini.h
113
include/iini.h
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
#include "sysenv.h"
|
#include "sysenv.h"
|
||||||
#if _OS_WIN
|
#if _OS_WIN
|
||||||
#define strcasecmp _stricmp
|
#define strcasecmp _stricmp
|
||||||
@ -43,18 +43,54 @@ static inline INICHAR* _skip_trailing(const INICHAR* str, const INICHAR* base)
|
|||||||
return (INICHAR*)str;
|
return (INICHAR*)str;
|
||||||
}
|
}
|
||||||
|
|
||||||
//static TCHAR* striptrailing(TCHAR* str)
|
static INICHAR* _strip_trailing(INICHAR* str)
|
||||||
|
{
|
||||||
|
INICHAR* ptr = _skip_trailing(strchr(str, '\0'), str);
|
||||||
|
assert(ptr != NULL);
|
||||||
|
*ptr = '\0';
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
//static TCHAR* cleanstring(TCHAR* string, enum quote_option* quotes)
|
||||||
//{
|
//{
|
||||||
// TCHAR* ptr = skiptrailing(_tcschr(str, '\0'), str);
|
// int isstring;
|
||||||
// assert(ptr != NULL);
|
// TCHAR* ep;
|
||||||
// *ptr = '\0';
|
//
|
||||||
// return str;
|
// assert(string != NULL);
|
||||||
|
// assert(quotes != NULL);
|
||||||
|
//
|
||||||
|
// /* Remove a trailing comment */
|
||||||
|
// isstring = 0;
|
||||||
|
// for (ep = string; *ep != '\0' && ((*ep != ';' && *ep != '#') || isstring); ep++) {
|
||||||
|
// if (*ep == '"') {
|
||||||
|
// if (*(ep + 1) == '"')
|
||||||
|
// ep++; /* skip "" (both quotes) */
|
||||||
|
// else
|
||||||
|
// isstring = !isstring; /* single quote, toggle isstring */
|
||||||
|
// }
|
||||||
|
// else if (*ep == '\\' && *(ep + 1) == '"') {
|
||||||
|
// ep++; /* skip \" (both quotes */
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// assert(ep != NULL && (*ep == '\0' || *ep == ';' || *ep == '#'));
|
||||||
|
// *ep = '\0'; /* terminate at a comment */
|
||||||
|
// striptrailing(string);
|
||||||
|
// /* Remove double quotes surrounding a value */
|
||||||
|
// *quotes = QUOTE_NONE;
|
||||||
|
// if (*string == '"' && (ep = _tcschr(string, '\0')) != NULL && *(ep - 1) == '"') {
|
||||||
|
// string++;
|
||||||
|
// *--ep = '\0';
|
||||||
|
// *quotes = QUOTE_DEQUOTE; /* this is a string, so remove escaped characters */
|
||||||
|
// }
|
||||||
|
// return string;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
//static inline int _
|
||||||
|
|
||||||
static int _get_key_string(FILE* fp, const INICHAR* section, const INICHAR* key,
|
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 idx_section, int idx_key, INICHAR* buf, size_t szbuf) {
|
||||||
int len = 0, idx = 0;
|
int len = 0, idx = 0;
|
||||||
INICHAR* start_ptr = NULL, * end_ptr = NULL;
|
INICHAR* start_ptr, * end_ptr;
|
||||||
INICHAR local_buf[INI_BUFFER_SIZE] = { 0 };
|
INICHAR local_buf[INI_BUFFER_SIZE] = { 0 };
|
||||||
|
|
||||||
assert(fp != NULL);
|
assert(fp != NULL);
|
||||||
@ -63,23 +99,47 @@ static int _get_key_string(FILE* fp, const INICHAR* section, const INICHAR* key,
|
|||||||
* parameter Section is NULL, only look at keys above the first section. If
|
* parameter Section is NULL, only look at keys above the first section. If
|
||||||
* idxSection is positive, copy the relevant section name.
|
* idxSection is positive, copy the relevant section name.
|
||||||
*/
|
*/
|
||||||
do {
|
if (len > 0 || idx_section >= 0) {
|
||||||
|
assert(idx_section >= 0 || section != NULL);
|
||||||
|
idx = -1;
|
||||||
do {
|
do {
|
||||||
if (fgets(local_buf, INI_BUFFER_SIZE, fp)) return 0;
|
do {
|
||||||
start_ptr = _skip_leading(local_buf);
|
if (fgets(local_buf, INI_BUFFER_SIZE, fp) == NULL) return 0;
|
||||||
end_ptr = strrchr(local_buf, ']');
|
start_ptr = _skip_leading(local_buf);
|
||||||
} while (*start_ptr != '[' || end_ptr == NULL);
|
end_ptr = strchr(local_buf, ']');
|
||||||
/* When arrived here, a section was found; now optionally skip leading and
|
} while (*start_ptr != '[' || end_ptr == NULL);
|
||||||
* trailing whitespace.
|
/* 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(start_ptr != NULL && *start_ptr == '[');
|
||||||
assert(end_ptr != NULL && *end_ptr == ']');
|
start_ptr = _skip_leading(start_ptr + 1);
|
||||||
end_ptr = _skip_trailing(end_ptr, start_ptr);
|
assert(end_ptr != NULL && *end_ptr == ']');
|
||||||
//} while (1);
|
end_ptr = _skip_trailing(end_ptr, start_ptr);
|
||||||
} while ((int)(end_ptr - start_ptr) == len);
|
} while (!((int)(end_ptr - start_ptr) == len && section != NULL && strncasecmp(start_ptr, section, len) == 0));
|
||||||
//( ((int)(end_ptr - start_ptr) != len || section == NULL || strncasecmp(start_ptr, section, len) != 0)
|
}
|
||||||
|
/* Now that the section has been found, find the entry.
|
||||||
|
* Stop searching upon leaving the section's area.
|
||||||
|
*/
|
||||||
|
assert(key != NULL || idx_key >= 0);
|
||||||
|
len = (key != NULL) ? (int)strlen(key) : 0;
|
||||||
|
idx = -1;
|
||||||
|
do {
|
||||||
|
if ((fgets(local_buf, INI_BUFFER_SIZE, fp) == NULL) || *(start_ptr = _skip_leading(local_buf)) == '[')
|
||||||
|
return 0;
|
||||||
|
start_ptr = _skip_leading(local_buf);
|
||||||
|
end_ptr = strchr(start_ptr, '='); /* Parse out the equal sign */
|
||||||
|
if (end_ptr == NULL)
|
||||||
|
end_ptr = strchr(start_ptr, ':');
|
||||||
|
} while (*start_ptr != ';' && *start_ptr != '#' && end_ptr != NULL &&
|
||||||
|
(!(len != 0 && (int)(_skip_trailing(end_ptr, start_ptr) - start_ptr) == len && strncasecmp(start_ptr, key, len) == 0)));
|
||||||
//&& ++idx != idx_section) );
|
//&& ++idx != idx_section) );
|
||||||
|
/* Copy up to BufferSize chars to buffer */
|
||||||
|
assert(end_ptr != NULL);
|
||||||
|
assert(*end_ptr == '=' || *end_ptr == ':');
|
||||||
|
start_ptr = _skip_leading(end_ptr + 1);
|
||||||
|
//sp = cleanstring(sp, "es); /* Remove a trailing comment */
|
||||||
|
strncpy(buf, start_ptr, szbuf);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ini_get_str(const INICHAR* filename, const INICHAR* section, const INICHAR* key,
|
static int ini_get_str(const INICHAR* filename, const INICHAR* section, const INICHAR* key,
|
||||||
@ -88,14 +148,15 @@ static int ini_get_str(const INICHAR* filename, const INICHAR* section, const IN
|
|||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
if (fp = fopen(filename, "rb")) {
|
if (fp = fopen(filename, "rb")) {
|
||||||
res = getkeystring();
|
res = _get_key_string(fp, section, key, -1, -1, buf, szbuf);
|
||||||
|
(void)fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res == 0) {
|
if (!res) {
|
||||||
res = strncpy(buf, (def_vaule == NULL) ? "" : def_vaule, szbuf);
|
strncpy(buf, ((def_vaule == NULL) ? (const char*)"" : def_vaule), szbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return strlen(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@ -1,8 +1,18 @@
|
|||||||
#include <iini.h>
|
#include <iini.h>
|
||||||
|
|
||||||
#include <llog.h>
|
//[First]
|
||||||
|
//String = noot # trailing commment
|
||||||
|
//Val = 1
|
||||||
|
//
|
||||||
|
//[Second]
|
||||||
|
//Val = 2
|
||||||
|
//#comment = 3
|
||||||
|
//String = mies
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
char buf[128] = { 0 };
|
||||||
|
ini_get_str("../test.ini", "First", "String", "nan", buf, 128);
|
||||||
|
printf("%s", buf);
|
||||||
|
if (getchar());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
8
test/ini/test.ini
Normal file
8
test/ini/test.ini
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[First]
|
||||||
|
String=noot # trailing commment
|
||||||
|
Val=1
|
||||||
|
|
||||||
|
[Second]
|
||||||
|
Val = 2
|
||||||
|
#comment=3
|
||||||
|
String = mies
|
Loading…
x
Reference in New Issue
Block a user