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