Commit 162773ede964a6632e60ae24b767c1ab0dc82b76

Authored by Arnaud Blanchard
1 parent 97aac14f

Add function to read and write files. i_read becomes obsolete

  1 +# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS
  2 +# Author: Arnaud Blanchard (November 2016)
  3 +# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
  4 +# 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".
  5 +# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
  6 +# 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.
  7 +# 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,
  8 +# 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.
  9 +# 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.
  10 +# 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.
  11 +
  12 +cmake_minimum_required(VERSION 2.6)
  13 +
  14 +#The name of the project is the basename of the directory
  15 +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  16 +project(${PROJECT_NAME})
  17 +
  18 +find_package(blc_channel REQUIRED)
  19 +find_package(blc_program REQUIRED)
  20 +
  21 +add_definitions(${BL_DEFINTIIONS})
  22 +include_directories(${BL_INCLUDE_DIRS})
  23 +add_executable(${PROJECT_NAME} i_fread.cpp)
  24 +target_link_libraries(${PROJECT_NAME} ${BL_LIBRARIES})
  25 +
  26 +
  27 +
  28 +
  1 +
  2 +
  3 +Load a tsv file and put it in a blc_channel.
  4 +
  5 +It between can be done line by line --with=period value in ms separating the time two lines.
  1 +# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2016)
  2 +# Author: Arnaud Blanchard (November 2016)
  3 +# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
  4 +# 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".
  5 +# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
  6 +# 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.
  7 +# 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,
  8 +# 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.
  9 +# 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.
  10 +# 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.
  11 +
  12 +
  13 +cd `dirname $0`
  14 +echo
  15 +echo "Checking `basename $PWD`"
  16 +echo "===================="
  17 +echo
  18 +echo "No check for now"
  19 +echo
  1 +#include "blc_core.h"
  2 +#include "blc_channel.h"
  3 +#include "blc_program.h"
  4 +#include <unistd.h>
  5 +#include <termios.h>
  6 +#include <libgen.h> //basename
  7 +
  8 +static int file_get_lines_number(FILE* file){
  9 + int ch, lines_nb;
  10 +
  11 + lines_nb=0;
  12 + while(feof(file)==0){
  13 + ch = fgetc(file);
  14 +
  15 + if (ch == '\n') lines_nb++;
  16 + else if ( ch == EOF ) if (feof(file)==0) EXIT_ON_SYSTEM_ERROR("Reading file");
  17 + }
  18 + return lines_nb;
  19 +}
  20 +
  21 +static void array_def_with_tsv_file_first_line(blc_array *array, char const *filename){
  22 + char const *ext;
  23 + FILE *file;
  24 + size_t linecap=0;
  25 + char *line=NULL;
  26 + int length;
  27 + char const *pos;
  28 + ext=blc_get_filename_extension(filename);
  29 + 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);
  31 + SYSTEM_ERROR_CHECK(getline(&line, &linecap, file), -1, "Reading '%s'", filename);
  32 +
  33 + pos=line;
  34 + length=0;
  35 + while(pos){
  36 + pos=strchr(pos+1, '\t');
  37 + if (pos) length++;
  38 + }
  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);
  56 +
  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);
  66 + fclose(file);
  67 +}
  68 +
  69 +
  70 +int main(int argc, char** argv){
  71 + char const *period_str, *text;
  72 + char const *channel_name, *filename, *time_str;
  73 + char const *number_str;
  74 + char *default_output;
  75 + int number;
  76 + long executing_time, previous_executing_time;
  77 + size_t linecap=0;
  78 + ssize_t line_size;
  79 + blc_channel channel;
  80 + FILE *file;
  81 +
  82 + int period;
  83 +
  84 +
  85 + 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");
  87 + blc_program_add_option(&number_str, 'n', "number", "integer", "Number of records (-1 for infinity)", "-1");
  88 + blc_program_add_option(&channel_name, 'o', "output_channel", "string", "Name of the channel to output the data", default_output);
  89 + 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);
  90 + blc_program_add_option(&time_str, 't', "time", NULL, "use time in first column (in µs).", NULL);
  91 + blc_program_add_parameter(&filename, "filename", 1, "File to load", NULL);
  92 + blc_program_init(&argc, &argv, blc_quit);
  93 + blc_command_forward_blc_channels();
  94 +
  95 + if (period_str){
  96 + 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);
  97 + period=strtod(period_str, NULL)*1000;
  98 + } else period=0;
  99 + number=strtod(number_str, NULL);
  100 +
  101 + array_def_with_tsv_file_first_line(&channel, filename);
  102 + channel.create_or_open(channel_name, BLC_CHANNEL_WRITE);
  103 + blc_loop_try_add_posting_semaphore(channel.sem_ack_data);
  104 +
  105 + 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);
  107 + BLC_COMMAND_LOOP(period){
  108 + if (number==blc_loop_iteration) blc_command_ask_quit();
  109 + else{
  110 + if (feof(file)){
  111 + if (number==-1) blc_command_ask_quit();
  112 + else EXIT_ON_ERROR("End of file and you request '%d' iterations. Only '%d' has been done.", number, blc_loop_iteration);
  113 + }
  114 + else{
  115 + fscan_tsv_floats(file, channel.floats, channel.dims[0].length);
  116 + 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
  119 + previous_executing_time=executing_time;
  120 + }
  121 + }
  122 + }
  123 + }
  124 + SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", filename);
  125 +
  126 + return EXIT_SUCCESS;
  127 +}
  1 +# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS
  2 +# Author: Arnaud Blanchard (November 2016)
  3 +# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
  4 +# 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".
  5 +# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
  6 +# 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.
  7 +# 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,
  8 +# 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.
  9 +# 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.
  10 +# 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.
  11 +
  12 +cmake_minimum_required(VERSION 2.6)
  13 +
  14 +#The name of the project is the basename of the directory
  15 +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  16 +project(${PROJECT_NAME})
  17 +
  18 +find_package(blc_channel REQUIRED)
  19 +find_package(blc_program REQUIRED)
  20 +
  21 +add_definitions(${BL_DEFINTIIONS})
  22 +include_directories(${BL_INCLUDE_DIRS})
  23 +add_executable(${PROJECT_NAME} o_fwrite.cpp)
  24 +target_link_libraries(${PROJECT_NAME} ${BL_LIBRARIES})
  25 +
  26 +
  27 +
  28 +
  1 +
  2 +
  3 +Load a tsv file and put it in a blc_channel.
  4 +
  5 +It between can be done line by line --with=period value in ms separating the time two lines.
  1 +# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2016)
  2 +# Author: Arnaud Blanchard (November 2016)
  3 +# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
  4 +# 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".
  5 +# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
  6 +# 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.
  7 +# 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,
  8 +# 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.
  9 +# 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.
  10 +# 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.
  11 +
  12 +
  13 +cd `dirname $0`
  14 +echo
  15 +echo "Checking `basename $PWD`"
  16 +echo "===================="
  17 +echo
  18 +echo "No check for now"
  19 +echo
  1 +#include "blc_core.h"
  2 +#include "blc_channel.h"
  3 +#include "blc_program.h"
  4 +#include <unistd.h>
  5 +#include <termios.h>
  6 +#include <libgen.h> //basename
  7 +
  8 +int main(int argc, char** argv){
  9 + blc_channel channel;
  10 + char const *period_str;
  11 + char const *channel_name, *filename, *time_str;
  12 + char const *number_str;
  13 + char const *ext;
  14 + FILE *file;
  15 + int number;
  16 + int period;
  17 + struct timeval timer;
  18 +
  19 +
  20 + blc_program_set_description("Write data of blc_channel");
  21 + blc_program_add_option(&filename, 'f', "filename", "filename", "Filename of the file to record", NULL);
  22 + blc_program_add_option(&number_str, 'n', "number", "integer", "Number of records (-1 for infinity)", "-1");
  23 + blc_program_add_option(&period_str, 'p', "period", "integer", "Period in ms to read line by line", "0");
  24 + blc_program_add_option(&time_str, 't', "time", NULL, "record time in first column (ansolute in µs)", NULL);
  25 + blc_program_add_parameter(&channel_name, "blc_channel-in", 1, "channel to save", NULL);
  26 + blc_program_init(&argc, &argv, blc_quit);
  27 + blc_command_forward_blc_channels();
  28 +
  29 + if (filename==NULL) EXIT_ON_ERROR("You need to specify a file -f<name>.tsv");
  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
  32 + period=strtod(period_str, NULL)*1000;
  33 + number=strtod(number_str, NULL);
  34 +
  35 + channel.open(channel_name, BLC_CHANNEL_READ);
  36 + blc_loop_try_add_waiting_semaphore(channel.sem_new_data);
  37 + blc_loop_try_add_posting_semaphore(channel.sem_ack_data);
  38 +
  39 + SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Creating '%s'", filename);
  40 +
  41 + BLC_COMMAND_LOOP(period){
  42 + if (time_str) fprintf(file, "%ld\t", blc_loop_timer.tv_sec*1000000+blc_loop_timer.tv_usec);
  43 + channel.fprint_tsv(file);
  44 + }
  45 + SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", filename);
  46 +
  47 + return EXIT_SUCCESS;
  48 +}