]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/util/string.c
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "spdk/stdinc.h"
36 #include "spdk/string.h"
39 spdk_vsprintf_append_realloc(char *buffer
, const char *format
, va_list args
)
43 int orig_size
= 0, new_size
;
45 /* Original buffer size */
47 orig_size
= strlen(buffer
);
50 /* Necessary buffer size */
51 va_copy(args_copy
, args
);
52 new_size
= vsnprintf(NULL
, 0, format
, args_copy
);
58 new_size
+= orig_size
+ 1;
60 new_buffer
= realloc(buffer
, new_size
);
61 if (new_buffer
== NULL
) {
65 vsnprintf(new_buffer
+ orig_size
, new_size
- orig_size
, format
, args
);
71 spdk_sprintf_append_realloc(char *buffer
, const char *format
, ...)
76 va_start(args
, format
);
77 ret
= spdk_vsprintf_append_realloc(buffer
, format
, args
);
84 spdk_vsprintf_alloc(const char *format
, va_list args
)
86 return spdk_vsprintf_append_realloc(NULL
, format
, args
);
90 spdk_sprintf_alloc(const char *format
, ...)
95 va_start(args
, format
);
96 ret
= spdk_vsprintf_alloc(format
, args
);
121 spdk_strsepq(char **stringp
, const char *delim
)
124 int quoted
= 0, bslash
= 0;
132 while (*q
!= '\0' && *q
!= '\n') {
133 /* eat quoted characters */
139 if (quoted
== '"' && *q
== '\\') {
143 } else if (*q
== quoted
) {
150 } else if (*q
== '\\') {
154 } else if (*q
== '"' || *q
== '\'') {
161 if (strchr(delim
, *q
) == NULL
) {
173 while (*q
!= '\0' && strchr(delim
, *q
) != NULL
) {
186 spdk_str_trim(char *s
)
196 while (*p
!= '\0' && isspace(*p
)) {
202 while (q
- 1 >= p
&& isspace(*(q
- 1))) {
207 /* if remove header, move */
220 spdk_strcpy_pad(void *dst
, const char *src
, size_t size
, int pad
)
226 memcpy(dst
, src
, len
);
227 memset((char *)dst
+ len
, pad
, size
- len
);
229 memcpy(dst
, src
, size
);
234 spdk_strlen_pad(const void *str
, size_t size
, int pad
)
236 const uint8_t *start
;
240 pad_byte
= (uint8_t)pad
;
241 start
= (const uint8_t *)str
;
247 iter
= start
+ size
- 1;
249 if (*iter
!= pad_byte
) {
250 return iter
- start
+ 1;
254 /* Hit the start of the string finding only pad_byte. */
262 spdk_parse_ip_addr(char *ip
, char **host
, char **port
)
285 } else if (*p
!= ':') {
318 spdk_str_chomp(char *s
)
320 size_t len
= strlen(s
);
324 if (s
[len
- 1] != '\r' && s
[len
- 1] != '\n') {
337 spdk_strerror_r(int errnum
, char *buf
, size_t buflen
)
341 #if defined(__USE_GNU)
343 new_buffer
= strerror_r(errnum
, buf
, buflen
);
344 if (new_buffer
!= NULL
) {
345 snprintf(buf
, buflen
, "%s", new_buffer
);
351 rc
= strerror_r(errnum
, buf
, buflen
);
355 snprintf(buf
, buflen
, "Unknown error %d", errnum
);
360 spdk_parse_capacity(const char *cap_str
, uint64_t *cap
, bool *has_prefix
)
365 rc
= sscanf(cap_str
, "%"SCNu64
"%c", cap
, &bin_prefix
);
369 } else if (rc
== 0) {
371 /* No scanf matches - the string does not start with a digit */
380 switch (bin_prefix
) {
391 *cap
*= 1024 * 1024 * 1024;
401 spdk_mem_all_zero(const void *data
, size_t size
)
403 const uint8_t *buf
= data
;
415 spdk_strtol(const char *nptr
, int base
)
420 /* Since strtoll() can legitimately return 0, LONG_MAX, or LONG_MIN
421 * on both success and failure, the calling program should set errno
422 * to 0 before the call.
426 val
= strtol(nptr
, &endptr
, base
);
428 if (!errno
&& *endptr
!= '\0') {
429 /* Non integer character was found. */
431 } else if (errno
== ERANGE
&& (val
== LONG_MAX
|| val
== LONG_MIN
)) {
432 /* Overflow occurred. */
434 } else if (errno
!= 0 && val
== 0) {
435 /* Other error occurred. */
437 } else if (val
< 0) {
438 /* Input string was negative number. */
446 spdk_strtoll(const char *nptr
, int base
)
451 /* Since strtoll() can legitimately return 0, LLONG_MAX, or LLONG_MIN
452 * on both success and failure, the calling program should set errno
453 * to 0 before the call.
457 val
= strtoll(nptr
, &endptr
, base
);
459 if (!errno
&& *endptr
!= '\0') {
460 /* Non integer character was found. */
462 } else if (errno
== ERANGE
&& (val
== LLONG_MAX
|| val
== LLONG_MIN
)) {
463 /* Overflow occurred. */
465 } else if (errno
!= 0 && val
== 0) {
466 /* Other error occurred. */
468 } else if (val
< 0) {
469 /* Input string was negative number. */