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