File: strfuncs.h

package info (click to toggle)
gpsd 3.27-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,056 kB
  • sloc: ansic: 74,438; python: 16,521; sh: 890; cpp: 848; php: 225; makefile: 197; perl: 111; javascript: 26; xml: 11
file content (61 lines) | stat: -rw-r--r-- 1,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * strfuncs.h - string functions
 *
 * This file is Copyright by the GPSD project
 * SPDX-License-Identifier: BSD-2-clause
 */
#ifndef _GPSD_STRFUNCS_H_
#define _GPSD_STRFUNCS_H_

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "compiler.h"


static inline bool str_starts_with(const char *str, const char *prefix)
{
    return 0 == strncmp(str, prefix, strlen(prefix));
}


PRINTF_FUNC(3, 4)
static inline void str_appendf(char *str, size_t alloc_size,
                               const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    (void)vsnprintf(str + strlen(str), alloc_size - strlen(str), format, ap);
    va_end(ap);
}


static inline void str_vappendf(char *str, size_t alloc_size,
                                const char *format, va_list ap)
{
    (void) vsnprintf(str + strlen(str), alloc_size - strlen(str), format, ap);
}


static inline void str_rstrip_char(char *str, char ch)
{
    if (0 != strlen(str) &&
        str[strlen(str) - 1] == ch) {
        str[strlen(str) - 1] = '\0';
    }
}

/* memset() for a volatile destination
 * dest = destination
 * c = fill character
 * count = sizeof(dest)
 */
static inline void memset_volatile(volatile void *dest, char c, size_t count)
{
    volatile char *ptr = (volatile char*)dest;
    while (0 < count--) {
        *ptr++ = c;
    }
}
#endif  // _GPSD_STRFUNCS_H_