project_files/frontlib/util/util.h
changeset 7230 240620f46dd7
parent 7227 1c859f572d72
child 7269 5b0aeef8ba2a
equal deleted inserted replaced
7227:1c859f572d72 7230:240620f46dd7
     1 #ifndef FLIB_UTIL_H_
     1 #ifndef FLIB_UTIL_H_
     2 #define FLIB_UTIL_H_
     2 #define FLIB_UTIL_H_
     3 
     3 
     4 #include <stddef.h>
     4 #include <stddef.h>
     5 #include <stdarg.h>
     5 #include <stdarg.h>
       
     6 #include <stdbool.h>
     6 
     7 
     7 /**
     8 /**
     8  * Prints a format string to a newly allocated buffer of the required size.
     9  * Prints a format string to a newly allocated buffer of the required size.
     9  * Parameters are like those for printf. Returns NULL on error.
    10  * Parameters are like those for printf. Returns NULL on error.
    10  *
    11  *
    44  * is available. Otherwise behaves exactly like calloc.
    45  * is available. Otherwise behaves exactly like calloc.
    45  */
    46  */
    46 void *flib_calloc(size_t count, size_t elementsize);
    47 void *flib_calloc(size_t count, size_t elementsize);
    47 
    48 
    48 /**
    49 /**
       
    50  * Simple realloc wrapper that automatically logs an error if no memory
       
    51  * is available. Otherwise behaves exactly like realloc.
       
    52  */
       
    53 void *flib_realloc(void *ptr, size_t size);
       
    54 
       
    55 /**
    49  * Replace all non-alphanumeric and non-ascii bytes with escape
    56  * Replace all non-alphanumeric and non-ascii bytes with escape
    50  * sequences in the form %XX. Does not modify the original string,
    57  * sequences in the form %XX. Does not modify the original string,
    51  * but returns a newly allocated one that must be free()d. Returns
    58  * but returns a newly allocated one that must be free()d. Returns
    52  * null on failure or if null was passed as argument.
    59  * null on failure or if null was passed as argument.
    53  *
    60  *
    54  * This should work fine with all ASCII-based charsets including UTF-8.
    61  * This should work fine with all ASCII-based charsets including UTF-8.
    55  */
    62  */
    56 char *flib_urlencode(const char *str);
    63 char *flib_urlencode(const char *str);
    57 
    64 
    58 /**
    65 /**
       
    66  * Replace some bytes with escape sequences in the form %XX.
       
    67  * Does not modify the original string, but returns a newly allocated
       
    68  * one that must be free()d.
       
    69  *
       
    70  * All bytes for which the predicate function returns true are escaped.
       
    71  *
       
    72  * Returns null on failure or if null was passed as argument.
       
    73  */
       
    74 char *flib_urlencode_pred(const char *str, bool (*needsEscaping)(char c));
       
    75 
       
    76 /**
    59  * Replace escape sequences of the form %XX with their byte values.
    77  * Replace escape sequences of the form %XX with their byte values.
    60  * Does not modify the original string, but returns a newly allocated
    78  * Does not modify the original string, but returns a newly allocated
    61  * one that must be free()d. Returns null on failure or if null was
    79  * one that must be free()d. Returns null on failure or if null was
    62  * passed as argument.
    80  * passed as argument.
    63  */
    81  */