plibsys 0.0.5
pstring.h File Reference

String manipulation routines. More...

#include <pmacros.h>
#include <ptypes.h>

Go to the source code of this file.

Functions

P_LIB_API pcharp_strdup (const pchar *str)
 Copies a string.
 
P_LIB_API pcharp_strchomp (const pchar *str)
 Removes trailing and leading whitespaces.
 
P_LIB_API pcharp_strtok (pchar *str, const pchar *delim, pchar **buf)
 Tokenizes a string by given delimiters.
 
P_LIB_API double p_strtod (const pchar *str)
 Converts a string to double without a locale dependency.
 

Detailed Description

String manipulation routines.

Author
Alexander Saprykin

Strings are represented as a sequence of single-byte characters (from the ASCII table) with the trailing zero character (\0).

Some useful string manipulation routines are represented here.

Definition in file pstring.h.

Function Documentation

◆ p_strchomp()

P_LIB_API pchar * p_strchomp ( const pchar * str)

Removes trailing and leading whitespaces.

Parameters
strString with the trailing zero to process.
Returns
Newlly allocated string in case of success, NULL otherwise. The caller takes ownership of the returned string.
Since
0.0.1

◆ p_strdup()

P_LIB_API pchar * p_strdup ( const pchar * str)

Copies a string.

Parameters
strString with the trailing zero to copy.
Returns
Copy of the str in case of success, NULL otherwise. The caller takes ownership of the returned string.
Since
0.0.1

◆ p_strtod()

P_LIB_API double p_strtod ( const pchar * str)

Converts a string to double without a locale dependency.

Parameters
strString to convert.
Returns
Floating point value in case of success, 0 otherwise.
Since
0.0.1

Since the atof() system call is locale dependent, you can use this call to convert string variables to double values. The decimal point is '.' as in the 'C' locale.

◆ p_strtok()

P_LIB_API pchar * p_strtok ( pchar * str,
const pchar * delim,
pchar ** buf )

Tokenizes a string by given delimiters.

Parameters
[in,out]strString to tokenize.
delimList of delimiters to split the string.
bufContext to store tokenize info.
Returns
Pointer to a splitted zero-terminated string in case of success, NULL otherwise.
Since
0.0.1
Note
The str is modified by this call, so take care for that. The returned pointer points on a str substring, so you do not need to call p_free() on it.

The common usage of this call is following:

pchar *token, *buf;
pchar str[] = "This is a test string"
pchar delim[] = " \t"
...
token = p_strtok (str, delim, &buf);
while (token != NULL) {
printf ("Splitted string: %s\n", token);
token = p_strtok (NULL, delim, &buf);
}
P_LIB_API pchar * p_strtok(pchar *str, const pchar *delim, pchar **buf)
Tokenizes a string by given delimiters.
char pchar
Type for a char.
Definition ptypes.h:116

Take attention that you need to pass the original string only once, after that you should pass NULL instead. You can also pass different delimiters each time.

Some platforms do not support the third parameter and it can be remained unused. In that case this call wouldn't be thread-safe.