Commit 5b38088b authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Merge branch 'master' of https://framagit.org/blibs/blc_core

parents e9fd48b6 c4f23b37
......@@ -120,6 +120,8 @@ typedef struct blc_array
void init_with_blc_file(char const *filename);
/**Update the content of the memory with the content of the blc file. The blc_array has to be properly defined and allocated before. .blc format is a specific format, whre de first line is the properties of the blc_array and them it is raw binary memory. It may be a problem with endianness */
void update_with_blc_file(char const *filename);
void def_with_tsv_file(char const *filename);
/**Same as update_with_blc_file but this reas .tsv, tab separated values (i.e. 0.75 0.33 0.55 ). Typically used with excel and similar. You have to care yourself to define and allocate an appropriate blc_array.*/
void update_with_tsv_file(char const *filename);
......
/* 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".
......@@ -55,159 +53,159 @@ END_EXTERN_C
blc_array::blc_array():type('NDEF'), format('NDEF'), dims(NULL), dims_nb(0), total_length(0){}
blc_array::blc_array(uint32_t type, uint32_t format, int dims_nb, int length0, ...){
va_list arguments;
va_start(arguments, length0);
vdef_array( type, format, dims_nb, length0, arguments);
allocate();
va_end(arguments);
va_list arguments;
va_start(arguments, length0);
vdef_array( type, format, dims_nb, length0, arguments);
allocate();
va_end(arguments);
}
blc_array::~blc_array(){
if (dims) FREE(dims);
if (dims) FREE(dims);
}
void blc_array::vdef_array(uint32_t type, uint32_t format, int dims_nb, int length, va_list arguments){
total_length=1;
this->dims_nb=dims_nb;
this->type=type;
this->format=format;
dims=vcreate_blc_dims(&size, type, dims_nb, length, arguments);
total_length=size/get_type_size();
total_length=1;
this->dims_nb=dims_nb;
this->type=type;
this->format=format;
dims=vcreate_blc_dims(&size, type, dims_nb, length, arguments);
total_length=size/get_type_size();
}
void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, int length, ...){
va_list arguments;
va_start(arguments, length);
vdef_array(type, format, dims_nb, length, arguments);
va_end(arguments);
va_list arguments;
va_start(arguments, length);
vdef_array(type, format, dims_nb, length, arguments);
va_end(arguments);
}
void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim *dims){
this->type=type;
this->format=format;
this->dims_nb=dims_nb;
size=get_type_size();
this->dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
memcpy(this->dims, dims, sizeof(blc_dim)*dims_nb);
size =dims[dims_nb-1].length*dims[dims_nb-1].step;
total_length=size/get_type_size();
this->type=type;
this->format=format;
this->dims_nb=dims_nb;
size=get_type_size();
this->dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
memcpy(this->dims, dims, sizeof(blc_dim)*dims_nb);
size =dims[dims_nb-1].length*dims[dims_nb-1].step;
total_length=size/get_type_size();
}
void blc_array::def_array(uint32_t type, uint32_t format, char const *dims_string){
this->type=type;
this->format=format;
sscan_dims(dims_string);
this->type=type;
this->format=format;
sscan_dims(dims_string);
}
void blc_array::init(char const *properties){
sscan_properties(properties);
allocate();
sscan_properties(properties);
allocate();
}
void blc_array::vinit(uint32_t type, uint32_t format, int dims_nb, int length, va_list arguments){
vdef_array(type, format, dims_nb, length, arguments);
allocate();
vdef_array(type, format, dims_nb, length, arguments);
allocate();
}
void blc_array::init(uint32_t type, uint32_t format, int dims_nb, int length, ...){
va_list arguments;
va_start(arguments, length);
vinit(type, format, dims_nb, length, arguments);
va_end(arguments);
va_list arguments;
va_start(arguments, length);
vinit(type, format, dims_nb, length, arguments);
va_end(arguments);
}
void blc_array::add_dim(int length, int step){
blc_dim dim;
dim.length=length;
dim.step=step;
APPEND_ITEM(&dims, &dims_nb, &dim);
size=step*length; //Check if it always true ??
total_length=size/get_type_size();
blc_dim dim;
dim.length=length;
dim.step=step;
APPEND_ITEM(&dims, &dims_nb, &dim);
size=step*length; //Check if it always true ??
total_length=size/get_type_size();
}
void blc_array::add_dim(int length){
if (dims_nb==0) add_dim(length, get_type_size());
else add_dim(length, dims[dims_nb-1].length*dims[dims_nb-1].step);
if (dims_nb==0) add_dim(length, get_type_size());
else add_dim(length, dims[dims_nb-1].length*dims[dims_nb-1].step);
}
void blc_array::set_dims(int dims_nb, int length, ...){
blc_dim *dim;
va_list args;
if (dims) FREE(dims);
this->dims_nb=dims_nb;
dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
size=get_type_size();
va_start(args, length);
FOR_EACH(dim, dims, dims_nb)
{
size*=length;
dim->step=1;
dim->length=length;
length=va_arg(args, int);
}
va_end(args);
total_length=size/get_type_size();
blc_dim *dim;
va_list args;
if (dims) FREE(dims);
this->dims_nb=dims_nb;
dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
size=get_type_size();
va_start(args, length);
FOR_EACH(dim, dims, dims_nb)
{
size*=length;
dim->step=1;
dim->length=length;
length=va_arg(args, int);
}
va_end(args);
total_length=size/get_type_size();
}
int blc_array::get_type_size(){
return blc_get_type_size(type);
return blc_get_type_size(type);
}
int blc_array::sprint_dims(char *string, int string_size){
int i, width=0;
if (dims_nb==0) width=snprintf(string, string_size, "0");
else {
width=snprintf(string, string_size, "%lu", dims[0].length);
for(i=1; i<dims_nb; i++) width+=snprintf(string+width, string_size-width, "x%lu", dims[i].length);
}
if (width >= string_size) EXIT_ON_ERROR("The reserved size %d is too small to store the %d dims.",size, dims_nb);
return width;
int i, width=0;
if (dims_nb==0) width=snprintf(string, string_size, "0");
else {
width=snprintf(string, string_size, "%lu", dims[0].length);
for(i=1; i<dims_nb; i++) width+=snprintf(string+width, string_size-width, "x%lu", dims[i].length);
}
if (width >= string_size) EXIT_ON_ERROR("The reserved size %d is too small to store the %d dims.",size, dims_nb);
return width;
}
int blc_array::fprint_dims(FILE *file)const {
int i, width=0;
if (dims_nb==0) width+=fprintf(file, "0");
else {
width+=fprintf(file, "%lu", dims[0].length);
for(i=1; i<dims_nb; i++) width+=fprintf(file, "x%lu", dims[i].length);
}
return width;
int i, width=0;
if (dims_nb==0) width+=fprintf(file, "0");
else {
width+=fprintf(file, "%lu", dims[0].length);
for(i=1; i<dims_nb; i++) width+=fprintf(file, "x%lu", dims[i].length);
}
return width;
}
void blc_array::fscan_dims(FILE *file){
blc_dim *dim;
int length;
size=get_type_size();
FSCANF(1, file, "%d", &length);
if (dims) FREE(dims);
dims_nb=0;
if (length != 0){
do{
dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
dim->length=length;
dim->step=size;
size*=dim->length;
}while(fscanf(file, "x%d", &length)==1);
}
total_length=size/get_type_size();
blc_dim *dim;
int length;
size=get_type_size();
FSCANF(1, file, "%d", &length);
if (dims) FREE(dims);
dims_nb=0;
if (length != 0){
do{
dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
dim->length=length;
dim->step=size;
size*=dim->length;
}while(fscanf(file, "x%d", &length)==1);
}
total_length=size/get_type_size();
}
int blc_array::sscan_dims(char const *string){
......@@ -237,64 +235,64 @@ int blc_array::sscan_dims(char const *string){
//Should use code in common with fprint_info
void blc_array::sprint_properties(char *buffer, size_t buffer_size){
int width;
uint32_t str_type;
uint32_t str_format;
if (type==0) EXIT_ON_ERROR("The type should not be NULL. Use 'NDEF' by default.");
if (format==0) EXIT_ON_ERROR("The format should not be NULL. Use 'NDEF' by default.");
width=snprintf(buffer, buffer_size, "%.4s %.4s ", UINT32_TO_STRING(str_type, type), UINT32_TO_STRING(str_format, format));
width+=sprint_dims(buffer+width,buffer_size-width);
int width;
uint32_t str_type;
uint32_t str_format;
if (type==0) EXIT_ON_ERROR("The type should not be NULL. Use 'NDEF' by default.");
if (format==0) EXIT_ON_ERROR("The format should not be NULL. Use 'NDEF' by default.");
width=snprintf(buffer, buffer_size, "%.4s %.4s ", UINT32_TO_STRING(str_type, type), UINT32_TO_STRING(str_format, format));
width+=sprint_dims(buffer+width,buffer_size-width);
}
void blc_array::fprint_properties(FILE *file){
int width;
uint32_t net_type = htonl(type);
uint32_t net_format = htonl(format);
if (type==0) EXIT_ON_ERROR("The type should not be NULL. Use 'NDEF' by default.");
if (format==0) EXIT_ON_ERROR("The format should not be NULL. Use 'NDEF' by default.");
fprintf(file, "%.4s %.4s ", (char*)&net_type, (char*)&net_format);
width=fprint_dims(file);
int width;
uint32_t net_type = htonl(type);
uint32_t net_format = htonl(format);
if (type==0) EXIT_ON_ERROR("The type should not be NULL. Use 'NDEF' by default.");
if (format==0) EXIT_ON_ERROR("The format should not be NULL. Use 'NDEF' by default.");
fprintf(file, "%.4s %.4s ", (char*)&net_type, (char*)&net_format);
width=fprint_dims(file);
}
//Should use code in common with fscan_info
void blc_array::sscan_properties(char const *string){
int ret, pos;
dims_nb=0;
data=NULL;
size=0;
ret = sscanf(string, "%4c %4c%n", (char*)&type, (char*)&format, &pos);
if (ret == EOF) EXIT_ON_ERROR("End of FILE");
else if (ret!=2) EXIT_ON_ERROR("%d parameters have been read instead of 2 in '%s'.", ret, string);
string+=pos;
NTOHL(type);
NTOHL(format);
string+=sscan_dims(string);
int ret, pos;
dims_nb=0;
data=NULL;
size=0;
ret = sscanf(string, "%4c %4c%n", (char*)&type, (char*)&format, &pos);
if (ret == EOF) EXIT_ON_ERROR("End of FILE");
else if (ret!=2) EXIT_ON_ERROR("%d parameters have been read instead of 2 in '%s'.", ret, string);
string+=pos;
NTOHL(type);
NTOHL(format);
string+=sscan_dims(string);
}
void blc_array::fscan_properties(FILE *file){
int ret;
dims_nb=0;
data=NULL;
size=0;
ret = fscanf(file, "%4c %4c ", (char*)&type, (char*)&format);
if (ret == EOF) EXIT_ON_ERROR("End of FILE");
else if (ret!=2) EXIT_ON_ERROR("%d parameters have been read instead of 2", ret);
NTOHL(type);
NTOHL(format);
fscan_dims(file);
int ret;
dims_nb=0;
data=NULL;
size=0;
ret = fscanf(file, "%4c %4c ", (char*)&type, (char*)&format);
if (ret == EOF) EXIT_ON_ERROR("End of FILE");
else if (ret!=2) EXIT_ON_ERROR("%d parameters have been read instead of 2", ret);
NTOHL(type);
NTOHL(format);
fscan_dims(file);
}
//is it usefull ??
......@@ -302,7 +300,7 @@ void blc_array::fscan_properties(FILE *file){
size_t blc_array::get_minimum_size(){
blc_dim *dim;
size_t size = get_type_size();
FOR_EACH(dim, dims, dims_nb) {
if (dim->length==0) EXIT_ON_ERROR("Length must not be 0 on dim '%d'");
size*=dim->length;
......@@ -311,250 +309,275 @@ size_t blc_array::get_minimum_size(){
}*/
void blc_array::fprint_debug (FILE *file) const{
uint32_t type_str, format_str;
fprintf(file, "\nblc_array:\n type:%.4s, format:%.4s, dims_nb:%d, size:%ld \n", UINT32_TO_STRING(type_str, type), UINT32_TO_STRING(format_str, format), dims_nb, size);
fprint_dims(file);
fprintf(file, "\n");
if (data==NULL) fprintf(file, "data is null\n");
uint32_t type_str, format_str;
fprintf(file, "\nblc_array:\n type:%.4s, format:%.4s, dims_nb:%d, size:%ld \n", UINT32_TO_STRING(type_str, type), UINT32_TO_STRING(format_str, format), dims_nb, size);
fprint_dims(file);
fprintf(file, "\n");
if (data==NULL) fprintf(file, "data is null\n");
}
void blc_array::def_with_blc_file(char const *filename){
FILE *file;
const char *ext;
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file);
fscanf(file, "\n");
fclose(file);
FILE *file;
const char *ext;
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file);
fscanf(file, "\n");
fclose(file);
}
void blc_array::init_with_blc_file(char const *filename){
FILE *file;
ssize_t ret;
const char *ext;
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file);
fscanf(file, "\n");
allocate();
SYSTEM_ERROR_CHECK(ret=fread(data, 1, size, file), -1, "Reading the blc_array data for '%s'", filename);
if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Reading only '%ld' bytes for file '%s'", ret, filename);
fclose(file);
FILE *file;
ssize_t ret;
const char *ext;
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file);
fscanf(file, "\n");
allocate();
SYSTEM_ERROR_CHECK(ret=fread(data, 1, size, file), -1, "Reading the blc_array data for '%s'", filename);
if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Reading only '%ld' bytes for file '%s'", ret, filename);
fclose(file);
}
void blc_array::update_with_blc_file(char const *filename){
FILE *file;
ssize_t ret;
char const *ext;
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file);
fscanf(file, "\n");
SYSTEM_ERROR_CHECK(ret=fread(data, size, 1, file), -1, "Reading the blc_array data for '%s'", filename);
if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Reading only '%l' bytes for file '%s'", ret, filename);
fclose(file);
FILE *file;
ssize_t ret;
char const *ext;
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file);
fscanf(file, "\n");
SYSTEM_ERROR_CHECK(ret=fread(data, size, 1, file), -1, "Reading the blc_array data for '%s'", filename);
if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Reading only '%l' bytes for file '%s'", ret, filename);
fclose(file);
}
void blc_array::save_blc_file(char const *filename){
FILE *file;
ssize_t ret;
char const *ext;
ext=blc_get_filename_extension(filename);
//This test is mainly useful to avoid to accidently erase files.
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Opening filename '%s'.", filename);
fprint_properties(file);
fprintf(file, "\n");
SYSTEM_ERROR_CHECK(ret=fwrite(data, 1, size, file), -1, "Writing the blc_array data for '%s'", filename);
if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Writing only '%ld' bytes for file '%s'", ret, filename);
fclose(file);
FILE *file;
ssize_t ret;
char const *ext;
ext=blc_get_filename_extension(filename);
//This test is mainly useful to avoid to accidently erase files.
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Opening filename '%s'.", filename);
fprint_properties(file);
fprintf(file, "\n");
SYSTEM_ERROR_CHECK(ret=fwrite(data, 1, size, file), -1, "Writing the blc_array data for '%s'", filename);
if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Writing only '%ld' bytes for file '%s'", ret, filename);
fclose(file);
}
void blc_array::fprint_tsv(FILE *file){
int dim, length, i, j;
uint32_t type_str;
blc_dim *tmp_dims;
dim = 0;
j = 0;
tmp_dims = MANY_ALLOCATIONS(dims_nb, blc_dim);
memcpy(tmp_dims, dims, dims_nb * sizeof(blc_dim));
while (dim != dims_nb)
{
if (dim == 0)
{
length = tmp_dims[0].length;
switch (type)
{
case 'INT8': if (format=='TEXT')fprintf(file, "%-*s ", length, chars);
else FOR(i, length) fprintf(file, "%4d\nt", chars[i + j]);
break;
case 'UIN8':
FOR(i, length)
fprintf(file, "%3u\t", uchars[i + j]);
break;
case 'IN16':
FOR(i, length)
fprintf(file, "%6d\t", ints16[i + j]);
break;
case 'UI16':
FOR(i, length)
fprintf(file, "%5u\t", uints16[i + j]);
break;
case 'IN32':
FOR(i, length)
fprintf(file, "%d\t", uints32[i + j]);
break;
case 'UI32':
FOR(i, length)
fprintf(file, "%u\t", uints32[i + j]);
break;
case 'FL32':
FOR(i, length)
fprintf(file, "%f\t", floats[i + j]);
break;
case 'FL64':
FOR(i, length)