1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef MINDSPORE_LITE_SRC_COMMON_FILE_UTILS_H_
18 #define MINDSPORE_LITE_SRC_COMMON_FILE_UTILS_H_
19
20 #include <sys/stat.h>
21 #if (defined(_WIN32) || defined(_WIN64)) && defined(_MSC_VER)
22 #define F_OK 0
23 using mode_t = int;
24 #include <io.h>
25 #else
26 #include <unistd.h>
27 #endif
28 #include <cstdio>
29 #include <cstdlib>
30 #include <ctime>
31 #include <string>
32 #include <iostream>
33 #include <memory>
34 #include <fstream>
35 #include "src/common/utils.h"
36 #include "src/common/log_adapter.h"
37 #include "src/litert/inner_allocator.h"
38
39 namespace mindspore {
40 namespace lite {
41 constexpr const char MINDIR_POSTFIX[] = ".mindir";
42 #ifdef _WIN32
43 constexpr const char FILE_SEPARATOR[] = "\\";
44 #else
45 constexpr const char FILE_SEPARATOR[] = "/";
46 #endif
47 bool IsCharEndWith(const char *src, const char *end);
48
49 std::fstream *OpenFile(const std::string &file_path, std::ios_base::openmode open_mode = std::ios::in | std::ios::out);
50
51 char *ReadFileSegment(const std::string &file, int64_t offset, int64_t len);
52
53 char *ReadFile(const char *file, size_t *size, std::shared_ptr<Allocator> allocator = nullptr);
54
55 std::string RealPath(const char *path);
56
57 int CreateDir(const std::string &file_path);
58
59 int CreateOutputDir(std::string *file_path);
60
61 template <typename T>
WriteToTxt(const std::string & file_path,void * data,size_t element_size)62 void WriteToTxt(const std::string &file_path, void *data, size_t element_size) {
63 if (data == nullptr) {
64 MS_LOG(ERROR) << "data is nullptr.";
65 return;
66 }
67 std::ofstream out_file;
68 out_file.open(file_path, std::ios::out);
69 if (!out_file.is_open()) {
70 MS_LOG(ERROR) << "file open failed";
71 return;
72 }
73 auto real_data = reinterpret_cast<T *>(data);
74 for (size_t i = 0; i < element_size; i++) {
75 out_file << real_data[i] << " ";
76 }
77 out_file.close();
78 }
79
WriteStrToFile(const std::string & file_path,const std::string & file_name,const std::string & content)80 inline std::string WriteStrToFile(const std::string &file_path, const std::string &file_name,
81 const std::string &content) {
82 std::fstream fs;
83 auto real_path = lite::RealPath(file_path.c_str());
84 auto full_path = real_path + "/" + file_name;
85 fs.open(full_path, std::ios::out);
86 if (!fs.good() || !fs.is_open()) {
87 MS_LOG(ERROR) << "Open dot file failed: " << full_path;
88 return "";
89 }
90 fs.write(content.c_str(), content.size());
91 fs.flush();
92 fs.close();
93 return full_path;
94 }
95
96 std::string GetDirectory(const std::string &path);
97
98 bool ParserPathAndModelName(const std::string &output_path, std::string *save_path, std::string *model_name);
99
ChangeFileMode(const std::string & file_name,mode_t mode)100 static inline void ChangeFileMode(const std::string &file_name, mode_t mode) {
101 if (access(file_name.c_str(), F_OK) == -1) {
102 return;
103 }
104 if (chmod(file_name.c_str(), mode) != 0) {
105 MS_LOG(WARNING) << "Change file `" << file_name << "` to mode " << std::oct << mode << " fail.";
106 }
107 }
108
109 inline int WriteToBin(const std::string &file_path, const void *data, const size_t size,
110 #ifdef _MSC_VER
111 int mode = _S_IREAD
112 #else
113 mode_t mode = S_IRUSR
114 #endif
115 ) {
116 if (data == nullptr) {
117 MS_LOG(ERROR) << "data is nullptr.";
118 return RET_ERROR;
119 }
120 std::ofstream out_file;
121 out_file.open(file_path.c_str(), std::ios::binary);
122 if (!out_file.good() || !out_file.is_open()) {
123 return RET_ERROR;
124 }
125 out_file.write(reinterpret_cast<const char *>(data), size);
126 out_file.close();
127 ChangeFileMode(file_path, mode);
128 return RET_OK;
129 }
130
131 } // namespace lite
132 } // namespace mindspore
133
134 #endif // MINDSPORE_LITE_SRC_COMMON_FILE_UTILS_H_
135