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 <cstdio>
21 #include <cstdlib>
22 #include <ctime>
23 #include <string>
24 #include <iostream>
25 #include <memory>
26 #include <fstream>
27 #include "src/common/utils.h"
28 #include "src/common/log_adapter.h"
29
30 namespace mindspore {
31 namespace lite {
32 char *ReadFile(const char *file, size_t *size);
33
34 std::string RealPath(const char *path);
35
36 int CreateOutputDir(std::string *dir);
37
38 template <typename T>
WriteToTxt(const std::string & file_path,void * data,size_t element_size)39 void WriteToTxt(const std::string &file_path, void *data, size_t element_size) {
40 if (data == nullptr) {
41 MS_LOG(ERROR) << "data is nullptr.";
42 return;
43 }
44 std::ofstream out_file;
45 out_file.open(file_path, std::ios::out);
46 if (!out_file.is_open()) {
47 MS_LOG(ERROR) << "file open failed";
48 return;
49 }
50 auto real_data = reinterpret_cast<T *>(data);
51 for (size_t i = 0; i < element_size; i++) {
52 out_file << real_data[i] << " ";
53 }
54 out_file.close();
55 }
56
WriteToBin(const std::string & file_path,void * data,const size_t size)57 inline int WriteToBin(const std::string &file_path, void *data, const size_t size) {
58 if (data == nullptr) {
59 MS_LOG(ERROR) << "data is nullptr.";
60 return RET_ERROR;
61 }
62 std::ofstream out_file;
63 out_file.open(file_path.c_str(), std::ios::binary);
64 if (!out_file.good() || !out_file.is_open()) {
65 return RET_ERROR;
66 }
67 out_file.write(reinterpret_cast<char *>(data), size);
68 out_file.close();
69 return RET_OK;
70 }
71
72 std::string GetAndroidPackageName();
73 std::string GetAndroidPackagePath();
74 } // namespace lite
75 } // namespace mindspore
76
77 #endif // MINDSPORE_LITE_SRC_COMMON_FILE_UTILS_H_
78