]>
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
) {
51 #define nw_string_format(s, format, ...) \
52 __nw_string_format(s, sizeof(s), format, __VA_ARGS__)
54 static inline int __nw_string_format(char* s
, const size_t length
,
55 const char* format
, ...) {
59 // Call __nw_string_vformat
60 va_start(args
, format
);
61 r
= __nw_string_vformat(s
, length
, format
, args
);
67 #define nw_string_set(s, value) __nw_string_set(s, sizeof(s), value)
69 static inline int __nw_string_set(char* s
, const size_t length
, const char* value
) {
70 // If value is NULL, we will overwrite the buffer with just zeros
72 for (unsigned int i
= 0; i
< length
; i
++)
78 // Otherwise just copy
79 return __nw_string_format(s
, length
, "%s", value
);
82 static inline int nw_string_lstrip(char* s
) {
85 // Count any leading spaces
86 while (*p
&& isspace(*p
))
89 // Move the string to the beginning of the buffer
93 // Terminate the string
99 static inline int nw_string_rstrip(char* s
) {
100 ssize_t l
= strlen(s
) - 1;
102 while (l
>= 0 && isspace(s
[l
]))
108 static inline int nw_string_strip(char* s
) {
111 r
= nw_string_lstrip(s
);
115 r
= nw_string_rstrip(s
);
122 static inline void nw_string_empty(char* s
) {
131 typedef struct nw_string_table
{
136 static inline const char* nw_string_table_lookup_string(
137 const nw_string_table_t
* table
, const int id
) {
138 const nw_string_table_t
* entry
= NULL
;
140 for (entry
= table
; entry
->string
; entry
++)
142 return entry
->string
;
147 static inline int nw_string_table_lookup_id(
148 const nw_string_table_t
* table
, const char* string
) {
149 const nw_string_table_t
* entry
= NULL
;
151 for (entry
= table
; entry
->string
; entry
++)
152 if (strcmp(entry
->string
, string
) == 0)
158 #define NW_STRING_TABLE_LOOKUP_ID(type, table, method) \
159 __attribute__((unused)) static type method(const char* s) { \
160 return nw_string_table_lookup_id(table, s); \
163 #define NW_STRING_TABLE_LOOKUP_STRING(type, table, method) \
164 __attribute__((unused)) static const char* method(const type id) { \
165 return nw_string_table_lookup_string(table, id); \
168 #define NW_STRING_TABLE_LOOKUP(type, table) \
169 NW_STRING_TABLE_LOOKUP_ID(type, table, table ## _from_string) \
170 NW_STRING_TABLE_LOOKUP_STRING(type, table, table ## _to_string)
176 #define nw_path_join(s, first, second) \
177 __nw_path_join(s, sizeof(s), first, second)
179 static inline int __nw_path_join(char* s
, const size_t length
,
180 const char* first
, const char* second
) {
182 return __nw_string_format(s
, length
, "%s", second
);
185 return __nw_string_format(s
, length
, "%s", first
);
187 // Remove leading slashes from second argument
188 while (*second
== '/')
191 return __nw_string_format(s
, length
, "%s/%s", first
, second
);
194 static inline const char* nw_path_basename(const char* path
) {
195 const char* basename
= strrchr(path
, '/');
202 #endif /* NETWORKD_STRING_H */