1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef OHOS_HDI_FILE_H 10 #define OHOS_HDI_FILE_H 11 12 #include <cstdio> 13 #include <string> 14 15 namespace OHOS { 16 namespace HDI { 17 class File { 18 public: 19 File(const std::string &path, unsigned int mode); 20 21 ~File(); 22 23 void OpenByRead(const std::string &path); 24 IsValid()25 inline bool IsValid() const 26 { 27 return fd_ != nullptr; 28 } 29 GetPath()30 inline std::string GetPath() const 31 { 32 return path_; 33 } 34 35 char GetChar(); 36 37 char PeekChar(); 38 39 bool IsEof() const; 40 GetCharLineNumber()41 inline size_t GetCharLineNumber() const 42 { 43 return lineNo_; 44 } 45 GetCharColumnNumber()46 inline size_t GetCharColumnNumber() const 47 { 48 return columnNo_; 49 } 50 51 size_t ReadData(void *data, size_t size) const; 52 53 bool WriteData(const void *data, size_t size) const; 54 55 void Flush(); 56 57 bool Reset(); 58 59 bool Skip(long size); 60 61 void Close(); 62 63 static bool CreateParentDir(const std::string &path); 64 65 static std::string AdapterPath(const std::string &path); 66 67 static std::string AdapterRealPath(const std::string &path); 68 69 static std::string RealPath(const std::string &path); 70 71 static bool CheckValid(const std::string &path); 72 73 size_t GetHashKey(); 74 75 static constexpr unsigned int READ = 0x1; 76 static constexpr unsigned int WRITE = 0x2; 77 static constexpr unsigned int APPEND = 0x4; 78 79 private: 80 size_t Read(); 81 82 static constexpr int BUFFER_SIZE = 1024; 83 84 char buffer_[BUFFER_SIZE] = {0}; 85 size_t size_ = 0; 86 size_t position_ = 0; 87 size_t columnNo_ = 1; 88 size_t lineNo_ = 1; 89 bool isEof_ = false; 90 bool isError_ = false; 91 92 FILE *fd_ = nullptr; 93 std::string path_; 94 unsigned int mode_ = 0; 95 }; 96 } // namespace HDI 97 } // namespace OHOS 98 99 #endif // OHOS_HDI_FILE_H