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 "util/file.h"
17
18 #include <cstdlib>
19 #include <cstring>
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace Idl {
24 #ifdef __MINGW32__
25 constexpr unsigned int File::READ;
26 constexpr unsigned int File::WRITE;
27 constexpr unsigned int File::APPEND;
28 #endif
29
File(const String & path,int mode)30 File::File(const String& path, int mode)
31 : mode_(mode)
32 {
33 if (path.IsEmpty()) {
34 return;
35 }
36
37 if (mode_ & READ) {
38 fd_ = fopen(path.string(), "r");
39 } else if (mode_ & WRITE) {
40 fd_ = fopen(path.string(), "w+");
41 } else if (mode_ & APPEND) {
42 fd_ = fopen(path.string(), "a+");
43 }
44
45 if (fd_ != nullptr) {
46 #ifndef __MINGW32__
47 char* absolutePath = realpath(path.string(), nullptr);
48 if (absolutePath != nullptr) {
49 path_ = absolutePath;
50 free(absolutePath);
51 } else {
52 path_ = path;
53 }
54 #else
55 char absolutePath[_MAX_PATH];
56 _fullpath(absolutePath, path.string(), _MAX_PATH);
57 path_ = absolutePath;
58 #endif
59 }
60 }
61
~File()62 File::~File()
63 {
64 Close();
65 }
66
GetChar()67 char File::GetChar()
68 {
69 char c = PeekChar();
70
71 if (position_ + 1 <= size_) {
72 position_++;
73
74 if (c != '\n') {
75 columnNo_++;
76 } else {
77 columnNo_ = 0;
78 lineNo_++;
79 }
80 }
81 return c;
82 }
83
PeekChar()84 char File::PeekChar()
85 {
86 if (position_ + 1 > size_) {
87 int ret = Read();
88 if (ret == -1) {
89 isEof_ = true;
90 }
91 }
92
93 return buffer_[position_];
94 }
95
IsEof() const96 bool File::IsEof() const
97 {
98 return isEof_ || buffer_[position_] == static_cast<char>(-1);
99 }
100
Read()101 int File::Read()
102 {
103 if (isEof_ || isError_) {
104 return -1;
105 }
106
107 (void)memset_s(buffer_, BUFFER_SIZE, 0, BUFFER_SIZE);
108 size_t count = fread(buffer_, 1, BUFFER_SIZE - 1, fd_);
109 if (count < BUFFER_SIZE - 1) {
110 isError_ = ferror(fd_) != 0;
111 buffer_[count] = -1;
112 }
113 size_ = count;
114 position_ = 0;
115 return count != 0 ? count : -1;
116 }
117
ReadData(void * data,size_t size)118 bool File::ReadData(void* data, size_t size)
119 {
120 if (data == nullptr || size == 0) {
121 return true;
122 }
123
124 if (fd_ == nullptr) {
125 return false;
126 }
127
128 size_t count = fread(data, size, 1, fd_);
129 return count == 1;
130 }
131
WriteData(const void * data,size_t size)132 bool File::WriteData(const void* data, size_t size)
133 {
134 if (data == nullptr || size == 0) {
135 return true;
136 }
137
138 if (fd_ == nullptr || !(mode_ & (WRITE | APPEND))) {
139 return false;
140 }
141
142 size_t count = fwrite(data, size, 1, fd_);
143 return count == 1;
144 }
145
Flush()146 void File::Flush()
147 {
148 if ((mode_ & (WRITE | APPEND)) && fd_ != nullptr) {
149 fflush(fd_);
150 }
151 }
152
Reset()153 bool File::Reset()
154 {
155 if (fd_ == nullptr) {
156 return false;
157 }
158
159 return fseek(fd_, 0, SEEK_SET) == 0;
160 }
161
Skip(long size)162 bool File::Skip(long size)
163 {
164 if (fd_ == nullptr) {
165 return false;
166 }
167
168 return fseek(fd_, size, SEEK_CUR) == 0;
169 }
170
Close()171 void File::Close()
172 {
173 if (fd_ != nullptr) {
174 fclose(fd_);
175 fd_ = nullptr;
176 }
177 }
178 }
179 }
180