file_utils.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief Basic function to work with file system and UNICODE symbols
7  * @file file_utils.h
8  */
9 
10 #pragma once
11 
12 // clang-format off
13 #include <string>
14 #include <cstring>
15 
16 #include "ie_api.h"
18 
19 namespace FileUtils {
20 
21 #ifdef ENABLE_UNICODE_PATH_SUPPORT
22 
23 /**
24  * @brief Conversion from wide character string to a single-byte chain.
25  * @param wstr A wide-char string
26  * @return A multi-byte string
27  */
28 INFERENCE_ENGINE_API_CPP(std::string) wStringtoMBCSstringChar(const std::wstring& wstr);
29 
30 /**
31  * @brief Conversion from single-byte chain to wide character string.
32  * @param str A null-terminated string
33  * @return A wide-char string
34  */
35 INFERENCE_ENGINE_API_CPP(std::wstring) multiByteCharToWString(const char* str);
36 
37 #endif // ENABLE_UNICODE_PATH_SUPPORT
38 
39 template <typename T> struct FileTraits;
40 
41 #ifdef _WIN32
42 
43 /// @brief File path separator
44 const char FileSeparator = '\\';
45 
46 template<> struct FileTraits<char> {
47  constexpr static const auto FileSeparator = ::FileUtils::FileSeparator;
48  static std::string PluginLibraryPrefix() { return { }; }
49  static std::string PluginLibraryExt() { return { "dll" }; }
50 };
51 template<> struct FileTraits<wchar_t> {
52  constexpr static const auto FileSeparator = L'\\';
53  static std::wstring PluginLibraryPrefix() { return { }; }
54  static std::wstring PluginLibraryExt() { return { L"dll" }; }
55 };
56 #elif defined __APPLE__
57 /// @brief File path separator
58 const char FileSeparator = '/';
59 template<> struct FileTraits<char> {
60  constexpr static const auto FileSeparator = ::FileUtils::FileSeparator;
61  static std::string PluginLibraryPrefix() { return { "lib" }; }
62  static std::string PluginLibraryExt() { return { "so" }; }
63 };
64 template<> struct FileTraits<wchar_t> {
65  constexpr static const auto FileSeparator = L'/';
66  static std::wstring PluginLibraryPrefix() { return { L"lib" }; }
67  static std::wstring PluginLibraryExt() { return { L"so" }; }
68 };
69 #else
70 /// @brief File path separator
71 const char FileSeparator = '/';
72 template<> struct FileTraits<char> {
73  constexpr static const auto FileSeparator = ::FileUtils::FileSeparator;
74  static std::string PluginLibraryPrefix() { return { "lib" }; }
75  static std::string PluginLibraryExt() { return { "so" }; }
76 };
77 template<> struct FileTraits<wchar_t> {
78  constexpr static const auto FileSeparator = L'/';
79  static std::wstring PluginLibraryPrefix() { return { L"lib" }; }
80  static std::wstring PluginLibraryExt() { return { L"so" }; }
81 };
82 #endif
83 
84 /**
85  * @brief Interface function to get absolute path of file
86  * @ingroup ie_dev_api_file_utils
87  * @param filePath - path to file, can be relative to current working directory
88  * @return Absolute path of file
89  * @throw InferenceEngine::Exception if any error occurred
90  */
91 INFERENCE_ENGINE_API_CPP(std::string) absoluteFilePath(const std::string& filePath);
92 
93 /**
94  * @brief Interface function to create directorty recursively by given path
95  * @ingroup ie_dev_api_file_utils
96  * @param dirPath - path to file, can be relative to current working directory
97  * @throw InferenceEngine::Exception if any error occurred
98  */
99 INFERENCE_ENGINE_API_CPP(void) createDirectoryRecursive(const std::string& dirPath);
100 
101 /**
102  * @brief Interface function to check if directory exists for given path
103  * @ingroup ie_dev_api_file_utils
104  * @param path - path to directory
105  * @return true if directory exists, false otherwise
106  */
107 INFERENCE_ENGINE_API_CPP(bool) directoryExists(const std::string& path);
108 
109 /**
110  * @brief Interface function to get the size of a file. The function supports UNICODE path
111  * @ingroup ie_dev_api_file_utils
112  * @param fileName - name of the file
113  * @return size of the file
114  */
115 INFERENCE_ENGINE_API(long long) fileSize(const char *fileName);
116 
117 #ifdef ENABLE_UNICODE_PATH_SUPPORT
118 
119 /**
120  * @brief Returns file size for file with UNICODE path name
121  * @ingroup ie_dev_api_file_utils
122  *
123  * @param[in] fileName The file name
124  *
125  * @return { description_of_the_return_value }
126  */
127 inline long long fileSize(const wchar_t* fileName) {
128  return fileSize(::FileUtils::wStringtoMBCSstringChar(fileName).c_str());
129 }
130 
131 #endif // ENABLE_UNICODE_PATH_SUPPORT
132 
133 /**
134  * @brief Function to get the size of a file. The function supports UNICODE path
135  * @ingroup ie_dev_api_file_utils
136  * @param f - string name of the file
137  * @return size of the file
138  */
139 template <typename C, typename = InferenceEngine::details::enableIfSupportedChar<C>>
140 inline long long fileSize(const std::basic_string<C> &f) {
141  return fileSize(f.c_str());
142 }
143 
144 /**
145  * @brief check if file with a given filename exists. The function supports UNICODE path
146  * @ingroup ie_dev_api_file_utils
147  * @param fileName - given filename
148  * @return true is exists
149  */
150 template <typename C, typename = InferenceEngine::details::enableIfSupportedChar<C>>
151 inline bool fileExist(const C * fileName) {
152  return fileSize(fileName) >= 0;
153 }
154 
155 /**
156  * @brief check if file with a given filename exists. The function supports UNICODE path
157  * @ingroup ie_dev_api_file_utils
158  * @param fileName - string with a given filename
159  * @return true is exists
160  */
161 template <typename C, typename = InferenceEngine::details::enableIfSupportedChar<C>>
162 inline bool fileExist(const std::basic_string<C> &fileName) {
163  return fileExist(fileName.c_str());
164 }
165 
166 /**
167  * @brief CPP Interface function to combint path with filename. The function supports UNICODE path
168  * @ingroup ie_dev_api_file_utils
169  * @param folder - path to add filename to
170  * @param file - filename to add to path
171  * @return string with combination of the path and the filename divided by file separator
172  */
173 
174 template <typename C, typename = InferenceEngine::details::enableIfSupportedChar<C>>
175 inline std::basic_string<C> makePath(const std::basic_string<C> &folder, const std::basic_string<C> &file) {
176  if (folder.empty())
177  return file;
178  return folder + FileTraits<C>::FileSeparator + file;
179 }
180 
181 template <typename C> struct DotSymbol;
182 template <> struct DotSymbol<char> { constexpr static const char value = '.'; };
183 template <> struct DotSymbol<wchar_t> { constexpr static const wchar_t value = L'.'; };
184 
185 /**
186  * @brief CPP Interface function to extract extension from filename
187  * @ingroup ie_dev_api_file_utils
188  * @param filename - string with the name of the file which extension should be extracted
189  * @return string with extracted file extension
190  */
191 template <typename C, typename = InferenceEngine::details::enableIfSupportedChar<C>>
192 inline std::basic_string<C> fileExt(const std::basic_string<C> &filename) {
193  auto pos = filename.rfind(DotSymbol<C>::value);
194  if (pos == std::string::npos)
195  return {};
196  return filename.substr(pos + 1);
197 }
198 
199 template <typename C, typename = InferenceEngine::details::enableIfSupportedChar<C>>
200 inline std::basic_string<C> makePluginLibraryName(const std::basic_string<C> &path, const std::basic_string<C> &input) {
201  std::basic_string<C> separator(1, FileTraits<C>::FileSeparator);
202  if (path.empty())
203  separator = {};
204  return path + separator + FileTraits<C>::PluginLibraryPrefix() + input + DotSymbol<C>::value + FileTraits<C>::PluginLibraryExt();
205 }
206 
207 #ifdef ENABLE_UNICODE_PATH_SUPPORT
208 
209 using FilePath = std::wstring;
210 
211 inline std::string fromFilePath(const FilePath & path) {
212  return ::FileUtils::wStringtoMBCSstringChar(path);
213 }
214 
215 inline FilePath toFilePath(const std::string & path) {
216  return ::FileUtils::multiByteCharToWString(path.c_str());
217 }
218 
219 #else
220 
221 using FilePath = std::string;
222 
223 inline std::string fromFilePath(const FilePath & path) {
224  return path;
225 }
226 
227 inline FilePath toFilePath(const std::string & path) {
228  return path;
229 }
230 
231 #endif // ENABLE_UNICODE_PATH_SUPPORT
232 
233 } // namespace FileUtils
234 // clang-format on
235 
236 namespace InferenceEngine {
237 
238 /**
239  * @brief Returns a path to Inference Engine library
240  * @ingroup ie_dev_api_file_utils
241  * @return A `std::string` path to Inference Engine library
242  */
243 INFERENCE_ENGINE_API_CPP(std::string) getIELibraryPath();
244 
245 #ifdef ENABLE_UNICODE_PATH_SUPPORT
246 
247 /**
248  * @brief Returns a unicode path to Inference Engine library
249  * @ingroup ie_dev_api_file_utils
250  * @return A `std::wstring` path to Inference Engine library
251  */
252 INFERENCE_ENGINE_API_CPP(std::wstring) getIELibraryPathW();
253 
254 inline ::FileUtils::FilePath getInferenceEngineLibraryPath() {
255  return getIELibraryPathW();
256 }
257 
258 #else
259 
260 inline ::FileUtils::FilePath getInferenceEngineLibraryPath() {
261  return getIELibraryPath();
262 }
263 
264 #endif // ENABLE_UNICODE_PATH_SUPPORT
265 
266 } // namespace InferenceEngine
const char FileSeparator
File path separator.
Definition: file_utils.h:71
void createDirectoryRecursive(const std::string &dirPath)
Interface function to create directorty recursively by given path.
std::string getIELibraryPath()
Returns a path to Inference Engine library.
long long fileSize(const std::basic_string< C > &f)
Function to get the size of a file. The function supports UNICODE path.
Definition: file_utils.h:140
std::basic_string< C > fileExt(const std::basic_string< C > &filename)
CPP Interface function to extract extension from filename.
Definition: file_utils.h:192
std::string absoluteFilePath(const std::string &filePath)
Interface function to get absolute path of file.
std::basic_string< C > makePath(const std::basic_string< C > &folder, const std::basic_string< C > &file)
CPP Interface function to combint path with filename. The function supports UNICODE path.
Definition: file_utils.h:175
bool fileExist(const std::basic_string< C > &fileName)
check if file with a given filename exists. The function supports UNICODE path
Definition: file_utils.h:162
bool directoryExists(const std::string &path)
Interface function to check if directory exists for given path.
Inference Engine Plugin API namespace.
Definition: file_utils.h:181
Definition: file_utils.h:39