libtools
Modules | Macros | Typedefs | Functions
libtools

This is a set of application functions that can be useful not only within this project. More...

Collaboration diagram for libtools:

Modules

 List macros
 Lite list implementation from Linux kernel.
 
 String routines
 Functionality for creating SQL queries, making strong passwords, trimming a string if there are any space characters, parsing string as URL.
 
 Network routines
 HTTP server, HTTP client, socket functionality.
 
 Input/Output routines
 Functionality includes closing descriptors of current process, running server as a daemon, recursive remove of a directory or a file, opening and locking file, closing file.
 

Macros

#define SHA1_STRING_LENGTH   (SHA_DIGEST_LENGTH * 2 + 1)
 length of SHA1 string More...
 
#define countof(x)   (sizeof(x) / sizeof(x[0]))
 Calculate length of array. More...
 
#define unused(x)   ((void)x)
 Variable can be not used in Release build, to suppress compiler warning this macro should be used. More...
 
#define min(a, b)
 return minimal number More...
 
#define max(a, b)
 return maximum number More...
 
#define cast_ptr(t, x)
 cast pointer More...
 

Typedefs

typedef struct mailbox * mailbox_t
 mailbox handle More...
 
typedef uint8_t sha1_t[SHA_DIGEST_LENGTH]
 variable type for storing SHA1 hash More...
 

Functions

void strdump (const void *str, size_t len)
 print pritable characters of buffer More...
 
void hexdump8 (const void *buf, size_t len)
 print hexdump More...
 
void cdump8 (const char *name, const void *buf, size_t len)
 print data like a C array More...
 
char * read_env (const char *env, char *s, size_t len)
 read environment variable More...
 
char * bin2hex (const void *data, size_t len, char *hex, size_t hex_len)
 convert binary data to hex string More...
 
size_t hex2bin (const char *hex, void *data, size_t data_len)
 convert hex string to binary data More...
 
mailbox_t mailbox_alloc (mailbox_t *mb, unsigned len) __attribute__((warn_unused_result))
 allocate new mailbox More...
 
void mailbox_destroy (mailbox_t mb)
 destroy mailbox More...
 
int mailbox_post (mailbox_t mb, void *data)
 post message into mailbox More...
 
int mailbox_wait (mailbox_t mb, void **data)
 retrive message from mailbox More...
 
int mailbox_timedwait (mailbox_t mb, void **data, unsigned timeout)
 retrive message from mailbox with or wait for message More...
 
char * mysql_vasprintf (MYSQL *mysql, char **str, const char *format, va_list args)
 allocate and format mysql query string More...
 
char * mysql_asprintf (MYSQL *mysql, char **str, const char *format,...)
 allocate and format mysql query string More...
 
int mysql_transaction (MYSQL *mysql)
 Start new transaction. More...
 
int mysql_qprintf (MYSQL *mysql, const char *format,...)
 Send a MySQL query. More...
 
int sha1_file (const char *path, sha1_t *hash)
 calculate SHA1 hash for specified file More...
 

Detailed Description

This is a set of application functions that can be useful not only within this project.

This set contains functionality for working with sockets, files, MySQL, strings, regular expressions, directories, etc.

Macro Definition Documentation

#define cast_ptr (   t,
 
)
Value:
__extension__ ({ \
(typeof(t)*)x; \
})

cast pointer

Parameters
[in]ttarget type
[in]xpointer
#define countof (   x)    (sizeof(x) / sizeof(x[0]))

Calculate length of array.

#define max (   a,
 
)
Value:
__extension__ ({ \
typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a < _b ? _b : _a; \
})

return maximum number

#define min (   a,
 
)
Value:
__extension__ ({ \
typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _b : _a; \
})

return minimal number

#define SHA1_STRING_LENGTH   (SHA_DIGEST_LENGTH * 2 + 1)

length of SHA1 string

#define unused (   x)    ((void)x)

Variable can be not used in Release build, to suppress compiler warning this macro should be used.

Typedef Documentation

typedef struct mailbox* mailbox_t

mailbox handle

typedef uint8_t sha1_t[SHA_DIGEST_LENGTH]

variable type for storing SHA1 hash

Function Documentation

char* bin2hex ( const void *  data,
size_t  len,
char *  hex,
size_t  hex_len 
)

convert binary data to hex string

Parameters
[in]datapointer to binary data
[in]lenlength of binary data
[out]hexpointer to result buffer
[in]hex_lenlength of buffet at pointer hex
Returns
pointer to hex string
Return values
NULLerror occurred
void cdump8 ( const char *  name,
const void *  buf,
size_t  len 
)

print data like a C array

Parameters
[in]namename of C array
[in]bufpointer to data
[in]lenlength of data
size_t hex2bin ( const char *  hex,
void *  data,
size_t  data_len 
)

convert hex string to binary data

