Commit 82810dce authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Add functions for saving blc_array in raw (blc) and tsv

parent 78b7ed8c
......@@ -40,13 +40,17 @@ typedef struct blc_array
:blc_mem {
blc_array();
void init(char const *properties);
/**def and does not allocate*/
void vdef_array(uint32_t type, uint32_t format, int dims_nb, int length0, 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 def_array(uint32_t type, uint32_t format, char const *dims_string);
/*the name should be changed*/
void init(char const *properties);
void destroy();
void add_dim(int length, int step);
......@@ -57,8 +61,8 @@ typedef struct blc_array
int fprint_dims(FILE *file) const;
void fprint_debug(FILE *file) const;
void fscan_dims(FILE *file);
void sscan_properties(char const *string);
void fscan_properties(FILE *file);
void sscan_properties(char const *string);
void fscan_properties(FILE *file);
void sprint_properties(char *buffer, size_t buffer_size);
void fprint_properties(FILE *file);
......@@ -72,6 +76,20 @@ typedef struct blc_array
int get_type_size();
size_t get_minimum_size();
void def_with_blc_file(char const *filename);
void init_with_blc_file(char const *filename);
void update_with_blc_file(char const *filename);
void save_blc_file(char const *filename);
void fprint_tsv(FILE *file);
void save_tsv_file(char const *filename);
#else
{
......
......@@ -23,6 +23,7 @@
#include "blc_text.h"
#include <signal.h>
#include <stdio.h> //fopen
blc_array::blc_array():dims(NULL), dims_nb(0){};
......@@ -30,25 +31,24 @@ void blc_array::init(char const *properties){
sscan_properties(properties);
}
void blc_array::vdef_array(uint32_t type, uint32_t format, int dims_nb, int length, va_list arguments){
blc_dim *dim;
this->dims_nb=dims_nb;
this->type=type;
this->format=format;
dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
size=get_type_size();
FOR_EACH(dim, dims, dims_nb)
{
dim->step=size;
size*=length;
dim->length=length;
length=va_arg(arguments, int);
}
this->dims_nb=dims_nb;
this->type=type;
this->format=format;
dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
size=get_type_size();
FOR_EACH(dim, dims, dims_nb)
{
dim->step=size;
size*=length;
dim->length=length;
length=va_arg(arguments, int);
}
}
void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, int length, ...){
......@@ -61,16 +61,16 @@ void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, int lengt
}
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;
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;
}
void blc_array::def_array(uint32_t type, uint32_t format, char const *dims_string){
......@@ -102,9 +102,9 @@ void blc_array::add_dim(int length){
void blc_array::set_dims(int dims_nb, int length, ...){
blc_dim *dim;
va_list args;
::free(dims);
this->dims_nb=dims_nb;
dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
......@@ -122,11 +122,11 @@ void blc_array::set_dims(int dims_nb, int length, ...){
int blc_array::get_type_size(){
switch (type){
case 'INT8': case 'UIN8': return 1;
case 'UI16':case 'IN16': return 2;
case 'FL32':case 'UI32':case 'IN32': return 4;
case 'FL64': case'UI64':case 'IN64': return 8;
case 'CHAR': case 'TEXT': EXIT_ON_ARRAY_ERROR(this, "The type is unknown. You probably mean 'INT8'");
case 'INT8': case 'UIN8': return 1;
case 'UI16':case 'IN16': return 2;
case 'FL32':case 'UI32':case 'IN32': return 4;
case 'FL64': case'UI64':case 'IN64': return 8;
case 'CHAR': case 'TEXT': EXIT_ON_ARRAY_ERROR(this, "The type is unknown. You probably mean 'INT8'");
default: EXIT_ON_ARRAY_ERROR(this, "The type is unknown.");
}
return -1;
......@@ -169,10 +169,10 @@ void blc_array::fscan_dims(FILE *file){
dims_nb=0;
if (length != 0){
do{
dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
dim->length=length;
dim->step=size;
size*=dim->length;
dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
dim->length=length;
dim->step=size;
size*=dim->length;
}while(fscanf(file, "x%d", &length)==1);
}
}
......@@ -221,7 +221,7 @@ void blc_array::fprint_properties(FILE *file){
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);
}
......@@ -262,7 +262,7 @@ void blc_array::fscan_properties(FILE *file){
fscan_dims(file);
}
//usefull ??
//is it usefull ??
size_t blc_array::get_minimum_size(){
blc_dim *dim;
size_t size = get_type_size();
......@@ -276,32 +276,193 @@ 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");
}
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);
}
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);
}
/* C wrapper */
extern "C" {
// Envoie un message d'erreur avec name_of_file, name_of_function, number_of_line et affiche le message formate avec les parametres variables. Puis exit le programme avec le parametre EXIT_FAILURE. To be used with EXIT_ON_ERROR.
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, ...){
va_list arguments;
va_start(arguments, message);
fprintf(stderr, "\n%s: %s \t %s \t %i :\nError: ", blc_program_name, name_of_file, name_of_function, numero_of_line);
color_vfprintf(BLC_BRIGHT_RED, stderr, message, arguments);
va_end(arguments);
array->fprint_debug(stderr);
fprintf(stderr, "\n\n");
fflush(stderr);
raise(SIGABRT);
exit(EXIT_FAILURE);
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);
}
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);
}
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)
fprintf(file, "%lf\t", doubles[i + j]);
break;
default:
EXIT_ON_ARRAY_ERROR(this, "You cannot display type: '%.4s'", UINT32_TO_STRING(type_str, type));
}
dim++;
}
else
{
//I increment what I have just done
j+=tmp_dims[dim-1].length;
tmp_dims[dim].length--;
if (tmp_dims[dim].length == 0)
{
tmp_dims[dim].length = dims[dim].length;
dim++;
}
else dim--;
if (dim==dims_nb-2 )fprintf(file, "\n");
else fprintf(file, "\t");
}
}
fprintf(file, "\n");
::free(tmp_dims);
}
void blc_array_destroy(blc_array *array){
array->destroy();
void blc_array::save_tsv_file(char const *filename){
FILE *file;
char const *ext;
ext=blc_get_filename_extension(filename);
//This test is mainly useful to avoid to accidently erase files. At least it is a .tsv
if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' has not tsv extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Opening filename '%s' for writing.", filename);
this->fprint_tsv(file);
fclose(file);
}
/* C wrapper */
extern "C" {
// Envoie un message d'erreur avec name_of_file, name_of_function, number_of_line et affiche le message formate avec les parametres variables. Puis exit le programme avec le parametre EXIT_FAILURE. To be used with EXIT_ON_ERROR.
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, ...){
va_list arguments;
va_start(arguments, message);
fprintf(stderr, "\n%s: %s \t %s \t %i :\nError: ", blc_program_name, name_of_file, name_of_function, numero_of_line);
color_vfprintf(BLC_BRIGHT_RED, stderr, message, arguments);
va_end(arguments);
array->fprint_debug(stderr);
fprintf(stderr, "\n\n");
fflush(stderr);
raise(SIGABRT);
exit(EXIT_FAILURE);
}
void blc_array_destroy(blc_array *array){
array->destroy();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment