Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

All Data Structures Files Functions Variables Typedefs Macros Groups
Data Structures | Macros | Typedefs | Functions
N_STR: replacement to charstrings with dynamic resizing and boundary checking

## Data Structures

struct | N_STR

## Macros

#define | local_strdup(__src_)
#define | WILDMAT_ABORT -2
#define | WILDMAT_NEGATE_CLASS '^'
#define | n_printf(__n_var,...)

## Typedefs

typedef int32_t | NSTRBYTE
typedef struct N_STR | N_STR

## Functions

N_STR * | new_nstr (NSTRBYTE size)
| create a new N_STR string More...
int | empty_nstr (N_STR *nstr)
| empty a N_STR string More...
N_STR * | nstrdup (N_STR *msg)
| Duplicate a N_STR. More...
int | char_to_nstr_ex (char *from, NSTRBYTE nboct, N_STR **to)
| Convert a char into a N_STR, extended version. More...
N_STR * | char_to_nstr (char *src)
| Convert a char into a N_STR, short version. More...
int | nstrcat_ex (N_STR *dest, void *src, NSTRBYTE size, NSTRBYTE blk_size, int resize_flag)
| Append data into N_STR using internal N_STR size and cursor position. More...
int | nstrcat (N_STR *dst, N_STR *src)
| Add N_STR *src content to N_STR *dst, resizing it if needed. More...
int | nstrcat_bytes_ex (N_STR *dest, void *src, NSTRBYTE size)
| Append data into N_STR using internal N_STR size and cursor position. Wrapper to nstrcat_ex, with resize_flag on and a block size one byte higher than size. More...
int | nstrcat_bytes (N_STR *dest, void *src)
| Append data into N_STR using internal N_STR size and cursor position. Wrapper to nstrcat_ex, with resize_flag on and a block size one byte higher than size. More...
N_STR * | file_to_nstr (char *filename)
| Load a whole file into a N_STR. Be aware of the NSTRBYTE addressing limit (2GB commonly) More...
int | nstr_to_file (N_STR *n_str, char *filename)
| Write a N_STR content into a file. More...
void | free_nstr_ptr (void *ptr)
| Free a N_STR pointer structure. More...
int | free_nstr (N_STR **ptr)
| Free a N_STR structure and set the pointer to NULL. More...
int | str_to_int_ex (const char *s, NSTRBYTE start, NSTRBYTE end, int *i, const int base)
| Helper for string[start to end] to integer. Automatically add /0 for conversion. Leave values untouched if any error occur. Work on a copy of the chunk. More...
int | str_to_int (const char *s, int *i, const int base)
| Helper for string to integer. More...
int | skipw (char *string, char toskip, NSTRBYTE *iterator, int inc)
| skip while 'toskip' occurence is found from 'iterator' to the next non 'toskip' position. The new iterator index is automatically stored, returning to it first value if an error append. More...
int | skipu (char *string, char toskip, NSTRBYTE *iterator, int inc)
| skip until 'toskip' occurence is found from 'iterator' to the next 'toskip' value. The new iterator index is automatically stored, returning to it first value if an error append. More...
int | strup (char *string, char *dest)
| Upper case a string. More...
int | strlo (char *string, char *dest)
| Upper case a string. More...
int | strcpy_u (char *from, char *to, NSTRBYTE to_size, char split, NSTRBYTE *it)
| Copy from start to dest until from[ iterator ] == split. More...
char ** | split (char *chaine, const char *delim, int vide)
| Split the strings into a an array of char *pointer , ended by a NULL one. Max 256 splitted elements per call. More...
int | split_count (char **split_result)
| Count split elements. More...
int | free_split_result (char ***tab)
| Free a split result allocated array. More...
int | write_and_fit (char **dest, NSTRBYTE *size, NSTRBYTE *written, char *src)
int | scan_dir (const char *dir, LIST *result, const int recurse)
| Scan a list of directory and return a list of char *file. More...
int | wildmat (register const char *text, register const char *p)
| Written by Rich Salz rsalz at osf.org, refurbished by me. Wildcard pattern matching . More...
int | wildmatcase (register const char *text, register const char *p)
| Written by Rich Salz rsalz at osf.org, refurbished by me. Wildcard pattern matching case insensitive. More...
char * | str_replace (const char *string, const char *substr, const char *replacement)
| Replace "substr" by "replacement" inside string taken from http://coding.debuntu.org/c-implementing-str_replace-replace-all-occurrences-substring By Chantra. More...

