]> git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/cutils.h
hw: Use IEC binary prefix definitions from "qemu/units.h"
[mirror_qemu.git] / include / qemu / cutils.h
1 #ifndef QEMU_CUTILS_H
2 #define QEMU_CUTILS_H
3
4 #include "qemu/fprintf-fn.h"
5 #include "qemu/units.h"
6
7 /**
8 * pstrcpy:
9 * @buf: buffer to copy string into
10 * @buf_size: size of @buf in bytes
11 * @str: string to copy
12 *
13 * Copy @str into @buf, including the trailing NUL, but do not
14 * write more than @buf_size bytes. The resulting buffer is
15 * always NUL terminated (even if the source string was too long).
16 * If @buf_size is zero or negative then no bytes are copied.
17 *
18 * This function is similar to strncpy(), but avoids two of that
19 * function's problems:
20 * * if @str fits in the buffer, pstrcpy() does not zero-fill the
21 * remaining space at the end of @buf
22 * * if @str is too long, pstrcpy() will copy the first @buf_size-1
23 * bytes and then add a NUL
24 */
25 void pstrcpy(char *buf, int buf_size, const char *str);
26 /**
27 * strpadcpy:
28 * @buf: buffer to copy string into
29 * @buf_size: size of @buf in bytes
30 * @str: string to copy
31 * @pad: character to pad the remainder of @buf with
32 *
33 * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
34 * rest of the buffer with the @pad character. If @str is too large
35 * for the buffer then it is truncated, so that @buf contains the
36 * first @buf_size characters of @str, with no terminator.
37 */
38 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
39 /**
40 * pstrcat:
41 * @buf: buffer containing existing string
42 * @buf_size: size of @buf in bytes
43 * @s: string to concatenate to @buf
44 *
45 * Append a copy of @s to the string already in @buf, but do not
46 * allow the buffer to overflow. If the existing contents of @buf
47 * plus @str would total more than @buf_size bytes, then write
48 * as much of @str as will fit followed by a NUL terminator.
49 *
50 * @buf must already contain a NUL-terminated string, or the
51 * behaviour is undefined.
52 *
53 * Returns: @buf.
54 */
55 char *pstrcat(char *buf, int buf_size, const char *s);
56 /**
57 * strstart:
58 * @str: string to test
59 * @val: prefix string to look for
60 * @ptr: NULL, or pointer to be written to indicate start of
61 * the remainder of the string
62 *
63 * Test whether @str starts with the prefix @val.
64 * If it does (including the degenerate case where @str and @val
65 * are equal) then return true. If @ptr is not NULL then a
66 * pointer to the first character following the prefix is written
67 * to it. If @val is not a prefix of @str then return false (and
68 * @ptr is not written to).
69 *
70 * Returns: true if @str starts with prefix @val, false otherwise.
71 */
72 int strstart(const char *str, const char *val, const char **ptr);
73 /**
74 * stristart:
75 * @str: string to test
76 * @val: prefix string to look for
77 * @ptr: NULL, or pointer to be written to indicate start of
78 * the remainder of the string
79 *
80 * Test whether @str starts with the case-insensitive prefix @val.
81 * This function behaves identically to strstart(), except that the
82 * comparison is made after calling qemu_toupper() on each pair of
83 * characters.
84 *
85 * Returns: true if @str starts with case-insensitive prefix @val,
86 * false otherwise.
87 */
88 int stristart(const char *str, const char *val, const char **ptr);
89 /**
90 * qemu_strnlen:
91 * @s: string
92 * @max_len: maximum number of bytes in @s to scan
93 *
94 * Return the length of the string @s, like strlen(), but do not
95 * examine more than @max_len bytes of the memory pointed to by @s.
96 * If no NUL terminator is found within @max_len bytes, then return
97 * @max_len instead.
98 *
99 * This function has the same behaviour as the POSIX strnlen()
100 * function.
101 *
102 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
103 */
104 int qemu_strnlen(const char *s, int max_len);
105 /**
106 * qemu_strsep:
107 * @input: pointer to string to parse
108 * @delim: string containing delimiter characters to search for
109 *
110 * Locate the first occurrence of any character in @delim within
111 * the string referenced by @input, and replace it with a NUL.
112 * The location of the next character after the delimiter character
113 * is stored into @input.
114 * If the end of the string was reached without finding a delimiter
115 * character, then NULL is stored into @input.
116 * If @input points to a NULL pointer on entry, return NULL.
117 * The return value is always the original value of *@input (and
118 * so now points to a NUL-terminated string corresponding to the
119 * part of the input up to the first delimiter).
120 *
121 * This function has the same behaviour as the BSD strsep() function.
122 *
123 * Returns: the pointer originally in @input.
124 */
125 char *qemu_strsep(char **input, const char *delim);
126 #ifdef HAVE_STRCHRNUL
127 static inline const char *qemu_strchrnul(const char *s, int c)
128 {
129 return strchrnul(s, c);
130 }
131 #else
132 const char *qemu_strchrnul(const char *s, int c);
133 #endif
134 time_t mktimegm(struct tm *tm);
135 int qemu_fdatasync(int fd);
136 int fcntl_setfl(int fd, int flag);
137 int qemu_parse_fd(const char *param);
138 int qemu_strtoi(const char *nptr, const char **endptr, int base,
139 int *result);
140 int qemu_strtoui(const char *nptr, const char **endptr, int base,
141 unsigned int *result);
142 int qemu_strtol(const char *nptr, const char **endptr, int base,
143 long *result);
144 int qemu_strtoul(const char *nptr, const char **endptr, int base,
145 unsigned long *result);
146 int qemu_strtoi64(const char *nptr, const char **endptr, int base,
147 int64_t *result);
148 int qemu_strtou64(const char *nptr, const char **endptr, int base,
149 uint64_t *result);
150
151 int parse_uint(const char *s, unsigned long long *value, char **endptr,
152 int base);
153 int parse_uint_full(const char *s, unsigned long long *value, int base);
154
155 int qemu_strtosz(const char *nptr, char **end, uint64_t *result);
156 int qemu_strtosz_MiB(const char *nptr, char **end, uint64_t *result);
157 int qemu_strtosz_metric(const char *nptr, char **end, uint64_t *result);
158
159 /* used to print char* safely */
160 #define STR_OR_NULL(str) ((str) ? (str) : "null")
161
162 bool buffer_is_zero(const void *buf, size_t len);
163 bool test_buffer_is_zero_next_accel(void);
164
165 /*
166 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
167 * Input is limited to 14-bit numbers
168 */
169
170 int uleb128_encode_small(uint8_t *out, uint32_t n);
171 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
172
173 #endif