UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
string.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2014-2024 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <assert.h>
39#include <ctype.h>
40#include <stdint.h>
41#include <signal.h>
42#include <string.h>
43
44#ifdef _WIN32
45#include <io.h>
46#else
47#include <unistd.h>
48#endif
49
50#include "debug.h"
51#include "utils/string.h"
52
71void replace_all(char *in, const char *from, const char *to) {
72 assert(strlen(from) >= strlen(to) && "Longer dst pattern than src!");
73 assert(strlen(from) > 0 && "From pattern should be non-empty!");
74 char *tmp = in;
75 while ((tmp = strstr(tmp, from)) != NULL) {
76 memcpy(tmp, to, strlen(to));
77 if (strlen(to) < strlen(from)) { // move the rest
78 size_t len = strlen(tmp + strlen(from));
79 char *src = tmp + strlen(from);
80 char *dst = tmp + strlen(to);
81 memmove(dst, src, len);
82 dst[len] = '\0';
83 }
84 tmp += strlen(to);
85 }
86}
87
91bool is_prefix_of(const char *haystack, const char *needle) {
92 return strncasecmp(haystack, needle, strlen(needle)) == 0;
93}
94
95char *strrpbrk(char *s, const char *accept) {
96 char *end = s + strlen(s) - 1;
97 while (end >= s) {
98 const char *accept_it = accept;
99 while (*accept_it != '\0') {
100 if (*accept_it++ == *end) {
101 return end;
102 }
103 }
104 end--;
105 }
106 return NULL;
107}
108
109bool ends_with(const char *haystick, const char *needle) {
110 if (strlen(haystick) < strlen(needle)) {
111 return false;
112 }
113 return strcmp(haystick + strlen(haystick) - strlen(needle), needle) == 0;
114}
115
135void strappend(char **ptr, const char *ptr_end, const char *src) {
136 while (*src != '\0') {
137 if (*ptr == ptr_end) {
138 return;
139 }
140 *(*ptr)++ = *src++;
141 }
142}
143
144void write_all(int fd, size_t len, const char *msg) {
145 const char *ptr = msg;
146 do {
147 ssize_t written = write(fd, ptr, len);
148 if (written < 0) {
149 break;
150 }
151 len -= written;
152 ptr += written;
153 } while (len > 0);
154}
155
162void
163append_sig_desc(char **ptr, const char *ptr_end, int signum)
164{
165 strappend(ptr, ptr_end, " (");
166#ifdef _WIN32
167#define SIGERR(x) [x] = #x
168 // from MinGW signal.h, unsure if other signals occur natively in MSW
169 const char *signames[NSIG] = {
170 SIGERR(SIGINT), SIGERR(SIGILL), SIGERR(SIGABRT_COMPAT),
171 SIGERR(SIGFPE), SIGERR(SIGSEGV), SIGERR(SIGTERM),
172 SIGERR(SIGBREAK), SIGERR(SIGABRT), // SIGERR(SIGABRT2),
173 };
174#undef SIGERR
175 if (signames[signum] == NULL) {
176 strappend(ptr, ptr_end, "UNKNOWN");
177 } else {
178 strappend(ptr, ptr_end, signames[signum]);
179 }
180#elif __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 32)
181 strappend(ptr, ptr_end, sigabbrev_np(signum));
182 strappend(ptr, ptr_end, " - ");
183 strappend(ptr, ptr_end, sigdescr_np(signum));
184#else
185 strappend(ptr, ptr_end, sys_siglist[signum]);
186#endif
187 strappend(ptr, ptr_end, ")");
188}
189
198const char *
199pretty_print_fourcc(const void *fcc)
200{
201 enum {
202 CHAR_LEN = 5, // at worst [XXX]
203 CHARS = sizeof(uint32_t),
204 MAX_LEN = CHARS * CHAR_LEN + 1 /* '\0' */,
205 };
206 _Thread_local static char out[MAX_LEN];
207 char *out_ptr = out;
208 const unsigned char *fourcc = fcc;
209
210 for (int i = 0; i < CHARS; ++i) {
211 if (isprint(fourcc[i])) {
212 *out_ptr++ = (char) fourcc[i];
213 } else {
214 const int written =
215 snprintf(out_ptr, CHAR_LEN + 1, "[%hhu]", fourcc[i]);
216 out_ptr+= written;
217 }
218 }
219 *out_ptr = '\0';
220 return out;
221}
int void size_t * len
Definition misc.h:94
static ssize_t write_all(fd_t fd, const void *buf, size_t count)
struct msghdr msg
static void strappend(char **dst, size_t *dst_alloc_len, const char *src)
Definition sdp.c:366
bool is_prefix_of(const char *haystack, const char *needle)
Definition string.c:91
char * strrpbrk(char *s, const char *accept)
same as strpbrk but finds in a reverse order (last occurence returned)
Definition string.c:95
bool ends_with(const char *haystick, const char *needle)
Definition string.c:109
void replace_all(char *in, const char *from, const char *to)
Replaces all occurencies of 'from' to 'to' in string 'in'.
Definition string.c:71
const char * pretty_print_fourcc(const void *fcc)
void append_sig_desc(char **ptr, const char *ptr_end, int signum)