-
Notifications
You must be signed in to change notification settings - Fork 26
/
changeid.cc
248 lines (170 loc) · 3.81 KB
/
changeid.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "A_Event.h"
#include "packet.h"
#include "Cframe.h"
#include "framePackets.h"
#include "dataBlock.h"
#include "frameRoutines.h"
#include "frameHdr.h"
#include "fileEventiterator.h"
#include "testEventiterator.h"
#include "phenixTypes.h"
#include "ophBuffer.h"
#include "oEvent.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#define DDEVENTITERATOR 1
#define FILEEVENTITERATOR 2
#define TESTEVENTITERATOR 3
#define DDPOOL 4
#define DFILE 5
#define DNULL 6
class X_Event : public A_Event
{
public:
// constructors and destructors
X_Event( PHDWORD *);
X_Event( int *);
~X_Event();
// info & debug utils
int change_id(const int oldid, const int newid);
};
X_Event::X_Event (PHDWORD *data)
: A_Event(data)
{ }
X_Event::X_Event (int *data)
: A_Event(data)
{ }
X_Event::~X_Event ()
{ }
int X_Event::change_id (const int oldid, const int newid)
{
int i = 0;
PHDWORD *fp;
PHDWORD *pp;
UINT ids = oldid;
while ( (fp = framelist[i++]) )
{
if ( ( pp = findFramePacketId (fp, ids) ) != ptrFailure)
{
setPacketId (pp, newid);
}
}
return 0;
}
#if defined(SunOS) || defined(Linux) || defined(OSF1)
void sig_handler(int);
#else
void sig_handler(...);
#endif
void exitmsg()
{
COUT << "** usage: changeid infile outfile id1 id2 " << std::endl;
exit(0);
}
void exithelp()
{
COUT << std::endl;
}
// The global pointer to the Eventiterator (we must be able to
// get at it in the signal handler)
Eventiterator *it;
int
main(int argc, char *argv[])
{
int c;
int status = 0;
int eventnr = 0;
extern int optind;
PHDWORD *buffer;
oBuffer *ob;
int fd;
int buffer_size = 2000000;
if (argc < 2) exitmsg();
while ((c = getopt(argc, argv, "s:d:n:w:vhi")) != EOF)
{
switch (c)
{
// the -s (source type) switch
case 'h':
exithelp();
break;
default:
break;
}
}
// install some handlers for the most common signals
signal(SIGKILL, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGINT, sig_handler);
// see if we can open the file
it = new fileEventiterator(argv[optind], status);
if (status)
{
delete it;
COUT << "Could not open input stream" << std::endl;
exit(1);
}
unlink ( argv[optind+1] );
fd = open(argv[optind+1], O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE ,
S_IRWXU | S_IROTH | S_IRGRP );
if ( fd < 0)
{
COUT << "Could not open file: " << argv[optind+1] << std::endl;
exit (1);
}
buffer = new PHDWORD [buffer_size];
ob = new ophBuffer (fd, buffer, buffer_size);
int paircount = 0;
int idold[1000];
int idnew[1000];
int argind = 0;
while ( optind +2 + argind +1 < argc)
{
sscanf(argv[optind +2 + argind] , "%d", &idold[paircount]);
sscanf(argv[optind +2 + argind + 1], "%d", &idnew[paircount]);
COUT << "changing " << idold[paircount] << " -> " << idnew[paircount] << std::endl;
argind+=2;
paircount++;
}
// ok. now go through the events
Event *evt;
X_Event *nevt;
int *eb;
int nw;
int j;
while ( ( evt = it->getNextEvent()) )
{
eb = new int [evt->getEvtLength() +100];
evt->Copy (eb, evt->getEvtLength() +100, &nw);
nevt = new X_Event(eb);
for (j=0; j<paircount; j++)
nevt->change_id(idold[j] , idnew[j]);
ob->addEvent(nevt);
eventnr++;
delete evt;
delete nevt;
delete [] eb;
}
delete it;
delete ob;
close(fd);
return 0;
}
#if defined(SunOS) || defined(Linux) || defined(OSF1)
void sig_handler(int i)
#else
void sig_handler(...)
#endif
{
COUT << "sig_handler: signal seen " << std::endl;
if (it) delete it;
exit(0);
}