Line data Source code
1 : #include "SymbolsDirectory.h"
2 : #include "Logger.h"
3 : #include <filesystem>
4 : #include <string>
5 : #include "FeaturesFile.h"
6 : #include "AttrListFile.h"
7 : #include <memory>
8 : #include "../../Text/Utf8Sanitizer.h"
9 :
10 : namespace Odb::Lib::FileModel::Design
11 : {
12 6576 : SymbolsDirectory::SymbolsDirectory(const std::filesystem::path& path)
13 6576 : : m_name("")
14 13152 : , m_path(path)
15 : {
16 6576 : }
17 :
18 6216 : bool SymbolsDirectory::Parse()
19 : {
20 :
21 6216 : if (!std::filesystem::exists(m_path)) return false;
22 6216 : else if (!std::filesystem::is_directory(m_path)) return false;
23 :
24 6216 : m_name = std::filesystem::path(m_path).filename().string();
25 :
26 12432 : loginfo("Parsing symbols directory: " + m_name + "...");
27 :
28 6216 : if (!ParseFeaturesFile(m_path)) return false;
29 6216 : if (!ParseAttrListFile(m_path)) return false;
30 :
31 12432 : loginfo("Parsing symbols directory: " + m_name + " complete");
32 :
33 6216 : return true;
34 : }
35 :
36 6216 : std::string SymbolsDirectory::GetName() const { return m_name; }
37 0 : std::filesystem::path SymbolsDirectory::GetPath() const { return m_path; }
38 :
39 4476 : const FeaturesFile& SymbolsDirectory::GetFeaturesFile() const { return m_featuresFile; }
40 4476 : const AttrListFile& SymbolsDirectory::GetAttrListFile() const { return m_attrListFile; }
41 :
42 4988 : std::unique_ptr<Odb::Lib::Protobuf::SymbolsDirectory> SymbolsDirectory::to_protobuf() const
43 : {
44 4988 : auto message = std::make_unique<Odb::Lib::Protobuf::SymbolsDirectory>();
45 9976 : message->set_name(Odb::Lib::Text::ToUtf8(m_name));
46 9976 : message->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
47 4988 : message->mutable_attrlistfile()->CopyFrom(*m_attrListFile.to_protobuf());
48 4988 : message->mutable_featurefile()->CopyFrom(*m_featuresFile.to_protobuf());
49 4988 : return message;
50 0 : }
51 :
52 360 : void SymbolsDirectory::from_protobuf(const Odb::Lib::Protobuf::SymbolsDirectory& message)
53 : {
54 360 : m_name = message.name();
55 360 : m_path = message.path();
56 360 : m_attrListFile.from_protobuf(message.attrlistfile());
57 360 : m_featuresFile.from_protobuf(message.featurefile());
58 360 : }
59 :
60 6216 : bool SymbolsDirectory::ParseFeaturesFile(const std::filesystem::path& directory)
61 : {
62 18648 : return m_featuresFile.Parse(directory);
63 : }
64 :
65 6216 : bool SymbolsDirectory::ParseAttrListFile(const std::filesystem::path& directory)
66 : {
67 6216 : return m_attrListFile.Parse(directory);
68 : }
69 :
70 0 : bool SymbolsDirectory::Save(const std::filesystem::path& directory)
71 : {
72 0 : auto symbolDir = directory / m_name;
73 0 : if (!create_directory(symbolDir)) return false;
74 :
75 : //FeaturesFile m_featuresFile;
76 0 : std::ofstream featuresFile(symbolDir / "features");
77 0 : if (!featuresFile.is_open()) return false;
78 0 : if (!m_featuresFile.Save(featuresFile)) return false;
79 0 : featuresFile.close();
80 :
81 : //AttrListFile m_attrListFile;
82 0 : std::ofstream attrlistFile(symbolDir / "attrlist");
83 0 : if (!attrlistFile.is_open()) return false;
84 0 : if (!m_attrListFile.Save(attrlistFile)) return false;
85 0 : attrlistFile.close();
86 :
87 0 : return true;
88 0 : }
89 : }
|