38static int issection(
int c);
39static int iskey(
int c);
40static int isvalue(
int c);
42static size_t skip(
const char *begin,
const char *end,
struct floc *at);
44static void membuf_print_chars(
struct membuf *buf,
const char *s,
size_t n);
46static void config_print_ini_func(
const char *section,
const char *key,
47 const char *value,
void *data);
59 const void *map =
frbuf_map(buf, 0, &size);
67 const char *begin = map;
68 const char *end = begin + size;
88 const char *cp =
begin;
94 cp += skip(cp,
end, at);
100 if ((
end && cp >=
end) || !*cp)
105 cp += skip(cp,
end, at);
107 membuf_print_chars(§ion, cp, chars);
109 cp += skip(cp,
end, at);
114 "expected ']' after section name");
117 "expected section name after '['");
120 }
else if ((chars =
lex_ctype(&iskey, cp,
end, at)) > 0) {
121 membuf_print_chars(&key, cp, chars);
123 cp += skip(cp,
end, at);
126 cp += skip(cp,
end, at);
145 "expected '\"' after string");
148 &isvalue, cp,
end, at);
149 membuf_print_chars(&value, cp, chars);
158 "expected '=' after key");
162 if (isgraph((
unsigned char)*cp))
164 "unknown character '%c'", *cp);
167 "unknown character '\\%o'",
225 } ctx = { .pbegin = pbegin, .end = end, .section = NULL, .chars = 0 };
235 return isgraph(c) && c !=
'#' && c !=
';' && c !=
'[' && c !=
']';
241 return isgraph(c) && c !=
'#' && c !=
';' && c !=
'=';
247 return isprint(c) && c !=
'#' && c !=
';';
251skip(
const char *begin,
const char *end,
struct floc *at)
255 const char *cp = begin;
265membuf_print_chars(
struct membuf *buf,
const char *s,
size_t n)
270 while (n && isspace((
unsigned char)s[n - 1]))
281config_print_ini_func(
const char *section,
const char *key,
const char *value,
294 char **pbegin = ctx->pbegin;
295 char *end = ctx->end;
296 size_t chars = ctx->chars;
298 if (ctx->section != section) {
299 ctx->section = section;
320 size_t n = strlen(value);
322 int esc = isspace((
unsigned char)value[0])
323 || isspace((
unsigned char)value[n - 1]);
325 for (
size_t i = 0; !esc && i < n; i++)
326 esc = !isvalue((
unsigned char)value[i]);
This header file is part of the utilities library; it contains the configuration functions.
const char * config_set(config_t *config, const char *section, const char *key, const char *value)
Sets a key in or removes a key from a configuration struct.
void config_foreach(const config_t *config, config_foreach_func_t *func, void *data)
Invokes a function for each key in a configuration struct.
size_t config_parse_ini_file(config_t *config, const char *filename)
Parses an INI file and adds the keys to a configuration struct.
size_t config_print_ini_file(const config_t *config, const char *filename)
Prints a configuration struct to an INI file.
size_t config_parse_ini_text(config_t *config, const char *begin, const char *end, struct floc *at)
Parses a string in INI-format and adds the keys to a configuration struct.
size_t config_print_ini_text(const config_t *config, char **pbegin, char *end)
Prints a configuration struct in INI-format to a memory buffer.
This header file is part of the utilities library; it contains the diagnostic declarations.
void diag_if(enum diag_severity severity, int errc, const struct floc *at, const char *format,...)
Emits a diagnostic message occurring at a location in a text file.
void diag(enum diag_severity severity, int errc, const char *format,...)
Emits a diagnostic message.
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
This header file is part of the utilities library; it contains the read file buffer declarations.
const void * frbuf_map(frbuf_t *buf, intmax_t pos, size_t *psize)
Maps (part of) the contents of a read file buffer to memory.
void frbuf_destroy(frbuf_t *buf)
Destroys a read file buffer.
frbuf_t * frbuf_create(const char *filename)
Creates a new read file buffer.
This header file is part of the utilities library; it contains the (atomic) write file buffer declara...
void * fwbuf_map(fwbuf_t *buf, intmax_t pos, size_t *psize)
Maps (part of) the contents of a write file buffer to memory.
void fwbuf_destroy(fwbuf_t *buf)
Destroys a write file buffer.
int fwbuf_commit(fwbuf_t *buf)
Commits all changes to a write file buffer to disk if all previous file operations were successful,...
fwbuf_t * fwbuf_create(const char *filename)
Creates a new (atomic) write file buffer.
This header file is part of the utilities library; it contains the lexer function declarations.
size_t lex_char(int c, const char *begin, const char *end, struct floc *at)
Lexes the specified character from a memory buffer.
size_t lex_ctype(int(*ctype)(int), const char *begin, const char *end, struct floc *at)
Greedily lexes a sequence of characters of the specified class from a memory buffer.
size_t lex_line_comment(const char *delim, const char *begin, const char *end, struct floc *at)
Lexes a single line-comment (excluding the line break) starting with the specified delimiter from a m...
size_t lex_c99_str(const char *begin, const char *end, struct floc *at, char *s, size_t *pn)
Lexes a UTF-8 encoded Unicode string from a memory buffer.
size_t lex_break(const char *begin, const char *end, struct floc *at)
Lexes a single line break from a memory buffer.
This header file is part of the utilities library; it contains the memory buffer declarations.
void * membuf_begin(const struct membuf *buf)
Returns a pointer to the first byte in a memory buffer.
size_t membuf_reserve(struct membuf *buf, size_t size)
Resizes a memory buffer, if necessary, to make room for at least an additional size bytes.
void membuf_fini(struct membuf *buf)
Finalizes a memory buffer.
size_t membuf_write(struct membuf *buf, const void *ptr, size_t size)
Writes data to a memory buffer.
void * membuf_alloc(struct membuf *buf, size_t *size)
Creates region of *size bytes in a memory buffer, starting at the current position indicator given by...
void membuf_clear(struct membuf *buf)
Clears a memory buffer.
#define MEMBUF_INIT
The static initializer for struct membuf.
This header file is part of the utilities library; it contains the printing function declarations.
size_t print_c99_str(char **pbegin, char *end, const char *s, size_t n)
Prints a UTF-8 encoded Unicode string to a memory buffer.
size_t print_char(char **pbegin, char *end, int c)
Prints a single character to a memory buffer.
This is the internal header file of the utilities library.
An read file buffer struct.
An (atomic) write file buffer struct.
A location in a text file.
const char * filename
The name of the file.
char * end
A pointer to one past the last byte in the buffer.
char * begin
A pointer to the first byte in the buffer.