Commit 162773ede964a6632e60ae24b767c1ab0dc82b76

Authored by Arnaud Blanchard
1 parent 97aac14f

Add function to read and write files. i_read becomes obsolete

# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS
# Author: Arnaud Blanchard (November 2016)
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
# users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
cmake_minimum_required(VERSION 2.6)
#The name of the project is the basename of the directory
get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(${PROJECT_NAME})
find_package(blc_channel REQUIRED)
find_package(blc_program REQUIRED)
add_definitions(${BL_DEFINTIIONS})
include_directories(${BL_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} i_fread.cpp)
target_link_libraries(${PROJECT_NAME} ${BL_LIBRARIES})
... ...
Load a tsv file and put it in a blc_channel.
It between can be done line by line --with=period value in ms separating the time two lines.
... ...
# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2016)
# Author: Arnaud Blanchard (November 2016)
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
# users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
cd `dirname $0`
echo
echo "Checking `basename $PWD`"
echo "===================="
echo
echo "No check for now"
echo
... ...
#include "blc_core.h"
#include "blc_channel.h"
#include "blc_program.h"
#include <unistd.h>
#include <termios.h>
#include <libgen.h> //basename
static int file_get_lines_number(FILE* file){
int ch, lines_nb;
lines_nb=0;
while(feof(file)==0){
ch = fgetc(file);
if (ch == '\n') lines_nb++;
else if ( ch == EOF ) if (feof(file)==0) EXIT_ON_SYSTEM_ERROR("Reading file");
}
return lines_nb;
}
static void array_def_with_tsv_file_first_line(blc_array *array, char const *filename){
char const *ext;
FILE *file;
size_t linecap=0;
char *line=NULL;
int length;
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);
pos=line;
length=0;
while(pos){
pos=strchr(pos+1, '\t');
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);
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);
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;
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");
blc_program_add_option(&channel_name, 'o', "output_channel", "string", "Name of the channel to output the data", default_output);
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);
blc_program_add_option(&time_str, 't', "time", NULL, "use time in first column (in µs).", NULL);
blc_program_add_parameter(&filename, "filename", 1, "File to load", NULL);
blc_program_init(&argc, &argv, blc_quit);
blc_command_forward_blc_channels();
if (period_str){
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);
period=strtod(period_str, NULL)*1000;
} 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);
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);
BLC_COMMAND_LOOP(period){
if (number==blc_loop_iteration) blc_command_ask_quit();
else{
if (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);
}
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
previous_executing_time=executing_time;
}
}
}
}
SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", filename);
return EXIT_SUCCESS;
}
... ...
# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS
# Author: Arnaud Blanchard (November 2016)
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
# users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
cmake_minimum_required(VERSION 2.6)
#The name of the project is the basename of the directory
get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(${PROJECT_NAME})
find_package(blc_channel REQUIRED)
find_package(blc_program REQUIRED)
add_definitions(${BL_DEFINTIIONS})
include_directories(${BL_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} o_fwrite.cpp)
target_link_libraries(${PROJECT_NAME} ${BL_LIBRARIES})
... ...
Load a tsv file and put it in a blc_channel.
It between can be done line by line --with=period value in ms separating the time two lines.
... ...
# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2016)
# Author: Arnaud Blanchard (November 2016)
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
# users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
cd `dirname $0`
echo
echo "Checking `basename $PWD`"
echo "===================="
echo
echo "No check for now"
echo
... ...
#include "blc_core.h"
#include "blc_channel.h"
#include "blc_program.h"
#include <unistd.h>
#include <termios.h>
#include <libgen.h> //basename
int main(int argc, char** argv){
blc_channel channel;
char const *period_str;
char const *channel_name, *filename, *time_str;
char const *number_str;
char const *ext;
FILE *file;
int number;
int period;
struct timeval timer;
blc_program_set_description("Write data of blc_channel");
blc_program_add_option(&filename, 'f', "filename", "filename", "Filename of the file to record", NULL);
blc_program_add_option(&number_str, 'n', "number", "integer", "Number of records (-1 for infinity)", "-1");
blc_program_add_option(&period_str, 'p', "period", "integer", "Period in ms to read line by line", "0");
blc_program_add_option(&time_str, 't', "time", NULL, "record time in first column (ansolute in µs)", NULL);
blc_program_add_parameter(&channel_name, "blc_channel-in", 1, "channel to save", NULL);
blc_program_init(&argc, &argv, blc_quit);
blc_command_forward_blc_channels();
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);
channel.open(channel_name, BLC_CHANNEL_READ);
blc_loop_try_add_waiting_semaphore(channel.sem_new_data);
blc_loop_try_add_posting_semaphore(channel.sem_ack_data);
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);
channel.fprint_tsv(file);
}
SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", filename);
return EXIT_SUCCESS;
}
... ...