1/*
2 * Copyright (c) 2024 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#ifndef OHOS_IDL_FILE_H
17#define OHOS_IDL_FILE_H
18
19#include <cstdio>
20#include <set>
21#include <string>
22
23namespace OHOS {
24namespace Idl {
25class File {
26public:
27    File(const std::string &path, unsigned int mode);
28
29    ~File();
30
31    void OpenByRead(const std::string &path);
32
33    inline bool IsValid() const
34    {
35        return fd_ != nullptr;
36    }
37
38    inline std::string GetPath() const
39    {
40        return path_;
41    }
42
43    char GetChar();
44
45    char PeekChar();
46
47    bool IsEof() const;
48
49    inline size_t GetCharLineNumber() const
50    {
51        return lineNo_;
52    }
53
54    inline size_t GetCharColumnNumber() const
55    {
56        return columnNo_;
57    }
58
59    size_t ReadData(void *data, size_t size) const;
60
61    bool WriteData(const void *data, size_t size) const;
62
63    void Flush() const;
64
65    bool Reset() const;
66
67    bool Skip(long size) const;
68
69    void Close();
70
71    static bool CreateParentDir(const std::string &path);
72
73    static bool CreatePartDir(const std::string &partPath);
74
75    static std::string AdapterPath(const std::string &path);
76
77    static std::string AdapterRealPath(const std::string &path);
78
79    static std::string RealPath(const std::string &path);
80
81    static bool CheckValid(const std::string &path);
82
83    static std::set<std::string> FindFiles(const std::string &rootDir);
84
85    size_t GetHashKey();
86
87    static constexpr unsigned int READ = 0x1;
88    static constexpr unsigned int WRITE = 0x2;
89    static constexpr unsigned int APPEND = 0x4;
90
91private:
92    size_t Read();
93
94    static constexpr int BUFFER_SIZE = 1024;
95
96    char buffer_[BUFFER_SIZE] = {0};
97    size_t size_ = 0;
98    size_t position_ = 0;
99    size_t columnNo_ = 1;
100    size_t lineNo_ = 1;
101    bool isEof_ = false;
102    bool isError_ = false;
103
104    FILE *fd_ = nullptr;
105    std::string path_;
106    unsigned int mode_ = 0;
107};
108} // namespace Idl
109} // namespace OHOS
110
111#endif // OHOS_IDL_FILE_H