-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmi_resolver.cpp
More file actions
121 lines (85 loc) · 3.16 KB
/
Copy pathmi_resolver.cpp
File metadata and controls
121 lines (85 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "model_includes/impl/mi_resolver.hpp"
#include "model_includes/impl/mi_std_library.hpp"
#include "model_includes/api/enums/mi_file_type.hpp"
#include "fs/api/fs_file_system.hpp"
#include "project/api/prj_project.hpp"
#include <optional>
#include <filesystem>
//------------------------------------------------------------------------------
namespace model_includes {
//------------------------------------------------------------------------------
Resolver::Resolver( const fs::FileSystem & _fs, const project::Project & _project )
: m_fs{ _fs }
, m_project{ _project }
{
}
//------------------------------------------------------------------------------
const std::filesystem::path & Resolver::getProjectFolder() const
{
return m_project.getProjectDir();
}
//------------------------------------------------------------------------------
Resolver::PathOpt Resolver::resolvePath(
const std::filesystem::path & _startFile,
stdfwd::string_view _fileName
) const
{
if( PathOpt pathOpt = checkInCurrentDir( _startFile, _fileName ); pathOpt )
return pathOpt;
if( PathOpt pathOpt = findInIncludeFolders( _fileName ); pathOpt )
return pathOpt;
return std::nullopt;
}
//------------------------------------------------------------------------------
FileType Resolver::resolveFileType( const std::filesystem::path & _file )
{
FileType result = FileType::ProjectFile;
if( _file.has_parent_path() )
return result;
const std::string name = _file.string();
const StdLibrary & library = getStdLibrary();
const bool isStdLib = library.isExists( name );
result = isStdLib ? FileType::StdLibraryFile : FileType::ProjectFile;
return result;
}
//------------------------------------------------------------------------------
Resolver::PathOpt Resolver::checkInCurrentDir(
const std::filesystem::path & _startFile,
stdfwd::string_view _fileName
) const
{
std::filesystem::path startDir = _startFile.parent_path();
if( startDir.is_relative() )
startDir = getProjectFolder() / startDir;
const std::filesystem::path file = startDir / _fileName;
if( isExistFile( file ) )
return PathOpt{ file };
return std::nullopt;
}
//------------------------------------------------------------------------------
Resolver::PathOpt Resolver::findInIncludeFolders( std::string_view _fileName ) const
{
const project::Project::IncludeDirIndex count = m_project.getIncludeDirsCount();
for( project::Project::IncludeDirIndex i = 0; i < count; ++i )
{
std::filesystem::path includeDir = m_project.getIncludeDir( i );
if( includeDir.is_relative() )
includeDir = getProjectFolder() / includeDir;
const std::filesystem::path file = includeDir / _fileName;
if( isExistFile( file ) )
return PathOpt{ file };
}
return std::nullopt;
}
//------------------------------------------------------------------------------
bool Resolver::isExistFile( const std::filesystem::path & _filePath ) const
{
return m_fs.isExistFile( _filePath );
}
//------------------------------------------------------------------------------
const StdLibrary & Resolver::getStdLibrary()
{
return StdLibrary::getInstance();
}
//------------------------------------------------------------------------------
}