libite
Macros | Functions
strlite.h File Reference
#include <stdint.h>
#include <string.h>
#include <sys/param.h>
#include "strdupa.h"
#include "strndupa.h"
#include "strnlen.h"
Include dependency graph for strlite.h:
This graph shows which files directly or indirectly include this file:

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...
 

Detailed Description

Author
Joachim Wiberg
Date
2008-2021

Macro Definition Documentation

◆ max

#define max (   a,
 
)
Value:
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})

Geneirc max() macro, if a > b => a, else b.

◆ min

#define min (   a,
 
)
Value:
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})

Geneirc min() macro, if a < b => a, else b.

Function Documentation

◆ strlcat()

size_t strlcat ( char *  dst,
const char *  src,
size_t  dsize 
)

Safe version of strncat() from OpenBSD.

Parameters
dstDestination string
srcSource string
dsizeTotal 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)).

Returns
strlen(src) + MIN(dsize, strlen(initial dst)). If retval >= dsize, truncation occurred.

◆ strlcpy()

size_t strlcpy ( char *  dst,
const char *  src,
size_t  dsize 
)

Safe version of strncpy() from OpenBSD.

Parameters
dstDestination string
srcSource string
dsizeTotal 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).

Returns
strlen(src); if retval >= dsize, truncation occurred.

◆ strmatch()

int strmatch ( const char *  str,
const char **  list 
)

Finds matching strings from a list.

Parameters
strString to look for.
listNUL 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.

Returns
-1 on error, otherwise the index to the matching string.

◆ strnmatch()

int strnmatch ( const char *  str,
const char **  list,
size_t  num 
)

Finds matching strings from a finite list.

Parameters
strString to look for
listList of strings to search.
numNumber 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.

Returns
-1 on error, otherwise the index to the matching string.

◆ strtonum()

long long strtonum ( const char *  numstr,
long long  minval,
long long  maxval,
const char **  errstrp 
)

Reliably convert string value to an integer.

Parameters
numstrString to convert to a number
minvalLower bound to check number against
maxvalUpper bound to check number against
errstrpPointer 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.

Returns
The result of the conversion, unless the value would exceed the provided bounds or is invalid. On error, 0 is returned, errno is set, and 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.

◆ strtrim()

char* strtrim ( char *  str)

Strip leading and trailing whitespace from a string.

Parameters
strThe string to trim

Trims a string from any leading and trailing white-space, returns the trimmed result in the same buffer.

Returns
If 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.