Orcania
Potluck with different functions for different purposes that can be shared among C programs
Functions
string functions

Functions

char * str_replace (const char *source, const char *str_old, const char *str_new)
 
char * o_strdup (const char *source)
 
char * o_strndup (const char *source, size_t len)
 
int o_strcmp (const char *p1, const char *p2)
 
int o_strncmp (const char *p1, const char *p2, size_t n)
 
char * o_strcpy (char *p1, const char *p2)
 
char * o_strncpy (char *p1, const char *p2, size_t n)
 
int o_strcasecmp (const char *p1, const char *p2)
 
int o_strncasecmp (const char *p1, const char *p2, size_t n)
 
char * o_strstr (const char *haystack, const char *needle)
 
char * o_strnstr (const char *haystack, const char *needle, size_t len)
 
char * o_strcasestr (const char *haystack, const char *needle)
 
char * o_strchr (const char *haystack, int c)
 
const char * o_strnchr (const char *haystack, size_t len, char c)
 
char * o_strrchr (const char *haystack, int c)
 
const char * o_strrnchr (const char *haystack, size_t len, char c)
 
int o_strnullempty (const char *s)
 
size_t o_strlen (const char *s)
 
const char * trimwhitespace (char *str)
 
char * trimcharacter (char *str, char to_remove)
 
char * msprintf (const char *message,...)
 
char * mstrcatf (char *source, const char *message,...)
 

Detailed Description

These functions are used for string manipulation Types of functions available:

Function Documentation

◆ msprintf()

char* msprintf ( const char *  message,
  ... 
)

char * msprintf(const char * message, ...) Implementation of sprintf that return a malloc'd char * with the string construction because life is too short to use 3 lines instead of 1 but don't forget to free the returned value after use! Disclaimer: When I made this function, I didn't know asprintf existed. If I did I surely wouldn't make msprintf although, msprintf has the advantage to work with o_malloc and o_free so it can use specific memory allocation functions

Parameters
messagethe message format to concat to source, printf format
Returns
a heap-allocated char * containing the result string

◆ mstrcatf()

char* mstrcatf ( char *  source,
const char *  message,
  ... 
)

