1 /*
2 * Copyright (c) 2023 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 "mock.h"
17
18 #ifdef BUILD_NON_SDK_VER
19 #include <filesystem>
20 #endif
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace TextEngine {
MockIFStream(const std::string & filename,std::ios_base::openmode mode)25 MockIFStream::MockIFStream(const std::string &filename, std::ios_base::openmode mode)
26 : std::ifstream(filename, mode) {}
27
StdFilesystemIsOpen() const28 bool MockIFStream::StdFilesystemIsOpen() const
29 {
30 return std::ifstream::is_open();
31 }
32
StdFilesystemSeekg(pos_type pos)33 std::istream &MockIFStream::StdFilesystemSeekg(pos_type pos)
34 {
35 return std::ifstream::seekg(pos);
36 }
37
StdFilesystemSeekg(off_type off,std::ios_base::seekdir dir)38 std::istream &MockIFStream::StdFilesystemSeekg(off_type off, std::ios_base::seekdir dir)
39 {
40 return std::ifstream::seekg(off, dir);
41 }
42
StdFilesystemTellg()43 std::ifstream::pos_type MockIFStream::StdFilesystemTellg()
44 {
45 return std::ifstream::tellg();
46 }
47
StdFilesystemRead(char_type * s,std::streamsize count)48 std::istream &MockIFStream::StdFilesystemRead(char_type *s, std::streamsize count)
49 {
50 return std::ifstream::read(s, count);
51 }
52
StdFilestystemClose()53 void MockIFStream::StdFilestystemClose()
54 {
55 std::ifstream::close();
56 }
57
58 #ifdef BUILD_NON_SDK_VER
StdFilesystemExists(const std::string & p,std::error_code & ec)59 bool StdFilesystemExists(const std::string &p, std::error_code &ec)
60 {
61 return std::filesystem::exists(p, ec);
62 }
63 #else
StdFilesystemExists(const std::string & p)64 bool StdFilesystemExists(const std::string &p)
65 {
66 std::ifstream f(p.c_str());
67 return f.good();
68 }
69 #endif
70 } // namespace TextEngine
71 } // namespace Rosen
72 } // namespace OHOS
73