/
dmerge.C
162 lines (128 loc) · 2.73 KB
/
dmerge.C
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
#include <stdio.h>
#include <time.h>
#include <string.h>
#define FOPEN64 0
int WebTime(char *name);
struct Logfile {
Logfile() {
in = NULL;
date = 0;
}
~Logfile() {
if (in) fclose (in);
in = NULL;
}
void OpenFile(char *name) {
#if FOPEN64
in = fopen64(name, "r");
#else
in = fopen(name, "r");
#endif
date = -1;
if (in == NULL) date = 0;
ReadNext();
}
void ReadNext() {
if (!date) return;
if (feof(in)) {
date = 0;
return;
}
buf[0] = 0;
fgets(buf, 4096, in);
// eliminate monitor scripts :)
if (strstr(buf, "monitor")) {
ReadNext();
return;
}
char *time = strchr(buf, '[');
if (!time) {
ReadNext();
return;
}
time ++;
char *end = strchr(time, ' ');
if (!end) {
ReadNext();
return;
}
// copy from time to end into datebuf
strncpy(datebuf, time, end - time);
datebuf[end - time] = 0;
date = WebTime(datebuf);
};
FILE *in;
char buf[4096];
char datebuf[512];
long date;
};
int WebTime(char *name)
{
int day, year, h, m, s;
int month;
struct tm t;
char mo[100];
char *tmp;
int val;
while (tmp = strchr(name, '/')) {
tmp[0] = ' ';
}
while (tmp = strchr(name, ':')) {
tmp[0] = ' ';
}
val = sscanf(name, "%d %s %d %d %d %d", &day, mo, &year, &h, &m, &s);
month = 0;
if (!strcmp(mo, "Feb")) month = 1;
if (!strcmp(mo, "Mar")) month = 2;
if (!strcmp(mo, "Apr")) month = 3;
if (!strcmp(mo, "May")) month = 4;
if (!strcmp(mo, "Jun")) month = 5;
if (!strcmp(mo, "Jul")) month = 6;
if (!strcmp(mo, "Aug")) month = 7;
if (!strcmp(mo, "Sep")) month = 8;
if (!strcmp(mo, "Oct")) month = 9;
if (!strcmp(mo, "Nov")) month = 10;
if (!strcmp(mo, "Dec")) month = 11;
memset(&t, 0, sizeof(struct tm));
t.tm_sec = s;
t.tm_min = m;
t.tm_hour = h;
t.tm_mon = month;
t.tm_year = year-1900;
t.tm_mday = day;
return mktime(&t);
}
int main(int argc, char **argv)
{
int i;
int nf = argc - 1;
Logfile *lf = new Logfile[nf];
for (i = 0; i < nf; i++) {
lf[i].OpenFile(argv[i + 1]);
if (!lf[i].in) {
fprintf(stderr, "Warning: %s failed to open.\n", argv[i + 1]);
}
}
// now, while there's a valid file, output the best line we find in the set
long valid;
do {
valid = false;
int minindex = -1;
int min = 0x7fffFFFF;
for (i = 0; i < nf; i++) {
if (!lf[i].date) continue;
if (lf[i].date > 0) valid = true;
if (lf[i].date < min) {
min = lf[i].date;
minindex = i;
}
valid = true;
}
if (minindex != -1) {
fputs(lf[minindex].buf, stdout);
lf[minindex].ReadNext();
}
} while (valid);
delete[] lf;
return 0;
}