OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
ToolsFile.cpp
Go to the documentation of this file.
1#include "ToolsFile.h"
2#include <fstream>
3#include <sstream>
4#include <charconv>
5#include <stdexcept>
6#include <cerrno>
7#include <cmath>
8#include <cstdlib>
9#include "../parse_error.h"
10#include "../invalid_odb_error.h"
11#include "Logger.h"
12#include "str_utils.h"
13#include "../../Constants.h"
14#include <ArchiveExtractor.h>
15#include "../../Text/Utf8Sanitizer.h"
16
17namespace
18{
19 // shortest round-trippable decimal form (avoids both precision loss and
20 // fixed/scientific surprises in Save output)
21 std::string format_double(double value)
22 {
23 // non-finite values have no ODB++ representation; emitting inf/nan
24 // would produce a file our own parser (isfinite check) rejects
25 if (!std::isfinite(value))
26 {
27 throw std::domain_error("non-finite numeric value cannot be serialized to the tools file grammar");
28 }
29 char buffer[64];
30 const auto result = std::to_chars(buffer, buffer + sizeof(buffer), value);
31 if (result.ec != std::errc())
32 {
33 return "0";
34 }
35 return std::string(buffer, result.ptr);
36 }
37
38 // numeric fields parse through validated helpers: a malformed or
39 // hostile tools file surfaces as parse_error with file/line context.
40 // from_chars (not stoi/stoul/stod) so that validation is total rather
41 // than exception-only: trailing garbage, sign abuse on unsigned fields,
42 // and out-of-target-range values are all rejected — sto* would accept
43 // "5abc", wrap "-5", or truncate >UINT_MAX on LP64
45
46 unsigned int parseUnsigned(const std::filesystem::path& path, const std::string& line,
47 int lineNumber, const std::string& token, const std::string& value)
48 {
49 unsigned int result = 0;
50 const auto* last = value.data() + value.size();
51 const auto [ptr, ec] = std::from_chars(value.data(), last, result);
52 if (ec != std::errc() || ptr != last)
53 {
54 throw_parse_error(path, line, token, lineNumber);
55 }
56 return result;
57 }
58
59 double parseDouble(const std::filesystem::path& path, const std::string& line,
60 int lineNumber, const std::string& token, const std::string& value)
61 {
62 // std::from_chars for floating point is unavailable on libc++
63 // (Xcode), so the double path uses strtod. Strictness matches
64 // from_chars for the grammar we care about: full consumption,
65 // range, finite result. Known deviation: C99 strtod also accepts
66 // hex-float literals ("0x10") which from_chars would reject; a
67 // hex value is still a valid double and re-serializes as decimal,
68 // so this is a documented difference, not a defect
69 if (value.empty())
70 {
71 throw_parse_error(path, line, token, lineNumber);
72 }
73 errno = 0;
74 char* end = nullptr;
75 const double result = std::strtod(value.c_str(), &end);
76 if (end != value.c_str() + value.size() || errno == ERANGE || !std::isfinite(result))
77 {
78 throw_parse_error(path, line, token, lineNumber);
79 }
80 return result;
81 }
82}
83
85{
87 : m_path("")
88 , m_directory("")
89 , m_units("")
90 {
91 }
92
94 {
95 switch (type)
96 {
97 case Type::Plated:
99 case Type::NonPlated:
100 return subType == SubType::Standard;
101 case Type::Via:
103 default:
104 return false;
105 }
106 }
107
109 {
110 switch (type)
111 {
112 case Type::Plated: return TYPE_PLATED;
113 case Type::NonPlated: return TYPE_NON_PLATED;
114 case Type::Via: return TYPE_VIA;
115 default: return "UNKNOWN";
116 }
117 }
118
120 {
121 switch (subType)
122 {
123 case SubType::Standard: return SUB_TYPE_STANDARD;
124 case SubType::PressFit: return SUB_TYPE_PRESS_FIT;
125 case SubType::Photo: return SUB_TYPE_PHOTO;
126 case SubType::Laser: return SUB_TYPE_LASER;
127 default: return "UNKNOWN";
128 }
129 }
130
131 std::string ToolsFile::GetUnits() const
132 {
133 return m_units;
134 }
135
137 {
138 return m_thickness;
139 }
140
141 const std::vector<std::string>& ToolsFile::GetUserParams() const
142 {
143 return m_userParams;
144 }
145
147 {
148 return m_tools;
149 }
150
151 std::filesystem::path ToolsFile::GetPath() const
152 {
153 return m_path;
154 }
155
156 bool ToolsFile::Parse(std::filesystem::path directory)
157 {
158 std::ifstream toolsFile;
159 int lineNumber = 0;
160 std::string line;
161
162 try
163 {
164 m_directory = directory;
165
166 loginfo("checking for extraction...");
167
168 auto toolsFilePath = Utils::ArchiveExtractor::getUncompressedFilePath(m_directory, TOOLS_FILENAME);
169 if (std::filesystem::exists(toolsFilePath) && std::filesystem::is_regular_file(toolsFilePath))
170 {
171 loginfo("found tools file: [" + toolsFilePath.string() + "]");
172 }
173
174 m_path = toolsFilePath;
175
176 if (!std::filesystem::exists(m_path))
177 {
178 // not every layer carries a drill tools table (only drill/rout
179 // layers do); absence is not an error
180 logwarn("tools file does not exist: [" + m_directory.string() + "]");
181 return true;
182 }
183 else if (!std::filesystem::is_regular_file(m_path))
184 {
185 auto message = "tools is not a file: [" + m_path.string() + "]";
186 throw invalid_odb_error(message.c_str());
187 }
188
189 toolsFile.open(m_path.string(), std::ios::in);
190 if (!toolsFile.is_open())
191 {
192 auto message = "unable to open tools file: [" + m_path.string() + "]";
193 throw invalid_odb_error(message.c_str());
194 }
195
196 auto parsingRecord = false;
197 std::shared_ptr<ToolRecord> pCurrentRecord;
198
199 while (std::getline(toolsFile, line))
200 {
201 lineNumber++;
202
203 Utils::str_trim(line);
204 if (line.empty()) continue;
205 if (line.find(Constants::COMMENT_TOKEN) == 0) continue;
206
207 std::stringstream lineStream(line);
208
209 if (!parsingRecord)
210 {
211 // a TOOLS array record opens with "TOOLS {" — no '=' on the
212 // line, so check the array token before key/value parsing.
213 // Require the brace (optionally spaced) so a TOOLS-prefixed
214 // parameter line is never mistaken for a record open.
215 if (line.rfind(TOOLS_ARRAY_TOKEN, 0) == 0)
216 {
217 // char_traits::length over strlen: constexpr and provably bounded
218 // for this class constant (Flawfinder CWE-126 flags strlen on
219 // non-literal pointers as a potential over-read)
220 auto afterToken = line.substr(std::char_traits<char>::length(TOOLS_ARRAY_TOKEN));
221 Utils::str_trim(afterToken);
222 if (afterToken != Constants::ARRAY_RECORD_OPEN_TOKEN)
223 {
224 throw_parse_error(m_path, line, TOOLS_ARRAY_TOKEN, lineNumber);
225 }
226 pCurrentRecord = std::make_shared<ToolRecord>();
227 parsingRecord = true;
228 continue;
229 }
230
231 // global parameter
232 std::string key;
233 if (!std::getline(lineStream, key, '='))
234 {
235 throw_parse_error(m_path, line, "", lineNumber);
236 }
237
238 Utils::str_trim(key);
239
240 if (key == Constants::UNITS_TOKEN)
241 {
242 std::string value;
243 std::getline(lineStream, value);
244 Utils::str_trim(value);
245 m_units = value;
246 }
247 else if (key == THICKNESS_KEY)
248 {
249 // obsolete per spec; tolerated and preserved for round-tripping
250 std::string value;
251 std::getline(lineStream, value);
252 Utils::str_trim(value);
253 m_thickness = value.empty() ? 0.0 : parseDouble(m_path, line, lineNumber, key, value);
254 }
255 else if (key == USER_PARAMS_KEY)
256 {
257 std::string value;
258 std::getline(lineStream, value);
259 Utils::str_trim(value);
260 if (!value.empty())
261 {
262 m_userParams.push_back(value);
263 }
264 }
265 else
266 {
267 throw_parse_error(m_path, line, key, lineNumber);
268 }
269 }
270 else
271 {
272 // inside a TOOLS record
274 {
275 if (pCurrentRecord->type == ToolRecord::Type::Unknown)
276 {
277 throw_parse_error(m_path, line, TYPE_KEY, lineNumber);
278 }
279 else if (!pCurrentRecord->hasNum)
280 {
281 // spec requires a tool number; without this a
282 // NUM-less record silently commits with num=0
283 throw_parse_error(m_path, line, NUM_KEY, lineNumber);
284 }
285 else if (!pCurrentRecord->subTypeIsLegal())
286 {
287 throw_parse_error(m_path, line, TYPE2_KEY, lineNumber);
288 }
289
290 m_tools.push_back(pCurrentRecord);
291 pCurrentRecord = nullptr;
292 parsingRecord = false;
293 continue;
294 }
295
296 std::string key;
297 if (!std::getline(lineStream, key, '='))
298 {
299 throw_parse_error(m_path, line, "", lineNumber);
300 }
301
302 Utils::str_trim(key);
303
304 if (line.find('=') == std::string::npos)
305 {
306 // no '=' on the line at all — not a key/value field
307 // (trailing '=' with empty value, e.g. BIT=, is legal)
308 throw_parse_error(m_path, line, key, lineNumber);
309 }
310 std::string value;
311 std::getline(lineStream, value);
312 Utils::str_trim(value);
313
314 if (key == NUM_KEY)
315 {
316 pCurrentRecord->num = parseUnsigned(m_path, line, lineNumber, key, value);
317 pCurrentRecord->hasNum = true;
318 }
319 else if (key == TYPE_KEY)
320 {
321 if (value == ToolRecord::TYPE_PLATED) pCurrentRecord->type = ToolRecord::Type::Plated;
322 else if (value == ToolRecord::TYPE_NON_PLATED) pCurrentRecord->type = ToolRecord::Type::NonPlated;
323 else if (value == ToolRecord::TYPE_VIA) pCurrentRecord->type = ToolRecord::Type::Via;
324 else throw_parse_error(m_path, line, value, lineNumber);
325 }
326 else if (key == TYPE2_KEY)
327 {
328 if (value == ToolRecord::SUB_TYPE_STANDARD) pCurrentRecord->subType = ToolRecord::SubType::Standard;
329 else if (value == ToolRecord::SUB_TYPE_PRESS_FIT) pCurrentRecord->subType = ToolRecord::SubType::PressFit;
330 else if (value == ToolRecord::SUB_TYPE_PHOTO) pCurrentRecord->subType = ToolRecord::SubType::Photo;
331 else if (value == ToolRecord::SUB_TYPE_LASER) pCurrentRecord->subType = ToolRecord::SubType::Laser;
332 else throw_parse_error(m_path, line, value, lineNumber);
333 }
334 else if (key == MIN_TOL_KEY)
335 {
336 pCurrentRecord->minTol = value.empty() ? 0.0 : parseDouble(m_path, line, lineNumber, key, value);
337 }
338 else if (key == MAX_TOL_KEY)
339 {
340 pCurrentRecord->maxTol = value.empty() ? 0.0 : parseDouble(m_path, line, lineNumber, key, value);
341 }
342 else if (key == BIT_KEY)
343 {
344 pCurrentRecord->bit = value;
345 }
346 else if (key == FINISH_SIZE_KEY)
347 {
348 // spec: -1 when not set
349 pCurrentRecord->finishSize = value.empty() ? -1.0 : parseDouble(m_path, line, lineNumber, key, value);
350 }
351 else if (key == DRILL_SIZE_KEY)
352 {
353 pCurrentRecord->drillSize = value.empty() ? -1.0 : parseDouble(m_path, line, lineNumber, key, value);
354 }
355 else
356 {
357 throw_parse_error(m_path, line, key, lineNumber);
358 }
359 }
360 }
361
362 if (parsingRecord)
363 {
364 throw_parse_error(m_path, "EOF", Constants::ARRAY_RECORD_CLOSE_TOKEN, lineNumber);
365 }
366
367 toolsFile.close();
368 }
369 catch (parse_error& pe)
370 {
371 auto m = pe.toString("Parse Error:");
372 logerror(m);
373 toolsFile.close();
374 throw;
375 }
376 catch (std::exception& e)
377 {
378 parse_info pi(m_path, line, lineNumber);
379 const auto m = pi.toString();
380 logexception_msg(e, m);
381 toolsFile.close();
382 // bare rethrow preserves the original exception type; rethrowing
383 // the caught variable would slice invalid_odb_error/parse_error to
384 // std::exception
385 throw;
386 }
387
388 return true;
389 }
390
391 bool ToolsFile::Save(std::ostream& os)
392 {
393 os << Constants::UNITS_TOKEN << "=" << m_units << std::endl;
394 os << THICKNESS_KEY << "=" << format_double(m_thickness) << std::endl;
395 for (const auto& userParam : m_userParams)
396 {
397 os << USER_PARAMS_KEY << "=" << userParam << std::endl;
398 }
399
400 for (const auto& pTool : m_tools)
401 {
402 if (pTool->type == ToolRecord::Type::Unknown)
403 {
404 // TYPE=UNKNOWN is not part of the file grammar; a record with
405 // no type cannot round-trip and must fail loudly, not corrupt
406 throw_parse_error(m_path, "", TYPE_KEY, 0);
407 }
408 if (pTool->subType == ToolRecord::SubType::Unknown)
409 {
410 throw_parse_error(m_path, "", TYPE2_KEY, 0);
411 }
412 os << TOOLS_ARRAY_TOKEN << " " << Constants::ARRAY_RECORD_OPEN_TOKEN << std::endl;
413 os << '\t' << NUM_KEY << "=" << pTool->num << std::endl;
414 os << '\t' << TYPE_KEY << "=" << ToolRecord::typeName(pTool->type) << std::endl;
415 os << '\t' << TYPE2_KEY << "=" << ToolRecord::subTypeName(pTool->subType) << std::endl;
416 os << '\t' << MIN_TOL_KEY << "=" << format_double(pTool->minTol) << std::endl;
417 os << '\t' << MAX_TOL_KEY << "=" << format_double(pTool->maxTol) << std::endl;
418 os << '\t' << BIT_KEY << "=" << pTool->bit << std::endl;
419 os << '\t' << FINISH_SIZE_KEY << "=" << format_double(pTool->finishSize) << std::endl;
420 os << '\t' << DRILL_SIZE_KEY << "=" << format_double(pTool->drillSize) << std::endl;
421 os << Constants::ARRAY_RECORD_CLOSE_TOKEN << std::endl;
422 }
423
424 return true;
425 }
426
427 std::unique_ptr<Odb::Lib::Protobuf::ToolsFile> ToolsFile::to_protobuf() const
428 {
429 auto message = std::make_unique<Odb::Lib::Protobuf::ToolsFile>();
430
431 message->set_directory(Odb::Lib::Text::ToUtf8(m_directory.string()));
432 message->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
433 message->set_units(Odb::Lib::Text::ToUtf8(m_units));
434 message->set_thickness(m_thickness);
435 for (const auto& userParam : m_userParams)
436 {
437 message->add_userparams(Odb::Lib::Text::ToUtf8(userParam));
438 }
439 for (const auto& pTool : m_tools)
440 {
441 message->add_tools()->CopyFrom(*pTool->to_protobuf());
442 }
443
444 return message;
445 }
446
447 void ToolsFile::from_protobuf(const Odb::Lib::Protobuf::ToolsFile& message)
448 {
449 m_directory = message.directory();
450 m_path = message.path();
451 m_units = message.units();
452 m_thickness = message.thickness();
453 for (const auto& userParam : message.userparams())
454 {
455 m_userParams.push_back(userParam);
456 }
457 for (const auto& tool : message.tools())
458 {
459 auto pTool = std::make_shared<ToolRecord>();
460 pTool->from_protobuf(tool);
461 m_tools.push_back(pTool);
462 }
463 }
464
465 std::unique_ptr<Odb::Lib::Protobuf::ToolsFile::Tool> ToolsFile::ToolRecord::to_protobuf() const
466 {
467 auto message = std::make_unique<Odb::Lib::Protobuf::ToolsFile::Tool>();
468
469 message->set_num(num);
470 message->set_type(static_cast<Odb::Lib::Protobuf::ToolsFile::ToolType>(type));
471 message->set_subtype(static_cast<Odb::Lib::Protobuf::ToolsFile::ToolSubType>(subType));
472 message->set_mintol(minTol);
473 message->set_maxtol(maxTol);
474 message->set_bit(Odb::Lib::Text::ToUtf8(bit));
475 message->set_finishsize(finishSize);
476 message->set_drillsize(drillSize);
477
478 return message;
479 }
480
481 void ToolsFile::ToolRecord::from_protobuf(const Odb::Lib::Protobuf::ToolsFile::Tool& message)
482 {
483 num = message.num();
484 // keep the NUM-present invariant consistent with the parse path
485 // (hasNum gates the close-brace NUM-required check); proto presence
486 // is the wire equivalent of "NUM= appeared in the file"
487 hasNum = message.has_num();
488 // proto3 delivers out-of-range enum values from hostile/foreign wire
489 // messages verbatim; coerce them to Unknown so Save()'s existing
490 // Unknown refusal catches them instead of writing TYPE2=<integer
491 // garbage> that cannot re-parse
492 const auto typeValue = message.type();
493 type = Protobuf::ToolsFile::ToolType_IsValid(typeValue)
494 ? static_cast<Type>(typeValue)
495 : Type::Unknown;
496 // absent sub-type deserializes to the parse-time default (STANDARD),
497 // not Unknown — matches a record parsed from a file without TYPE2
498 if (!message.has_subtype())
499 {
500 subType = SubType::Standard;
501 }
502 else
503 {
504 const auto subTypeValue = message.subtype();
505 subType = Protobuf::ToolsFile::ToolSubType_IsValid(subTypeValue)
506 ? static_cast<SubType>(subTypeValue)
507 : SubType::Unknown;
508 }
509 minTol = message.mintol();
510 maxTol = message.maxtol();
511 bit = message.bit();
512 // absent sizes keep the spec's -1 "unset" sentinel rather than 0.0
513 finishSize = message.has_finishsize() ? message.finishsize() : -1.0;
514 drillSize = message.has_drillsize() ? message.drillsize() : -1.0;
515 }
516}
#define logexception_msg(e, m)
Definition Logger.h:29
#define logerror(s)
Definition Logger.h:17
#define logwarn(s)
Definition Logger.h:20
#define loginfo(s)
Definition Logger.h:14
static const auto ARRAY_RECORD_CLOSE_TOKEN
Definition Constants.h:15
static const auto ARRAY_RECORD_OPEN_TOKEN
Definition Constants.h:14
std::filesystem::path GetPath() const
std::unique_ptr< Odb::Lib::Protobuf::ToolsFile > to_protobuf() const override
static constexpr const char * TYPE_KEY
Definition ToolsFile.h:105
static constexpr const char * MIN_TOL_KEY
Definition ToolsFile.h:107
static constexpr const char * TYPE2_KEY
Definition ToolsFile.h:106
static constexpr const char * MAX_TOL_KEY
Definition ToolsFile.h:108
bool Parse(std::filesystem::path directory)
bool Save(std::ostream &os) override
const ToolRecord::Vector & GetTools() const
static constexpr const char * BIT_KEY
Definition ToolsFile.h:109
static constexpr const char * NUM_KEY
Definition ToolsFile.h:104
void from_protobuf(const Odb::Lib::Protobuf::ToolsFile &message) override
static constexpr const char * FINISH_SIZE_KEY
Definition ToolsFile.h:110
static constexpr const char * THICKNESS_KEY
Definition ToolsFile.h:102
static constexpr const char * TOOLS_FILENAME
Definition ToolsFile.h:100
static constexpr const char * DRILL_SIZE_KEY
Definition ToolsFile.h:111
const std::vector< std::string > & GetUserParams() const
static constexpr const char * TOOLS_ARRAY_TOKEN
Definition ToolsFile.h:101
static constexpr const char * USER_PARAMS_KEY
Definition ToolsFile.h:103
std::string toString(const std::string &message="") const
static std::filesystem::path getUncompressedFilePath(const std::filesystem::path &directory, const std::string &filename)
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.
std::string & str_trim(std::string &s)
Definition str_utils.cpp:38
#define throw_parse_error(dataFile, dataLine, dataToken, dataLineNumber)
Definition parse_error.h:12
void from_protobuf(const Odb::Lib::Protobuf::ToolsFile::Tool &message) override
static constexpr const char * SUB_TYPE_STANDARD
Definition ToolsFile.h:64
static constexpr const char * SUB_TYPE_PRESS_FIT
Definition ToolsFile.h:65
static constexpr const char * SUB_TYPE_PHOTO
Definition ToolsFile.h:66
std::unique_ptr< Odb::Lib::Protobuf::ToolsFile::Tool > to_protobuf() const override
static constexpr const char * SUB_TYPE_LASER
Definition ToolsFile.h:67
static constexpr const char * TYPE_NON_PLATED
Definition ToolsFile.h:62
static constexpr const char * TYPE_PLATED
Definition ToolsFile.h:61
static const char * subTypeName(SubType subType)
std::vector< std::shared_ptr< ToolRecord > > Vector
Definition ToolsFile.h:78
std::string toString(const std::string &message="") const
Definition parse_info.cpp:6