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