1 /**
2 * Copyright (C) ARM Limited 2010-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 */
9 #include <string.h>
10 #include <stdlib.h>
11 #include <limits.h>
12 #include "SessionXML.h"
13 #include "Logging.h"
14 #include "OlyUtility.h"
15 #include "SessionData.h"
17 static const char* TAG_SESSION = "session";
18 static const char* TAG_IMAGE = "image";
20 static const char* ATTR_VERSION = "version";
21 static const char* ATTR_CALL_STACK_UNWINDING = "call_stack_unwinding";
22 static const char* ATTR_BUFFER_MODE = "buffer_mode";
23 static const char* ATTR_SAMPLE_RATE = "sample_rate";
24 static const char* ATTR_DURATION = "duration";
25 static const char* ATTR_PATH = "path";
26 static const char* ATTR_LIVE_RATE = "live_rate";
28 SessionXML::SessionXML(const char* str) {
29 parameters.buffer_mode[0] = 0;
30 parameters.sample_rate[0] = 0;
31 parameters.duration = 0;
32 parameters.call_stack_unwinding = false;
33 parameters.live_rate = 0;
34 parameters.images = NULL;
35 mPath = 0;
36 mSessionXML = (char*)str;
37 logg->logMessage(mSessionXML);
38 }
40 SessionXML::~SessionXML() {
41 if (mPath != 0) {
42 free(mSessionXML);
43 }
44 }
46 void SessionXML::parse() {
47 mxml_node_t *tree;
48 mxml_node_t *node;
50 tree = mxmlLoadString(NULL, mSessionXML, MXML_NO_CALLBACK);
51 node = mxmlFindElement(tree, tree, TAG_SESSION, NULL, NULL, MXML_DESCEND);
53 if (node) {
54 sessionTag(tree, node);
55 mxmlDelete(tree);
56 return;
57 }
59 logg->logError(__FILE__, __LINE__, "No session tag found in the session.xml file");
60 handleException();
61 }
63 void SessionXML::sessionTag(mxml_node_t *tree, mxml_node_t *node) {
64 int version = 0;
65 if (mxmlElementGetAttr(node, ATTR_VERSION)) version = strtol(mxmlElementGetAttr(node, ATTR_VERSION), NULL, 10);
66 if (version != 1) {
67 logg->logError(__FILE__, __LINE__, "Invalid session.xml version: %d", version);
68 handleException();
69 }
71 // copy to pre-allocated strings
72 if (mxmlElementGetAttr(node, ATTR_BUFFER_MODE)) {
73 strncpy(parameters.buffer_mode, mxmlElementGetAttr(node, ATTR_BUFFER_MODE), sizeof(parameters.buffer_mode));
74 parameters.buffer_mode[sizeof(parameters.buffer_mode) - 1] = 0; // strncpy does not guarantee a null-terminated string
75 }
76 if (mxmlElementGetAttr(node, ATTR_SAMPLE_RATE)) {
77 strncpy(parameters.sample_rate, mxmlElementGetAttr(node, ATTR_SAMPLE_RATE), sizeof(parameters.sample_rate));
78 parameters.sample_rate[sizeof(parameters.sample_rate) - 1] = 0; // strncpy does not guarantee a null-terminated string
79 }
81 // integers/bools
82 parameters.call_stack_unwinding = util->stringToBool(mxmlElementGetAttr(node, ATTR_CALL_STACK_UNWINDING), false);
83 if (mxmlElementGetAttr(node, ATTR_DURATION)) parameters.duration = strtol(mxmlElementGetAttr(node, ATTR_DURATION), NULL, 10);
84 if (mxmlElementGetAttr(node, ATTR_LIVE_RATE)) parameters.live_rate = strtol(mxmlElementGetAttr(node, ATTR_LIVE_RATE), NULL, 10);
86 // parse subtags
87 node = mxmlGetFirstChild(node);
88 while (node) {
89 if (mxmlGetType(node) != MXML_ELEMENT) {
90 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
91 continue;
92 }
93 if (strcmp(TAG_IMAGE, mxmlGetElement(node)) == 0) {
94 sessionImage(node);
95 }
96 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
97 }
98 }
100 void SessionXML::sessionImage(mxml_node_t *node) {
101 int length = strlen(mxmlElementGetAttr(node, ATTR_PATH));
102 struct ImageLinkList *image;
104 image = (struct ImageLinkList *)malloc(sizeof(struct ImageLinkList));
105 image->path = (char*)malloc(length + 1);
106 image->path = strdup(mxmlElementGetAttr(node, ATTR_PATH));
107 image->next = parameters.images;
108 parameters.images = image;
109 }