-
Notifications
You must be signed in to change notification settings - Fork 1
/
microstamp.c
76 lines (71 loc) · 2.2 KB
/
microstamp.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
#include <stdio.h>
#include <sys/time.h>
#include "microstamp.h"
#define BUF_SIZE 1024
int main(int argc, char *argv[])
{
microstamp(stdin, stdout);
}
void microstamp(FILE *in, FILE *out)
{
char buffer[BUF_SIZE];
int bufptr = 0;
int state = 1;
int pending;
int len, ptr;
struct timeval time;
while ((pending = fgetc(in)) != EOF) {
buffer[bufptr++] = pending;
switch (state) {
case 1:
// attente de l’entête
if (pending = 129) {
state = 2;
}
break;
case 2:
state = 3;
break;
case 3:
// attente d’un type d’argument
if (pending == 128) {
// fin de trame
gettimeofday(&time, NULL);
buffer[bufptr-1] = 132;
int sec = time.tv_sec;
buffer[bufptr++] = sec & 0xFF;
buffer[bufptr++] = (sec >> 8) & 0xFF;
buffer[bufptr++] = (sec >> 16) & 0xFF;
buffer[bufptr++] = (sec >> 24) & 0xFF;
buffer[bufptr++] = 148;
int msec = time.tv_usec;
buffer[bufptr++] = msec & 0xFF;
buffer[bufptr++] = (msec >> 8) & 0xFF;
buffer[bufptr++] = (msec >> 16) & 0xFF;
buffer[bufptr++] = (msec >> 24) & 0xFF;
buffer[bufptr] = 128;
fwrite(buffer, sizeof(char), bufptr+1, out);
fflush(out);
bufptr = 0;
state = 1;
} else if (checktype(pending)) {
// autre type
len = pending & 0xF;
ptr = 0;
state = 4;
} else {
// erreur
bufptr = 0;
state = 1;
}
break;
case 4:
// donnée d’un argument
ptr++;
if (ptr == len) {
state = 3;
}
break;
}
}
}