Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / file_utils.h
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * \brief Basic function to work with file system
7  * \file file_utils.h
8  */
9 #pragma once
10
11 #include <string>
12 #ifdef _WIN32
13 # ifndef NOMINMAX
14 #  define NOMINMAX
15 # endif
16 # define _WINSOCKAPI_
17 # include <windows.h>
18 # include <profileapi.h>
19 #endif
20
21 #ifdef __MACH__
22 # include <mach/clock.h>
23 # include <mach/mach.h>
24 #endif
25
26 #include "ie_api.h"
27
28 namespace FileUtils {
29 #ifdef _WIN32
30 /// @brief File path separator
31 const char FileSeparator = '\\';
32 const char SharedLibraryExt[] = "dll";
33 #elif __APPLE__
34 const char SharedLibraryExt[] = "dylib";
35 /// @brief File path separator
36 const char FileSeparator = '/';
37 #else
38 const char SharedLibraryExt[] = "so";
39 /// @brief File path separator
40 const char FileSeparator = '/';
41 #endif
42 /// @brief Alternative file path separator
43 const char FileSeparator2 = '/';  // second option
44
45 /**
46  * @brief Interface function to get the size of a file
47  * @param fileName - name of the file
48  * @return size of the file
49  */
50 INFERENCE_ENGINE_API_CPP(long long) fileSize(const char *fileName);
51
52 /**
53  * @brief Function to get the size of a file
54  * @param f - string name of the file
55  * @return size of the file
56  */
57 inline long long fileSize(const std::string &f) {
58     return fileSize(f.c_str());
59 }
60
61 /**
62  * @brief check if file with a given filename exists
63  * @param fileName - given filename
64  * @return true is exists
65  */
66 inline bool fileExist(const char *fileName) {
67     return fileSize(fileName) >= 0;
68 }
69
70 /**
71  * @brief check if file with a given filename exists
72  * @param fileName - string with a given filename
73  * @return true is exists
74  */
75 inline bool fileExist(const std::string &fileName) {
76     return fileExist(fileName.c_str());
77 }
78
79 /**
80  * @brief CPP Interface function to read a file. In case of read error throws an exception
81  * @param file_name - name of the file to read
82  * @param buffer - buffer to read file to
83  * @param maxSize - maximum size in bytes to read
84  */
85 INFERENCE_ENGINE_API_CPP(void) readAllFile(const std::string &file_name, void *buffer, size_t maxSize);
86
87 /**
88  * @brief CPP Interface function to extract path part of a filename
89  * @param filepath - filename to extract path part from
90  * @return string with path part of the filename
91  */
92 INFERENCE_ENGINE_API_CPP(std::string) folderOf(const std::string &filepath);
93
94 /**
95  * @brief CPP Interface function to combint path with filename
96  * @param folder - path to add filename to
97  * @param file - filename to add to path
98  * @return string with combination of the path and the filename divided by file separator
99  */
100 INFERENCE_ENGINE_API_CPP(std::string) makePath(const std::string &folder, const std::string &file);
101
102 /**
103  * @brief CPP Interface function to remove file extension
104  * @param filepath - filename with extension
105  * @return string containing filename without extension
106  */
107 INFERENCE_ENGINE_API_CPP(std::string) fileNameNoExt(const std::string &filepath);
108
109 /**
110  * @brief CPP Interface function to extract extension from filename
111  * @param filename - name of the file which extension should be extracted
112  * @return string with extracted file extension
113  */
114 INFERENCE_ENGINE_API_CPP(std::string) fileExt(const char *filename);
115
116 /**
117 * @brief CPP Interface function to extract extension from filename
118 * @param filename - string with the name of the file which extension should be extracted
119 * @return string with extracted file extension
120 */
121 INFERENCE_ENGINE_API_CPP(std::string) fileExt(const std::string &filename);
122
123 /**
124 * @brief CPP Interface function to check if given filename belongs to shared library
125 * @param filename - file name to check
126 * @return true if filename is a shared library filename
127 */
128 INFERENCE_ENGINE_API_CPP(bool) isSharedLibrary(const std::string &fileName);
129
130 /**
131  * @brief TODO: description
132  * @return TODO: please use c++11 chrono module for time operations
133  */
134 inline long long GetMicroSecTimer() {
135 #ifdef _WIN32
136     static LARGE_INTEGER Frequency = { 0 };
137     LARGE_INTEGER timer;
138     if (Frequency.QuadPart == 0) QueryPerformanceFrequency(&Frequency);
139     QueryPerformanceCounter(&timer);
140     return (timer.QuadPart * 1000000) / Frequency.QuadPart;
141 #else
142     struct timespec now;
143     #ifdef __MACH__
144     clock_serv_t cclock;
145     mach_timespec_t mts;
146     host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
147     clock_get_time(cclock, &mts);
148     mach_port_deallocate(mach_task_self(), cclock);
149     now.tv_sec = mts.tv_sec;
150     now.tv_nsec = mts.tv_nsec;
151     #else
152     clock_gettime(CLOCK_REALTIME, &now);
153     #endif
154     return now.tv_sec * 1000000L + now.tv_nsec / 1000;
155 #endif
156 }
157 }  // namespace FileUtils