]>
git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/cutils.h
4 #include "qemu/fprintf-fn.h"
5 #include "qemu/units.h"
9 * @buf: buffer to copy string into
10 * @buf_size: size of @buf in bytes
11 * @str: string to copy
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.
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
25 void pstrcpy(char *buf
, int buf_size
, const char *str
);
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
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.
38 void strpadcpy(char *buf
, int buf_size
, const char *str
, char pad
);
41 * @buf: buffer containing existing string
42 * @buf_size: size of @buf in bytes
43 * @s: string to concatenate to @buf
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.
50 * @buf must already contain a NUL-terminated string, or the
51 * behaviour is undefined.
55 char *pstrcat(char *buf
, int buf_size
, const char *s
);
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
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).
70 * Returns: true if @str starts with prefix @val, false otherwise.
72 int strstart(const char *str
, const char *val
, const char **ptr
);
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
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
85 * Returns: true if @str starts with case-insensitive prefix @val,
88 int stristart(const char *str
, const char *val
, const char **ptr
);
92 * @max_len: maximum number of bytes in @s to scan
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
99 * This function has the same behaviour as the POSIX strnlen()
102 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
104 int qemu_strnlen(const char *s
, int max_len
);
107 * @input: pointer to string to parse
108 * @delim: string containing delimiter characters to search for
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).
121 * This function has the same behaviour as the BSD strsep() function.
123 * Returns: the pointer originally in @input.
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
)
129 return strchrnul(s
, c
);
132 const char *qemu_strchrnul(const char *s
, int c
);
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
,
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
,
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
,
148 int qemu_strtou64(const char *nptr
, const char **endptr
, int base
,
151 int parse_uint(const char *s
, unsigned long long *value
, char **endptr
,
153 int parse_uint_full(const char *s
, unsigned long long *value
, int base
);
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
);
159 /* used to print char* safely */
160 #define STR_OR_NULL(str) ((str) ? (str) : "null")
162 bool buffer_is_zero(const void *buf
, size_t len
);
163 bool test_buffer_is_zero_next_accel(void);
166 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
167 * Input is limited to 14-bit numbers
170 int uleb128_encode_small(uint8_t *out
, uint32_t n
);
171 int uleb128_decode_small(const uint8_t *in
, uint32_t *n
);