00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00160
00161
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
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
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