1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include <file_utils.h>
6 #include "details/ie_exception.hpp"
12 #include <mach/clock.h>
13 #include <mach/mach.h>
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)
22 long long FileUtils::fileSize(const char *fileName) {
23 std::ifstream in(fileName, std::ios_base::binary | std::ios_base::ate);
27 void FileUtils::readAllFile(const std::string &file_name, void *buffer, size_t maxSize) {
28 std::ifstream inputFile;
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)) {
34 THROW_IE_EXCEPTION << "cannot read " << maxSize << " bytes from file " << file_name;
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);
47 std::string FileUtils::makePath(const std::string &folder, const std::string &file) {
48 if (folder.empty()) return file;
49 return folder + FileSeparator + file;
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);
58 std::string FileUtils::fileExt(const char *filename) {
59 return fileExt(std::string(filename));
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);
68 bool FileUtils::isSharedLibrary(const std::string& fileName) {
69 return 0 == strncasecmp(fileExt(fileName).c_str(), SharedLibraryExt, strlen(SharedLibraryExt));