35 static int issection(
int c);
36 static int iskey(
int c);
37 static int isvalue(
int c);
39 static size_t skip(
const char *begin,
const char *end,
struct floc *at);
41 static void membuf_print_chars(
struct membuf *buf,
const char *s,
size_t n);
43 static void config_print_ini_func(
const char *section,
const char *key,
44 const char *value,
void *data);
56 const void *map =
frbuf_map(buf, 0, &size);
64 const char *begin = map;
65 const char *end = begin + size;
85 const char *cp =
begin;
91 cp += skip(cp,
end, at);
97 if ((
end && cp >=
end) || !*cp)
102 cp += skip(cp,
end, at);
104 membuf_print_chars(§ion, cp, chars);
106 cp += skip(cp,
end, at);
111 "expected ']' after section name");
114 "expected section name after '['");
117 }
else if ((chars =
lex_ctype(&iskey, cp,
end, at)) > 0) {
118 membuf_print_chars(&key, cp, chars);
120 cp += skip(cp,
end, at);
123 cp += skip(cp,
end, at);
142 "expected '\"' after string");
145 &isvalue, cp,
end, at);
146 membuf_print_chars(&value, cp, chars);
155 "expected '=' after key");
159 if (isgraph((
unsigned char)*cp))
161 "unknown character '%c'", *cp);
164 "unknown character '\\%o'",
222 } ctx = { .pbegin = pbegin, .end = end, .section = NULL, .chars = 0 };
232 return isgraph(c) && c !=
'#' && c !=
';' && c !=
'[' && c !=
']';
238 return isgraph(c) && c !=
'#' && c !=
';' && c !=
'=';
244 return isprint(c) && c !=
'#' && c !=
';';
248 skip(
const char *begin,
const char *end,
struct floc *at)
252 const char *cp = begin;
262 membuf_print_chars(
struct membuf *buf,
const char *s,
size_t n)
267 while (n && isspace((
unsigned char)s[n - 1]))
278 config_print_ini_func(
const char *section,
const char *key,
const char *value,
291 char **pbegin = ctx->pbegin;
292 char *end = ctx->end;
293 size_t chars = ctx->chars;
295 if (ctx->section != section) {
296 ctx->section = section;
317 size_t n = strlen(value);
319 int esc = isspace((
unsigned char)value[0])
320 || isspace((
unsigned char)value[n - 1]);
322 for (
size_t i = 0; !esc && i < n; i++)
323 esc = !isvalue((
unsigned char)value[i]);
This header file is part of the utilities library; it contains the configuration functions.
void config_foreach(const config_t *config, config_foreach_func_t *func, void *data)
Invokes a function for each key in a configuration struct.
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.
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.
frbuf_t * frbuf_create(const char *filename)
Creates a new read file buffer.
void frbuf_destroy(frbuf_t *buf)
Destroys a read file buffer.
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.
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.