Commit 58ab0be4829781229807bf19654208f7de44c8ec
1 parent
ca4c9a0b
Add optional time column. There is still a problem with i_fread
Showing
2 changed files
with
18 additions
and
38 deletions
@@ -18,7 +18,7 @@ static int file_get_lines_number(FILE* file){ | @@ -18,7 +18,7 @@ static int file_get_lines_number(FILE* file){ | ||
18 | return lines_nb; | 18 | return lines_nb; |
19 | } | 19 | } |
20 | 20 | ||
21 | -static void array_def_with_tsv_file_first_line(blc_array *array, char const *filename){ | 21 | +static void array_def_with_tsv_file_first_line(blc_array *array, char const *filename, char const *time_str){ |
22 | char const *ext; | 22 | char const *ext; |
23 | FILE *file; | 23 | FILE *file; |
24 | size_t linecap=0; | 24 | size_t linecap=0; |
@@ -37,51 +37,25 @@ static void array_def_with_tsv_file_first_line(blc_array *array, char const *fil | @@ -37,51 +37,25 @@ static void array_def_with_tsv_file_first_line(blc_array *array, char const *fil | ||
37 | if (pos) length++; | 37 | if (pos) length++; |
38 | } | 38 | } |
39 | 39 | ||
40 | - array->def_array('FL32', 'NDEF', 1, length); | ||
41 | - fclose(file); | ||
42 | -} | ||
43 | - | ||
44 | -//We suppose they are float and only one dim | ||
45 | -static void array_def_with_tsv_file(blc_array *array, char const *filename){ | ||
46 | - char const *ext; | ||
47 | - FILE *file; | ||
48 | - size_t linecap=0; | ||
49 | - char *line=NULL; | ||
50 | - int length, lines_nb; | ||
51 | - char const *pos; | ||
52 | - ext=blc_get_filename_extension(filename); | ||
53 | - if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' does not .tsv extension but '%s'", filename, ext); | ||
54 | - SYSTEM_ERROR_CHECK(file=fopen(filename,"r"), NULL, "opening '%s'", filename); | ||
55 | - SYSTEM_ERROR_CHECK(getline(&line, &linecap, file), -1, "Reading '%s'", filename); | 40 | + if (time_str) length--; //The first coulmn dos not count it is the time. |
56 | 41 | ||
57 | - pos=line; | ||
58 | - length=0; | ||
59 | - while(pos){ | ||
60 | - pos=strchr(pos+1, '\t'); | ||
61 | - if (pos)length++; | ||
62 | - } | ||
63 | - lines_nb=file_get_lines_number(file)+1; //The first line was alreay read | ||
64 | - | ||
65 | - array->def_array('FL32', 'NDEF', 2, length, lines_nb); | 42 | + array->def_array('FL32', 'NDEF', 1, length); |
66 | fclose(file); | 43 | fclose(file); |
67 | } | 44 | } |
68 | 45 | ||
69 | - | ||
70 | int main(int argc, char** argv){ | 46 | int main(int argc, char** argv){ |
71 | char const *period_str, *text; | 47 | char const *period_str, *text; |
72 | char const *channel_name, *filename, *time_str; | 48 | char const *channel_name, *filename, *time_str; |
73 | char const *number_str; | 49 | char const *number_str; |
74 | char *default_output; | 50 | char *default_output; |
75 | - int number; | 51 | + int number, ret; |
76 | long executing_time, previous_executing_time; | 52 | long executing_time, previous_executing_time; |
77 | size_t linecap=0; | 53 | size_t linecap=0; |
78 | ssize_t line_size; | 54 | ssize_t line_size; |
79 | blc_channel channel; | 55 | blc_channel channel; |
80 | FILE *file; | 56 | FILE *file; |
81 | - | ||
82 | int period; | 57 | int period; |
83 | 58 | ||
84 | - | ||
85 | asprintf(&default_output, "/%s%d", basename(argv[0]), getpid()); //This will not be free but it is only allocate once | 59 | asprintf(&default_output, "/%s%d", basename(argv[0]), getpid()); //This will not be free but it is only allocate once |
86 | blc_program_set_description("Update channel with a tsv file"); | 60 | blc_program_set_description("Update channel with a tsv file"); |
87 | blc_program_add_option(&number_str, 'n', "number", "integer", "Number of records (-1 for infinity)", "-1"); | 61 | blc_program_add_option(&number_str, 'n', "number", "integer", "Number of records (-1 for infinity)", "-1"); |
@@ -98,12 +72,14 @@ int main(int argc, char** argv){ | @@ -98,12 +72,14 @@ int main(int argc, char** argv){ | ||
98 | } else period=0; | 72 | } else period=0; |
99 | number=strtod(number_str, NULL); | 73 | number=strtod(number_str, NULL); |
100 | 74 | ||
101 | - array_def_with_tsv_file_first_line(&channel, filename); | ||
102 | - channel.create_or_open(channel_name, BLC_CHANNEL_WRITE); | 75 | + array_def_with_tsv_file_first_line(&channel, filename, time_str); |
76 | + channel.create_or_open(channel_name, BLC_CHANNEL_READ); | ||
103 | blc_loop_try_add_posting_semaphore(channel.sem_ack_data); | 77 | blc_loop_try_add_posting_semaphore(channel.sem_ack_data); |
104 | 78 | ||
105 | SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening '%s'", filename); | 79 | SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening '%s'", filename); |
106 | - if (time_str) SYSTEM_SUCCESS_CHECK(fscanf(file, "%ld\t", &previous_executing_time), 1, "Reading time in file '%s'", file->_bf); | 80 | + if (time_str) SYSTEM_SUCCESS_CHECK(fscanf(file, "%ld\t", &previous_executing_time), 1, "Reading time in file '%s'", filename); |
81 | + | ||
82 | + channel.publish(); | ||
107 | BLC_COMMAND_LOOP(period){ | 83 | BLC_COMMAND_LOOP(period){ |
108 | if (number==blc_loop_iteration) blc_command_ask_quit(); | 84 | if (number==blc_loop_iteration) blc_command_ask_quit(); |
109 | else{ | 85 | else{ |
@@ -114,8 +90,12 @@ int main(int argc, char** argv){ | @@ -114,8 +90,12 @@ int main(int argc, char** argv){ | ||
114 | else{ | 90 | else{ |
115 | fscan_tsv_floats(file, channel.floats, channel.dims[0].length); | 91 | fscan_tsv_floats(file, channel.floats, channel.dims[0].length); |
116 | if (time_str) { | 92 | if (time_str) { |
117 | - SYSTEM_SUCCESS_CHECK(fscanf(file, "%ld\t", &executing_time), 1, "Reading time in file '%s'", file->_bf); | ||
118 | - 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 | 93 | + ret=fscanf(file, "%lld\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 | ||
119 | previous_executing_time=executing_time; | 99 | previous_executing_time=executing_time; |
120 | } | 100 | } |
121 | } | 101 | } |
@@ -29,8 +29,8 @@ int main(int argc, char** argv){ | @@ -29,8 +29,8 @@ int main(int argc, char** argv){ | ||
29 | if (filename==NULL) EXIT_ON_ERROR("You need to specify a file -f<name>.tsv"); | 29 | if (filename==NULL) EXIT_ON_ERROR("You need to specify a file -f<name>.tsv"); |
30 | ext=blc_get_filename_extension(filename); | 30 | ext=blc_get_filename_extension(filename); |
31 | if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' does not .tsv extension but '%s'", filename, ext); //This is to avoid accidental overwriting | 31 | if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' does not .tsv extension but '%s'", filename, ext); //This is to avoid accidental overwriting |
32 | - period=strtod(period_str, NULL)*1000; | ||
33 | - number=strtod(number_str, NULL); | 32 | + period=strtol(period_str, NULL,10)*1000; |
33 | + number=strtol(number_str, NULL,10); | ||
34 | 34 | ||
35 | channel.open(channel_name, BLC_CHANNEL_READ); | 35 | channel.open(channel_name, BLC_CHANNEL_READ); |
36 | blc_loop_try_add_waiting_semaphore(channel.sem_new_data); | 36 | blc_loop_try_add_waiting_semaphore(channel.sem_new_data); |
@@ -39,7 +39,7 @@ int main(int argc, char** argv){ | @@ -39,7 +39,7 @@ int main(int argc, char** argv){ | ||
39 | SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Creating '%s'", filename); | 39 | SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Creating '%s'", filename); |
40 | 40 | ||
41 | BLC_COMMAND_LOOP(period){ | 41 | BLC_COMMAND_LOOP(period){ |
42 | - if (time_str) fprintf(file, "%ld\t", blc_loop_timer.tv_sec*1000000+blc_loop_timer.tv_usec); | 42 | + if (time_str) fprintf(file, "%lld\t", blc_loop_timer.tv_sec*1000000+blc_loop_timer.tv_usec); |
43 | channel.fprint_tsv(file); | 43 | channel.fprint_tsv(file); |
44 | } | 44 | } |
45 | SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", filename); | 45 | SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", filename); |
-
Please register or login to post a comment