libite 2.6.1
strlite.h File Reference
#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)
 
#define max(a, b)
 

Functions

int strnmatch (const char *str, const char **list, size_t num)
 
int strmatch (const char *str, const char **list)
 
size_t strlcpy (char *dst, const char *src, size_t siz)
 
size_t strlcat (char *dst, const char *src, size_t siz)
 
long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp)
 
char * strtrim (char *str)
 

Detailed Description

Author
Joachim Wiberg
Date
2008-2021

Definition in file strlite.h.

Macro Definition Documentation

◆ LIBITE_STRING_H_

#define LIBITE_STRING_H_

Definition at line 37 of file strlite.h.

◆ max

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

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

Definition at line 58 of file strlite.h.

◆ min

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

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

Definition at line 49 of file strlite.h.

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.

Definition at line 45 of file strlcat.c.

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

Definition at line 44 of file strlcpy.c.

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

Definition at line 74 of file strmatch.c.

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

Definition at line 43 of file strmatch.c.

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

Definition at line 75 of file strtonum.c.

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

Definition at line 44 of file strtrim.c.