Debian Codegraph @

src/App/MappedName.cpp

raw
1 /****************************************************************************
2 * Copyright (c) 2020 Zheng, Lei (realthunder) <[email protected]>*
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ****************************************************************************/
22
23 #include <unordered_set>
24
25 #include "MappedName.h"
26
27 #include "Base/Console.h"
28
29 #include <boost/iostreams/device/array.hpp>
30 #include <boost/iostreams/stream.hpp>
31
32
33 FC_LOG_LEVEL_INIT("MappedName", true, 2); // NOLINT
34
35 namespace Data
36 {
37
38 void MappedName::compact() const
39 {
40 auto self = const_cast<MappedName*>(
41 this); // FIXME this is a workaround for a single call in ElementMap::addName()
42
43 if (this->raw) {
44 self->data = QByteArray(self->data.constData(), self->data.size());
45 self->raw = false;
46 }
47 }
48
49
50 int MappedName::findTagInElementName(long* tagOut,
51 int* lenOut,
52 std::string* postfixOut,
53 char* typeOut,
54 bool negative,
55 bool recursive) const
56 {
57 bool hex = true;
58 int pos = this->rfind(POSTFIX_TAG);
59
60 // Example name, POSTFIX_TAG == ;:H
61 // #94;:G0;XTR;:H19:8,F;:H1a,F;BND:-1:0;:H1b:10,F
62 // ^
63 // |
64 // pos
65
66 if (pos < 0) {
67 pos = this->rfind(POSTFIX_DECIMAL_TAG);
68 if (pos < 0) {
69 return -1;
70 }
71 hex = false;
72 }
73 int offset = pos + (int)POSTFIX_TAG_SIZE;
74 long _tag = 0;
75 int _len = 0;
76 char sep = 0;
77 char sep2 = 0;
78 char tp = 0;
79 char eof = 0;
80
81 int size {0};
82 const char* nameAsChars = this->toConstString(offset, size);
83
84 // check if the number followed by the tagPosfix is negative
85 bool isNegative = (nameAsChars[0] == '-');
86 if (isNegative) {
87 ++nameAsChars;
88 --size;
89 }
90 boost::iostreams::stream<boost::iostreams::array_source> iss(nameAsChars, size);
91 if (!hex) {
92 // no hex is an older version of the encoding scheme
93 iss >> _tag >> sep;
94 }
95 else {
96 // The purpose of tagOut postfixOut is to encode one model operation. The
97 // 'tagOut' field is used to record the own object ID of that model shape,
98 // and the 'lenOut' field indicates the length of the operation codes
99 // before the tagOut postfixOut. These fields are in hex. The trailing 'F' is
100 // the shape typeOut of this element, 'F' for face, 'E' edge, and 'V' vertex.
101 //
102 // #94;:G0;XTR;:H19:8,F;:H1a,F;BND:-1:0;:H1b:10,F
103 // | | ^^ ^^
104 // | | | |
105 // ---lenOut = 0x10--- tagOut lenOut
106
107 iss >> std::hex;
108 // _tag field can be skipped, if it is 0
109 if (nameAsChars[0] == ',' || nameAsChars[0] == ':') {
110 iss >> sep;
111 }
112 else {
113 iss >> _tag >> sep;
114 }
115 }
116
117 if (isNegative) {
118 _tag = -_tag;
119 }
120
121 if (sep == ':') {
122 // ':' is followed by _len field.
123 //
124 // For decTagPostfix() (i.e. older encoding scheme), this is the length
125 // of the string before the entire postfixOut (A postfixOut may contain
126 // multiple segments usually separated by ELEMENT_MAP_PREFIX.
127 //
128 // For newer POSTFIX_TAG, this counts the number of characters that
129 // proceeds this tagOut postfixOut segment that forms the op code (see
130 // example above).
131 //
132 // The reason of this change is so that the postfixOut can stay the same
133 // regardless of the prefix, which can increase memory efficiency.
134 //
135 iss >> _len >> sep2 >> tp >> eof;
136
137 // The next separator to look for is either ':' for older tagOut postfixOut, or ','
138 if (!hex && sep2 == ':') {
139 sep2 = ',';
140 }
141 }
142 else if (hex && sep == ',') {
143 // ',' is followed by a single character that indicates the element typeOut.
144 iss >> tp >> eof;
145 sep = ':';
146 sep2 = ',';
147 }
148
149 if (_len < 0 || sep != ':' || sep2 != ',' || tp == 0 || eof != 0) {
150 return -1;
151 }
152
153 if (hex) {
154 if (pos - _len < 0) {
155 return -1;
156 }
157 if ((_len != 0) && recursive && (tagOut || lenOut)) {
158 // in case of recursive tagOut postfixOut (used by hierarchy element
159 // map), look for any embedded tagOut postfixOut
160 int next = MappedName::fromRawData(*this, pos - _len, _len).rfind(POSTFIX_TAG);
161 if (next >= 0) {
162 next += pos - _len;
163 // #94;:G0;XTR;:H19:8,F;:H1a,F;BND:-1:0;:H1b:10,F
164 // ^ ^
165 // | |
166 // next pos
167 //
168 // There maybe other operation codes after this embedded tagOut
169 // postfixOut, search for the separator.
170 //
171 int end {0};
172 if (pos == next) {
173 end = -1;
174 }
175 else {
176 end = MappedName::fromRawData(*this, next + 1, pos - next - 1)
177 .find(ELEMENT_MAP_PREFIX);
178 }
179 if (end >= 0) {
180 end += next + 1;
181 // #94;:G0;XTR;:H19:8,F;:H1a,F;BND:-1:0;:H1b:10,F
182 // ^
183 // |
184 // end
185 _len = pos - end;
186 // #94;:G0;XTR;:H19:8,F;:H1a,F;BND:-1:0;:H1b:10,F
187 // | |
188 // -- lenOut --
189 }
190 else {
191 _len = 0;
192 }
193 }
194 }
195
196 // Now convert the 'lenOut' field back to the length of the remaining name
197 //
198 // #94;:G0;XTR;:H19:8,F;:H1a,F;BND:-1:0;:H1b:10,F
199 // | |
200 // ----------- lenOut -----------
201 _len = pos - _len;
202 }
203 if (typeOut) {
204 *typeOut = tp;
205 }
206 if (tagOut) {
207 if (_tag == 0 && recursive) {
208 return MappedName(*this, 0, _len)
209 .findTagInElementName(tagOut, lenOut, postfixOut, typeOut, negative);
210 }
211 if (_tag > 0 || negative) {
212 *tagOut = _tag;
213 }
214 else {
215 *tagOut = -_tag;
216 }
217 }
218 if (lenOut) {
219 *lenOut = _len;
220 }
221 if (postfixOut) {
222 *postfixOut = this->toString(pos);
223 }
224 return pos;
225 }
226
227 } // namespace Data