-
Notifications
You must be signed in to change notification settings - Fork 52
/
storage.cc
163 lines (141 loc) · 3.82 KB
/
storage.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "storage.hh"
#include "fmt/format.h"
#include "fmt/printf.h"
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <iostream>
using namespace std;
std::string getPath(std::string_view root, time_t s, uint64_t sourceid, bool create)
{
auto comps = getPathComponents(root, s, sourceid);
std::string path;
for(unsigned int pos = 0; pos < comps.size() - 1 ; ++pos) {
path += comps[pos] +"/";
if(create)
mkdir(path.c_str(), 0770);
}
path += comps[comps.size()-1]+".gnss";
return path;
}
vector<string> getPathComponents(std::string_view root, time_t s, uint64_t sourceid)
{
// path: source/year/month/day/hour.pb
vector<string> ret;
ret.push_back((string)root);
ret.push_back(fmt::sprintf("%08x", sourceid));
struct tm tm;
gmtime_r(&s, &tm);
ret.push_back(to_string(tm.tm_year+1900));
ret.push_back(to_string(tm.tm_mon+1));
ret.push_back(to_string(tm.tm_mday));
ret.push_back(to_string(tm.tm_hour)+".pb");
return ret;
}
bool getNMM(int fd, NavMonMessage& nmm, uint32_t& offset)
{
char bert[4];
if(read(fd, bert, 4) != 4 || bert[0]!='b' || bert[1]!='e' || bert[2] !='r' || bert[3]!='t') {
return false;
}
uint16_t len;
if(read(fd, &len, 2) != 2)
return false;
len = htons(len);
char buffer[len];
if(read(fd, buffer, len) != len)
return false;
nmm.ParseFromString(string(buffer, len));
offset += 4 + 2 + len;
return true;
}
bool getNMM(FILE* fp, NavMonMessage& nmm, uint32_t& offset)
{
char bert[4];
if(fread(bert, 1, 4, fp) != 4 || bert[0]!='b' || bert[1]!='e' || bert[2] !='r' || bert[3]!='t') {
return false;
}
uint16_t len;
if(fread(&len, 1, 2, fp) != 2)
return false;
len = htons(len);
char buffer[len];
if(fread(buffer, 1, len, fp) != len)
return false;
nmm.ParseFromString(string(buffer, len));
offset += 4 + 2 + len;
return true;
}
bool getRawNMM(int fd, timespec& t, string& raw, uint32_t& offset)
{
char bert[4];
int res;
if((res=read(fd, bert, 4)) != 4 || bert[0]!='b' || bert[1]!='e' || bert[2] !='r' || bert[3]!='t') {
if(res != 4)
return false;
for(int s=0;; ++s ) {
cerr<<"Skipping character hunting for good magic.. "<<s<<endl;
bert[0] = bert[1];
bert[1] = bert[2];
bert[2] = bert[3];
res = read(fd, bert + 3, 1);
if(res != 1)
return false;
if(bert[0]=='b' && bert[1]=='e' && bert[2] =='r' && bert[3]=='t')
break;
}
}
uint16_t len;
if(read(fd, &len, 2) != 2)
return false;
len = htons(len);
char buffer[len];
if(read(fd, buffer, len) != len)
return false;
NavMonMessage nmm;
raw.assign(buffer, len);
nmm.ParseFromString(raw);
t.tv_sec = nmm.localutcseconds();
t.tv_nsec = nmm.localutcnanoseconds();
offset += 4 + 2 + len;
return true;
}
bool getRawNMM(FILE* fp, timespec& t, string& raw, uint32_t& offset)
{
char bert[4];
int res;
if((res=fread(bert, 1, 4, fp)) != 4 || bert[0]!='b' || bert[1]!='e' || bert[2] !='r' || bert[3]!='t') {
if(res != 4) {
// cerr<<"EOF"<<endl;
return false;
}
for(int s=0;; ++s ) {
cerr<<"Skipping character hunting for good magic.. "<<s<<endl;
bert[0] = bert[1];
bert[1] = bert[2];
bert[2] = bert[3];
res = fread(bert+3, 1,1, fp);
if(res != 1)
return false;
if(bert[0]=='b' && bert[1]=='e' && bert[2] =='r' && bert[3]=='t') {
cerr<<"Resync!"<<endl;
break;
}
}
}
uint16_t len;
if(fread(&len, 1, 2, fp) != 2)
return false;
len = htons(len);
char buffer[len];
if(fread(buffer, 1, len, fp) != len)
return false;
NavMonMessage nmm;
raw.assign(buffer, len);
nmm.ParseFromString(raw);
t.tv_sec = nmm.localutcseconds();
t.tv_nsec = nmm.localutcnanoseconds();
offset += 4 + 2 + len;
return true;
}