BLC core : Core of Basic Libraries for C/C++
Core functions used by almost all the projects of BLAAR
- 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
- blc_array a structure to manage n-dimensional arrays. It also manage type and format of the data
- blc_realtime few helpers to use time, POSIX semaphore and pthreads
Exemples
Text
#include "blc_core.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, "\nWriting in blue\n\n");
underline_printf('=', "Writing with inlining with '='");
//printing 3 floats of the array values
fprint_tsv_floats(stderr, values, 3);
//Getting the size in characters of the current terminal
blc_terminal_get_size(&columns_nb, &rows_nb);
printf("\nSize of the terminal %dx%d\n", columns_nb, rows_nb);
//Writing size in human readable format
fprint_human_size(stdout, 1000456670); //1Kb = 1024b
printf("\n");
return EXIT_SUCCESS;
}
text graphs
This is the output of blc_core/t_array see the code and the cmake config file
You can modify the code to dynamically refresh the graph with something like:
...
//We display 100 times the graph with the sinusoid shifted each time
for(shift =0; shift <100; shift ++){
// We set a sinusoid in the vector (converting [-1f, 1f] -> [0, 255]) and we shift it
for(i=0; i!=vector.size; i++) vector.uchars[i]=128+127*sinf(i/2.f+ shift);
//If it is not the first time we erase the previous graph by putting back (ANSI escape code) the cursor 16 lines (height of the graph) up. 'sprint' stand for fprint on stderr
if (shift != 0) blc_eprint_cursor_up(16); //blc_text.h function
/*We graph the vector, with title "vector test", the height of 16 chars, the limit max 256 and limit min 0.
Text on abscissa "position" and text on ordinate "intensity"*/
vector.fprint_graph_uchars(stderr, "Vector test", 16, 256, 0, "position", "intensity");
//We stop 100ms to let time to the user to see the graph.
usleep(100000);
}
...
Manipulating blc_array 2D matrix
These displays could be dynamical as well
Structures
blc_mem
A blc_mem is a memory with a pointer of an union (i.e. a memory that can be automatically casted) and a size.
blc_mem mem(8*sizeof(float)); // Allocates 32 bytes for 8 floats
mem.floats[3]=4.3f; //Allocate the fourth float
The possible elements are:
void *data; char *chars; uchar *uchars;
int16_t *ints16; uint16_t *uints16;
int32_t *ints32; uint32_t *uints32;
float *floats; double *doubles;
blc_array
This is a blc_mem which contains information about the structure of the data (types, format, dimensions, ...).
The types are a four bytes uint32_t integer representing four ASCII chars ( comparaison are much faster and easier than char[4] ). They are used to allocate the right amount of memory for each element.
'UIN8': .uchars[i], 'INT8' .chars[i]: 1 byte
'UI16': .uints16[i], 'IN16' .ints16[i]: 2 bytes
'UI32': .uints32[i], 'IN32' .ints32[i], 'FL32' .floats[i]: 4 bytes
'FL64': .doubles[i] : 8 bytes
- format describe how to interpret data. They follow the fourcc convention but you can use your own format.
- 'NDEF' : not defined (default), 'TEXT', 'UTF8' : interpret as strings, 'Y800' : black and white image, 'RGB3' image in RGB, 'YUV2',... 'LPCM' : Linear Pulse Code Modulation, sound in non compressed format, ...
dims_nb represent the type of strutcture: 1 for vector, 2 for matrix, 3 for cubes, ...
dims is an array of blc_dim containing the number of elements of each dim (length) and the step to use to go from one element of the dim to the next one. Usually step is the product of the length of the previous dimensions. For example an image 800x600 of 3 components of colors would be:
- dims_nb=3; dims[0].length=3; dims[0].step=1; dims[1].length=800; dims[1].step=3; dims[2].length=600; dims[2].step=2400; //1x3x800
For the complete API use ./doc_api.sh blibs/blc_core