Commit 8f4ab168 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

initial files for core functions of blc

parents
# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2016)
# Author: Arnaud Blanchard
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
# users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
cmake_minimum_required(VERSION 2.6)
project(blc_core)
add_definitions(-Wall -Wextra -Wno-multichar)
include_directories(include)
#source files
set(sources
src/blc_text.cpp
src/blc_tools.cpp
src/blc_mem.cpp
src/blc_array.cpp
src/blc_realtime.cpp
)
set(BLC_CORE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE)
add_library(shared_blc_core SHARED ${sources})
add_library(static_blc_core STATIC ${sources})
#Both librairies have the same name only the extension will change depending on the OS
set_target_properties(shared_blc_core PROPERTIES OUTPUT_NAME blc_core)
set_target_properties(static_blc_core PROPERTIES OUTPUT_NAME blc_core)
#Describe what will be to install or in the package by default the prefix (CMAKE_INSTALL_PREFIX) is '/usr/’
install(DIRECTORY include/ DESTINATION include)
install(TARGETS shared_blc_core static_blc_core DESTINATION lib)
set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Arnaud Blanchard")
include(CPack)
#Building examples
#add_executable(blc_demo examples/blc_demo.c)
#target_link_libraries(blc_demo shared_blc)
#Add a target to generate documentation
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxyfile.in doxyfile.doxy) #Replace the CMAKE variables in the Doxyfile
add_custom_target(doc_${PROJECT_NAME} ALL ${DOXYGEN_EXECUTABLE} doxyfile.doxy COMMENT "Generating API documentation with Doxygen" VERBATIM )
else(DOXYGEN_FOUND)
message("You need to install doxygen to generate the doc.")
endif(DOXYGEN_FOUND)
BLC core
========
Copyright : [ETIS](http://www.etis.ensea.fr/neurocyber) - ENSEA, University of Cergy-Pontoise, CNRS (2011-2016)
Author : [Arnaud Blanchard](http://arnaudblanchard.thoughtsheet.com)
Licence : [CeCILL v2.1](http://www.cecill.info/licences/Licence_CeCILL_V2-en.html)
Core of Basic Libraries for C/C++
---------------------------------
It is composed of three modules (need a POSIX system), for Linux and OSX.
- **blc_text** manage ASCII terminals (colors, cursor, sizes)
- **blc_tools** macros and functions to simplify coding and memory management
- **blc_mem** a memory structure (data pointer and size) essentially to manage dynamic memory buffers
Building
--------
You need git, g++, cmake and doxygen for the documentation:
- Ubuntu: `sudo apt-get install git g++ cmake doxygen`
- OSX with [homebrew](http://brew.sh): `brew install git g++ cmake doxygen`
You can copy past the following code.
git clone https://framagit.org/blaar/blc_core.git
cd blc_core
mkdir build
cd build
cmake ..
make
The created librairy `libblc_core.{so|dylib}` and `libblc_core.a` will be in `build/`
You can create a debian package (.deb) with:
make package
Documentation
-------------
The [documentation](build/doc/html/index.html) is in `build/doc/{html|latex}` (the library need to be compiled before).
PROJECT_NAME = "BLC core"
OUTPUT_DIRECTORY = doc
OPTIMIZE_OUTPUT_FOR_C = YES
EXTRACT_LOCAL_CLASSES = NO
HIDE_UNDOC_MEMBERS = YES
HIDE_UNDOC_CLASSES = YES
HIDE_FRIEND_COMPOUNDS = YES
HIDE_IN_BODY_DOCS = YES
CASE_SENSE_NAMES = NO
SORT_MEMBER_DOCS = NO
QUIET = YES
INPUT = ${CMAKE_CURRENT_SOURCE_DIR}
RECURSIVE = YES
USE_MDFILE_AS_MAINPAGE = ${CMAKE_CURRENT_SOURCE_DIR}/README.md
ALPHABETICAL_INDEX = NO
HTML_OUTPUT = html
LATEX_OUTPUT = latex
MACRO_EXPANSION = YES
PREDEFINED = __cplusplus = 1
REPEAT_BRIEF = YES
SKIP_FUNCTION_MACROS = NO
CLASS_DIAGRAMS = NO
/* Basic Library for C/C++ (blclib)
Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2014)
Author: A. Blanchard
This software is a computer program whose purpose is to simulate neural networks and control robots or simulations.
This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
 users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
 In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
 that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured
 and, more generally, to use and operate it in the same conditions as regards security.
 The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms. */
/**
@date Apr 28, 2014
@author Arnaud Blanchard
@defgroup blc_mem Memory strucuture with a pointer and a size.
@{
This kind of functionnalities are more complete with the Standard Template Library (STL) but it much simpler here.
*/
#ifndef BLC_ARRAY_H
#define BLC_ARRAY_H
#include "blc_tools.h"
#include "blc_mem.h"
#define EXIT_ON_ARRAY_ERROR(array, ...) blc_array_fatal_error(array, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
typedef struct blc_dim {
int length;
size_t step;
} blc_dim;
typedef struct blc_array
#ifdef __cplusplus
:blc_mem {
blc_array();
void vdef_array(uint32_t type, uint32_t format, int dims_nb, int length, va_list arguments);
void def_array(uint32_t type, uint32_t format, int dims_nb, int length0, ...);
void def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim *dims);
void destroy();
void add_dim(int length, int step);
void add_dim(int length);
void set_dims(int dims_nb, int length, ...);
int sprint_dims(char *string, int size);
int fprint_dims(FILE *file) const;
void fprint_debug(FILE *file) const;
void fscan_dims(FILE *file);
void sscan_dims(char const* scan);
int get_type_size();
size_t get_minimum_size();
#else
{
blc_mem mem;
#endif
blc_dim * dims;
int dims_nb;
uint32_t type, format;
} blc_array;
START_EXTERN_C
void blc_array_fatal_error(blc_array const *array, const char *name_of_file, const char* name_of_function, int numero_of_line, const char *message, ...);
void blc_array_destroy(blc_array *narray);
END_EXTERN_C
///@}
#endif
#ifndef BLC_CORE_H
#define BLC_CORE_H
#include "blc_array.h"
#include "blc_mem.h"
#include "blc_realtime.h"
#include "blc_text.h"
#include "blc_tools.h"
#endif
#ifndef BLC_CORE_H
#define BLC_CORE_H
#include "blc_array.h"
#include "blc_mem.h"
#include "blc_text.h"
#include "blc_tools.h"
#endif
/* Basic Library for C/C++ (blclib)
Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2014)
Author: A. Blanchard
This software is a computer program whose purpose is to simulate neural networks and control robots or simulations.
This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
 users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
 In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
 that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured
 and, more generally, to use and operate it in the same conditions as regards security.
 The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms. */
/**
@date Apr 28, 2014
@author Arnaud Blanchard
@defgroup blc_mem blc Memory
@{
@brief Structure with a data pointer and a size. This kind of functionnalities are more complete with the Standard Template Library (STL) or boost, but it is much simpler here.
@code{.c}
typedef struct blc_mem {
//This union is a convenient shortcut for casting the data pointer.
union{
void *data;
char *chars;
uchar *uchars;
int16_t *ints16;
uint16_t *uints16;
int32_t *ints32;
uint32_t *uints32;
float *floats;
double *doubles;
};
size_t size;
}
@endcode
*/
#ifndef BLC_MEM_H
#define BLC_MEM_H
#include "blc_tools.h"
#include <stdio.h>
typedef struct blc_mem {
//We can use only one of this datatpe at a time
union{
void *data;
char *chars;
uchar *uchars;
int16_t *ints16;
uint16_t *uints16;
int32_t *ints32;
uint32_t *uints32;
float *floats;
double *doubles;
};
size_t size;
#ifdef __cplusplus
blc_mem();
/** Create and allocate memory of size size */
blc_mem(size_t size);
/**really allocates the data of size previously set*/
void allocate();
/** Change the size of memory. The content is not preserved. Use size = 0 to free the memory. */
void allocate(size_t size);
/// Allocate more memory if needed but does not free memory
void allocate_min(size_t size);
/** Change the size of memory. But keep the content.*/
void reallocate(size_t size);
/** Increases the memory of size and adds the new content.*/
void append(char const *new_data, size_t new_data_size);
/** Replace the memory with the new data and size*/
void replace(char const *new_data, size_t size);
/** Reset all the value to 'value'. */
void reset(int value=0);
#endif
}blc_mem;
START_EXTERN_C
#define BLC_MEM_INITIALIZER {.data=NULL, .size=0}
/// Allocate or reallocate memory as needed but lose the content.
void blc_mem_allocate(blc_mem *mem, size_t size);
/// Reallocate memory and keep the content.
void blc_mem_reallocate(blc_mem *mem, size_t size);
/// Adapt the size and replace the content with the new one
void blc_mem_replace(blc_mem *mem, char const* data, size_t size);
/// Increase the size and append the data at the end.
void blc_mem_append(blc_mem *mem, char const* data, size_t size);
END_EXTERN_C
///@}
#endif
/* Basic Library for C/C++ (blclib)
Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2014)
Author: Arnaud Blanchard
This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
 users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
 In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
 that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured
 and, more generally, to use and operate it in the same conditions as regards security.
 The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms. */
/**
@defgroup blc_realtime realtime
Few functions helping for pseudo realtime applications.
@{*/
#ifndef BLC_REALTIME_H
#define BLC_REALTIME_H
#include "blc_tools.h"
#include <semaphore.h>
#define EXIT_ON_PTHREAD_ERROR(pthread_error_id, ...) fatal_pthread_error(__FILE__, __FUNCTION__, __LINE__, pthread_error_id, NULL, __VA_ARGS__)
#define PTHREAD_CHECK(command, error_variable, ...) do{if (((error_variable)=(command))) fatal_pthread_error(__FILE__, __FUNCTION__, __LINE__, error_variable, STRINGIFY(command), __VA_ARGS__);}while(0)
START_EXTERN_C
void fatal_pthread_error(const char *name_of_file, const char* name_of_function, int numero_of_line, int error_id, const char *command, const char *message, ...);
/// Return the time difference between the timeval struct and the actual time. If time is empty, it is filled with the current time and 0 is returned.
long us_time_diff(struct timeval *time);
///Check weather a semaphore is locked. You may use sem_getvalue instead but it does not exist on darwin (Mac OSX).
int sem_is_locked(sem_t *sem);
/**Try to lock the mutex in timeout micro seconds.
@return 1 in case of immediate success, -1 in case of succes after microseconds, 0 in case of failure */
int blc_mutex_trylock_in_time(pthread_mutex_t *mutex, uint32_t microseconds);
END_EXTERN_C
#endif
///@}
\ No newline at end of file
/*
Basic Library for C/C++ (blclib)
Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2015)
Author: Arnaud Blanchard
This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
 users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
 In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
 that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured
 and, more generally, to use and operate it in the same conditions as regards security.
 The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.*/
/**
@defgroup blc_text Text_tools
@{
@brief Provide tools to manipulate text on files or on the terminal (color, formating, ...).
Consider using ncurses ( https://www.gnu.org/software/ncurses ) for more interactive tools with the terminal.
@code {.c}
#include "blc.h"
int main(int argc, char **argv){
int rows_nb, columns_nb;
float values[3]={1.3, 4.2, 5.5};
color_printf(BLC_BLUE, "\nJ'écris en bleu\n\n");
underline_printf('=', "J'écris souligné avec des '='");
fprint_tsv_floats(stdout, values, 3);
blc_terminal_get_size(&columns_nb, &rows_nb);
printf("\nTaille du terminal %dx%d\n", columns_nb, rows_nb);
fprint_human_size(stdout, 1000456670); //1Kb = 1024b
printf("\n");
return EXIT_SUCCESS;
}
@endcode
*/
#ifndef BLC_TEXT_H
#define BLC_TEXT_H
#include <stdio.h>
#include <stdarg.h>
#include "blc_tools.h"
#include "blc_mem.h"
#define MOVE_BEGIN_PREVIOUS_N_LINES "%dF"
#define DEL_END_SCREEN "0J"
#define DEL_BEGIN_SCREEN "1J"
#define DEL_BEGIN_LINE "1K"
#define DEL_ALL_LINE "2K"
///Number of gradual colors we can use.
#define BLC_BAR_COLORS_NB 12
#define BLC_BRIGHT 0x8
///Code to be used in text coloring function (color_id).
enum BLC_COLORS{
BLC_BLACK = 0,
BLC_RED = 1,
BLC_GREEN = 2,
BLC_YELLOW = 3,
BLC_BLUE = 4,
BLC_MAGENTA = 5,
BLC_CYAN = 6,
BLC_WHITE = 7,
BLC_GREY = 8,
BLC_BRIGHT_RED = 9,
BLC_BRIGHT_GREEN = 10,
BLC_BRIGHT_YELLOW = 11,
BLC_BRIGHT_BLUE = 12,
BLC_BRIGHT_MAGENTA = 13,
BLC_BRIGHT_CYAN = 14,
BLC_BRIGHT_WHITE = 15,
BLC_COLORS_NB
};
// eprint for info print (stderr) to oppose standard cursor (stdout) usually not used
#define blc_eprint_cursor_position(row, column) eprintf_escape_command("%d;%dH", row, column);
#define blc_eprint_cursor_up(rows_nb) eprintf_escape_command("%dA", rows_nb);
#define blc_eprint_cursor_down(rows_nb) eprintf_escape_command("%dB", rows_nb);
#define blc_eprint_del_end_line() eprintf_escape_command("0K");
#define blc_eprint_del_end_screen() eprintf_escape_command("0J");
#define blc_fprint_del_end_line(file) fprintf_escape_command(file, "0K");
//Shortcut to escape directly on stdout
#define printf_escape_command( ...) if (blc_stdout_ansi) fprintf_escape_command(stdout, __VA_ARGS__)
//Shortcut to escape directly on stderr
#define eprintf_escape_command( ...) if (blc_stderr_ansi) fprintf_escape_command(stderr, __VA_ARGS__)
///Start a new color for the standart output.
#define print_start_color(color_id) fprint_start_color(stdout, color_id)
///Reset the default color for the standart output.
#define print_stop_color() fprint_stop_color(stdout)
///
#define color_printf(color_id, ...) color_fprintf(color_id, stdout, __VA_ARGS__)
///
#define color_eprintf(color_id, ...) color_fprintf(color_id, stderr, __VA_ARGS__)
///apply on stdout
#define underline_printf(char_pattern, ...) underline_fprintf( char_pattern, stdout, __VA_ARGS__)
///apply on stdout
#define print_human_size(size) fprint_human_size(stdout, size)
///apply on stdout
#define print_tsv_floats(values, values_nb) fprint_tsv_floats(stdout, values, values_nb);
///apply on stdout
#define scan_tsv_floats(values, values_nb) fscan_tsv_floats(stdin, values, values_nb);
///Variable setting wether we can use ANSI escape codes. This value is true by default, use terminal_ansi_detect() to set its value.
extern int blc_stdout_ansi;
///Variable setting wether we can use ANSI escape codes. This value is true by default, use terminal_ansi_detect() to set its value.
extern int blc_stderr_ansi;
///Contain the color_id going gradually from dark_blue to dark_red corresponding to @ref BLC_BAR_COLORS_NB scales.
extern uchar blc_bar_colors[BLC_BAR_COLORS_NB];
START_EXTERN_C
///Try to get the position of the cursor on the terminal.
int blc_fterminal_try_to_get_cursor_position(FILE *file, int *x, int *y);
///Test wether the terminal is ANSI. color etc ..
int blc_stdout_ansi_detect();
///
int blc_stderr_ansi_detect();
///Get the size of the terminal windows in characters.
void blc_terminal_get_size(int *columns, int *lines);
///Set a callback to be called each time the terminal window is resized.
void blc_terminal_set_resize_callback(void (*callback)(int columns_nb, int rows_nb));
///Send an escape command on the terminal.
void fprintf_escape_command(FILE *file, char const *format, ...);
///Set a new color for the terminal.
void fprint_start_color(FILE *file, int color_id);
///Reset the default color for the terminal
void fprint_stop_color(FILE *file);
///Change the backgroungd color.
void fprint_backgournd_start_color(FILE *file, int color_id);
///Set back the background color to default.
void fprint_backgournd_stop_color(FILE *file);
///Print a text in color.
void color_fprintf(int color_id, FILE *file, char const *format, ...);
///Put a color text in a char string
int color_sprintf(int color_id, char *string, const char *format, ...);
///Print a text in color.
void color_vfprintf(int color_id, FILE *file, char const *format, va_list arguments);
///Put a color text in a char string
int color_vsprintf(int color_id, char *string, char const *format, va_list arguments);
///Underline with the pattern char the text given in parameter.
void underline_fprintf(char pattern, FILE * file, char const *format, ...);
///Print to the file a human readable format of the size ( Kb, Mb, Gb, ...).
void fprint_human_size(FILE *file, size_t size);
///Print in tab separated values the array of values.
void fprint_tsv_floats(FILE *file, float *values, int values_nb);
///Read tab separated values in the files and set values with the result.
void fscan_tsv_floats(FILE *file, float *values, int values_nb);
int fprint_fourcc(FILE *file, uint32_t value);
char* sprint_fourcc(char *string, uint32_t value);
int blc_sscan_dims(int **lengths, char const *str);
END_EXTERN_C
///@}
#endif
/* Basic Library for C/C++ (blclib)
Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2014)
Author: A. Blanchard
This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
 users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
 In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
 that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured
 and, more generally, to use and operate it in the same conditions as regards security.
 The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
Created on: Apr 28, 2011
*/
#ifndef BLC_TOOLS_H
#define BLC_TOOLS_H
/**
@defgroup blc_tools Basic tools (memory, error) ...
@{*/
///Start a C function definition block. The function inside this block will be able to be call by C programs even if it is compiled in C++.
#ifdef __cplusplus
#define START_EXTERN_C extern "C" {
///End a C definition block.
#define END_EXTERN_C }
#else
#define START_EXTERN_C
///End a C definition block.
#define END_EXTERN_C
#endif
#include <stdio.h> /* FILE* */
#include <stdlib.h> /* size_t */
#include <stdarg.h> /* variable arguments ... */
#include <string.h> /*memset*/
#include <stdint.h> /* uint32_t */
#include <sys/param.h>
#include <arpa/inet.h> /* htonl, ... */
/// Set zeros to any structures. These structures have to be static otherwise the macro cannot determine the size of the structure.
#define CLEAR(structure) memset(&(structure), 0, sizeof(structure))
/// Clip the value on the number between 0 and 255. Usefull to convert any number to a uchar.
#define CLIP_UCHAR(x) (uchar)((x) < 0 ? 0 : ((x) >= 256 ? 255 : (x)))
///Shorcut to do a loop from 0 to max-1 element. If it is not break, the last iterator after the loop is max.
#define FOR(iterator, max) for (iterator = 0; iterator != max; ++iterator)
///Shorcut to do a loop from max-1 elements to 0. This is a bit faster. If it is not broken, the last iterator after the loop is -1.
#define FOR_INV(iterator, max) for (iterator = max; iterator--;)
/// Do a modulo even with negative numbers.
#define MOD(a,b) ((((a)%(b))+(abs(b)))%(b))
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX //Darwin (APPLE) does not have HOST_NAME_MAX definition
#endif
///definition of qsort_r in the with the BSD syntax even on gnuC. Be careful the compar function passed in argument has to be adapted as well. The void *arg is expected as a third argument on gnuC and it is as a first on Darwin
#ifdef BSD
#define QSORT_R qsort_r