Parameters
[in]hexpointer to string with hex numbers
[out]datapointer to data buffer
[in]data_lenlength of data buffer at pointer data
Returns
length of converted data
void hexdump8 ( const void *  buf,
size_t  len 
)

print hexdump

Parameters
[in]bufpointer to data
[in]lenlength of data
mailbox_t mailbox_alloc ( mailbox_t mb,
unsigned  len 
)

allocate new mailbox

Parameters
[out]mbpointer to new mailbox
[in]lenlength of mailbox
Returns
pointer to new mailbox
Return values
NULLerror occurred

Function update errno if error occurred.

void mailbox_destroy ( mailbox_t  mb)

destroy mailbox

Parameters
[in]mbpointer to mailbox

If mailbox is NULL, no operation is performed.

int mailbox_post ( mailbox_t  mb,
void *  data 
)

post message into mailbox

Parameters
[in]mbpointer to mailbox
[in]datamessage
Returns
on success, zero is returned
Return values
-1error occured

If the mailbox is full, the function will wait for the availability of space.

int mailbox_timedwait ( mailbox_t  mb,
void **  data,
unsigned  timeout 
)

retrive message from mailbox with or wait for message

Parameters
[in]mbpointer to mailbox
[out]datamessage
[in]timeouttimeout for message
Returns
on success, zero is returned
Return values
-1error occured

If the mailbox is empty, the function will wait for message until timeout expired.

int mailbox_wait ( mailbox_t  mb,
void **  data 
)

retrive message from mailbox

Parameters
[in]mbpointer to mailbox
[out]datamessage
Returns
on success, zero is returned
Return values
-1error occured

If the mailbox is empty, the function will wait for message.

char* mysql_asprintf ( MYSQL *  mysql,
char **  str,
const char *  format,
  ... 
)

allocate and format mysql query string

Parameters
[in]mysqlmysql connection handle
[out]strmysql query string
[in]formatformat of output string
Returns
pointer to allocated string
Return values
NULLerror occurred

This function is similar to asprintf(), but has another string format. Next interpreted sequences supported:

  • %int% argument is integer
  • %double% argument is double
  • %bool% same as %int%, boolean
  • %str% NULL-terminated string, can be NULL
  • %time% unix time, time_t
  • %% one character '%'

Zero %int% values, empty or NULL strings (%str%) serialized to SQL NULL. Boolean values serialized to TRUE (!= 0) or FALSE (0)

Next sequences never serialized to NULL:

  • %INT% argument is integer
  • %DOUBLE% argument is double
  • %STR% NULL-terminated string, can be NULL
  • %RSTR% string without quotes

Function updates errno, if error occurred:

  • EINVAL argument format have invalid interpreted sequences
int mysql_qprintf ( MYSQL *  mysql,
const char *  format,
  ... 
)

Send a MySQL query.

Parameters
[in]mysqlmysql connection handle
[in]formatmysql query string
Returns
on success, zero is returned
Return values
-1error occurred
See also
mysql_asprintf

Function update errno, if error occurred:

  • EIO I/O error (in most cases MySQL error)
int mysql_transaction ( MYSQL *  mysql)

Start new transaction.

Parameters
[in]mysqlconnection handle
Returns
on success, zero is returned
Return values
-1error occurred
Remarks
automatically commit current transaction, before starting new
char* mysql_vasprintf ( MYSQL *  mysql,
char **  str,
const char *  format,
va_list  args 
)

allocate and format mysql query string

Parameters
[in]mysqlmysql connection handle
[out]strmysql query string
[in]formatformat of output string
Returns
pointer to allocated string
Return values
NULLerror occurred

This function is similar to asprintf(), but has another string format. Next interpreted sequences supported:

  • %int% argument is integer
  • %double% argument is double
  • %bool% same as %int%, boolean
  • %str% NULL-terminated string, can be NULL
  • %time% unix time, time_t
  • %% one character '%'

Zero %int% values, empty or NULL strings (%str%) serialized to SQL NULL. Boolean values serialized to TRUE (!= 0) or FALSE (0)

Next sequences never serialized to NULL:

  • %INT% argument is integer
  • %DOUBLE% argument is double
  • %STR% NULL-terminated string, can be NULL
  • %RSTR% string without quotes

Function updates errno, if error occurred:

  • EINVAL argument format have invalid interpreted sequences
    Parameters
    [in]argslist of arguments
char* read_env ( const char *  env,
char *  s,
size_t  len 
)

read environment variable

Parameters
[in]envvariable name
[in]spointer to string buffer
[in]lenlength of buffer at s
Returns
pointer to string buffer s
Return values
NULLenvironment variable don't exist
int sha1_file ( const char *  path,
sha1_t hash 
)

calculate SHA1 hash for specified file

Parameters
[in]pathfilename
[out]hashSHA1 hash sum
Returns
on success, zero is returned
Return values
-1error occurred
void strdump ( const void *  str,
size_t  len 
)

print pritable characters of buffer

Parameters
[in]strpointer to buffer
[in]lenlength