## Detailed Description

## Macro Definition Documentation

#define local_strdup | ( |  | __src_ | ) |

Value:
( \
{ \
char *str = NULL ; \
if( (__src_) ) \
{ \
int len = strlen( (__src_) ); \
Malloc( str , char , len + 5 ); \
if( !str ) \
{ \
n_log( LOG_ERR , "Couldn't allocate %d byte for duplicating \"%s\"" , (__src_) ); \
} \
else \
{ \
for( int it = 0 ; it <= len ; it ++ ) \
{ \
str[ it ] = (__src_)[ it ] ; \
} \
} \
} \
str ; \
} \
)

local strdup

Definition at line 38 of file n_str.h.

#define n_printf | ( |  | __n_var,
|  |  | ...
| ) |  |

Value:
{ \
NSTRBYTE needed = 0 ; \
needed = snprintf( NULL , 0 , __VA_ARGS__ ); \
Malloc( buffer , char , needed + 1 );\
if( buffer )\
{ \
snprintf( buffer , needed + 1 , __VA_ARGS__ ); \
__n_var = buffer; \
} \
else \
{ \
__n_var = NULL ; \
}\
}

Do tar(1) matching rules, which ignore a trailing slash?

Macro to quickly allocate and sprintf to a new char *buffer

Definition at line 73 of file n_str.h.

#define WILDMAT_ABORT -2

Abort code to sped up pattern matching. Special thanks to Lars Mathiesen thori.nosp@m.nn@d.nosp@m.iku.d.nosp@m.k for the ABORT code.

Definition at line 64 of file n_str.h.

#define WILDMAT_NEGATE_CLASS '^'

What character marks an inverted character class?

Definition at line 66 of file n_str.h.

## Typedef Documentation

typedef struct N_STR N_STR

A double linked box including a string and his lenght

typedef int32_t NSTRBYTE

N_STR base unit

Definition at line 93 of file n_str.h.

## Function Documentation

N_STR * char_to_nstr | ( | char * | src | ) |

Convert a char into a N_STR, short version.

Parameters

  src | A char *string to convert

Returns
  A N_STR copy of src or NULL

Definition at line 94 of file n_str.c.

Here is the call graph for this function:

Here is the caller graph for this function:

char_to_nstr_ex | ( | char * | from,
|  | NSTRBYTE | nboct,
|  | N_STR ** | to
| ) |  |

Convert a char into a N_STR, extended version.

Parameters

  from | A char *string to convert
  nboct | The size to copy, from 1 octet to nboctet (ustrsizez( from ) )
  to | A N_STR pointer who will be Malloced

Returns
  True on success, FALSE on failure ( to will be set to NULL )

Definition at line 66 of file n_str.c.

Here is the caller graph for this function:

int empty_nstr | ( | N_STR * | nstr | ) |

empty a N_STR string

Returns
  TRUE or FALSE

Definition at line 18 of file n_str.c.

N_STR * file_to_nstr | ( | char * | filename | ) |

Load a whole file into a N_STR. Be aware of the NSTRBYTE addressing limit (2GB commonly)

Parameters

  filename | The filename to load inside a N_STR

Returns
  A valid N_STR or NULL

Definition at line 109 of file n_str.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int free_nstr | ( | N_STR ** | ptr | ) |

Free a N_STR structure and set the pointer to NULL.

Parameters

  ptr | A N_STR *object to free

Returns
  TRUE or FALSE

Definition at line 283 of file n_str.c.

Here is the caller graph for this function:

void free_nstr_ptr | ( | void * | ptr | ) |

Free a N_STR pointer structure.

Parameters

  ptr | A N_STR *object to free

Definition at line 299 of file n_str.c.

Here is the caller graph for this function:

int free_split_result | ( | char *** | tab | ) |

Free a split result allocated array.

Parameters

  tab | A pointer to a split result to free

Returns
  TRUE

Definition at line 653 of file n_str.c.

Here is the caller graph for this function:

N_STR * new_nstr | ( | NSTRBYTE | size | ) |

create a new N_STR string

