basic_io.cc
Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *---------------------           WMPSNIFFER          --------------------
00003  *------------------------------------------------------------------------
00004  *                                                         V7.0B  11/05/10
00005  *
00006  *
00007  *  File: basic_io.cc
00008  *  Authors: Danilo Tardioli
00009  *  ----------------------------------------------------------------------
00010  *  Copyright (C) 2000-2012, Universidad de Zaragoza, SPAIN
00011  *
00012  *  Contact Addresses: Danilo Tardioli                   dantard@unizar.es
00013  *
00014  *  RT-WMP is free software; you can  redistribute it and/or  modify it
00015  *  under the terms of the GNU General Public License  as published by the
00016  *  Free Software Foundation;  either  version 2, or (at  your option) any
00017  *  later version.
00018  *
00019  *  RT-WMP  is distributed  in the  hope  that  it will be   useful, but
00020  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
00021  *  MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
00022  *  General Public License for more details.
00023  *
00024  *  You should have received  a  copy of  the  GNU General Public  License
00025  *  distributed with RT-WMP;  see file COPYING.   If not,  write to the
00026  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
00027  *  02111-1307, USA.
00028  *
00029  *  As a  special exception, if you  link this  unit  with other  files to
00030  *  produce an   executable,   this unit  does  not  by  itself cause  the
00031  *  resulting executable to be covered by the  GNU General Public License.
00032  *  This exception does  not however invalidate  any other reasons why the
00033  *  executable file might be covered by the GNU Public License.
00034  *
00035  *----------------------------------------------------------------------*/
00036 #include <unistd.h>
00037 #include <cstdlib>
00038 #include <cstring>
00039 #include <cmath>
00040 #include <cstdio>
00041 #include <sys/io.h>
00042 #include <fcntl.h>
00043 #include <sys/stat.h>
00044 #include "wmp_config.h"
00045 
00046 #include "core/include/frames.h"
00047 #include "basic_io.h"
00048 #include <vector>
00049 #include <map>
00050 #include <iostream>
00051 #include <errno.h>
00052 
00053 struct  file_hdr_t{
00054         unsigned short num_of_nodes;
00055 };
00056 
00057 static file_hdr_t file_hdr;
00058 
00059 static int  num_nodes;
00060 
00061 
00062 
00063 file_t file,tmp_file;
00064 
00065 void io_init() {
00066         memset(&file,0,sizeof(file));
00067         num_nodes = 0;
00068 }
00069 void io_flush(){
00070         fflush(file.ptr);
00071 }
00072 
00073 std::vector<int> f_offset;
00074 std::map<int,int> serialToOffset;
00075 
00076 int io_change_file(char * filename){
00077         io_close_sim_data();
00078         strcpy(file.name, filename);
00079         return 0;
00080 }
00081 int io_change_file(){
00082         io_close_sim_data();
00083         return 0;
00084 }
00085 
00086 int io_open_sim_data(char * filename) {
00087 
00088         strcpy(file.name, filename);
00089 
00090         file.ptr = fopen(file.name, "r");
00091         if (!file.ptr) {
00092                 file.open = false;
00093                 return 0;
00094         }else{
00095                 file.open = true;
00096         }
00097 
00098         int n = fread(&file_hdr, 1,sizeof (file_hdr), file.ptr);
00099         if (n != sizeof(file_hdr)){
00100                 return 0;
00101         }
00102 
00103         num_nodes = file_hdr.num_of_nodes;
00104 
00105     char buf[sizeof(simData_Hdr) + sizeof(Token_Hdr)];
00106     simData_Hdr  * smd = (simData_Hdr *) buf;
00107     Token_Hdr * th = (Token_Hdr *) (buf + sizeof(simData_Hdr));
00108 
00109     file.pos = ftell(file.ptr);
00110     f_offset.clear();
00111 
00112     int idx = ftell(file.ptr);
00113     int unit = sizeof (simData_Hdr) + sizeof(Token_Hdr);
00114 
00115     std::cerr << "Scanning File " << file.name << std::endl;
00116     while (1) {
00117         n = fread(buf, 1, unit, file.ptr);
00118         if (n == unit) {
00119                         f_offset.push_back(idx);
00120                         serialToOffset[th->serial] = f_offset.size();
00121                         idx+=smd->simDataLen;
00122                         fseek(file.ptr, idx, SEEK_SET);
00123 
00124                 }else{
00125                         break;
00126                 }
00127     }
00128     std::cerr << "Done (" << f_offset.size() << " frames detected)" <<std::endl;
00129     return num_nodes;
00130 }
00131 
00132 int io_close_sim_data() {
00133         if (file.open){
00134                 fflush(file.ptr);
00135                 if (fclose(file.ptr)!=0){
00136                         fprintf(stderr,"Problems to close file...");
00137                         return 0;
00138                 }
00139         }
00140         file.open = false;
00141         return 1;
00142 }
00143 
00144 int io_get_pose_from_serial(int serial){
00145         return serialToOffset[serial];
00146 }
00147 
00148 int io_get_num_of_nodes() {
00149     return num_nodes;
00150 }
00151 
00152 int io_get_file_size() {
00153     struct stat stbuf;
00154     stat(file.name, &stbuf);
00155     return stbuf.st_size;
00156 }
00157 
00158 int io_go_to(int n) {
00159     //fseek(file.ptr, unit_size*n, SEEK_SET);
00160         //std::cerr << "Size: " << f_offset.size() << std::endl;
00161         //std::cerr << "request to go to " << n << "=" << f_offset.at(n) << std::endl;
00162         if (!file.open){
00163                 return -1;
00164         }
00165         if (f_offset.size() > n){
00166                 fseek(file.ptr, f_offset.at(n), SEEK_SET);
00167                 file.pos = n;
00168 
00169         } else{
00170                 file.pos = f_offset.size();
00171         }
00172         return file.pos;
00173         //std::cerr << "Done\n";
00174 }
00175 
00176 int io_read_next_sim_data(char * p) {
00177     simData_Hdr  * sdHdr = (simData_Hdr *) p;
00178     wmpFrame * r = (wmpFrame *) (p + sizeof (simData_Hdr));
00179 
00180     file.pos++;
00181     int nbytes = fread(sdHdr, 1,sizeof(simData_Hdr), file.ptr);
00182 
00183     if (nbytes == sizeof(simData_Hdr)) {
00184                         nbytes += fread(p + sizeof(simData_Hdr), 1, sdHdr->simDataLen
00185                                 - sizeof(simData_Hdr), file.ptr);
00186         } else {
00187                 return 0;
00188         }
00189 
00190     if (!valid_frame(r, nbytes,num_nodes)) {
00191         return 0;
00192     }
00193     return nbytes;
00194 }
00195 
00196 int io_read_sim_data(char * p, int pos) {
00197     file.pos = pos;
00198     fseek(file.ptr, f_offset.at(pos), SEEK_SET);
00199 
00200     simData_Hdr  * sdHdr = (simData_Hdr *) p;
00201     int nbytes = fread(sdHdr, 1, sizeof(simData_Hdr), file.ptr);
00202     nbytes += fread(p + sizeof(simData_Hdr), 1, sdHdr->simDataLen - sizeof(simData_Hdr), file.ptr);
00203 
00204     return nbytes;
00205 }
00206 
00207 int io_get_sim_data_num_of_elements() {
00208     return (int) f_offset.size();
00209 }
00210 
00211 int io_reopen_file_to_write(int _num_nodes) {
00212         file_hdr.num_of_nodes = _num_nodes;
00213         if (file.open){
00214                 fclose(file.ptr);
00215         }
00216         file.ptr = fopen(file.name, "w");
00217         if (!file.ptr) {
00218                 file.open = false;
00219                 return 0;
00220         }
00221         file.open = true;
00222         fwrite(&file_hdr, 1, sizeof(file_hdr), file.ptr);
00223         return 0;
00224 }
00225 
00226 int io_get_filepos(){
00227         return file.pos;
00228 }
00229 int io_write_sim_frame(char * fdata, int data_size){
00230         int size = fwrite(&fdata[0], 1, data_size, file.ptr);
00231         //fprintf(stderr,"data Written (%d bytes) \n", size);
00232         return size;
00233 }
00234 
00235 int io_write_on_tmp_file(char * fdata, int data_size){
00236 
00237         int size = fwrite(fdata, 1, data_size, tmp_file.ptr);
00238         fprintf(stderr,"data Written (%d bytes) \n", size);
00239         return 1;
00240 }
00241 
00242 int io_create_tmp_file() {
00243         tmp_file.ptr = fopen("tmp.vis", "w+");
00244         if (!tmp_file.ptr) {
00245                 fprintf(stderr,"Unable to open temp file!\n");
00246                 exit(0);
00247                 tmp_file.open = false;
00248                 return 0;
00249         }
00250         int size = fwrite(&file_hdr, 1, sizeof(file_hdr), tmp_file.ptr);
00251         fprintf(stderr,"Header Writter (%d bytes) \n", size);
00252         return 0;
00253 }
00254 int io_close_tmp_file(){
00255         return fclose(tmp_file.ptr  );
00256 }
00257 


ros_rt_wmp_sniffer
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Mon Oct 6 2014 08:27:57