blob: 2a80482e0b8d1f76b013519f225e5585081b17ff [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
2 * Copyright (C) ARM Limited 2013. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include "EventsXML.h"
10
11#include "CapturedXML.h"
12#include "Logging.h"
13#include "OlyUtility.h"
14#include "SessionData.h"
15
16char* EventsXML::getXML() {
17#include "events_xml.h" // defines and initializes char events_xml[] and int events_xml_len
18 char path[PATH_MAX];
19 mxml_node_t *xml;
20 FILE *fl;
21
22 // Avoid unused variable warning
23 (void)events_xml_len;
24
25 // Load the provided or default events xml
26 if (gSessionData->mEventsXMLPath) {
27 strncpy(path, gSessionData->mEventsXMLPath, PATH_MAX);
28 } else {
29 util->getApplicationFullPath(path, PATH_MAX);
30 strncat(path, "events.xml", PATH_MAX - strlen(path) - 1);
31 }
32 fl = fopen(path, "r");
33 if (fl) {
34 xml = mxmlLoadFile(NULL, fl, MXML_NO_CALLBACK);
35 fclose(fl);
36 } else {
37 logg->logMessage("Unable to locate events.xml, using default");
38 xml = mxmlLoadString(NULL, (char *)events_xml, MXML_NO_CALLBACK);
39 }
40
41 // Add dynamic events from the drivers
42 mxml_node_t *events = mxmlFindElement(xml, xml, "events", NULL, NULL, MXML_DESCEND);
43 if (!events) {
44 logg->logMessage("Unable to find <events> node in the events.xml");
45 handleException();
46 }
47 for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) {
48 driver->writeEvents(events);
49 }
50
51 char* string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
52 mxmlDelete(xml);
53
54 return string;
55}
56
57void EventsXML::write(const char* path) {
58 char file[PATH_MAX];
59
60 // Set full path
61 snprintf(file, PATH_MAX, "%s/events.xml", path);
62
63 char* buf = getXML();
64 if (util->writeToDisk(file, buf) < 0) {
65 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
66 handleException();
67 }
68
69 free(buf);
70}