Skip to content

Instantly share code, notes, and snippets.

@emctague
Created November 8, 2019 13:18
Show Gist options
  • Save emctague/9a6e29e7eb94d08f5a928c642f366df6 to your computer and use it in GitHub Desktop.
Save emctague/9a6e29e7eb94d08f5a928c642f366df6 to your computer and use it in GitHub Desktop.
uini - Single header-file INI parser
uini: header-only INI parser
by Ethan McTague - November 8, 2019
Example:
#include "uini.h"
void handler(const char *section, const char *key, const char *value, void *user) {
printf("%s::%s = %s\n", section, key, value);
}
int main () {
FILE *f = fopen("test.ini", "r");
uini_parse(f, handler, NULL);
fclose(f);
}
Explanation:
`uini_parse` is the main function of this library, parsing an INI file.
arguments:
- file - The file to parse from
- handler - A function pointer that handles each property
- user - A convenience pointer passed to the handler
`handler` is a function pointer to handle properties.
arguments:
- section - The name of the section (denoted by [name] in an ini file)
- key - The name of the property being set
- value - The value of the property being set (can be NULL if no value is provided)
- user - The pointer passed as the final argument of 'uini_parse', for convenience
`uini_trim` is a helper used by `uini_parse` that trims whitespace from both ends of a string.
arguments:
- in - The string to trim - will be modified in-place
returns: pointer to the start of the trimmed string
Licensing / use:
Feel free to use uini in your project. It is licensed under the MIT license.
/*
uini - single header-file INI parser
Copyright (c) 2019 Ethan McTague
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/** Trim whitespace from start and end of string. */
static inline char *uini_trim (char *in) {
if (!in) return NULL;
while (isspace(*in)) in++;
char *end = in + strlen(in) - 1;
while (end > in && isspace(*end)) end--;
end[1] = '\0';
return in;
}
/** uini_parse - simple INI file parser
* For each property in the INI file `f`, `handler` is invoked, passing:
* - The section (no section = empty string) containing the property.
* - The name of the property.
* - The value of the property.
* - The user pointer passed as `user` to parseINI.
* Syntax errors are mostly ignored silently and flexibly.
* Failure to provide a value for a property will result in `value` being passed as NULL.
*/
static inline void uini_parse(FILE *f, void (*handler)(const char *section, const char *name, const char *value, void *user), void *user)
{
char *section = strdup(""), *line = NULL, *clean = NULL, *context = NULL;
size_t read = 0;
while (getline(&line, &read, f) > 0) {
char *clean = uini_trim(strtok_r(line, ";\r\n", &context));
if (!clean || !*clean) continue;
if (*clean == '[') {
free(section);
section = uini_trim(strdup(strtok_r(++clean, "]", &context)));
} else {
const char *key = uini_trim(strtok_r(clean, "=", &context));
if (!*key) continue;
handler(section, key, uini_trim(strtok_r(NULL, "\r\n", &context)), user);
}
}
free(section);
free(line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment