Commit 58ab0be4829781229807bf19654208f7de44c8ec

Authored by Arnaud Blanchard
1 parent ca4c9a0b

Add optional time column. There is still a problem with i_fread

... ... @@ -18,7 +18,7 @@ static int file_get_lines_number(FILE* file){
return lines_nb;
}
static void array_def_with_tsv_file_first_line(blc_array *array, char const *filename){
static void array_def_with_tsv_file_first_line(blc_array *array, char const *filename, char const *time_str){
char const *ext;
FILE *file;
size_t linecap=0;
... ... @@ -37,51 +37,25 @@ static void array_def_with_tsv_file_first_line(blc_array *array, char const *fil
if (pos) length++;
}
array->def_array('FL32', 'NDEF', 1, length);
fclose(file);
}
//We suppose they are float and only one dim
static void array_def_with_tsv_file(blc_array *array, char const *filename){
char const *ext;
FILE *file;
size_t linecap=0;
char *line=NULL;
int length, lines_nb;
char const *pos;
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' does not .tsv extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename,"r"), NULL, "opening '%s'", filename);
SYSTEM_ERROR_CHECK(getline(&line, &linecap, file), -1, "Reading '%s'", filename);
if (time_str) length--; //The first coulmn dos not count it is the time.
pos=line;
length=0;
while(pos){
pos=strchr(pos+1, '\t');
if (pos)length++;
}
lines_nb=file_get_lines_number(file)+1; //The first line was alreay read
array->def_array('FL32', 'NDEF', 2, length, lines_nb);
array->def_array('FL32', 'NDEF', 1, length);
fclose(file);
}
int main(int argc, char** argv){
char const *period_str, *text;
char const *channel_name, *filename, *time_str;
char const *number_str;
char *default_output;
int number;
int number, ret;
long executing_time, previous_executing_time;
size_t linecap=0;
ssize_t line_size;
blc_channel channel;
FILE *file;
int period;
asprintf(&default_output, "/%s%d", basename(argv[0]), getpid()); //This will not be free but it is only allocate once
blc_program_set_description("Update channel with a tsv file");
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){
} else period=0;
number=strtod(number_str, NULL);
array_def_with_tsv_file_first_line(&channel, filename);
channel.create_or_open(channel_name, BLC_CHANNEL_WRITE);
array_def_with_tsv_file_first_line(&channel, filename, time_str);
channel.create_or_open(channel_name, BLC_CHANNEL_READ);
blc_loop_try_add_posting_semaphore(channel.sem_ack_data);
SYSTEM_ERROR_CHECK(file=fopen(filename, "r"), NULL, "Opening '%s'", filename);
if (time_str) SYSTEM_SUCCESS_CHECK(fscanf(file, "%ld\t", &previous_executing_time), 1, "Reading time in file '%s'", file->_bf);
if (time_str) SYSTEM_SUCCESS_CHECK(fscanf(file, "%ld\t", &previous_executing_time), 1, "Reading time in file '%s'", filename);
channel.publish();
BLC_COMMAND_LOOP(period){
if (number==blc_loop_iteration) blc_command_ask_quit();
else{
... ... @@ -114,8 +90,12 @@ int main(int argc, char** argv){
else{
fscan_tsv_floats(file, channel.floats, channel.dims[0].length);
if (time_str) {
SYSTEM_SUCCESS_CHECK(fscanf(file, "%ld\t", &executing_time), 1, "Reading time in file '%s'", file->_bf);
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
ret=fscanf(file, "%lld\t", &executing_time);
if (ret==0 && feof(file)){
if (number==-1) blc_command_ask_quit();
else EXIT_ON_ERROR("End of file and you request '%d' iterations. Only '%d' has been done.", number, blc_loop_iteration);
}
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
previous_executing_time=executing_time;
}
}
... ...
... ... @@ -29,8 +29,8 @@ int main(int argc, char** argv){
if (filename==NULL) EXIT_ON_ERROR("You need to specify a file -f<name>.tsv");
ext=blc_get_filename_extension(filename);
if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' does not .tsv extension but '%s'", filename, ext); //This is to avoid accidental overwriting
period=strtod(period_str, NULL)*1000;
number=strtod(number_str, NULL);
period=strtol(period_str, NULL,10)*1000;
number=strtol(number_str, NULL,10);
channel.open(channel_name, BLC_CHANNEL_READ);
blc_loop_try_add_waiting_semaphore(channel.sem_new_data);
... ... @@ -39,7 +39,7 @@ int main(int argc, char** argv){
SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Creating '%s'", filename);
BLC_COMMAND_LOOP(period){
if (time_str) fprintf(file, "%ld\t", blc_loop_timer.tv_sec*1000000+blc_loop_timer.tv_usec);
if (time_str) fprintf(file, "%lld\t", blc_loop_timer.tv_sec*1000000+blc_loop_timer.tv_usec);
channel.fprint_tsv(file);
}
SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", filename);
... ...