]> git.mxchange.org Git - flightgear.git/blob - utils/xmlgrep/generic-protocol-analyse.c
FGCom: add silence threshold setting
[flightgear.git] / utils / xmlgrep / generic-protocol-analyse.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <malloc.h>
5 #include <assert.h>
6 #include "xml.h"
7
8 #define ROOTNODE        "/PropertyList/generic"
9 #define BUFLEN          4096
10
11 #define PRINT_ERROR_AND_EXIT(id) \
12     if (xmlErrorGetNo(id, 0) != XML_NO_ERROR) { \
13          const char *errstr = xmlErrorGetString(id, 0); \
14          size_t column = xmlErrorGetColumnNo(id, 0); \
15          size_t lineno = xmlErrorGetLineNo(id, 1); \
16          printf("Error at line %i, column %i: %s\n", lineno, column, errstr); \
17          exit(-1); \
18       }
19
20 void print_binary_protocol(void *, char *, char *);
21
22 int main(int argc, char **argv)
23 {
24    void *root_id, *protocol_id = 0;
25    char *filename;
26
27    if (argc != 2)
28    {
29       printf("Usage: %s <filename>\n", argv[0]);
30       exit(-1);
31    }
32
33    filename = argv[1];
34    root_id = xmlOpen(filename);
35    if (root_id) protocol_id = xmlNodeGet(root_id, ROOTNODE);
36
37    if (protocol_id)
38    {
39       void *dir_id;
40
41       dir_id = xmlNodeGet(protocol_id, "input");
42       if (dir_id)
43       {
44          if (!xmlNodeCompareString(dir_id, "binary_mode", "true")) {
45             print_binary_protocol(dir_id, filename, "input");
46          } else {
47             printf("Only binary mode support is implemented at the moment.\n");
48          }
49          free(dir_id);
50       }
51
52       dir_id = xmlNodeGet(protocol_id, "output");
53       if (dir_id)
54       {
55          if (!xmlNodeCompareString(dir_id, "binary_mode", "true")) {
56             print_binary_protocol(dir_id, filename, "output");
57          } else {
58             printf("Only binary mode support is implemented at the moment.\n");
59          }
60          free(dir_id);
61       }
62
63       free(protocol_id);
64    }
65
66    if (root_id)
67    {
68       xmlClose(root_id);
69    }
70
71    return 0;
72 }
73
74 /* -------------------------------------------------------------------------- */
75
76 void print_binary_protocol(void *id, char *filename, char *dir)
77 {
78    unsigned int i, num, pos = 0;
79    void *xid;
80
81    printf("\n%s\n", filename);
82    printf("Generic binary %s protocol packet description:\n\n", dir);
83    printf(" pos | size |  type  | factor     | description\n");
84    printf("-----|------|--------|------------|------------------------\n");
85    xid = xmlMarkId(id);
86    num = xmlNodeGetNum(xid, "chunk");
87    for (i=0; i<num; i++)
88    {
89       if (xmlNodeGetPos(id, xid, "chunk", i) != 0)
90       {
91          char factor[11];
92          char *name, *type;
93          int size;
94
95          type = xmlNodeGetString(xid, "type");
96          if (!strcasecmp(type, "bool")) {
97             size = 1;
98          } else if (!strcasecmp(type, "float")) {
99             size = 4;
100          } else if (!strcasecmp(type, "double")) {
101             size = 8;
102          } else if (!strcasecmp(type, "int")) {
103             size = 4;
104          } else {
105            printf("Unsupported type sepcified: '%s'\n\n", type);
106            free(type);
107            free(xid);
108            return;
109          }
110
111          xmlNodeCopyString(xid, "factor", (char *)&factor, 10);
112          name = xmlNodeGetString(xid, "name");
113
114          printf("%4i | %4i | %6s | %10s | %s\n", pos, size, type, factor, name);
115          pos += size;
116
117          free(type);
118          free(name);
119       }
120    }
121
122    printf("\ntotal package size: %i bytes\n\n", pos);
123    free(xid);
124 }