1 /**
2 * Copyright (C) ARM Limited 2013-2014. 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 */
9 #include "EventsXML.h"
11 #include "CapturedXML.h"
12 #include "Logging.h"
13 #include "OlyUtility.h"
14 #include "SessionData.h"
16 char* 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;
22 // Avoid unused variable warning
23 (void)events_xml_len;
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, (const char *)events_xml, MXML_NO_CALLBACK);
39 }
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 }
51 char* string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
52 mxmlDelete(xml);
54 return string;
55 }
57 void EventsXML::write(const char* path) {
58 char file[PATH_MAX];
60 // Set full path
61 snprintf(file, PATH_MAX, "%s/events.xml", path);
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 }
69 free(buf);
70 }