Commit a145383a authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Remove last tab in a line while writting TSV

parent 638bc75e
...@@ -53,169 +53,169 @@ END_EXTERN_C ...@@ -53,169 +53,169 @@ END_EXTERN_C
blc_array::blc_array():type('NDEF'), format('NDEF'), dims(NULL), dims_nb(0), total_length(0){} 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, ...){ blc_array::blc_array(uint32_t type, uint32_t format, int dims_nb, int length0, ...){
va_list arguments; va_list arguments;
va_start(arguments, length0); va_start(arguments, length0);
vdef_array( type, format, dims_nb, length0, arguments); vdef_array( type, format, dims_nb, length0, arguments);
allocate(); allocate();
va_end(arguments); va_end(arguments);
} }
blc_array::~blc_array(){ 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){ void blc_array::vdef_array(uint32_t type, uint32_t format, int dims_nb, int length, va_list arguments){
total_length=1; total_length=1;
this->dims_nb=dims_nb; this->dims_nb=dims_nb;
this->type=type; this->type=type;
this->format=format; this->format=format;
dims=vcreate_blc_dims(&size, type, dims_nb, length, arguments); dims=vcreate_blc_dims(&size, type, dims_nb, length, arguments);
total_length=size/get_type_size(); total_length=size/get_type_size();
} }
void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, int length, ...){ void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, int length, ...){
va_list arguments; va_list arguments;
va_start(arguments, length); va_start(arguments, length);
vdef_array(type, format, dims_nb, length, arguments); vdef_array(type, format, dims_nb, length, arguments);
va_end(arguments); va_end(arguments);
} }
void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim *dims){ void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim *dims){
this->type=type; this->type=type;
this->format=format; this->format=format;
this->dims_nb=dims_nb; this->dims_nb=dims_nb;
size=get_type_size(); size=get_type_size();
this->dims=MANY_ALLOCATIONS(dims_nb, blc_dim); this->dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
memcpy(this->dims, dims, sizeof(blc_dim)*dims_nb); memcpy(this->dims, dims, sizeof(blc_dim)*dims_nb);
size =dims[dims_nb-1].length*dims[dims_nb-1].step; size =dims[dims_nb-1].length*dims[dims_nb-1].step;
total_length=size/get_type_size(); total_length=size/get_type_size();
} }
void blc_array::def_array(uint32_t type, uint32_t format, char const *dims_string){ void blc_array::def_array(uint32_t type, uint32_t format, char const *dims_string){
this->type=type; this->type=type;
this->format=format; this->format=format;
sscan_dims(dims_string); sscan_dims(dims_string);
} }
void blc_array::init(char const *properties){ void blc_array::init(char const *properties){
sscan_properties(properties); sscan_properties(properties);
allocate(); allocate();
} }
void blc_array::vinit(uint32_t type, uint32_t format, int dims_nb, int length, va_list arguments){ 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); vdef_array(type, format, dims_nb, length, arguments);
allocate(); allocate();
} }
void blc_array::init(uint32_t type, uint32_t format, int dims_nb, int length, ...){ void blc_array::init(uint32_t type, uint32_t format, int dims_nb, int length, ...){
va_list arguments; va_list arguments;
va_start(arguments, length); va_start(arguments, length);
vinit(type, format, dims_nb, length, arguments); vinit(type, format, dims_nb, length, arguments);
va_end(arguments); va_end(arguments);
} }
void blc_array::add_dim(int length, int step){ void blc_array::add_dim(int length, int step){
blc_dim dim; blc_dim dim;
dim.length=length; dim.length=length;
dim.step=step; dim.step=step;
APPEND_ITEM(&dims, &dims_nb, &dim); APPEND_ITEM(&dims, &dims_nb, &dim);
size=step*length; //Check if it always true ?? size=step*length; //Check if it always true ??
total_length=size/get_type_size(); total_length=size/get_type_size();
} }
void blc_array::add_dim(int length){ void blc_array::add_dim(int length){
if (dims_nb==0) add_dim(length, get_type_size()); if (dims_nb==0) add_dim(length, get_type_size());
else add_dim(length, dims[dims_nb-1].length*dims[dims_nb-1].step); else add_dim(length, dims[dims_nb-1].length*dims[dims_nb-1].step);
} }
void blc_array::set_dims(int dims_nb, int length, ...){ void blc_array::set_dims(int dims_nb, int length, ...){
blc_dim *dim; blc_dim *dim;
va_list args; va_list args;
if (dims) FREE(dims); if (dims) FREE(dims);
this->dims_nb=dims_nb; this->dims_nb=dims_nb;
dims=MANY_ALLOCATIONS(dims_nb, blc_dim); dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
size=get_type_size(); size=get_type_size();
va_start(args, length); va_start(args, length);
FOR_EACH(dim, dims, dims_nb) FOR_EACH(dim, dims, dims_nb)
{ {
size*=length; size*=length;
dim->step=1; dim->step=1;
dim->length=length; dim->length=length;
length=va_arg(args, int); length=va_arg(args, int);
} }
va_end(args); va_end(args);
total_length=size/get_type_size(); total_length=size/get_type_size();
} }
int blc_array::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 blc_array::sprint_dims(char *string, int string_size){
int i, width=0; int i, width=0;
if (dims_nb==0) width=snprintf(string, string_size, "0"); if (dims_nb==0) width=snprintf(string, string_size, "0");
else { else {
width=snprintf(string, string_size, "%lu", dims[0].length); 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); 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); if (width >= string_size) EXIT_ON_ERROR("The reserved size '%d' is too small to store the '%d' dims.",size, dims_nb);
return width; return width;
} }
int blc_array::fprint_dims(FILE *file)const { int blc_array::fprint_dims(FILE *file)const {
int i, width=0; int i, width;
if (dims_nb==0) width+=fprintf(file, "0"); if (dims_nb==0) width=fprintf(file, "0");
else { else {
width+=fprintf(file, "%lu", dims[0].length); width=fprintf(file, "%lu", dims[0].length);
for(i=1; i<dims_nb; i++) width+=fprintf(file, "x%lu", dims[i].length); for(i=1; i<dims_nb; i++) width+=fprintf(file, "x%lu", dims[i].length);
} }
return width; return width;
} }
void blc_array::fscan_dims(FILE *file){ void blc_array::fscan_dims(FILE *file){
blc_dim *dim; blc_dim *dim;
int length; size_t length;
size=get_type_size(); size=get_type_size();
FSCANF(1, file, "%d", &length); FSCANF(1, file, "%lu", &length);
if (dims) FREE(dims); if (dims) FREE(dims);
dims_nb=0; dims_nb=0;
if (length != 0){ if (length != 0){
do{ do{
dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim); dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
dim->length=length; dim->length=length;
dim->step=size; dim->step=size;
size*=dim->length; size*=dim->length;
}while(fscanf(file, "x%d", &length)==1); }while(fscanf(file, "x%lu", &length)==1);
} }
total_length=size/get_type_size(); total_length=size/get_type_size();
} }
int blc_array::sscan_dims(char const *string){ int blc_array::sscan_dims(char const *string){
blc_dim *dim; blc_dim *dim;
int pos, total_pos; int pos, total_pos;
int length; size_t length;
size=get_type_size(); size=get_type_size();
SSCANF(1, string, "%d%n", &length, &pos); SSCANF(1, string, "%lu%n", &length, &pos);
total_pos=0; total_pos=0;
dims_nb=0; dims_nb=0;
if (dims) FREE(dims); if (dims) FREE(dims);
...@@ -235,349 +235,363 @@ int blc_array::sscan_dims(char const *string){ ...@@ -235,349 +235,363 @@ int blc_array::sscan_dims(char const *string){
//Should use code in common with fprint_info //Should use code in common with fprint_info
void blc_array::sprint_properties(char *buffer, size_t buffer_size){ void blc_array::sprint_properties(char *buffer, size_t buffer_size){
int width; int width;
uint32_t str_type; uint32_t str_type;
uint32_t str_format; uint32_t str_format;
if (type==0) EXIT_ON_ERROR("The type should not be NULL. Use 'NDEF' by default."); 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."); 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=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); width+=sprint_dims(buffer+width,buffer_size-width);
} }
void blc_array::fprint_properties(FILE *file){ void blc_array::fprint_properties(FILE *file){
int width; int width;
uint32_t net_type = htonl(type); uint32_t net_type = htonl(type);
uint32_t net_format = htonl(format); uint32_t net_format = htonl(format);
if (type==0) EXIT_ON_ERROR("The type should not be NULL. Use 'NDEF' by default."); 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."); 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); fprintf(file, "%.4s %.4s ", (char*)&net_type, (char*)&net_format);
width=fprint_dims(file); width=fprint_dims(file);
} }
//Should use code in common with fscan_info //Should use code in common with fscan_info
void blc_array::sscan_properties(char const *string){ void blc_array::sscan_properties(char const *string){
int ret, pos; int ret, pos;
dims_nb=0; dims_nb=0;
data=NULL; data=NULL;
size=0; size=0;
ret = sscanf(string, "%4c %4c%n", (char*)&type, (char*)&format, &pos); ret = sscanf(string, "%4c %4c%n", (char*)&type, (char*)&format, &pos);
if (ret == EOF) EXIT_ON_ERROR("End of FILE"); 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); else if (ret!=2) EXIT_ON_ERROR("%d parameters have been read instead of 2 in '%s'.", ret, string);
string+=pos; string+=pos;
NTOHL(type); NTOHL(type);
NTOHL(format); NTOHL(format);
string+=sscan_dims(string); string+=sscan_dims(string);
} }
void blc_array::fscan_properties(FILE *file){ void blc_array::fscan_properties(FILE *file){
int ret; int ret;
dims_nb=0; dims_nb=0;
data=NULL; data=NULL;
size=0; size=0;
ret = fscanf(file, "%4c %4c ", (char*)&type, (char*)&format); ret = fscanf(file, "%4c %4c ", (char*)&type, (char*)&format);
if (ret == EOF) EXIT_ON_ERROR("End of FILE"); 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); else if (ret!=2) EXIT_ON_ERROR("%d parameters have been read instead of 2", ret);
NTOHL(type); NTOHL(type);
NTOHL(format); NTOHL(format);
fscan_dims(file); fscan_dims(file);
} }
//is it usefull ?? //is it usefull ??
/* /*
size_t blc_array::get_minimum_size(){ size_t blc_array::get_minimum_size(){
blc_dim *dim; blc_dim *dim;
size_t size = get_type_size(); size_t size = get_type_size();
FOR_EACH(dim, dims, dims_nb) { FOR_EACH(dim, dims, dims_nb) {
if (dim->length==0) EXIT_ON_ERROR("Length must not be 0 on dim '%d'"); if (dim->length==0) EXIT_ON_ERROR("Length must not be 0 on dim '%d'");
size*=dim->length; size*=dim->length;
} }
return size; return size;
}*/ }*/
void blc_array::fprint_debug (FILE *file) const{ void blc_array::fprint_debug (FILE *file) const{
uint32_t type_str, format_str; 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); 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); fprint_dims(file);
fprintf(file, "\n"); fprintf(file, "\n");
if (data==NULL) fprintf(file, "data is null\n"); if (data==NULL) fprintf(file, "data is null\n");
} }
void blc_array::def_with_blc_file(char const *filename){ void blc_array::def_with_blc_file(char const *filename){
FILE *file; FILE *file;
const char *ext; const char *ext;
ext=blc_get_filename_extension(filename); ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext); 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); SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file); fscan_properties(file);
fscanf(file, "\n"); fscanf(file, "\n");
fclose(file); fclose(file);
} }
void blc_array::init_with_blc_file(char const *filename){ void blc_array::init_with_blc_file(char const *filename){
FILE *file; FILE *file;
ssize_t ret; ssize_t ret;
const char *ext; const char *ext;
ext=blc_get_filename_extension(filename); ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext); 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); SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file); fscan_properties(file);
fscanf(file, "\n"); fscanf(file, "\n");
allocate(); allocate();
SYSTEM_ERROR_CHECK(ret=fread(data, 1, size, file), -1, "Reading the blc_array data for '%s'", filename); 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); if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Reading only '%ld' bytes for file '%s'", ret, filename);
fclose(file); fclose(file);
} }
void blc_array::update_with_blc_file(char const *filename){ void blc_array::update_with_blc_file(char const *filename){
FILE *file; FILE *file;
ssize_t ret; ssize_t ret;
char const *ext; char const *ext;
ext=blc_get_filename_extension(filename); ext=blc_get_filename_extension(filename);
if (strcmp(ext, "blc")!=0) EXIT_ON_ERROR("'%s' does not .blc extension but '%s'", filename, ext); 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); SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
fscan_properties(file); fscan_properties(file);
fscanf(file, "\n"); fscanf(file, "\n");
SYSTEM_ERROR_CHECK(ret=fread(data, size, 1, file), -1, "Reading the blc_array data for '%s'", filename); 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); if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Reading only '%l' bytes for file '%s'", ret, filename);
fclose(file); fclose(file);
} }
void blc_array::save_blc_file(char const *filename){ void blc_array::save_blc_file(char const *filename){
FILE *file; FILE *file;
ssize_t ret; ssize_t ret;
char const *ext; char const *ext;
ext=blc_get_filename_extension(filename); ext=blc_get_filename_extension(filename);
//This test is mainly useful to avoid to accidently erase files. //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); 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); SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Opening filename '%s'.", filename);
fprint_properties(file); fprint_properties(file);
fprintf(file, "\n"); fprintf(file, "\n");
SYSTEM_ERROR_CHECK(ret=fwrite(data, 1, size, file), -1, "Writing the blc_array data for '%s'", filename); 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); if (ret!=size) EXIT_ON_ARRAY_ERROR(this, "Writing only '%ld' bytes for file '%s'", ret, filename);
fclose(file); fclose(file);
} }
void blc_array::fprint_tsv(FILE *file){ void blc_array::fprint_tsv(FILE *file){
int dim, length, i, j; int dim, length, i, j;
uint32_t type_str; uint32_t type_str;
blc_dim *tmp_dims; blc_dim *tmp_dims;
dim = 0; dim = 0;
j = 0; j = 0;