-------------------------------------------------------------------------------------- Description: Loads an included file. Arguments: fileName - File name to include (can be relative to parent path) parentData - Contents of the parent file as returned by a previous call to Open; used for resolving relative paths rootPath - Custom root path for resolving relative paths ----------------------------
| 86 | // rootPath - Custom root path for resolving relative paths |
| 87 | // -------------------------------------------------------------------------------------- |
| 88 | std::optional<CachingIncludeHandler::IncludedFile> CachingIncludeHandler::Open( const char* fileName, const char* parentData, const char* rootPath ) |
| 89 | { |
| 90 | std::lock_guard scope( m_cs ); |
| 91 | |
| 92 | std::string fullPath; |
| 93 | if( PathIsRelative( fileName ) ) |
| 94 | { |
| 95 | std::string parentPath; |
| 96 | if( parentData == nullptr ) |
| 97 | { |
| 98 | parentPath = rootPath ? rootPath : m_rootPath; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | auto parent = m_pathFromFile.find( parentData - 1 ); |
| 103 | if( parent != m_pathFromFile.end() ) |
| 104 | { |
| 105 | parentPath = parent->second; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | parentPath = ""; |
| 110 | } |
| 111 | } |
| 112 | if( auto joined = JoinPath( parentPath.c_str(), fileName ) ) |
| 113 | { |
| 114 | fullPath = *joined; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | return std::nullopt; |
| 119 | } |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | fullPath = fileName; |
| 124 | } |
| 125 | auto fileFromPath = m_fileFromPath.find( fullPath ); |
| 126 | if( fileFromPath != m_fileFromPath.end() ) |
| 127 | { |
| 128 | return fileFromPath->second; |
| 129 | } |
| 130 | |
| 131 | IncludedFile info; |
| 132 | info.fullPath = fullPath; |
| 133 | |
| 134 | FILE* file = nullptr; |
| 135 | fopen_s( &file, fullPath.c_str(), "rb" ); |
| 136 | if( !file ) |
| 137 | { |
| 138 | return std::nullopt; |
| 139 | } |
| 140 | |
| 141 | size_t fileSize = 0; |
| 142 | struct stat buf; |
| 143 | if( stat( fullPath.c_str(), &buf ) == 0 ) |
| 144 | { |
| 145 | info.modifiedTime = buf.st_mtime; |
no test coverage detected