Commit f42193dce8122169308fe60de36a1edd4ea9135c

Authored by Arnaud Blanchard
1 parent 74604ffc

Fix read tab value. Still only 2d array can be read

Showing 1 changed file with 19 additions and 14 deletions
  1 +//Read only array of dim 1
  2 +
1 #include "blc_core.h" 3 #include "blc_core.h"
2 #include "blc_channel.h" 4 #include "blc_channel.h"
3 #include "blc_program.h" 5 #include "blc_program.h"
@@ -18,6 +20,7 @@ static int file_get_lines_number(FILE* file){ @@ -18,6 +20,7 @@ static int file_get_lines_number(FILE* file){
18 return lines_nb; 20 return lines_nb;
19 } 21 }
20 22
  23 +
21 static void array_def_with_tsv_file_first_line(blc_array *array, char const *filename, char const *time_str){ 24 static void array_def_with_tsv_file_first_line(blc_array *array, char const *filename, char const *time_str){
22 char const *ext; 25 char const *ext;
23 FILE *file; 26 FILE *file;
@@ -29,12 +32,16 @@ static void array_def_with_tsv_file_first_line(blc_array *array, char const *fil @@ -29,12 +32,16 @@ static void array_def_with_tsv_file_first_line(blc_array *array, char const *fil
29 if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' does not .tsv extension but '%s'", filename, ext); 32 if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' does not .tsv extension but '%s'", filename, ext);
30 SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "opening '%s'", filename); 33 SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "opening '%s'", filename);
31 SYSTEM_ERROR_CHECK(getline(&line, &linecap, file), -1, "Reading '%s'", filename); 34 SYSTEM_ERROR_CHECK(getline(&line, &linecap, file), -1, "Reading '%s'", filename);
32 -  
33 pos=line; 35 pos=line;
34 length=0; 36 length=0;
35 while(pos){ 37 while(pos){
36 pos=strchr(pos+1, '\t'); 38 pos=strchr(pos+1, '\t');
37 - if (pos) length++; 39 + if (pos){
  40 + pos++;//next position;
  41 + if (pos[0]=='\t') EXIT_ON_ERROR("The array in file '%s' seems of dims 3 or more. Two tabs.", line);
  42 + if (pos[0]=='\n') EXIT_ON_ERROR("The array in file '%s' seems ill formed. 'tab' followed by return.", line);
  43 + }
  44 + length++;
38 } 45 }
39 46
40 if (time_str) length--; //The first coulmn dos not count it is the time. 47 if (time_str) length--; //The first coulmn dos not count it is the time.
@@ -44,20 +51,18 @@ static void array_def_with_tsv_file_first_line(blc_array *array, char const *fil @@ -44,20 +51,18 @@ static void array_def_with_tsv_file_first_line(blc_array *array, char const *fil
44 } 51 }
45 52
46 int main(int argc, char** argv){ 53 int main(int argc, char** argv){
47 - char const *period_str, *text; 54 + char const *period_str;
48 char const *channel_name, *filename, *time_str; 55 char const *channel_name, *filename, *time_str;
49 char const *number_str; 56 char const *number_str;
50 char *default_output; 57 char *default_output;
51 int number, ret; 58 int number, ret;
52 uint64_t executing_time, previous_executing_time; 59 uint64_t executing_time, previous_executing_time;
53 - size_t linecap=0;  
54 - ssize_t line_size;  
55 blc_channel channel; 60 blc_channel channel;
56 FILE *file; 61 FILE *file;
57 int period; 62 int period;
58 63
59 asprintf(&default_output, "/%s%d", basename(argv[0]), getpid()); //This will not be free but it is only allocate once 64 asprintf(&default_output, "/%s%d", basename(argv[0]), getpid()); //This will not be free but it is only allocate once
60 - blc_program_set_description("Update channel with a tsv file"); 65 + blc_program_set_description("Update channel with a tsv file. Only array of dim 1 !");
61 blc_program_add_option(&number_str, 'n', "number", "integer", "Number of records (-1 for infinity)", "-1"); 66 blc_program_add_option(&number_str, 'n', "number", "integer", "Number of records (-1 for infinity)", "-1");
62 blc_program_add_option(&channel_name, 'o', "output_channel", "string", "Name of the channel to output the data", default_output); 67 blc_program_add_option(&channel_name, 'o', "output_channel", "string", "Name of the channel to output the data", default_output);
63 blc_program_add_option(&period_str, 'p', "period", "integer", "Period in ms to read line by line (default 0 i.e. as fast as possible)", NULL); 68 blc_program_add_option(&period_str, 'p', "period", "integer", "Period in ms to read line by line (default 0 i.e. as fast as possible)", NULL);
@@ -65,12 +70,12 @@ int main(int argc, char** argv){ @@ -65,12 +70,12 @@ int main(int argc, char** argv){
65 blc_program_add_parameter(&filename, "filename", 1, "File to load", NULL); 70 blc_program_add_parameter(&filename, "filename", 1, "File to load", NULL);
66 blc_program_init(&argc, &argv, blc_quit); 71 blc_program_init(&argc, &argv, blc_quit);
67 blc_command_forward_blc_channels(); 72 blc_command_forward_blc_channels();
68 - 73 +
69 if (period_str){ 74 if (period_str){
70 if (time_str) EXIT_ON_ERROR("You can either use fix period or first column time but not both: period '%s' and time (-t) activated", period_str); 75 if (time_str) EXIT_ON_ERROR("You can either use fix period or first column time but not both: period '%s' and time (-t) activated", period_str);
71 period=strtod(period_str, NULL)*1000; 76 period=strtod(period_str, NULL)*1000;
72 } else period=0; 77 } else period=0;
73 - number=strtod(number_str, NULL); 78 + number=strtol(number_str, NULL, 10);
74 79
75 array_def_with_tsv_file_first_line(&channel, filename, time_str); 80 array_def_with_tsv_file_first_line(&channel, filename, time_str);
76 channel.create_or_open(channel_name, BLC_CHANNEL_WRITE); 81 channel.create_or_open(channel_name, BLC_CHANNEL_WRITE);
@@ -90,12 +95,12 @@ int main(int argc, char** argv){ @@ -90,12 +95,12 @@ int main(int argc, char** argv){
90 else{ 95 else{
91 fscan_tsv_floats(file, channel.floats, channel.dims[0].length); 96 fscan_tsv_floats(file, channel.floats, channel.dims[0].length);
92 if (time_str) { 97 if (time_str) {
93 - ret=fscanf(file, "%llu\t", &executing_time);  
94 - if (ret==0 && feof(file)){  
95 - if (number==-1) blc_command_ask_quit();  
96 - else EXIT_ON_ERROR("End of file and you request '%d' iterations. Only '%d' has been done.", number, blc_loop_iteration);  
97 - }  
98 - blc_command_loop_period=executing_time-previous_executing_time; //We request the BLC_COMMAND_LOOP to last the time during two records. This overwrite period effect 98 + ret=fscanf(file, "%llu\t", &executing_time);
  99 + if (ret==0 && feof(file)){
  100 + if (number==-1) blc_command_ask_quit();
  101 + else EXIT_ON_ERROR("End of file and you request '%d' iterations. Only '%d' has been done.", number, blc_loop_iteration);
  102 + }
  103 + blc_command_loop_period=executing_time-previous_executing_time; //We request the BLC_COMMAND_LOOP to last the time during two records. This overwrite period effect
99 previous_executing_time=executing_time; 104 previous_executing_time=executing_time;
100 } 105 }
101 } 106 }