Blame view

src/ParamOutputImpl/Plot/ContextFileWriter.cc 1.72 KB
8499fbdd   Benjamin Renard   Context file gene...
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
/*
 * ContextFileWriter.cc
 *
 *  Created on: 12 aug. 2015
 *      Author: AKKA
 */

#include "ContextFileWriter.hh"

namespace plot
{

ContextFileWriter::ContextFileWriter() : _writer(NULL) {

}

ContextFileWriter::~ContextFileWriter() {
	closeWriter();
}

bool ContextFileWriter::initWriter(const char* filePath) {
	if (_writer != NULL)
		return false;

	LIBXML_TEST_VERSION

	_writer = xmlNewTextWriterFilename(filePath, 0);
	if (_writer == NULL) {
		LOG4CXX_WARN(gLogger, "Error to init the context writer");
		return false;
	}

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

	if (rc < 0) {
		LOG4CXX_WARN(gLogger, "Error to init the context writer");
		return false;
	}

	return startElement("context");
}

bool ContextFileWriter::closeWriter() {
	if (_writer == NULL)
		return false;

	int rc = xmlTextWriterEndDocument(_writer);
	if (rc < 0)
		return false;

	xmlFreeTextWriter(_writer);

	_writer = NULL;

	return true;
}

bool ContextFileWriter::startElement(const char* name) {
	if (_writer == NULL)
		return false;

	int rc = xmlTextWriterStartElement(_writer, BAD_CAST name);
	if (rc < 0) {
		LOG4CXX_WARN(gLogger, "Cannot start element");
		return false;
	}

	return true;
}

bool ContextFileWriter::endElement() {
	if (_writer == NULL)
		return false;

	int rc = xmlTextWriterEndElement(_writer);
	if (rc < 0) {
		LOG4CXX_WARN(gLogger, "Cannot end element");
		return false;
	}

	return true;
}

bool ContextFileWriter::addAttribute(const char* name, const char* value) {
	if (_writer == NULL)
		return false;

	int rc = xmlTextWriterWriteAttribute(_writer, BAD_CAST name, BAD_CAST value);
	if (rc < 0) {
		LOG4CXX_WARN(gLogger, "Cannot add attribute");
		return false;
	}

	return false;
}

} /* namespace Plot */