]>
git.ipfire.org Git - people/ms/network.git/blob - src/networkd/string.h
1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
21 #ifndef NETWORKD_STRING_H
22 #define NETWORKD_STRING_H
30 #define nw_string_vformat(s, format, ...) \
31 __nw_string_vformat(s, sizeof(s), format, __VA_ARGS__)
33 static inline int __nw_string_vformat(char* s
, const size_t length
,
34 const char* format
, va_list args
) {
35 // Write string to buffer
36 const ssize_t required
= vsnprintf(s
, length
, format
, args
);
42 // Check if the entire string could be written
43 if ((size_t)required
>= length
) {
52 #define nw_string_format(s, format, ...) \
53 __nw_string_format(s, sizeof(s), format, __VA_ARGS__)
55 static inline int __nw_string_format(char* s
, const size_t length
,
56 const char* format
, ...) {
60 // Call __nw_string_vformat
61 va_start(args
, format
);
62 r
= __nw_string_vformat(s
, length
, format
, args
);
68 #define nw_string_set(s, value) __nw_string_set(s, sizeof(s), value)
70 static inline int __nw_string_set(char* s
, const size_t length
, const char* value
) {
71 // If value is NULL, we will overwrite the buffer with just zeros
73 for (unsigned int i
= 0; i
< length
; i
++)
79 // Otherwise just copy
80 return __nw_string_format(s
, length
, "%s", value
);
83 static inline int nw_string_lstrip(char* s
) {
86 // Count any leading spaces
87 while (*p
&& isspace(*p
))
90 // Move the string to the beginning of the buffer
94 // Terminate the string
100 static inline int nw_string_rstrip(char* s
) {
101 ssize_t l
= strlen(s
) - 1;
103 while (l
>= 0 && isspace(s
[l
]))
109 static inline int nw_string_strip(char* s
) {
112 r
= nw_string_lstrip(s
);
116 r
= nw_string_rstrip(s
);
123 static inline void nw_string_empty(char* s
) {
132 typedef struct nw_string_table
{
137 static inline const char* nw_string_table_lookup_string(
138 const nw_string_table_t
* table
, const int id
) {
139 const nw_string_table_t
* entry
= NULL
;
141 for (entry
= table
; entry
->string
; entry
++)
143 return entry
->string
;
148 static inline int nw_string_table_lookup_id(
149 const nw_string_table_t
* table
, const char* string
) {
150 const nw_string_table_t
* entry
= NULL
;
152 for (entry
= table
; entry
->string
; entry
++)
153 if (strcmp(entry
->string
, string
) == 0)
159 #define NW_STRING_TABLE_LOOKUP_ID(type, table, method) \
160 __attribute__((unused)) static type method(const char* s) { \
161 return nw_string_table_lookup_id(table, s); \
164 #define NW_STRING_TABLE_LOOKUP_STRING(type, table, method) \
165 __attribute__((unused)) static const char* method(const type id) { \
166 return nw_string_table_lookup_string(table, id); \
169 #define NW_STRING_TABLE_LOOKUP(type, table) \
170 NW_STRING_TABLE_LOOKUP_ID(type, table, table ## _from_string) \
171 NW_STRING_TABLE_LOOKUP_STRING(type, table, table ## _to_string)
177 #define nw_path_join(s, first, second) \
178 __nw_path_join(s, sizeof(s), first, second)
180 static inline int __nw_path_join(char* s
, const size_t length
,
181 const char* first
, const char* second
) {
183 return __nw_string_format(s
, length
, "%s", second
);
186 return __nw_string_format(s
, length
, "%s", first
);
188 // Remove leading slashes from second argument
189 while (*second
== '/')
192 return __nw_string_format(s
, length
, "%s/%s", first
, second
);
195 static inline const char* nw_path_basename(const char* path
) {
196 const char* basename
= strrchr(path
, '/');
203 #endif /* NETWORKD_STRING_H */