libite
|
#include <stdint.h>
#include <string.h>
#include <sys/param.h>
#include "strdupa.h"
#include "strndupa.h"
#include "strnlen.h"
Go to the source code of this file.
Macros | |
#define | LIBITE_STRING_H_ |
#define | min(a, b) |
Geneirc min() macro, if a < b => a, else b. More... | |
#define | max(a, b) |
Geneirc max() macro, if a > b => a, else b. More... | |
Functions | |
int | strnmatch (const char *str, const char **list, size_t num) |
Finds matching strings from a finite list. More... | |
int | strmatch (const char *str, const char **list) |
Finds matching strings from a list. More... | |
size_t | strlcpy (char *dst, const char *src, size_t siz) |
Safe version of strncpy() from OpenBSD. More... | |
size_t | strlcat (char *dst, const char *src, size_t siz) |
Safe version of strncat() from OpenBSD. More... | |
long long | strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp) |
Reliably convert string value to an integer. More... | |
char * | strtrim (char *str) |
Strip leading and trailing whitespace from a string. More... | |
#define max | ( | a, | |
b | |||
) |
Geneirc max() macro, if a > b => a, else b.
#define min | ( | a, | |
b | |||
) |
Geneirc min() macro, if a < b => a, else b.
size_t strlcat | ( | char * | dst, |
const char * | src, | ||
size_t | dsize | ||
) |
Safe version of strncat() from OpenBSD.
dst | Destination string |
src | Source string |
dsize | Total maximum size of dst |
Appends src
to string dst
of size dsize
(unlike strncat(), dsize
is the full size of dst
, not space left). At most dsize-1 characters will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
size_t strlcpy | ( | char * | dst, |
const char * | src, | ||
size_t | dsize | ||
) |
Safe version of strncpy() from OpenBSD.
dst | Destination string |
src | Source string |
dsize | Total maximum size of dst |
This function copies string src
to buffer dst
of size dsize
bytes. At most dsize-1 chars will be copied. Always NUL terminates (unless dsize==0).
int strmatch | ( | const char * | str, |
const char ** | list | ||
) |
Finds matching strings from a list.
str | String to look for. |
list | NUL terminated list of strings to search. |
This function searches the list
of strings for str
. If a (partial) match is found it returns the index in the list
.
Please note, the list
MUST be terminated by a NUL element. If that is not possible, we recommend using strnmatch() instead.
int strnmatch | ( | const char * | str, |
const char ** | list, | ||
size_t | num | ||
) |
Finds matching strings from a finite list.
str | String to look for |
list | List of strings to search. |
num | Number of entries in list . |
This function searches the list
of strings for str
. If a (partial) match is found it returns the index in the list
.
Very similar in function to strmatch(), but works for sets of strings that are not NUL
terminated.
long long strtonum | ( | const char * | numstr, |
long long | minval, | ||
long long | maxval, | ||
const char ** | errstrp | ||
) |
Reliably convert string value to an integer.
numstr | String to convert to a number |
minval | Lower bound to check number against |
maxval | Upper bound to check number against |
errstrp | Pointer to error string |
This function converts the string in numstr
to a long long value. The function was designed to facilitate safe, robust programming and overcome the shortcomings of the atoi(3) and strtol(3) family of interfaces.
The string may begin with an arbitrary amount of whitespace (as determined by isspace(3)) followed by a single optional ‘+’ or ‘-’ sign.
The remainder of the string is converted to a long long value according to base 10.
The value obtained is then checked against the provided minval
and maxval
bounds. If errstrp
is non-NULL, strtonum() stores an error string in *errstrp
indicating the failure.
errstrp
points to an error message. errstr
is set to NULL
on success; this fact can be used to differentiate a successful return of 0 from an error. char* strtrim | ( | char * | str | ) |
Strip leading and trailing whitespace from a string.
str | The string to trim |
Trims a string from any leading and trailing white-space, returns the trimmed result in the same buffer.
str
is a valid, non-NULL string this function returns the same string stripped from whitespace. This function only returns NULL
if str
itself is NULL
.