2015-10-02 18:13:11 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
2016-01-26 17:47:22 +00:00
|
|
|
* Copyright (c) Intel Corporation.
|
2015-10-02 18:13:11 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2016-04-29 18:04:33 +00:00
|
|
|
/** \file
|
|
|
|
* String utility functions
|
|
|
|
*/
|
|
|
|
|
2015-10-02 18:13:11 +00:00
|
|
|
#ifndef SPDK_STRING_H
|
|
|
|
#define SPDK_STRING_H
|
|
|
|
|
2017-05-01 20:22:48 +00:00
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
|
2016-02-12 14:52:35 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-10-02 18:13:11 +00:00
|
|
|
/**
|
|
|
|
* sprintf with automatic buffer allocation.
|
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* The return value is the formatted string, which should be passed to free()
|
|
|
|
* when no longer needed.
|
|
|
|
*
|
|
|
|
* \param format Format for the string to print.
|
|
|
|
*
|
|
|
|
* \return the formatted string on success, or NULL on failure.
|
2015-10-02 18:13:11 +00:00
|
|
|
*/
|
2016-02-08 21:13:07 +00:00
|
|
|
char *spdk_sprintf_alloc(const char *format, ...) __attribute__((format(printf, 1, 2)));
|
2015-10-02 18:13:11 +00:00
|
|
|
|
2016-11-18 17:48:24 +00:00
|
|
|
/**
|
|
|
|
* vsprintf with automatic buffer allocation.
|
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* The return value is the formatted string, which should be passed to free()
|
|
|
|
* when no longer needed.
|
|
|
|
*
|
|
|
|
* \param format Format for the string to print.
|
|
|
|
* \param args A value that identifies a variable arguments list.
|
|
|
|
*
|
|
|
|
* \return the formatted string on success, or NULL on failure.
|
2016-11-18 17:48:24 +00:00
|
|
|
*/
|
|
|
|
char *spdk_vsprintf_alloc(const char *format, va_list args);
|
|
|
|
|
2018-12-12 06:00:01 +00:00
|
|
|
/**
|
|
|
|
* Append string using vsprintf with automatic buffer re-allocation.
|
|
|
|
*
|
|
|
|
* The return value is the formatted string, in which the original string in
|
|
|
|
* buffer is unchanged and the specified formatted string is appended.
|
|
|
|
*
|
|
|
|
* The returned string should be passed to free() when no longer needed.
|
|
|
|
*
|
|
|
|
* If buffer is NULL, the call is equivalent to spdk_sprintf_alloc().
|
|
|
|
* If the call fails, the original buffer is left untouched.
|
|
|
|
*
|
|
|
|
* \param buffer Buffer which has a formatted string.
|
|
|
|
* \param format Format for the string to print.
|
|
|
|
*
|
|
|
|
* \return the formatted string on success, or NULL on failure.
|
|
|
|
*/
|
|
|
|
char *spdk_sprintf_append_realloc(char *buffer, const char *format, ...);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append string using vsprintf with automatic buffer re-allocation.
|
|
|
|
* The return value is the formatted string, in which the original string in
|
|
|
|
* buffer is unchanged and the specified formatted string is appended.
|
|
|
|
*
|
|
|
|
* The returned string should be passed to free() when no longer needed.
|
|
|
|
*
|
|
|
|
* If buffer is NULL, the call is equivalent to spdk_sprintf_alloc().
|
|
|
|
* If the call fails, the original buffer is left untouched.
|
|
|
|
*
|
|
|
|
* \param buffer Buffer which has a formatted string.
|
|
|
|
* \param format Format for the string to print.
|
|
|
|
* \param args A value that identifies a variable arguments list.
|
|
|
|
*
|
|
|
|
* \return the formatted string on success, or NULL on failure.
|
|
|
|
*/
|
|
|
|
char *spdk_vsprintf_append_realloc(char *buffer, const char *format, va_list args);
|
|
|
|
|
2016-04-28 23:30:56 +00:00
|
|
|
/**
|
|
|
|
* Convert string to lowercase in place.
|
|
|
|
*
|
|
|
|
* \param s String to convert to lowercase.
|
2018-04-09 02:50:30 +00:00
|
|
|
*
|
|
|
|
* \return the converted string.
|
2016-04-28 23:30:56 +00:00
|
|
|
*/
|
|
|
|
char *spdk_strlwr(char *s);
|
|
|
|
|
2016-04-28 23:39:47 +00:00
|
|
|
/**
|
|
|
|
* Parse a delimited string with quote handling.
|
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* Note that the string will be modified in place to add the string terminator
|
|
|
|
* to each field.
|
2016-04-28 23:39:47 +00:00
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* \param stringp Pointer to starting location in string. *stringp will be updated
|
|
|
|
* to point to the start of the next field, or NULL if the end of the string has
|
|
|
|
* been reached.
|
|
|
|
* \param delim Null-terminated string containing the list of accepted delimiters.
|
2016-04-28 23:39:47 +00:00
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* \return a pointer to beginning of the current field.
|
2016-04-28 23:39:47 +00:00
|
|
|
*/
|
|
|
|
char *spdk_strsepq(char **stringp, const char *delim);
|
|
|
|
|
2016-04-28 23:41:45 +00:00
|
|
|
/**
|
|
|
|
* Trim whitespace from a string in place.
|
|
|
|
*
|
|
|
|
* \param s String to trim.
|
2018-04-09 02:50:30 +00:00
|
|
|
*
|
|
|
|
* \return the trimmed string.
|
2016-04-28 23:41:45 +00:00
|
|
|
*/
|
|
|
|
char *spdk_str_trim(char *s);
|
|
|
|
|
2017-08-11 23:27:04 +00:00
|
|
|
/**
|
|
|
|
* Copy the string version of an error into the user supplied buffer
|
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* \param errnum Error code.
|
|
|
|
* \param buf Pointer to a buffer in which to place the error message.
|
|
|
|
* \param buflen The size of the buffer in bytes.
|
2017-08-11 23:27:04 +00:00
|
|
|
*/
|
2017-12-27 18:03:04 +00:00
|
|
|
void spdk_strerror_r(int errnum, char *buf, size_t buflen);
|
2017-08-11 23:27:04 +00:00
|
|
|
|
2017-11-07 12:56:52 +00:00
|
|
|
/**
|
2018-04-09 02:50:30 +00:00
|
|
|
* Return the string version of an error from a static, thread-local buffer. This
|
|
|
|
* function is thread safe.
|
2017-11-07 12:56:52 +00:00
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* \param errnum Error code.
|
2017-11-07 12:56:52 +00:00
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* \return a pointer to buffer upon success.
|
2017-11-07 12:56:52 +00:00
|
|
|
*/
|
|
|
|
const char *spdk_strerror(int errnum);
|
|
|
|
|
2017-07-20 16:24:57 +00:00
|
|
|
/**
|
|
|
|
* Remove trailing newlines from the end of a string in place.
|
|
|
|
*
|
2018-06-18 14:26:07 +00:00
|
|
|
* Any sequence of trailing \\r and \\n characters is removed from the end of the
|
2018-04-09 02:50:30 +00:00
|
|
|
* string.
|
2017-07-20 16:24:57 +00:00
|
|
|
*
|
|
|
|
* \param s String to remove newline from.
|
2018-04-09 02:50:30 +00:00
|
|
|
*
|
|
|
|
* \return the number of characters removed.
|
2017-07-20 16:24:57 +00:00
|
|
|
*/
|
|
|
|
size_t spdk_str_chomp(char *s);
|
|
|
|
|
2016-08-19 17:01:13 +00:00
|
|
|
/**
|
2018-04-09 02:50:30 +00:00
|
|
|
* Copy a string into a fixed-size buffer, padding extra bytes with a specific
|
|
|
|
* character.
|
|
|
|
*
|
|
|
|
* If src is longer than size, only size bytes will be copied.
|
2016-08-19 17:01:13 +00:00
|
|
|
*
|
|
|
|
* \param dst Pointer to destination fixed-size buffer to fill.
|
|
|
|
* \param src Pointer to source null-terminated string to copy into dst.
|
|
|
|
* \param size Number of bytes to fill in dst.
|
|
|
|
* \param pad Character to pad extra space in dst beyond the size of src.
|
|
|
|
*/
|
|
|
|
void spdk_strcpy_pad(void *dst, const char *src, size_t size, int pad);
|
|
|
|
|
2016-11-21 20:06:24 +00:00
|
|
|
/**
|
|
|
|
* Find the length of a string that has been padded with a specific byte.
|
|
|
|
*
|
|
|
|
* \param str Right-padded string to find the length of.
|
|
|
|
* \param size Size of the full string pointed to by str, including padding.
|
|
|
|
* \param pad Character that was used to pad str up to size.
|
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* \return the length of the non-padded portion of str.
|
2016-11-21 20:06:24 +00:00
|
|
|
*/
|
|
|
|
size_t spdk_strlen_pad(const void *str, size_t size, int pad);
|
|
|
|
|
2017-02-01 17:05:51 +00:00
|
|
|
/**
|
2018-04-09 02:50:30 +00:00
|
|
|
* Parse an IP address into its hostname and port components. This modifies the
|
|
|
|
* IP address in place.
|
2017-02-01 17:05:51 +00:00
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* \param ip A null terminated IP address, including port. Both IPv4 and IPv6
|
|
|
|
* are supported.
|
|
|
|
* \param host Will point to the start of the hostname within ip. The string will
|
|
|
|
* be null terminated.
|
|
|
|
* \param port Will point to the start of the port within ip. The string will be
|
|
|
|
* null terminated.
|
2017-02-01 17:05:51 +00:00
|
|
|
*
|
2018-10-12 13:39:57 +00:00
|
|
|
* \return 0 on success. -EINVAL on failure.
|
2017-02-01 17:05:51 +00:00
|
|
|
*/
|
2017-02-10 23:00:51 +00:00
|
|
|
int spdk_parse_ip_addr(char *ip, char **host, char **port);
|
2017-02-01 17:05:51 +00:00
|
|
|
|
2017-12-13 19:21:28 +00:00
|
|
|
/**
|
|
|
|
* Parse a string representing a number possibly followed by a binary prefix.
|
2018-04-09 02:50:30 +00:00
|
|
|
*
|
2017-12-13 19:21:28 +00:00
|
|
|
* The string can contain a trailing "B" (KB,MB,GB) but it's not necessary.
|
|
|
|
* "128K" = 128 * 1024; "2G" = 2 * 1024 * 1024; "2GB" = 2 * 1024 * 1024;
|
|
|
|
* Additionally, lowercase "k", "m", "g" are parsed as well. They are processed
|
|
|
|
* the same as their uppercase equivalents.
|
|
|
|
*
|
2018-04-09 02:50:30 +00:00
|
|
|
* \param cap_str Null terminated string.
|
|
|
|
* \param cap Pointer where the parsed capacity (in bytes) will be put.
|
|
|
|
* \param has_prefix Pointer to a flag that will be set to describe whether given
|
|
|
|
* string contains a binary prefix.
|
|
|
|
*
|
|
|
|
* \return 0 on success, or negative errno on failure.
|
2017-12-13 19:21:28 +00:00
|
|
|
*/
|
|
|
|
int spdk_parse_capacity(const char *cap_str, uint64_t *cap, bool *has_prefix);
|
|
|
|
|
2018-02-21 23:04:17 +00:00
|
|
|
/**
|
|
|
|
* Check if a buffer is all zero (0x00) bytes or not.
|
|
|
|
*
|
|
|
|
* \param data Buffer to check.
|
|
|
|
* \param size Size of data in bytes.
|
2018-04-09 02:50:30 +00:00
|
|
|
*
|
|
|
|
* \return true if data consists entirely of zeroes, or false if any byte in data
|
|
|
|
* is not zero.
|
2018-02-21 23:04:17 +00:00
|
|
|
*/
|
|
|
|
bool spdk_mem_all_zero(const void *data, size_t size);
|
|
|
|
|
2019-01-22 23:27:37 +00:00
|
|
|
/**
|
|
|
|
* Convert the string in nptr to a long integer value according to the given base.
|
|
|
|
*
|
|
|
|
* spdk_strtol() does the additional error checking and allows only strings that
|
|
|
|
* contains only numbers and is positive number or zero. The caller only has to check
|
|
|
|
* if the return value is not negative.
|
|
|
|
*
|
|
|
|
* \param nptr String containing numbers.
|
|
|
|
* \param base Base which must be between 2 and 32 inclusive, or be the special value 0.
|
|
|
|
*
|
|
|
|
* \return positive number or zero on success, or negative errno on failure.
|
|
|
|
*/
|
|
|
|
long int spdk_strtol(const char *nptr, int base);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the string in nptr to a long long integer value according to the given base.
|
|
|
|
*
|
|
|
|
* spdk_strtoll() does the additional error checking and allows only strings that
|
|
|
|
* contains only numbers and is positive number or zero. The caller only has to check
|
|
|
|
* if the return value is not negative.
|
|
|
|
*
|
|
|
|
* \param nptr String containing numbers.
|
|
|
|
* \param base Base which must be between 2 and 32 inclusive, or be the special value 0.
|
|
|
|
*
|
|
|
|
* \return positive number or zero on success, or negative errno on failure.
|
|
|
|
*/
|
|
|
|
long long int spdk_strtoll(const char *nptr, int base);
|
|
|
|
|
2016-02-12 14:52:35 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-02 18:13:11 +00:00
|
|
|
#endif
|