naev 0.10.4
Functions
nstring.c File Reference

Some string routines for naev. More...

#include "nstring.h"
#include "log.h"

Go to the source code of this file.

Functions

char * strnstr (const char *haystack, const char *needle, size_t size)
 A bounded version of strstr. Conforms to BSD semantics. More...
 
char * strcasestr (const char *haystack, const char *needle)
 Finds a string inside another string case insensitively. More...
 
char * strndup (const char *s, size_t n)
 Return a pointer to a new string, which is a duplicate of the string s (or, if necessary, which contains the first nn bytes of s plus a terminating null). More...
 
int strsort (const void *p1, const void *p2)
 Sort function for sorting strings with qsort(). More...
 
int strsort_reverse (const void *p1, const void *p2)
 Order-reversed version of strsort(). More...
 
int vasprintf (char **strp, const char *fmt, va_list ap)
 Like vsprintf(), but it allocates a large-enough string and returns the pointer in the first argument. Conforms to GNU and BSD libc semantics. More...
 
int asprintf (char **strp, const char *fmt,...)
 Like sprintf(), but it allocates a large-enough string and returns the pointer in the first argument. Conforms to GNU and BSD libc semantics. More...
 
int scnprintf (char *text, size_t maxlen, const char *fmt,...)
 Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer. This makes it possible to chain these calls to concatenate into a buffer without introducing a potential bug every time. This call was first added to the Linux kernel by Juergen Quade. More...
 
int num2str (char dest[NUM2STRLEN], double n, int decimals)
 Converts an electronic warfare value to a string. More...
 
const char * num2strU (double n, int decimals)
 Unsafe version of num2str that uses an internal buffer. Every call overwrites the return value. More...
 

Detailed Description

Some string routines for naev.

Definition in file nstring.c.

Function Documentation

◆ asprintf()

int asprintf ( char **  strp,
const char *  fmt,
  ... 
)

Like sprintf(), but it allocates a large-enough string and returns the pointer in the first argument. Conforms to GNU and BSD libc semantics.

Parameters
[out]strpUsed to return the allocated char* in case of success. Caller must free. In case of failure, *strp is set to NULL, but don't rely on this because the GNU version doesn't guarantee it.
fmtSame as sprintf().
Returns
-1 if it failed, otherwise the number of bytes "printed".

Definition at line 161 of file nstring.c.

◆ num2str()

int num2str ( char  dest[NUM2STRLEN],
double  n,
int  decimals 
)

Converts an electronic warfare value to a string.

Parameters
[out]destString to write to.
nNumber to write.
decimalsNumber of decimals to write.

Definition at line 199 of file nstring.c.

◆ num2strU()

const char * num2strU ( double  n,
int  decimals 
)

Unsafe version of num2str that uses an internal buffer. Every call overwrites the return value.

Parameters
nNumber to write.
decimalsNumber of decimals to write.
Returns
Fancy string number.

Definition at line 230 of file nstring.c.

◆ scnprintf()

int scnprintf ( char *  text,
size_t  maxlen,
const char *  fmt,
  ... 
)

Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer. This makes it possible to chain these calls to concatenate into a buffer without introducing a potential bug every time. This call was first added to the Linux kernel by Juergen Quade.

Definition at line 178 of file nstring.c.

◆ strcasestr()

char * strcasestr ( const char *  haystack,
const char *  needle 
)

Finds a string inside another string case insensitively.

Parameters
haystackString to look into.
needleString to find.
Returns
Pointer in haystack where needle was found or NULL if not found.

Definition at line 68 of file nstring.c.

◆ strndup()

char * strndup ( const char *  s,
size_t  n 
)

Return a pointer to a new string, which is a duplicate of the string s (or, if necessary, which contains the first nn bytes of s plus a terminating null).

Taken from glibc. Conforms to POSIX.1-2008.

Definition at line 94 of file nstring.c.

◆ strnstr()

char * strnstr ( const char *  haystack,
const char *  needle,
size_t  size 
)

A bounded version of strstr. Conforms to BSD semantics.

Parameters
haystackThe string to search in
sizeThe size of haystack
needleThe string to search for
Returns
A pointer to the first occurrence of needle in haystack, or NULL

Definition at line 26 of file nstring.c.

◆ strsort()

int strsort ( const void *  p1,
const void *  p2 
)

Sort function for sorting strings with qsort().

Definition at line 108 of file nstring.c.

◆ strsort_reverse()

int strsort_reverse ( const void *  p1,
const void *  p2 
)

Order-reversed version of strsort().

Definition at line 116 of file nstring.c.

◆ vasprintf()

int vasprintf ( char **  strp,
const char *  fmt,
va_list  ap 
)

Like vsprintf(), but it allocates a large-enough string and returns the pointer in the first argument. Conforms to GNU and BSD libc semantics.

Parameters
[out]strpUsed to return the allocated char* in case of success. Caller must free. In case of failure, *strp is set to NULL, but don't rely on this because the GNU version doesn't guarantee it.
fmtSame as vsprintf().
apSame as vsprintf().
Returns
-1 if it failed, otherwise the number of bytes "printed".

Definition at line 132 of file nstring.c.