forked from paparazzi/paparazzi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mergelogs.c
89 lines (69 loc) · 1.32 KB
/
mergelogs.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
#include <stdio.h>
#include <string.h>
char buf[200];
float get_time(char* str)
{
float time = 0.0f;
sscanf(str,"%f", &time);
return time;
}
void add_time(char* str, float offset, FILE* fp)
{
float time = 0.0f;
char* c = strchr(str, ' ');
if (c == 0)
return;
sscanf(str,"%f", &time);
fprintf(fp, "%.3f%s", time+offset, c);
}
int merge(char* fn1, char* fn2)
{
FILE* f1 = fopen(fn1,"r+w");
FILE* f2 = fopen(fn2,"r");
int res = 0;
char str[200];
float offset = 0.0f;
int firstline = 1;
if ((f1 > 0) && (f2 > 0))
{
//
printf("Searching End Time in File '%s'\n",fn1);
while(fgets(str,sizeof(str),f1) != NULL)
{
}
printf("Last Line:\n%s\n",str);
offset = get_time(str);
printf("Last Time Stamp: %f\n", offset);
while(fgets(str,sizeof(str),f2) != NULL)
{
// Find initial time
//if (firstline)
//{
// firstline = 0;
// offset -= get_time(str);
//}
// Change Time and add to first file:
add_time(str, offset, f1);
}
}
else
{
fprintf(stderr,"Failed to open file\n");
res = -1;
}
if (f1 > 0)
fclose(f1);
if (f2 > 0)
fclose(f2);
return -1;
}
int main(int argc, char** argv)
{
if (argc < 3)
{
fprintf(stderr, "Program needs 2 logfile names to merge\n");
return -1;
}
printf("Merging '%s' and '%s'\n",argv[1], argv[2]);
return merge(argv[1],argv[2]);
}