-
Notifications
You must be signed in to change notification settings - Fork 4
/
xml2c_negotiate.cc
executable file
·129 lines (117 loc) · 3.66 KB
/
xml2c_negotiate.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
/*
* Id: $Id: xml2c_negotiate.cc 4753 2015-03-26 06:36:07Z cseagle $
* Last Updated: $LastChangedDate: 2015-03-26 06:36:07 +0000 (Thu, 26 Mar 2015) $
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "utils.h"
#include "xml2c_negotiate.h"
#include "logging.h"
#include "reasons.h"
Xml2cNegotiate::Xml2cNegotiate(xmlNode *n) {
xmlNode *typeNode = findChild(n, "type1");
if (typeNode == NULL) {
typeNode = findChild(n, "type2");
if (typeNode == NULL) {
log_fail("Missing type1 or type2 in <%s> element at line %d\n", n->name, n->line);
throw (int)PARSE_ERROR;
}
parseType2(typeNode);
}
else {
parseType1(typeNode);
}
}
void Xml2cNegotiate::parseType1(xmlNode *type1) {
povType = 1;
char *endptr;
unsigned int len;
xmlNode *ipMaskNode = findChild(type1, "ipmask");
xmlNode *regMaskNode = findChild(type1, "regmask");
xmlNode *regNumNode = findChild(type1, "regnum");
char *ipmaskStr = getNodeText(ipMaskNode, &len);
if (len == 0) {
log_fail("<%s> element contains no data at line %d\n", ipMaskNode->name, ipMaskNode->line);
throw (int)PARSE_ERROR;
}
ipmask = strtoul(ipmaskStr, &endptr, 0);
while (*endptr) {
if (!isspace(*endptr)) {
log_fail("Expected one unsigned integer in <%s> element at line %d\n", ipMaskNode->name, ipMaskNode->line);
throw (int)PARSE_ERROR;
}
endptr++;
}
char *regmaskStr = getNodeText(regMaskNode, &len);
if (len == 0) {
log_fail("<%s> element contains no data at line %d\n", regMaskNode->name, regMaskNode->line);
throw (int)PARSE_ERROR;
}
regmask = strtoul(regmaskStr, &endptr, 0);
while (*endptr) {
if (!isspace(*endptr)) {
log_fail("Expected one unsigned integer in <%s> element at line %d\n", regMaskNode->name, regMaskNode->line);
throw (int)PARSE_ERROR;
}
endptr++;
}
char *regnumStr = getNodeText(regNumNode, &len);
if (len == 0) {
log_fail("<%s> element contains no data at line %d\n", regNumNode->name, regNumNode->line);
throw (int)PARSE_ERROR;
}
regnum = strtoul(regnumStr, &endptr, 10);
while (*endptr) {
if (!isspace(*endptr)) {
log_fail("Expected one unsigned integer in <%s> element at line %d\n", regNumNode->name, regNumNode->line);
throw (int)PARSE_ERROR;
}
endptr++;
}
if (regnum > 7) {
log_fail("Expected integer in the range 0..7 (found %d) in <%s> element at line %d\n", regnum, regNumNode->name, regNumNode->line);
throw (int)PARSE_ERROR;
}
xmlFree(ipmaskStr);
xmlFree(regmaskStr);
xmlFree(regnumStr);
}
void Xml2cNegotiate::parseType2(xmlNode *type2) {
povType = 2;
}
void Xml2cNegotiate::generate(FILE *outfile) {
switch (povType) {
case 1:
fprintf(outfile, " negotiate_type1(0x%x, 0x%x, %u);\n", ipmask, regmask, regnum);
break;
case 2:
fprintf(outfile, " negotiate_type2();\n");
break;
default:
log_fail("Invalid pov type (%d) encountered while generating source\n", povType);
throw (int)PARSE_ERROR;
}
}
PovSubmit::PovSubmit(xmlNode *n) {
unsigned int varLen;
xmlNode *varNode = findChild(n, "var");
var = getNodeText(varNode, &varLen);
povType = 0;
}
PovSubmit::~PovSubmit() {
if (var != NULL) {
xmlFree(var);
}
}
void PovSubmit::generate(FILE *outfile) {
if (povType == 2) {
fprintf(outfile, " //*** submitting type 2 POV results\n");
if (var != NULL) {
fprintf(outfile, " submit_type2(\"%s\");\n", var);
}
else {
fprintf(outfile, " submit_type2(NULL);\n");
}
}
}