char * mstrcatf((char * source, const char * message, ...) A combination of strcat and msprintf that will concat source and message formatted and return the combination as a new allocated char * and will o_free source but don't forget to free the returned value after use!

Parameters
sourcethe source string to concat message, source will be free'd during the process
messagethe message format to concat to source, printf format
Returns
a heap-allocated char * containing the result string

◆ o_strcasecmp()

int o_strcasecmp ( const char *  p1,
const char *  p2 
)

o_strcasecmp a modified strcasecmp function that don't crash when p1 is NULL or p2 us NULL

Parameters
p1the first string to compare
p2the second string to compare
Returns
0 if p1 is equal to p2, negative number if p1 is inferior to p2, positive number otherwise

◆ o_strcasestr()

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

o_strcasestr a modified strcasestr function that don't crash when haystack is NULL or needle us NULL

Parameters
haystackthe string to be looked for
needlethe string to search in haystack
Returns
the pointer to the first occurence of needle in haystack or NULL if not found

◆ o_strchr()

char* o_strchr ( const char *  haystack,
int  c 
)

o_strchr a modified strchr function that don't crash when haystack is NULL

Parameters
haystackthe string to be looked for
cthe character to look for in haystack
Returns
the pointer to the first occurence of c in haystack or NULL if not found

◆ o_strcmp()

int o_strcmp ( const char *  p1,
const char *  p2 
)

o_strcmp a modified strcmp function that don't crash when p1 is NULL or p2 us NULL

Parameters
p1the first string to compare
p2the second string to compare
Returns
0 if p1 is equal to p2, negative number if p1 is inferior to p2, positive number otherwise

◆ o_strcpy()

char* o_strcpy ( char *  p1,
const char *  p2 
)

o_strcpy a modified strcpy function that don't crash when p1 is NULL or p2 us NULL

Parameters
p1the string to copy p2
p2the source string to be copied
Returns
p1

◆ o_strdup()

char* o_strdup ( const char *  source)

o_strdup a modified strdup function that don't crash when source is NULL, instead return NULL

Parameters
sourcethe source string to duplicate
Returns
a heap-allocated string Returned value must be free'd after use

◆ o_strlen()

size_t o_strlen ( const char *  s)

o_strlen a modified version of strlen that don't crash when s is NULL

Parameters
sthe string to calculate length
Returns
the length of s, 0 if s is NULL

◆ o_strncasecmp()

int o_strncasecmp ( const char *  p1,
const char *  p2,
size_t  n 
)

o_strncasecmp a modified strncasecmp function that don't crash when p1 is NULL or p2 us NULL

Parameters
p1the first string to compare
p2the second string to compare
nthe maximum number of char to compare
Returns
0 if p1 is equal to p2, negative number if p1 is inferior to p2, positive number otherwise

◆ o_strnchr()

const char* o_strnchr ( const char *  haystack,
size_t  len,
char  c 
)

o_strnchr a modified strnchr function that don't crash when haystack is NULL

Parameters
haystackthe string to be looked for
lenthe maxmimum length of characters to look for in haystack
cthe character to look for in haystack
Returns
the pointer to the first occurence of c in haystack or NULL if not found

◆ o_strncmp()

int o_strncmp ( const char *  p1,
const char *  p2,
size_t  n 
)

o_strncmp a modified strncmp function that don't crash when p1 is NULL or p2 us NULL

Parameters
p1the first string to compare
p2the second string to compare
nthe maximum number of char to compare
Returns
0 if p1 is equal to p2, negative number if p1 is inferior to p2, positive number otherwise

◆ o_strncpy()

char* o_strncpy ( char *  p1,
const char *  p2,
size_t  n 
)

o_strncpy

Parameters
p1the string to copy p2
p2the source string to be copied
nthe maximum characters to copy from p2 to p1
Returns
p1 a modified strncpy function that don't crash when p1 is NULL or p2 us NULL

◆ o_strndup()

char* o_strndup ( const char *  source,
size_t  len 
)

o_strndup a modified strndup function that don't crash when source is NULL, instead return NULL

Parameters
sourcethe source string to duplicate
lenthe maximum length of the string source to duplicate
Returns
a heap-allocated string Returned value must be free'd after use

◆ o_strnstr()

char* o_strnstr ( const char *  haystack,
const char *  needle,
size_t  len 
)

o_strnstr a modified strnstr function that don't crash when haystack is NULL or needle us NULL

Parameters
haystackthe string to be looked for
needlethe string to search in haystack
lenthe maximum length to look for in haystack
Returns
the pointer to the first occurence of needle in haystack or NULL if not found

◆ o_strnullempty()

int o_strnullempty ( const char *  s)

o_strnullempty return true if s is NULL or empty string, false otherwise

◆ o_strrchr()

char* o_strrchr ( const char *  haystack,
int  c 
)

o_strrchr a modified strrchr function that don't crash when haystack is NULL

Parameters
haystackthe string to be looked for
cthe character to look for in haystack
Returns
the pointer to the last occurence of c in haystack or NULL if not found

o_strrchr a modified strrchr function that don't crash when haystack is NULL

◆ o_strrnchr()

const char* o_strrnchr ( const char *  haystack,
size_t  len,
char  c 
)

o_strrnchr a modified strrnchr function that don't crash when haystack is NULL

Parameters
haystackthe string to be looked for
lenthe maxmimum length of characters to look for in haystack
cthe character to look for in haystack
Returns
the pointer to the last occurence of c in haystack or NULL if not found

◆ o_strstr()

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

o_strstr a modified strstr function that don't crash when haystack is NULL or needle us NULL

Parameters
haystackthe string to be looked for
needlethe string to search in haystack
Returns
the pointer to the first occurence of needle in haystack or NULL if not found

◆ str_replace()

char* str_replace ( const char *  source,
const char *  str_old,
const char *  str_new 
)

char * str_replace(const char * source, char * old, char * new) replace all occurences of old by new in the string source return a char * with the new value

Parameters
sourcethe source string
str_oldstring to be replaced in source
str_newnew string to replace
Returns
a heap-allocated string with the replacements applied return NULL on error returned value must be free'd after use

Orcania library

Different functions for different purposes but that can be shared between other projects

◆ trimcharacter()

char* trimcharacter ( char *  str,
char  to_remove 
)

Remove string of beginning and ending given character the string str will be modified

Parameters
strthe string to trim
to_removethe character to trim
Returns
the trimmed str

◆ trimwhitespace()

const char* trimwhitespace ( char *  str)

Remove string of beginning and ending whitespaces the string str will be modified

Parameters
strthe string to trim
Returns
the trimmed str