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