Parameters

  size | Size of the new string. 0 for no allocation.

Returns
  A new allocated N_STR or NULL

Definition at line 34 of file n_str.c.

Here is the caller graph for this function:

int nstr_to_file | ( | N_STR * | str,
|  | char * | filename
| ) |  |

Write a N_STR content into a file.

Parameters

  str | The N_STR *content to write down
  filename | The destination filename

Returns
  TRUE or FALSE

Definition at line 177 of file n_str.c.

int nstrcat | ( | N_STR * | dst,
|  | N_STR * | src
| ) |  |

Add N_STR *src content to N_STR *dst, resizing it if needed.

Parameters

  dst | The N_STR *destination
  src | The N_STR *source to concatenate into dst

Returns
  TRUE or FALSE

Definition at line 721 of file n_str.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int nstrcat_bytes | ( | N_STR * | dest,
|  | void * | data
| ) |  |

Append data into N_STR using internal N_STR size and cursor position. Wrapper to nstrcat_ex, with resize_flag on and a block size one byte higher than size.

Parameters

  dest | The N_STR *destination (accumulator)
  data | The data to append

Returns
  TRUE or FALSE

Definition at line 756 of file n_str.c.

Here is the call graph for this function:

int nstrcat_bytes_ex | ( | N_STR * | dest,
|  | void * | data,
|  | NSTRBYTE | size
| ) |  |

Append data into N_STR using internal N_STR size and cursor position. Wrapper to nstrcat_ex, with resize_flag on and a block size one byte higher than size.

Parameters

  dest | The N_STR *destination (accumulator)
  data | The data to append
  size | The number of octet of data we want to append in dest

Returns
  TRUE or FALSE

Definition at line 735 of file n_str.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int nstrcat_ex | ( | N_STR * | dest,
|  | void * | src,
|  | NSTRBYTE | size,
|  | NSTRBYTE | blk_size,
|  | int | resize_flag
| ) |  |

Append data into N_STR using internal N_STR size and cursor position.

Parameters

  dest | The N_STR *destination (accumulator)
  src | The data to append
  size | The number of octet of data we want to append in dest
  blk_size | In case of resizing, this is the increment which will be used to reach ( dest -> written + size )
  resize_flag | Set it to a positive non zero value to allow resizing, or to zero or negative to forbid resizing

Returns
  TRUE or FALSE

Definition at line 684 of file n_str.c.

Here is the caller graph for this function:

N_STR * nstrdup | ( | N_STR * | str | ) |

Duplicate a N_STR.

Parameters

  str | A N_STR *object to free

Returns
  A valid N_STR or NULL

Definition at line 317 of file n_str.c.

Here is the caller graph for this function:

int scan_dir | ( | const char * | dir,
|  | LIST * | result,
|  | const int | recurse
| ) |  |

Scan a list of directory and return a list of char *file.

Parameters

  dir | The directory to scan
  result | A pointer to a valid LIST for the results
  recurse | Recursive search if TRUE, directory only if FALSE

Returns
  TRUE or FALSE

Definition at line 818 of file n_str.c.

Here is the call graph for this function:

Here is the caller graph for this function:

skipu | ( | char * | string,
|  | char | toskip,
|  | NSTRBYTE * | iterator,
|  | int | inc
| ) |  |

skip until 'toskip' occurence is found from 'iterator' to the next 'toskip' value. The new iterator index is automatically stored, returning to it first value if an error append.

Parameters

  string | a char *stri:wqng to search in
  toskip | skipping while char character 'toskip' isnt found
  iterator | an int iteraor position on the string
  inc | an int to specify the step of skipping

Returns
  TRUE if success FALSE or ABORT if not

Definition at line 412 of file n_str.c.

int skipw | ( | char * | string,
|  | char | toskip,
|  | NSTRBYTE * | iterator,
|  | int | inc
| ) |  |

skip while 'toskip' occurence is found from 'iterator' to the next non 'toskip' position. The new iterator index is automatically stored, returning to it first value if an error append.

Parameters

  string | a char *string to search in
  toskip | skipping while char character 'toskip' is found
  iterator | an int iteraor position on the string
  inc | an int to specify the step of skipping

Returns
  TRUE if success FALSE or ABORT if not

Definition at line 358 of file n_str.c.

