7#include "../invalid_odb_error.h"
10#include <system_error>
14#include "../../Text/Utf8Sanitizer.h"
17using namespace std::filesystem;
33 m_stepsByName.clear();
34 m_symbolsDirectoriesByName.clear();
55 return path(m_filePath).filename().string();
65 return m_symbolsDirectoriesByName;
74 if (!exists(m_filePath))
return false;
76 if (is_regular_file(m_filePath))
79 if (!ExtractDesignArchive(m_filePath, extractedPath))
81 logerror(
"failed to extract archive: (" + m_filePath +
")");
85 m_rootDir = findRootDir(extractedPath);
88 if (is_directory(m_rootDir))
92 if (ParseDesignDirectory(m_rootDir))
96 loginfo(
"Successfully parsed. (" + s +
"s)");
103 throw std::runtime_error(
"Parsing failed.");
108 logerror(
"Failed to find root directory");
132 std::string tempName;
135 auto tempPath = temp_directory_path() / tempName;
136 if (!create_directory(tempPath))
return false;
138 auto rootPath = tempPath / archiveName;
139 if (!create_directory(rootPath))
return false;
140 if (!Save(rootPath))
return false;
143 std::string createdArchivePath;
145 if (createdArchivePath.empty())
return false;
148 path archiveFilename = path(createdArchivePath).filename();
149 path destPath = directory / archiveFilename;
152 if (ec.value() != 0)
return false;
157 bool FileArchive::Save(
const path& directory)
160 auto miscPath = directory /
"misc";
161 if (!create_directory(miscPath))
return false;
163 std::ofstream ofs1(miscPath /
"info", std::ios::out);
164 if (!m_miscInfoFile.
Save(ofs1))
return false;
168 std::ofstream ofs2(miscPath /
"sysattr");
169 if (!m_miscAttrListFile.
Save(ofs2))
return false;
173 auto fontsPath = directory /
"fonts";
174 if (!create_directory(fontsPath))
return false;
175 std::ofstream ofs3(fontsPath /
"standard");
176 if (!m_standardFontsFile.
Save(ofs3))
return false;
180 auto matrixPath = directory /
"matrix";
181 if (!create_directory(matrixPath))
return false;
182 std::ofstream ofs4(matrixPath /
"matrix");
183 if (!m_matrixFile.
Save(ofs4))
return false;
187 const auto stepsDirectory = directory /
"steps";
188 if (!create_directory(stepsDirectory))
return false;
189 for (
auto& kvStepDirectory : m_stepsByName)
191 if (!kvStepDirectory.second->Save(stepsDirectory))
return false;
195 const auto symbolsDirectory = directory /
"symbols";
196 if (!create_directory(symbolsDirectory))
return false;
197 for (
auto& kvSymbolsDirectory : m_symbolsDirectoriesByName)
199 if (!kvSymbolsDirectory.second->Save(symbolsDirectory))
return false;
205 bool FileArchive::ExtractDesignArchive(
const path& archivePath, path& extractedPath)
211 logerror(
"Unsupported archive type: (" + archivePath.string() +
")");
216 if (!extractor.Extract())
return false;
218 auto extracted = path(extractor.GetExtractionDirectory());
219 if (!exists(extracted))
return false;
221 extractedPath = extracted;
223 loginfo(
"Successfully extracted.");
229 std::string FileArchive::findRootDir(
const path& extractedPath)
231 if (pathContainsTopLevelDesignDirs(extractedPath))
233 return extractedPath.string();
237 for (
const auto& p : directory_iterator(extractedPath))
241 if (pathContainsTopLevelDesignDirs(p.path()))
243 return p.path().string();
252 bool FileArchive::pathContainsTopLevelDesignDirs(
const path& path)
254 for (
const auto& topLevelRootDirName : TOPLEVEL_DESIGN_DIR_NAMES)
256 auto rootLevelDirPath = path / topLevelRootDirName;
257 if (!exists(rootLevelDirPath))
return false;
264 std::unique_ptr<Protobuf::FileArchive> pFileArchiveMessage(
new Protobuf::FileArchive);
265 FillProtobufMessage(*pFileArchiveMessage,
true);
270 return pFileArchiveMessage;
275 std::unique_ptr<Protobuf::FileArchive> pFileArchiveMessage(
new Protobuf::FileArchive);
276 FillProtobufMessage(*pFileArchiveMessage,
false);
277 return pFileArchiveMessage;
280 void FileArchive::FillProtobufMessage(Protobuf::FileArchive& fileArchiveMessage,
bool includeSteps)
const
285 fileArchiveMessage.mutable_matrixfile()->CopyFrom(*m_matrixFile.
to_protobuf());
286 fileArchiveMessage.mutable_miscinfofile()->CopyFrom(*m_miscInfoFile.
to_protobuf());
287 fileArchiveMessage.mutable_standardfontsfile()->CopyFrom(*m_standardFontsFile.
to_protobuf());
288 fileArchiveMessage.mutable_miscattrlistfile()->CopyFrom(*m_miscAttrListFile.
to_protobuf());
292 for (
const auto& kvStepDirectoryRecord : m_stepsByName)
294 (*fileArchiveMessage.mutable_stepsbyname())[
Odb::Lib::Text::ToUtf8(kvStepDirectoryRecord.first)] = *kvStepDirectoryRecord.second->to_protobuf();
298 for (
const auto& kvSymbolsDirectory : m_symbolsDirectoriesByName)
300 (*fileArchiveMessage.mutable_symbolsdirectoriesbyname())[
Odb::Lib::Text::ToUtf8(kvSymbolsDirectory.first)] = *kvSymbolsDirectory.second->to_protobuf();
306 m_productName = message.productname();
307 m_filename = message.filename();
311 m_standardFontsFile.
from_protobuf(message.standardfontsfile());
312 m_miscAttrListFile.
from_protobuf(message.miscattrlistfile());
314 for (
const auto& kvStepDirectoryRecord : message.stepsbyname())
316 auto pStepDirectory = std::make_shared<StepDirectory>(
"");
317 pStepDirectory->from_protobuf(kvStepDirectoryRecord.second);
318 m_stepsByName[kvStepDirectoryRecord.first] = pStepDirectory;
321 for (
const auto& kvSymbolsDirectory : message.symbolsdirectoriesbyname())
323 auto pSymbolsDirectory = std::make_shared<SymbolsDirectory>(
"");
324 pSymbolsDirectory->from_protobuf(kvSymbolsDirectory.second);
325 m_symbolsDirectoriesByName[kvSymbolsDirectory.first] = pSymbolsDirectory;
329 bool FileArchive::ParseDesignDirectory(
const path& path)
331 if (!exists(path))
return false;
332 else if (!is_directory(path))
return false;
334 m_productName = path.stem().string();
336 if (! ParseStepDirectories(path))
return false;
337 if (! ParseMiscInfoFile(path))
return false;
338 if (! ParseMatrixFile(path))
return false;
339 if (! ParseStandardFontsFile(path))
return false;
340 if (! ParseSymbolsDirectories(path))
return false;
341 if (! ParseMiscAttrListFile(path))
return false;
346 bool FileArchive::ParseStepDirectories(
const path& path)
350 auto stepsPath = path /
"steps";
351 for (
auto& d : directory_iterator(stepsPath))
355 auto pStep = std::make_shared<StepDirectory>(d.path());
358 m_stepsByName[pStep->GetName()] = pStep;
362 logwarn(
"Failed to parse step: " + pStep->GetName());
368 loginfo(
"Parsing steps complete");
373 bool FileArchive::ParseMiscInfoFile(
const path& path)
375 loginfo(
"Parsing misc/info file...");
377 auto miscDirectory = path /
"misc";
378 if (!exists(miscDirectory))
380 auto message =
"misc directory does not exist: [" + miscDirectory.string() +
"]";
381 throw invalid_odb_error(message);
383 if (!is_directory(miscDirectory))
385 auto message =
"misc path is not a directory: [" + miscDirectory.string() +
"]";
386 throw invalid_odb_error(message);
389 if (!m_miscInfoFile.
Parse(miscDirectory))
return false;
391 loginfo(
"Parsing misc/info file complete");
396 bool FileArchive::ParseMiscAttrListFile(
const path& path)
398 loginfo(
"Parsing misc/attrlist file...");
400 auto miscDirectory = path /
"misc";
401 if (!exists(miscDirectory))
403 auto message =
"misc directory does not exist: [" + miscDirectory.string() +
"]";
404 throw invalid_odb_error(message);
406 if (!is_directory(miscDirectory))
408 auto message =
"misc path is not a directory: [" + miscDirectory.string() +
"]";
409 throw invalid_odb_error(message);
412 if (!m_miscAttrListFile.
Parse(miscDirectory))
return false;
414 loginfo(
"Parsing misc/attrlist file complete");
419 bool FileArchive::ParseMatrixFile(
const path& path)
421 loginfo(
"Parsing matrix/matrix file...");
423 auto matrixDir = path /
"matrix";
424 if (!exists(matrixDir))
426 auto message =
"matrix directory does not exist: [" + matrixDir.string() +
"]";
427 throw invalid_odb_error(message);
429 if (!is_directory(matrixDir))
431 auto message =
"matrix path is not a directory: [" + matrixDir.string() +
"]";
432 throw invalid_odb_error(message);
435 if (!m_matrixFile.
Parse(matrixDir))
return false;
437 loginfo(
"Parsing matrix/matrix file complete");
441 bool FileArchive::ParseStandardFontsFile(
const path& path)
443 loginfo(
"Parsing fonts/standard file...");
445 auto fontsDir = path /
"fonts";
446 if (!exists(fontsDir))
448 auto message =
"fonts directory does not exist: [" + fontsDir.string() +
"]";
449 throw invalid_odb_error(message);
451 if (!is_directory(fontsDir))
453 auto message =
"fonts path is not a directory: [" + fontsDir.string() +
"]";
454 throw invalid_odb_error(message);
457 if (!m_standardFontsFile.
Parse(fontsDir))
return false;
459 loginfo(
"Parsing fonts/standard file complete");
464 bool FileArchive::ParseSymbolsDirectories(
const path& path)
466 loginfo(
"Parsing symbols root directory...");
468 auto symbolsDirectory = path /
"symbols";
470 if (!exists(symbolsDirectory))
472 logwarn(
"symbols root directory does not exist (" + symbolsDirectory.string() +
")");
475 else if (!is_directory(symbolsDirectory))
477 logerror(
"symbols root directory exists but is a regular file/not a directory (" + symbolsDirectory.string() +
")");
481 for (
auto& d : directory_iterator(symbolsDirectory))
485 auto pSymbolsDirectory = std::make_shared<SymbolsDirectory>(d.path());
486 if (pSymbolsDirectory->Parse())
490 m_symbolsDirectoriesByName[pSymbolsDirectory->GetName()] = pSymbolsDirectory;
494 logerror(
"Parsing symbol directory: " + pSymbolsDirectory->GetName() +
" failed");
500 loginfo(
"Parsing symbols root directory complete");
507 return m_miscInfoFile;
517 return m_standardFontsFile;
522 return m_miscAttrListFile;
527 std::shared_ptr<FileModel::Design::StepDirectory> pStepDirectory;
532 if (stepName.empty())
535 pStepDirectory = steps.begin()->second;
539 auto findIt = steps.find(stepName);
540 if (findIt != steps.end())
542 pStepDirectory = findIt->second;
547 return pStepDirectory;
bool Parse(std::filesystem::path directory)
void from_protobuf(const Odb::Lib::Protobuf::AttrListFile &message) override
std::unique_ptr< Odb::Lib::Protobuf::AttrListFile > to_protobuf() const override
bool Save(std::ostream &os) override
std::string GetRootDir() const
bool SaveFileModel(const std::filesystem::path &directory)
const MiscInfoFile & GetMiscInfoFile() const
std::string GetProductName() const
std::shared_ptr< StepDirectory > GetStepDirectory(const std::string &stepName="") const
const StandardFontsFile & GetStandardFontsFile() const
const MatrixFile & GetMatrixFile() const
std::unique_ptr< Odb::Lib::Protobuf::FileArchive > to_protobuf() const override
std::unique_ptr< Odb::Lib::Protobuf::FileArchive > to_protobuf_skeleton() const
void from_protobuf(const Odb::Lib::Protobuf::FileArchive &message) override
const StepDirectory::StringMap & GetStepsByName() const
std::string GetFilePath() const
std::string GetFilename() const
const AttrListFile & GetMiscAttrListFile() const
const SymbolsDirectory::StringMap & GetSymbolsDirectoriesByName() const
bool Save(std::ostream &os) override
std::unique_ptr< Odb::Lib::Protobuf::MatrixFile > to_protobuf() const override
bool Parse(std::filesystem::path path) override
void from_protobuf(const Odb::Lib::Protobuf::MatrixFile &message) override
bool Parse(std::filesystem::path path) override
void from_protobuf(const Odb::Lib::Protobuf::MiscInfoFile &message) override
std::unique_ptr< Odb::Lib::Protobuf::MiscInfoFile > to_protobuf() const override
bool Save(std::ostream &os) override
void from_protobuf(const Odb::Lib::Protobuf::StandardFontsFile &message) override
std::unique_ptr< Odb::Lib::Protobuf::StandardFontsFile > to_protobuf() const override
bool Parse(std::filesystem::path path) override
bool Save(std::ostream &os) override
std::map< std::string, std::shared_ptr< StepDirectory > > StringMap
std::map< std::string, std::shared_ptr< SymbolsDirectory > > StringMap
std::string getElapsedSecondsString(const std::string &suffix="") const
void AssertAllStringFieldsAreValidUtf8(const google::protobuf::Message &msg, std::string_view msgName)
Debug-only assertion to verify all string fields in a message are valid UTF-8.
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.
void fastmove_file(const path &source, const path &dest, bool overwriteExisting, std::error_code &ec)