Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
bounded_ini.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** @file bounded_ini.h
19 * @brief Allocation-free flat key=value mechanics; callers own schemas and storage. */
20#ifndef USHER_BOUNDED_INI_H
21#define USHER_BOUNDED_INI_H
22
23#include <ctype.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <math.h>
27#include <stdbool.h>
28#include <stddef.h>
29#include <stdint.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/stat.h>
33#include <unistd.h>
34
35typedef bool (*ini_field_handler)(void *context, const char *key, const char *value);
36
37/** @brief Trim surrounding whitespace in a mutable INI field.
38 * @param[in,out] text Null-terminated field storage.
39 * @return Borrowed pointer into @p text.
40 * @details Terminate the field in place so the parser need not allocate
41 * storage for individual keys or values. */
42static inline char *ini_trim(char *text)
43{
44 while (isspace((unsigned char)*text)) ++text;
45 char *end = text + strlen(text);
46 while (end > text && isspace((unsigned char)end[-1])) --end;
47 *end = '\0';
48 return text;
49}
50
51/** @brief Parse digits-only uint64; overflow and signs are rejected. */
52static inline bool ini_uint(const char *text, uint64_t *number)
53{
54 if (!text || !*text || !number) return false;
55 uint64_t value = 0;
56 for (const unsigned char *digit = (const unsigned char *)text; *digit; ++digit) {
57 if (*digit < '0' || *digit > '9') return false;
58 const unsigned next = *digit - '0';
59 if (value > (UINT64_MAX - next) / 10) return false;
60 value = value * 10 + next;
61 }
62 *number = value;
63 return true;
64}
65
66/** @brief Parse finite decimal/exponent notation, rejecting range errors and suffixes. */
67static inline bool ini_real(const char *text, double *number)
68{
69 if (!text || !*text || !number) return false;
70 const char *cursor = text;
71 if (*cursor == '+' || *cursor == '-') ++cursor;
72 bool digit = false;
73 while (*cursor >= '0' && *cursor <= '9') { digit = true; ++cursor; }
74 if (*cursor == '.') {
75 ++cursor;
76 while (*cursor >= '0' && *cursor <= '9') { digit = true; ++cursor; }
77 }
78 if (!digit) return false;
79 if (*cursor == 'e' || *cursor == 'E') {
80 ++cursor;
81 if (*cursor == '+' || *cursor == '-') ++cursor;
82 const char *start = cursor;
83 while (*cursor >= '0' && *cursor <= '9') ++cursor;
84 if (cursor == start) return false;
85 }
86 if (*cursor) return false;
87 errno = 0;
88 char *end = NULL;
89 const double value = strtod(text, &end);
90 if (errno == ERANGE || *end || !isfinite(value)) return false;
91 *number = value;
92 return true;
93}
94
95/** @brief Read a nonempty regular file smaller than capacity and append NUL.
96 * @details Checks descriptor size/mtime across reading. Outputs are usable only
97 * on true; callers supply stable paths and validate exposure identity separately. */
98static inline bool ini_read_file(const char *filename, char *buffer, size_t capacity, size_t *length)
99{
100 if (!filename || !*filename || !buffer || capacity < 2 || !length) return false;
101 const int descriptor = open(filename, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
102 if (descriptor < 0) return false;
103 struct stat before, after;
104 size_t used = 0;
105 bool valid = false;
106 if (fstat(descriptor, &before) || !S_ISREG(before.st_mode) || before.st_size <= 0
107 || (uint64_t)before.st_size >= capacity) goto done;
108 while (used < capacity) {
109 const ssize_t count = read(descriptor, buffer + used, capacity - used);
110 if (count < 0 && errno == EINTR) continue;
111 if (count < 0) goto done;
112 if (!count) break;
113 used += (size_t)count;
114 }
115 if (!used || used >= capacity || used != (size_t)before.st_size
116 || fstat(descriptor, &after) || after.st_size != before.st_size
117 || after.st_mtim.tv_sec != before.st_mtim.tv_sec
118 || after.st_mtim.tv_nsec != before.st_mtim.tv_nsec) goto done;
119 buffer[used] = '\0';
120 *length = used;
121 valid = true;
122done:
123 if (close(descriptor)) valid = false;
124 return valid;
125}
126
127/** @brief Destructively parse flat fields with blank lines and #/; comment lines.
128 * @pre Buffer has at least length+1 writable bytes; callbacks retain no pointers.
129 * @details Rejects embedded NUL, sections and empty keys/values. The handler must
130 * reject unknown/duplicate keys and validate completeness after successful parsing.
131 * There are no quoting, escaping, multiline or inline-comment rules. */
132static inline bool ini_parse(char *buffer, size_t length, ini_field_handler handler, void *context)
133{
134 if (!buffer || !length || !handler || memchr(buffer, '\0', length)) return false;
135 buffer[length] = '\0';
136 char *line = buffer;
137 char *limit = buffer + length;
138 while (line < limit) {
139 char *next = (char *)memchr(line, '\n', (size_t)(limit - line));
140 if (next) *next++ = '\0';
141 else next = limit;
142 char *entry = ini_trim(line);
143 line = next;
144 if (!*entry || *entry == '#' || *entry == ';') continue;
145 char *separator = strchr(entry, '=');
146 if (!separator) return false;
147 *separator++ = '\0';
148 const char *key = ini_trim(entry);
149 const char *value = ini_trim(separator);
150 if (!*key || !*value) return false;
151 for (const unsigned char *character = (const unsigned char *)key; *character; ++character) {
152 if (!isalnum(*character) && *character != '_' && *character != '.') return false;
153 }
154 if (!handler(context, key, value)) return false;
155 }
156 return true;
157}
158
159#endif
static bool ini_uint(const char *text, uint64_t *number)
Parse digits-only uint64; overflow and signs are rejected.
Definition bounded_ini.h:52
static bool ini_real(const char *text, double *number)
Parse finite decimal/exponent notation, rejecting range errors and suffixes.
Definition bounded_ini.h:67
static bool ini_read_file(const char *filename, char *buffer, size_t capacity, size_t *length)
Read a nonempty regular file smaller than capacity and append NUL.
Definition bounded_ini.h:98
bool(* ini_field_handler)(void *context, const char *key, const char *value)
Definition bounded_ini.h:35
static char * ini_trim(char *text)
Trim surrounding whitespace in a mutable INI field.
Definition bounded_ini.h:42
static bool ini_parse(char *buffer, size_t length, ini_field_handler handler, void *context)
Destructively parse flat fields with blank lines and #/; comment lines.
static unsigned capacity[COVERAGE_CAMERAS]
int descriptor
Definition chdk_pipe.c:142
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
Definition chdkbridge.c:56
static bool start(uint32_t session)
Admit a nonzero session and discard correlation from the previous one.
static size_t count
Definition pose_log.c:79
bool valid
Definition usher.c:537