1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "file_utils.h"
17 
18 #include <unistd.h>
19 #include <fstream>
20 
21 #include "common/log.h"
22 
23 namespace OHOS {
24 namespace NeuralNetworkRuntime {
FileUtils(const std::string & filename)25 FileUtils::FileUtils(const std::string &filename) :m_filename(filename)
26 {
27 }
28 
~FileUtils()29 FileUtils::~FileUtils()
30 {
31     if (!m_filename.empty()) {
32         int ret = unlink(m_filename.c_str());
33         if (ret != 0) {
34             LOGE("Failed to delete file: %s.", m_filename.c_str());
35         }
36     }
37 }
38 
WriteFile(const std::string & data)39 bool FileUtils::WriteFile(const std::string &data)
40 {
41     std::ofstream outFile(m_filename);
42     if (!outFile.is_open()) {
43         LOGE("Failed to open file: %s.", m_filename.c_str());
44         return false;
45     }
46     outFile.write(data.c_str(), data.length());
47     outFile.close();
48     return true;
49 }
50 } // namespace NeuralNetworkRuntime
51 } // namespace OHOS
52