forked from kenygia/MooseEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LsbObject.cpp
161 lines (146 loc) · 4.46 KB
/
LsbObject.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "LsbObject.h"
#include <boost/algorithm/string.hpp>
/**
Lookup the specified entity based on the given path. Path is delimited by '/' characters.
* @brief lookupByUniquePath
* @param entity
* @param path
* @return
*/
std::string& LsbObject::getLocalized1()
{
return localized1;
}
void LsbObject::setLocalized1(const std::string &value)
{
localized1 = value;
}
std::string& LsbObject::getLocalized2()
{
return localized2;
}
void LsbObject::setLocalized2(const std::string &value)
{
localized2 = value;
}
LsbObject *LsbObject::lookupByUniquePath(const char *path) {
LsbObject *entity = this;
char *pathCopy = new char[strlen(path) + 1];
strcpy(pathCopy, path);
LsbObject *current = entity;
std::vector<std::string> tokens;
boost::split(tokens, pathCopy, boost::is_any_of("/"));
for (int i=0; i<tokens.size(); ++i) {
std::string& tok = tokens[i];
bool found = false;
for (int j=0; j<current->getChildren().size(); ++j) {
LsbObject *child = current->getChildren()[j];
if (child->getName() == tok) {
current = child;
found = true;
break;
}
}
if (!found) {
delete []pathCopy;
return 0;
}
}
delete []pathCopy;
return current;
}
/**
Lookup the specified entity based on the given path. Path is delimited by '/' characters.
* @brief LsbObject::lookupByUniquePath
* @param entities
* @param path
* @return
*/
LsbObject *LsbObject::lookupByUniquePath(std::vector<LsbObject *>& entities, const char *path) {
char *pathCopy = new char[strlen(path) + 1];
strcpy(pathCopy, path);
std::vector<std::string> tokens;
boost::split(tokens, pathCopy, boost::is_any_of("/"));
for (int i=0; i<tokens.size(); ++i) {
std::string& tok = tokens[i];
for (int j=0; j<entities.size(); ++j) {
if (entities[j]->getName() == tok) {
delete []pathCopy;
std::string finalPath = path;
if (tokens.size() > 1) {
int prefixLen = tok.length() + 1;
finalPath = finalPath.substr(prefixLen, strlen(path) - prefixLen);
return entities[j]->lookupByUniquePath(finalPath.c_str());
}
return entities[j];
}
}
}
delete []pathCopy;
return 0;
}
std::vector<LsbObject *> LsbObject::lookupAllEntitiesWithName(LsbObject *object, const char *name) {
std::vector<LsbObject *> entityList;
for (int i=0; i<object->getChildren().size(); ++i) {
if (object->getChildren()[i]->getName() == name) {
entityList.push_back(object->getChildren()[i]);
}
}
return entityList;
}
std::vector<LsbObject *> LsbObject::extractPropertyForEachListItem(std::vector<LsbObject *>& list, const char *propertyName) {
std::vector<LsbObject *> propertyList;
for (int i=0; i<list.size(); ++i) {
LsbObject *object = list[i];
for (int j=0; j<object->getChildren().size(); ++j) {
LsbObject *child = object->getChildren()[j];
if (child->getName() == propertyName) {
propertyList.push_back(child);
}
}
}
return propertyList;
}
std::vector<LsbObject *> LsbObject::findItemsByAttribute(std::vector<LsbObject *>& list, const char *attributeName, const char *attributeValue, int valueLength) {
std::vector<LsbObject *> matches;
for (int i=0; i<list.size(); ++i) {
LsbObject *object = list[i];
for (int j=0; j<object->getChildren().size(); ++j) {
LsbObject *child = object->getChildren()[j];
if (child->getName() == attributeName && child->getDataSize() == valueLength && memcmp(child->getData(), attributeValue, valueLength) == 0) {
matches.push_back(object);
}
}
}
return matches;
}
LsbObject *LsbObject::getObjectCreator(LsbObject *object) {
long childId = object->getChildId();
LsbObject *objectGroup = object->getParent();
if (objectGroup != 0) {
LsbObject *groupContainerObject = objectGroup->getParent();
if (groupContainerObject != 0) {
LsbObject *creatorsObject = groupContainerObject->lookupByUniquePath("Creators");
if (childId < creatorsObject->getChildren().size()) {
LsbObject *objectCreator = creatorsObject->getChildren()[childId];
return objectCreator;
}
}
}
return 0;
}
LsbObject *LsbObject::getObjectFromCreator(LsbObject *creator, const char *objectGroupName) {
long childId = creator->getChildId();
LsbObject *creators = creator->getParent();
if (creators != 0) {
LsbObject *groupContainerObject = creators->getParent();
if (groupContainerObject != 0) {
LsbObject *objectGroup = groupContainerObject->lookupByUniquePath(objectGroupName);
if (childId < objectGroup->getChildren().size()) {
LsbObject *object = objectGroup->getChildren()[childId];
return object;
}
}
}
return 0;
}