Safir SDK Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TypeParser.h
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright Saab AB, 2004-2023 (http://safirsdkcore.com)
4*
5* Created by: Joel Ottosson / joot
6*
7*******************************************************************************
8*
9* This file is part of Safir SDK Core.
10*
11* Safir SDK Core is free software: you can redistribute it and/or modify
12* it under the terms of version 3 of the GNU General Public License as
13* published by the Free Software Foundation.
14*
15* Safir SDK Core is distributed in the hope that it will be useful,
16* but WITHOUT ANY WARRANTY; without even the implied warranty of
17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18* GNU General Public License for more Internals.
19*
20* You should have received a copy of the GNU General Public License
21* along with Safir SDK Core. If not, see <http://www.gnu.org/licenses/>.
22*
23******************************************************************************/
24#pragma once
25
26#include <boost/filesystem.hpp>
27#include <boost/algorithm/string.hpp>
28#include <codecvt>
29#include <memory>
30#include <Safir/Dob/Typesystem/ToolSupport/Internal/TypeParserImpl.h>
33
34namespace Safir
35{
36namespace Dob
37{
38namespace Typesystem
39{
40namespace ToolSupport
41{
51 static inline void GetFilesFromRootDirectories(const std::vector<boost::filesystem::path>& roots,
52 std::vector<boost::filesystem::path>& douFiles,
53 std::vector<boost::filesystem::path>& domFiles)
54 {
55 std::set<boost::filesystem::path> thisRootFiles; // Overrides are only allowed between different roots. Duplicates within the same root are not allowed.
56
57 for (const auto& root : roots)
58 {
59 thisRootFiles.clear();
60 boost::filesystem::path rootDir = boost::filesystem::canonical(root).string(std::codecvt_utf8_utf16<wchar_t>());
61 //Check paths are valid directories. We dont care about duplicates, that will only result in overriding with same file
62 if (!boost::filesystem::exists(rootDir) || !boost::filesystem::is_directory(rootDir))
63 {
64 throw ParseError("Invalid directory path", "The specified root directory does not exist.", root.string(), 8);
65 }
66
67 boost::filesystem::recursive_directory_iterator fileIt(rootDir), fileItEnd;
68 while (fileIt!=fileItEnd)
69 {
70 const boost::filesystem::path& fp=fileIt->path();
71
72 // Only handle dou- and dom- files
73 auto fileVec = boost::iequals(fp.extension().string(), ".dou") ? &douFiles : (boost::iequals(fp.extension().string(), ".dom") ? &domFiles : nullptr);
74 if (fileVec != nullptr && boost::filesystem::is_regular_file(fp))
75 {
76 if (thisRootFiles.find(fp.filename()) != thisRootFiles.end())
77 {
78 std::ostringstream os;
79 os<<"The directory '"<<root.string()<<"' contains duplicated version of file '"<<fp.filename().string()<<"'"<<std::endl;
80 throw ParseError("Duplicated dou/dom file", os.str(), fp.string(), 2);
81 }
82 thisRootFiles.insert(fp.filename());
83 fileVec->push_back(fp);
84 }
85
86 ++fileIt;
87 }
88 }
89 }
90
102 static inline std::shared_ptr<const TypeRepository> ParseTypeDefinitions(const std::vector<boost::filesystem::path>& roots)
103 {
104 std::vector<boost::filesystem::path> douFiles, domFiles;
105 GetFilesFromRootDirectories(roots, douFiles, domFiles);
106 return Internal::ParseTypeDefinitionsImpl(douFiles, domFiles);
107 }
108
117 static inline std::shared_ptr<const TypeRepository> ParseTypeDefinitions(const boost::filesystem::path& root)
118 {
119 std::vector<boost::filesystem::path> roots;
120 roots.push_back(root);
121 return ParseTypeDefinitions(roots);
122 }
123
124
125
126}
127}
128}
129} //end namespace Safir::Dob::Typesystem::ToolSupport
130
131
This namespace contains all the functionality and definitions of the SAFIR SDK.
Definition Backdoor.h:31
static void GetFilesFromRootDirectories(const std::vector< boost::filesystem::path > &roots, std::vector< boost::filesystem::path > &douFiles, std::vector< boost::filesystem::path > &domFiles)
Extract all files from the given root folders separated as dou- and dom- files.
Definition TypeParser.h:51
static std::shared_ptr< const TypeRepository > ParseTypeDefinitions(const std::vector< boost::filesystem::path > &roots)
Will validate and parse a complete set of dou- and dom-files.
Definition TypeParser.h:102
Exception used to report errors in dou- and dom- files.
Definition ParseError.h:49