eventlog.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <arpa/inet.h>
37 #include "eventlog.h"
38 
39 #define MAGIC ((int32_t) 0xEDA1DA01L)
40 
41 static inline int fread32(FILE *f, int32_t *v32)
42 {
43  int32_t v;
44  if (fread(&v, 4, 1, f) != 1)
45  return -1;
46  *v32 = ntohl(v);
47  return 0;
48 }
49 
50 static inline int fread64(FILE *f, int64_t *v64)
51 {
52  int32_t v1, v2;
53  if (fread32(f, &v1))
54  return -1;
55  if (fread32(f, &v2))
56  return -1;
57  *v64 = (((int64_t) v1)<<32) | (((int64_t) v2)&0xffffffff);
58  return 0;
59 }
60 
61 lcm_eventlog_t *lcm_eventlog_create(const char *path, const char *mode)
62 {
63  if(strcmp(mode, "r")) return NULL;
64  lcm_eventlog_t *l = (lcm_eventlog_t*) calloc(1, sizeof(lcm_eventlog_t));
65  l->f = fopen(path, mode);
66  if (l->f == NULL) {
67  free (l);
68  return NULL;
69  }
70  l->eventcount = 0;
71  return l;
72 }
73 
75 {
76  fclose(l->f);
77  free(l);
78 }
79 
81 {
83  (lcm_eventlog_event_t*) calloc(1, sizeof(lcm_eventlog_event_t));
84 
85  int32_t magic = 0;
86 
87  do {
88  int r = fgetc(l->f);
89  if (r < 0) goto eof;
90  magic = (magic << 8) | r;
91  } while( magic != MAGIC );
92 
93  fread64(l->f, &le->eventnum);
94  fread64(l->f, &le->timestamp);
95  fread32(l->f, &le->channellen);
96  fread32(l->f, &le->datalen);
97 
98  if (l->eventcount != le->eventnum) {
99  l->eventcount = le->eventnum;
100  }
101 
102  le->channel = (char*)calloc(1, le->channellen+1);
103  if (fread(le->channel, 1, le->channellen, l->f) != (size_t) le->channellen)
104  goto eof;
105 
106  le->data = calloc(1, le->datalen+1);
107  if (fread(le->data, 1, le->datalen, l->f) != (size_t) le->datalen)
108  goto eof;
109 
110  l->eventcount++;
111 
112  return le;
113 
114 eof:
115  return NULL;
116 }
117 
119 {
120  if (le->data) free(le->data);
121  if (le->channel) free(le->channel);
122  memset(le,0,sizeof(lcm_eventlog_event_t));
123  free(le);
124 }
125 
126 static int64_t get_event_time(lcm_eventlog_t *l)
127 {
128  int32_t magic = 0;
129  int r;
130 
131  do {
132  r = fgetc(l->f);
133  if (r < 0) goto eof;
134  magic = (magic << 8) | r;
135  } while( magic != MAGIC );
136 
137  int64_t event_num;
138  int64_t timestamp;
139  if (0 != fread64(l->f, &event_num)) return -1;
140  if (0 != fread64(l->f, &timestamp)) return -1;
141  fseeko (l->f, -20, SEEK_CUR);
142 
143  l->eventcount = event_num;
144 
145  return timestamp;
146 
147 eof:
148  return -1;
149 }
150 
152 {
153  fseeko (l->f, 0, SEEK_END);
154  off_t file_len = ftello(l->f);
155 
156  int64_t cur_time;
157  double frac1 = 0; // left bracket
158  double frac2 = 1; // right bracket
159  double prev_frac = -1;
160  double frac; // current position
161 
162  while (1) {
163  frac = 0.5*(frac1+frac2);
164  off_t offset = (off_t)(frac*file_len);
165  fseeko (l->f, offset, SEEK_SET);
166  cur_time = get_event_time (l);
167  if (cur_time < 0)
168  return -1;
169 
170  frac = (double)ftello (l->f)/file_len;
171  if ((frac > frac2) || (frac < frac1) || (frac1>=frac2))
172  break;
173 
174  double df = frac-prev_frac;
175  if (df < 0)
176  df = -df;
177  if (df < 1e-12)
178  break;
179 
180  if (cur_time == timestamp)
181  break;
182 
183  if (cur_time < timestamp)
184  frac1 = frac;
185  else
186  frac2 = frac;
187 
188  prev_frac = frac;
189  }
190 
191  return 0;
192 }
lcm_eventlog_event_t * lcm_eventlog_read_next_event(lcm_eventlog_t *l)
Definition: eventlog.cpp:80
int lcm_eventlog_seek_to_timestamp(lcm_eventlog_t *l, int64_t timestamp)
Definition: eventlog.cpp:151
void lcm_eventlog_destroy(lcm_eventlog_t *l)
Definition: eventlog.cpp:74
header file for eventlog.cpp Check the LCM instructions for more info
static int fread64(FILE *f, int64_t *v64)
Definition: eventlog.cpp:50
void lcm_eventlog_free_event(lcm_eventlog_event_t *le)
Definition: eventlog.cpp:118
lcm_eventlog_t * lcm_eventlog_create(const char *path, const char *mode)
Definition: eventlog.cpp:61
int64_t eventcount
Definition: eventlog.h:47
static int64_t get_event_time(lcm_eventlog_t *l)
Definition: eventlog.cpp:126
#define MAGIC
Definition: eventlog.cpp:39
static int fread32(FILE *f, int32_t *v32)
Definition: eventlog.cpp:41


mit_darpa_logs_player
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:32:15