LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
str_mem.c File Reference

Functions

char * str_n_cpy (char *dest, const char *src, size_t length)
 
char * str_n_cat (char *dest, const char *src, size_t length)
 
char * l_str_new (const char *str)
 
char * l_str_cat (char *dest, const char *src)
 
char * l_str_insert_at_b (char *dest, const char *src)
 
void l_str_free (char *str)
 
void * malloc2 (size_t size)
 
void * realloc2 (void *mem_block, size_t size)
 
void * calloc2 (size_t n_elements, size_t element_size)
 
void free2 (void *mem_block)
 
const char * readable_size (double size_bytes)
 
const char * itoa2 (long int n)
 
char * remove_char_from_str (char *str, char c)
 
char * remove_leading_whitespaces_from_str (char *str)
 
char * remove_trailing_whitespaces_from_str (char *str)
 
char * remove_surrounding_whitespaces_from_str (char *str)
 
zboolean str_is_num (const char *str2)
 
zboolean str_is_blank (const char *str)
 
const char * rnd_str (char mode, int length)
 

Detailed Description

 === A few strings and memory management functions ===

Function Documentation

◆ str_n_cpy()

char* str_n_cpy ( char *  dest,
const char *  src,
size_t  length 
)

Copy n bytes max from src to dest then add '\0' at end of dest.

◆ str_n_cat()

char* str_n_cat ( char *  dest,
const char *  src,
size_t  length 
)

Concanate n bytes max of src to dest then add '\0' at end of dest. Strings may not be identical and should not overlap.

◆ l_str_new()

char* l_str_new ( const char *  str)

Create new_str (allocate memory) and copy str (can be NULL) to new_str.

◆ l_str_cat()

char* l_str_cat ( char *  dest,
const char *  src 
)

Append src (can be NULL) to dest (re-allocate memory as necessary). dest must have been created by l_str_new()/malloc() - strings may overlap. Note: faster than l_str_insert_at_b().

◆ l_str_insert_at_b()

char* l_str_insert_at_b ( char *  dest,
const char *  src 
)

Insert src (can be NULL) at the beginning of dest (re-allocate memory as necessary). dest must have been created by l_str_new()/malloc() - strings may overlap. Note: slower than l_str_cat().

◆ l_str_free()

void l_str_free ( char *  str)

Free string created by l_str_new(), l_str_cat() or l_str_insert_at_b().

◆ malloc2()

void* malloc2 ( size_t  size)

Wrapper for malloc() which check returned value.

◆ realloc2()

void* realloc2 ( void *  mem_block,
size_t  size 
)

Wrapper for realloc() which check returned value.

◆ calloc2()

void* calloc2 ( size_t  n_elements,
size_t  element_size 
)

Wrapper for calloc() which check returned value.

◆ free2()

void free2 ( void *  mem_block)

Wrapper for free() which check returned value.

◆ readable_size()

const char* readable_size ( double  size_bytes)

Return size in readable format (KiB, MiB, GiB, TiB, ...) *** Allow up to N_SIMULTANEOUS_CALLS simultaneous calls *** Convention: we assume 2.5 MiB = 2 MiB + [0.5 x 1024 = 512] KiB, not 500 KiB. Otherwise, there is no way to express a value in the range 1000 - 1023, so x.y MiB = x MiB + 0.y MiB, that is: not y x 100 KiB but y x 102.4 KiB.

◆ itoa2()

const char* itoa2 ( long int  n)

itoa() is not ANSI C so this one could be useful. *** Allow up to N_SIMULTANEOUS_CALLS simultaneous calls ***

◆ remove_char_from_str()

char* remove_char_from_str ( char *  str,
char  c 
)

Modify string in place.

◆ remove_leading_whitespaces_from_str()

char* remove_leading_whitespaces_from_str ( char *  str)

Modify string in place.

◆ remove_trailing_whitespaces_from_str()

char* remove_trailing_whitespaces_from_str ( char *  str)

Modify string in place.

◆ remove_surrounding_whitespaces_from_str()

char* remove_surrounding_whitespaces_from_str ( char *  str)

Modify string in place.

◆ str_is_num()

zboolean str_is_num ( const char *  str2)

Check that str contains only digits (at least one) and whitespaces (may start and/or end with whitespaces), meaning numerical value is integer >= 0.

◆ str_is_blank()

zboolean str_is_blank ( const char *  str)

Check that str contains only whitespaces or is empty.

◆ rnd_str()

const char* rnd_str ( char  mode,
int  length 
)

Generate a random string up to 1023 chars long. mode = a -> alpha / d -> digits / b -> both