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 "util/string.h" 14 15 namespace OHOS { 16 namespace HDI { 17 class File { 18 public: 19 File(const String& path, unsigned int mode); 20 21 ~File(); 22 IsValid()23 inline bool IsValid() const 24 { 25 return fd_ != nullptr; 26 } 27 GetPath()28 inline String GetPath() const 29 { 30 return path_; 31 } 32 33 char GetChar(); 34 35 char PeekChar(); 36 37 bool IsEof() const; 38 GetCharLineNumber()39 inline int GetCharLineNumber() const 40 { 41 return lineNo_; 42 } 43 GetCharColumnNumber()44 inline int GetCharColumnNumber() const 45 { 46 return columnNo_; 47 } 48 49 bool ReadData(void* data, size_t size) const; 50 51 bool WriteData(const void* data, size_t size) const; 52 53 void Flush(); 54 55 bool Reset(); 56 57 bool Skip(long size); 58 59 void Close(); 60 61 static bool CreateParentDir(const String& path); 62 63 static String AdapterPath(const String& path); 64 65 size_t GetHashKey(); 66 67 static constexpr unsigned int READ = 0x1; 68 static constexpr unsigned int WRITE = 0x2; 69 static constexpr unsigned int APPEND = 0x4; 70 71 #ifndef __MINGW32__ 72 static constexpr char pathSeparator = '/'; 73 #else 74 static constexpr char pathSeparator = '\\'; 75 #endif 76 77 private: 78 int Read(); 79 80 bool CheckValid(const String& path); 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 String path_; 94 unsigned int mode_ = 0; 95 }; 96 } // namespace HDI 97 } // namespace OHOS 98 99 #endif // OHOS_HDI_FILE_H