Blame view

src/expressionParser/ParserResultFileWriter.cc 2.28 KB
aae52524   Benjamin Renard   Parser: Write res...
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
/*
 * ParserResultFileWriter.cc
 *
 *  Created on: 29 apr. 2019
 *      Author: AKKA
 */

#include "ParserResultFileWriter.hh"

namespace AMDA {
namespace parser {

bool ParserResultFileWriter::write(const char* filePath, std::vector<ParserResultFileWriter::ParserResult>& parserResults) {
	LIBXML_TEST_VERSION

	xmlTextWriterPtr writer = xmlNewTextWriterFilename(filePath, 0);
	if (writer == NULL) {
		return false;
	}

	int rc;
	rc = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);

	if (rc < 0) {
		xmlFreeTextWriter(writer);
		return false;
	}

	rc = xmlTextWriterStartElement(writer, BAD_CAST "expressions");
        if (rc < 0) {
		xmlFreeTextWriter(writer);
                return false;
        }

	for (auto parserRes : parserResults) {
		rc = xmlTextWriterStartElement(writer, BAD_CAST "expression");
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "success", BAD_CAST (parserRes.parseSuccess ? "true" : "false"));
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		rc = xmlTextWriterWriteElement(writer, BAD_CAST "ihm", BAD_CAST parserRes.ihmExpression.c_str());
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		rc = xmlTextWriterWriteElement(writer, BAD_CAST "kernel", BAD_CAST parserRes.kernelExpression.c_str());
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		rc = xmlTextWriterStartElement(writer, BAD_CAST "params");
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		for (auto param : parserRes.paramsList) {
			rc = xmlTextWriterWriteElement(writer, BAD_CAST "param", BAD_CAST param.c_str());
		}

		// close params node
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		// close expression node
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}
	}

	// close expressions node
	rc = xmlTextWriterEndElement(writer);
        if (rc < 0) {
		xmlFreeTextWriter(writer);
                return false;
        }

	rc = xmlTextWriterEndDocument(writer);
        if (rc < 0) {
		xmlFreeTextWriter(writer);
                return false;
	}

        xmlFreeTextWriter(writer);

	return true;
}

} /* namespace parser */
} /* namespace AMDA */