Commit 1babd387 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Add function to load tsv files

parent fa5759d6
......@@ -86,6 +86,7 @@ typedef struct blc_array
void save_blc_file(char const *filename);
void fprint_tsv(FILE *file);
void update_with_tsv_file(char const *filename);
void save_tsv_file(char const *filename);
void fprint_surface_uchars(FILE *file, int ansi_terminal=0);
......
......@@ -187,15 +187,25 @@ 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.
///Read tab separated floats in the files and set values with the result.
void fscan_tsv_floats(FILE *file, float *values, int values_nb);
///Read tab separated uchars in the files and set values with the result.
void fscan_tsv_uchars(FILE *file, uchar *values, int values_nb);
///Print a color scale usefull for fprint_graph
void blc_fprint_color_scale(FILE *file);
///Set stdin (keyboard) in non blocking mode
void blc_set_stdin_non_blocking_mode();
///Set back stdin as it was before calling blc_set_stdin_non_blocking_mode
void blc_set_back_stdin_mode();
int fprint_fourcc(FILE *file, uint32_t value);
char* sprint_fourcc(char *string, uint32_t value);
END_EXTERN_C
///@}
......
......@@ -162,11 +162,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;
......@@ -209,7 +209,7 @@ void blc_array::fscan_dims(FILE *file){
dims_nb=0;
if (dims) FREE(dims);
if (length != 0){
do{
dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
......@@ -417,34 +417,34 @@ void blc_array::fprint_tsv(FILE *file){
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]);
case 'INT8': if (format=='TEXT')fprintf(file, "%-*s ", length, chars);
else FOR(i, length) fprintf(file, "%4d\nt", chars[i + j]);
break;
case 'UIN8':
case 'UIN8':
FOR(i, length)
fprintf(file, "%3u\t", uchars[i + j]);
break;
case 'IN16':
case 'IN16':
FOR(i, length)
fprintf(file, "%6d\t", ints16[i + j]);
break;
case 'UI16':
case 'UI16':
FOR(i, length)
fprintf(file, "%5u\t", uints16[i + j]);
break;
case 'IN32':
case 'IN32':
FOR(i, length)
fprintf(file, "%d\t", uints32[i + j]);
break;
case 'UI32':
case 'UI32':
FOR(i, length)
fprintf(file, "%u\t", uints32[i + j]);
break;
case 'FL32':
case 'FL32':
FOR(i, length)
fprintf(file, "%f\t", floats[i + j]);
break;
case 'FL64':
case 'FL64':
FOR(i, length)
fprintf(file, "%lf\t", doubles[i + j]);
break;
......@@ -473,6 +473,86 @@ void blc_array::fprint_tsv(FILE *file){
FREE(tmp_dims);
}
void blc_array::update_with_tsv_file(char const *filename){
FILE *file;
char const *ext;
int dim, length, j;
blc_dim *tmp_dims;
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' has not .tsv extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening filename '%s'.", filename);
dim = 0;
j = 0;
tmp_dims = MANY_ALLOCATIONS(dims_nb, blc_dim);
memcpy(tmp_dims, dims, dims_nb * sizeof(blc_dim));
///TODO find a simpler way.
switch (type){
case 'FL32':
while (dim != dims_nb)
{
if (dim == 0)
{
length = tmp_dims[0].length;
fscan_tsv_floats(file, &floats[j], length);
//I increment what I have just done
j+=length;
dim++;
}
else
{
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 )fscanf(file, "\n");
else fscanf(file, "\t");
}
}
break;
case 'UIN8':
while (dim != dims_nb)
{
if (dim == 0)
{
length = tmp_dims[0].length;
fscan_tsv_uchars(file, &uchars[j], length);
//I increment what I have just done
j+=length;
dim++;
}
else
{
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 )fscanf(file, "\n");
else fscanf(file, "\t");
}
}
break;
}
FREE(tmp_dims);
fclose(file);
}
void blc_array::save_tsv_file(char const *filename){
FILE *file;
char const *ext;
......@@ -541,7 +621,7 @@ static void fprint_3Dmatrix(FILE *file, blc_mem *mem, int offset, int step0, int
console_buffer.chars[console_size++]=ansi_texts[value*3];
console_buffer.chars[console_size++]=ansi_texts[value*3+1];
console_buffer.chars[console_size++]=ansi_texts[value*3+2];
// console_buffer.data[console_size++]=' ';
previous_color_id=color_id;
}
......@@ -555,7 +635,7 @@ static void fprint_3Dmatrix(FILE *file, blc_mem *mem, int offset, int step0, int
if (j != height-1) console_buffer.chars[console_size++] = '\n';
}
else fprintf(file, "\n"); //White line to separate the data
}
fwrite(console_buffer.data, console_size, 1, file);
fprint_stop_color(file);
......
......@@ -28,11 +28,15 @@
#include <errno.h> //errno and EINT
#include <libgen.h>
static void (*resize_callback)(int columns_nb, int rows_nb);
uchar blc_bar_colors[BLC_BAR_COLORS_NB] = { BLC_BLUE, BLC_BRIGHT_BLUE, BLC_CYAN, BLC_BRIGHT_CYAN, BLC_GREEN, BLC_BRIGHT_GREEN, BLC_BRIGHT_YELLOW, BLC_YELLOW, BLC_BRIGHT_MAGENTA, BLC_MAGENTA, BLC_BRIGHT_RED, BLC_RED};
int blc_stdout_ansi = 1;
int blc_stderr_ansi = 1;
static struct termios blc_initial_tty_mode;
void signal_callback(int signal_id)
{
......@@ -245,9 +249,7 @@ void fprint_human_size(FILE *file, size_t size)
}
}
void underline_fprintf(char c, FILE * file, char const *format, ...)
{
void underline_fprintf(char c, FILE * file, char const *format, ...){
int i, size;
va_list arguments;
......@@ -274,10 +276,35 @@ void fscan_tsv_floats(FILE *file, float *values, int values_nb)
if (fscanf(file, "\n")!=0) EXIT_ON_ERROR("fscanf");
}
void fscan_tsv_uchars(FILE *file, uchar *values, int values_nb)
{
int i;
FOR(i, values_nb)
FSCANF(1, file, "%hhu\t", &values[i]);
if (fscanf(file, "\n")!=0) EXIT_ON_ERROR("fscanf");
}
void blc_fprint_color_scale(FILE *file){
int i;
FOR(i, BLC_BAR_COLORS_NB-1) color_fprintf( blc_bar_colors[i], file, "%d < ", i*256/BLC_BAR_COLORS_NB);
color_printf(blc_bar_colors[i],"%d\n", i*256/BLC_BAR_COLORS_NB);
}
void blc_set_stdin_non_blocking_mode(){
struct termios ttystate;
//get the terminal state
tcgetattr(STDIN_FILENO, &ttystate);
blc_initial_tty_mode = ttystate;
//turn off canonical mode and echo
ttystate.c_lflag &= ~(ICANON | ECHO);
//minimum of number input read.
ttystate.c_cc[VMIN] = 1;
//set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
void blc_set_back_stdin_mode(){
tcsetattr(STDIN_FILENO, TCSANOW, &blc_initial_tty_mode);
}
END_EXTERN_C
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