dev ini base not include comment part
This commit is contained in:
parent
20f69d5af8
commit
cc42c35c6e
@ -26,7 +26,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
static inline INICHAR* _skip_leading(const INICHAR* str)
|
||||
static inline INICHAR* _ini_skip_leading(const INICHAR* str)
|
||||
{
|
||||
assert(str != NULL);
|
||||
while ('\0' < *str && *str <= ' ')
|
||||
@ -34,7 +34,7 @@ static inline INICHAR* _skip_leading(const INICHAR* str)
|
||||
return (INICHAR*)str;
|
||||
}
|
||||
|
||||
static inline INICHAR* _skip_trailing(const INICHAR* str, const INICHAR* base)
|
||||
static inline INICHAR* _ini_skip_trailing(const INICHAR* str, const INICHAR* base)
|
||||
{
|
||||
assert(str != NULL);
|
||||
assert(base != NULL);
|
||||
@ -43,9 +43,9 @@ static inline INICHAR* _skip_trailing(const INICHAR* str, const INICHAR* base)
|
||||
return (INICHAR*)str;
|
||||
}
|
||||
|
||||
static INICHAR* _strip_trailing(INICHAR* str)
|
||||
static INICHAR* _ini_str_skip_trailing(INICHAR* str)
|
||||
{
|
||||
INICHAR* ptr = _skip_trailing(strchr(str, '\0'), str);
|
||||
INICHAR* ptr = _ini_skip_trailing(strchr(str, '\0'), str);
|
||||
assert(ptr != NULL);
|
||||
*ptr = '\0';
|
||||
return str;
|
||||
@ -85,7 +85,45 @@ static INICHAR* _strip_trailing(INICHAR* str)
|
||||
// return string;
|
||||
//}
|
||||
|
||||
//static inline int _
|
||||
static void _ini_clean_string(INICHAR** start_pptr, INICHAR** end_pptr) {
|
||||
/* Remove a trailing comment */
|
||||
int is_string = 0;
|
||||
*start_pptr = _ini_skip_leading(*end_pptr + 1);
|
||||
for (*end_pptr = *start_pptr; **end_pptr != '#' && **end_pptr != ';' && **end_pptr != '\0'; *end_pptr += 1) {
|
||||
//if(end)
|
||||
}
|
||||
**end_pptr = ' ';
|
||||
*end_pptr = _ini_skip_trailing(*end_pptr, *start_pptr);
|
||||
**end_pptr = '\0';
|
||||
}
|
||||
|
||||
static inline int _ini_str_get_section(INICHAR **start_pptr, INICHAR **end_pptr,
|
||||
INICHAR* buf, size_t szbuf) {
|
||||
*start_pptr = _ini_skip_leading(buf);
|
||||
*end_pptr = strchr(buf, ']');
|
||||
if (**start_pptr != '[' || *end_pptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
/* When arrived here, a section was found; now optionally skip leading and
|
||||
* trailing whitespace.
|
||||
*/
|
||||
assert(*start_pptr != NULL && **start_pptr == '[');
|
||||
*start_pptr = _ini_skip_leading(*start_pptr + 1);
|
||||
assert(*end_pptr != NULL && **end_pptr == ']');
|
||||
*end_pptr = _ini_skip_trailing(*end_pptr, *start_pptr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int _ini_str_get_key(INICHAR** start_pptr, INICHAR** end_pptr,
|
||||
INICHAR* buf, size_t szbuf) {
|
||||
*start_pptr = _ini_skip_leading(buf);
|
||||
if (**start_pptr == '[' || **start_pptr == '#' || **start_pptr == ':') return -1;
|
||||
|
||||
*end_pptr = strchr(*start_pptr, '='); /* Parse out the equal sign */
|
||||
if (*end_pptr == NULL) *end_pptr = strchr(*start_pptr, ':');
|
||||
if (*end_pptr == NULL) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _get_key_string(FILE* fp, const INICHAR* section, const INICHAR* key,
|
||||
int idx_section, int idx_key, INICHAR* buf, size_t szbuf) {
|
||||
@ -103,18 +141,8 @@ static int _get_key_string(FILE* fp, const INICHAR* section, const INICHAR* key,
|
||||
assert(idx_section >= 0 || section != NULL);
|
||||
idx = -1;
|
||||
do {
|
||||
do {
|
||||
if (fgets(local_buf, INI_BUFFER_SIZE, fp) == NULL) return 0;
|
||||
start_ptr = _skip_leading(local_buf);
|
||||
end_ptr = strchr(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);
|
||||
if (fgets(local_buf, INI_BUFFER_SIZE, fp) == NULL) return -1;
|
||||
if (_ini_str_get_section(&start_ptr, &end_ptr, local_buf, INI_BUFFER_SIZE) != 0) continue;
|
||||
} while (!((int)(end_ptr - start_ptr) == len && section != NULL && strncasecmp(start_ptr, section, len) == 0));
|
||||
}
|
||||
/* Now that the section has been found, find the entry.
|
||||
@ -124,22 +152,14 @@ static int _get_key_string(FILE* fp, const INICHAR* section, const INICHAR* key,
|
||||
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) );
|
||||
/* 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 */
|
||||
if (fgets(local_buf, INI_BUFFER_SIZE, fp) == NULL) return -1;
|
||||
if (_ini_str_get_key(&start_ptr, &end_ptr, local_buf, INI_BUFFER_SIZE) != 0) continue;
|
||||
} while (len != 0 && strncasecmp(start_ptr, key, len) != 0);
|
||||
|
||||
_ini_clean_string(&start_ptr, &end_ptr);
|
||||
/* Copy up to BufferSize chars to buffer */
|
||||
strncpy(buf, start_ptr, szbuf);
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ini_get_str(const INICHAR* filename, const INICHAR* section, const INICHAR* key,
|
||||
@ -149,14 +169,15 @@ static int ini_get_str(const INICHAR* filename, const INICHAR* section, const IN
|
||||
|
||||
if (fp = fopen(filename, "rb")) {
|
||||
res = _get_key_string(fp, section, key, -1, -1, buf, szbuf);
|
||||
(void)fclose(fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
if (res != 0) {
|
||||
strncpy(buf, ((def_vaule == NULL) ? (const char*)"" : def_vaule), szbuf);
|
||||
buf[szbuf] = '\0';
|
||||
}
|
||||
|
||||
return strlen(buf);
|
||||
res = strlen(buf);
|
||||
return res;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -9,10 +9,18 @@
|
||||
//#comment = 3
|
||||
//String = mies
|
||||
|
||||
#define FILE_PATH "../test.ini"
|
||||
|
||||
int main() {
|
||||
char buf[128] = { 0 };
|
||||
ini_get_str("../test.ini", "First", "String", "nan", buf, 128);
|
||||
printf("%s", buf);
|
||||
printf("First\n");
|
||||
ini_get_str(FILE_PATH, "First", "String", "nan", buf, 128);
|
||||
printf("%s\n", buf);
|
||||
ini_get_str(FILE_PATH, "First", "Val", "nan", buf, 128);
|
||||
printf("%s\n", buf);
|
||||
ini_get_str(FILE_PATH, "First", "Other", "nan", buf, 128);
|
||||
printf("%s\n", buf);
|
||||
|
||||
if (getchar());
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user