char ** split | ( | char * | str,
|  | const char * | delim,
|  | int | empty
| ) |  |

Split the strings into a an array of char *pointer , ended by a NULL one. Max 256 splitted elements per call.

Parameters

  str | The char *str to split
  delim | The delimiter, one or more characters
  empty | Empty flag. If 1, then empty delimited areas will be added as NULL entries, else they will be skipped.

Returns
  An array of char *, ended by a NULL entry.

Definition at line 554 of file n_str.c.

Here is the call graph for this function:

int split_count | ( | char ** | split_result | ) |

Count split elements.

Parameters

  split_result | A char **result from a split call

Returns
  The number of elements or -1 if errors

Definition at line 633 of file n_str.c.

char * str_replace | ( | const char * | string,
|  | const char * | substr,
|  | const char * | replacement
| ) |  |

Replace "substr" by "replacement" inside string taken from http://coding.debuntu.org/c-implementing-str_replace-replace-all-occurrences-substring By Chantra.

Parameters

  string | Original string to modify
  substr | String to search
  replacement | Substitution string

Returns
  A copy of the sustituted string or NULL

Definition at line 1010 of file n_str.c.

int str_to_int | ( | const char * | s,
|  | int * | i,
|  | const int | base
| ) |  |

Helper for string to integer.

Parameters

  s | String to convert
  i | A pointer to an integer variable which will receieve the value.
  base | Base for converting values

Returns
  TRUE or FALSE

Definition at line 265 of file n_str.c.

Here is the call graph for this function:

int str_to_int_ex | ( | const char * | s,
|  | NSTRBYTE | start,
|  | NSTRBYTE | end,
|  | int * | i,
|  | const int | base
| ) |  |

Helper for string[start to end] to integer. Automatically add /0 for conversion. Leave values untouched if any error occur. Work on a copy of the chunk.

Parameters

  s | String to convert
  start | Start position of the chunk
  end | End position of the chunk
  i | A pointer to an integer variable which will receieve the value.
  base | Base for converting values

Returns
  TRUE or FALSE

Definition at line 217 of file n_str.c.

Here is the caller graph for this function:

int strcpy_u | ( | char * | from,
|  | char * | to,
|  | NSTRBYTE | to_size,
|  | char | split,
|  | NSTRBYTE * | it
| ) |  |

Copy from start to dest until from[ iterator ] == split.

Parameters

  from | Source string
  to | Dest string
  to_size | the maximum size to write
  split | stopping character
  it | Save of iterator

Returns
  TRUE or FALSE

Definition at line 513 of file n_str.c.

strlo | ( | char * | string,
|  | char * | dest
| ) |  |

Upper case a string.

Parameters

  string | the string to change to lower case
  dest | the string where storing result

Warning
  string must be same size as dest

Returns
  TRUE or FALSE

Definition at line 488 of file n_str.c.

int strup | ( | char * | string,
|  | char * | dest
| ) |  |

Upper case a string.

Parameters

  string | the string to change to upper case
  dest | the string where storing result

Warning
  string must be same size as dest

Returns
  TRUE or FALSE

Definition at line 465 of file n_str.c.

int wildmat | ( | register const char * | text,
|  | register const char * | p
| ) |  |

Written by Rich Salz rsalz at osf.org, refurbished by me. Wildcard pattern matching .

Parameters

  text | The source text to search
  p | The text to search, with wildcards

Returns
  return TRUE, FALSE, or WILDMAT_ABORT.

Definition at line 874 of file n_str.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int wildmatcase | ( | register const char * | text,
|  | register const char * | p
| ) |  |

Written by Rich Salz rsalz at osf.org, refurbished by me. Wildcard pattern matching case insensitive.

Parameters

  text | The source text to search
  p | The text to search, with wildcards

Returns
  return TRUE, FALSE, or WILDMAT_ABORT.

Definition at line 941 of file n_str.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int write_and_fit | ( | char ** | dest,
|  | NSTRBYTE * | size,
|  | NSTRBYTE * | written,
|  | char * | src
| ) |  |

Parameters

  dest | The dest string
  size | The current size, will be updated if written + strlen( dest) > size
  written | the number of octet added
  src | The source string to add

Returns
  TRUE on success or FALSE on a realloc error

Definition at line 779 of file n_str.c.