Debian Codegraph @

src/App/Range.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Eivind Kvedalen <[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 <boost/regex.hpp>
24 #include <cassert>
25 #include <sstream>
26 #include <string>
27 #include <cmath>
28 #include <regex>
29
30 #include <string_view>
31 #include <Base/Exception.h>
32 #include "Range.h"
33
34
35 using namespace App;
36
37 const int App::CellAddress::MAX_ROWS = 16384;
38 const int App::CellAddress::MAX_COLUMNS = 26 * 26 + 26;
39
40 namespace App
41 {
42 // From a given cell address the '$' must be at within the first
43 // few characters
44 bool maybeAbsolute(std::string_view address)
45 {
46 const int MAX_COLUMNS_LETTERS = 2;
47
48 address = address.substr(0, MAX_COLUMNS_LETTERS + 1);
49 return address.find("$") != std::string_view::npos;
50 }
51 } // namespace App
52
53 Range::Range(const char* range, bool normalize)
54 {
55 std::string from;
56 std::string to;
57
58 assert(range);
59
60 if (!strchr(range, ':')) {
61 from = range;
62 to = range;
63 }
64 else {
65 std::string s = range;
66 from = s.substr(0, s.find(':'));
67 to = s.substr(s.find(':') + 1);
68 }
69
70 CellAddress begin(from);
71 CellAddress end(to);
72
73 row_begin = begin.row();
74 col_begin = begin.col();
75 row_end = end.row();
76 col_end = end.col();
77
78 if (normalize) {
79 this->normalize();
80 }
81 row_curr = row_begin;
82 col_curr = col_begin;
83 }
84
85 Range::Range(int _row_begin, int _col_begin, int _row_end, int _col_end, bool normalize)
86 : row_begin(_row_begin)
87 , col_begin(_col_begin)
88 , row_end(_row_end)
89 , col_end(_col_end)
90 {
91 if (normalize) {
92 this->normalize();
93 }
94 row_curr = row_begin;
95 col_curr = col_begin;
96 }
97
98 Range::Range(const CellAddress& from, const CellAddress& to, bool normalize)
99 : row_begin(from.row())
100 , col_begin(from.col())
101 , row_end(to.row())
102 , col_end(to.col())
103 {
104 if (normalize) {
105 this->normalize();
106 }
107 row_curr = row_begin;
108 col_curr = col_begin;
109 }
110
111 void Range::normalize()
112 {
113 if (row_begin > row_end) {
114 std::swap(row_begin, row_end);
115 }
116 if (col_begin > col_end) {
117 std::swap(col_begin, col_end);
118 }
119 }
120
121 bool Range::next()
122 {
123 if (row_curr < row_end) {
124 row_curr++;
125
126 return true;
127 }
128 if (col_curr < col_end) {
129 if (row_curr == row_end + 1) {
130 return false;
131 }
132 row_curr = row_begin;
133 ++col_curr;
134 return true;
135 }
136 return false;
137 }
138
139 /**
140 * @brief Decode a row specification into a 0-based integer.
141 *
142 * @param rowstr Row specified as a string, with "1" being the first row.
143 *
144 * @returns The row.
145 */
146
147 int App::decodeRow(const std::string& rowstr, bool silent)
148 {
149 int row = validRow(rowstr);
150
151 if (silent || row >= 0) {
152 return row;
153 }
154
155 throw Base::IndexError("Invalid row specification.");
156 }
157
158 /**
159 * Assumes well-formed input. A through ZZZ. 0-based output
160 */
161 int columnStringToNum(const std::string& colstr)
162 {
163 double out {0};
164 int pos {0};
165 for (auto chr = colstr.crbegin(); chr != colstr.crend(); chr++) {
166 out += (*chr - 'A' + 1) * std::pow(26, pos++);
167 }
168
169 return static_cast<int>(out - 1);
170 }
171
172 /**
173 * @brief Decode a column name string into a 0-based integer.
174 *
175 * @param colstr input string.
176 *
177 * @returns The column.
178 *
179 */
180
181 int App::decodeColumn(const std::string& colstr, bool silent)
182 {
183 if (validColumn(colstr)) {
184 return columnStringToNum(colstr);
185 }
186
187 if (silent) {
188 return -1;
189 }
190
191 throw Base::IndexError("Invalid column specification");
192 }
193
194 /**
195 * @brief Determine whether a row specification is valid or not.
196 *
197 * @param rowstr Row specified as a string, with "1" being the first row.
198 *
199 * @returns 0 or positive on success, -1 on error.
200 */
201
202 int App::validRow(const std::string& rowstr)
203 {
204 char* end;
205 int i = strtol(rowstr.c_str(), &end, 10);
206
207 if (i <= 0 || i > CellAddress::MAX_ROWS || *end) {
208 return -1;
209 }
210
211 return i - 1;
212 }
213
214 /**
215 * @brief Determine if a string is a valid column specification.
216 *
217 * @param colstr input string.
218 *
219 * @returns true if valid, false if not.
220 *
221 */
222
223 bool App::validColumn(const std::string& colstr)
224 {
225 return boost::regex_match(colstr, boost::regex("[A-Z]{1,3}"));
226 }
227
228 /**
229 * @brief Convert a string address into integer \a row and \a column.
230 * row and col are 0-based.
231 *
232 * This function will throw an exception if the specified \a address is invalid.
233 *
234 * @param strAddress Address to parse.
235 *
236 */
237
238 App::CellAddress App::stringToAddress(const char* strAddress, bool silent)
239 {
240 assert(strAddress);
241
242 static boost::regex e("(\\$?[A-Z]{1,2})(\\$?[0-9]{1,5})");
243 boost::cmatch cm;
244
245 if (boost::regex_match(strAddress, cm, e)) {
246 bool absCol = (cm[1].first[0] == '$');
247 std::string r, c;
248 if (absCol) {
249 c = std::string(cm[1].first + 1, cm[1].second);
250 }
251 else {
252 c = std::string(cm[1].first, cm[1].second);
253 }
254
255 bool absRow = (cm[2].first[0] == '$');
256 if (absRow) {
257 r = std::string(cm[2].first + 1, cm[2].second);
258 }
259 else {
260 r = std::string(cm[2].first, cm[2].second);
261 }
262
263 return CellAddress(decodeRow(r, silent), decodeColumn(c, silent), absRow, absCol);
264 }
265 else if (silent) {
266 return CellAddress();
267 }
268
269 throw Base::RuntimeError("Invalid cell specifier.");
270 }
271
272 /**
273 * @brief Convert given \a cell address into its string representation.
274 *
275 * @returns Address given as a string.
276 */
277
278 std::string App::CellAddress::toString(Cell cell) const
279 {
280 std::stringstream s;
281
282 Base::Flags<Cell> flags(cell);
283 if (flags.testFlag(Cell::ShowColumn)) {
284 if (_absCol && flags.testFlag(Cell::Absolute)) {
285 s << '$';
286 }
287 if (col() < 26) {
288 s << static_cast<char>('A' + col());
289 }
290 else {
291 int colnum = col() - 26;
292
293 s << static_cast<char>('A' + (colnum / 26));
294 s << static_cast<char>('A' + (colnum % 26));
295 }
296 }
297
298 if (flags.testFlag(Cell::ShowRow)) {
299 if (_absRow && flags.testFlag(Cell::Absolute)) {
300 s << '$';
301 }
302 s << (row() + 1);
303 }
304
305 return s.str();
306 }
307
308 /*!
309 * \brief App::CellAddress::parseAbsoluteAddress
310 * \param address
311 * If the passed string is a valid and absolute cell address it will be assigned to this instance.
312 * \return True if it's an absolute cell address and false otherwise
313 */
314 bool App::CellAddress::parseAbsoluteAddress(const char* address)
315 {
316 if (maybeAbsolute(address)) {
317 CellAddress addr = stringToAddress(address, true);
318 if (addr.isValid()) {
319 *this = addr;
320 return true;
321 }
322 }
323 return false;
324 }