Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / file_utils.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <file_utils.h>
6 #include "details/ie_exception.hpp"
7 #include <fstream>
8
9 #include <w_unistd.h>
10
11 #ifdef __MACH__
12     #include <mach/clock.h>
13     #include <mach/mach.h>
14 #endif
15
16 #if defined(WIN32) || defined(WIN64)
17     // Copied from linux libc sys/stat.h:
18     #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
19     #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
20 #endif
21
22 long long FileUtils::fileSize(const char *fileName) {
23     std::ifstream in(fileName, std::ios_base::binary | std::ios_base::ate);
24     return in.tellg();
25 }
26
27 void FileUtils::readAllFile(const std::string &file_name, void *buffer, size_t maxSize) {
28     std::ifstream inputFile;
29
30     inputFile.open(file_name, std::ios::binary | std::ios::in);
31     if (!inputFile.is_open()) THROW_IE_EXCEPTION << "cannot open file " << file_name;
32     if (!inputFile.read(reinterpret_cast<char *>(buffer), maxSize)) {
33         inputFile.close();
34         THROW_IE_EXCEPTION << "cannot read " << maxSize << " bytes from file " << file_name;
35     }
36
37     inputFile.close();
38 }
39
40 std::string FileUtils::folderOf(const std::string &filepath) {
41     auto pos = filepath.rfind(FileSeparator);
42     if (pos == std::string::npos) pos = filepath.rfind(FileSeparator2);
43     if (pos == std::string::npos) return "";
44     return filepath.substr(0, pos);
45 }
46
47 std::string FileUtils::makePath(const std::string &folder, const std::string &file) {
48     if (folder.empty()) return file;
49     return folder + FileSeparator + file;
50 }
51
52 std::string FileUtils::fileNameNoExt(const std::string &filepath) {
53     auto pos = filepath.rfind('.');
54     if (pos == std::string::npos) return filepath;
55     return filepath.substr(0, pos);
56 }
57
58 std::string FileUtils::fileExt(const char *filename) {
59     return fileExt(std::string(filename));
60 }
61
62 std::string FileUtils::fileExt(const std::string &filename) {
63     auto pos = filename.rfind('.');
64     if (pos == std::string::npos) return "";
65     return filename.substr(pos + 1);
66 }
67
68 bool FileUtils::isSharedLibrary(const std::string& fileName) {
69     return 0 == strncasecmp(fileExt(fileName).c_str(), SharedLibraryExt, strlen(SharedLibraryExt));
70 }