File.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright @ 2015 Hangzhou Topzen Ltd.
  3. // Author: Zeng Hanyang (zenghy@es.zju.edu.cn) @ 2015-05
  4. //
  5. #ifndef _FILE_H
  6. #define _FILE_H
  7. #include "Types.h"
  8. namespace tzc {
  9. class DECLDLL File {
  10. public:
  11. typedef enum {
  12. OpenModeRead = 0x01,
  13. OpenModeWrite = 0x02,
  14. OpenModeAppend = 0x04,
  15. OpenModeBinary = 0x08,
  16. OpenModeText = 0x10, // default in stream file in windows
  17. OpenModeSync = 0x20
  18. } FileOpenMode;
  19. typedef enum {
  20. SeekPosSet = 0,
  21. SeekPosCur,
  22. SeekPosEnd
  23. } FileSeekPos;
  24. #if defined(WIN32) || defined(WINDOWS)
  25. typedef HANDLE FileHandle;
  26. #define INVALID_FILE_HANDLE (INVALID_HANDLE_VALUE)
  27. #elif defined(LINUX)
  28. typedef int FileHandle;
  29. #define INVALID_FILE_HANDLE (-1)
  30. #endif
  31. protected:
  32. File(const std::string &);
  33. File(); // tmpfile
  34. public:
  35. virtual ~File();
  36. //return 0 for success, -1 for failed
  37. virtual TZ_Int32 Open(TZ_Uint32 openMode = OpenModeRead | OpenModeWrite
  38. | OpenModeSync) = 0;
  39. virtual TZ_Int32 Close(void) = 0;
  40. // return the real Bytes, negative number for failed
  41. virtual TZ_Int32 Read(TZ_BYTE *, TZ_Uint32) = 0;
  42. virtual TZ_Int32 Write(const TZ_BYTE *, TZ_Uint32) = 0;
  43. // return 0 for success, -1 for failed, for stream file
  44. virtual TZ_Int32 ReadLine(TZ_CHAR *, TZ_Uint32) = 0;
  45. virtual TZ_Int32 WriteLine(const TZ_CHAR *) = 0;
  46. // 0 for success, -1 for failed
  47. virtual TZ_Int32 Seek(TZ_Int64, FileSeekPos) = 0;
  48. // -1 for failed, otherwise return current offset
  49. virtual TZ_Int64 Tell(void) = 0;
  50. virtual TZ_Int64 Truncate(TZ_Uint64 newSize) = 0;
  51. // returns zero on success, otherwise returns -1
  52. virtual TZ_INT Lock(TZ_BOOL shared = FALSE, TZ_BOOL blocking = FALSE) = 0;
  53. // returns zero on success, otherwise returns -1
  54. virtual TZ_INT Unlock() = 0;
  55. // -1 for failed, otherwise return current size
  56. TZ_Int64 GetFileSize(void);
  57. const std::string & GetFilePath() const;
  58. // none-zero for in the end, zero for not
  59. TZ_BOOL IsEndOfFile(void);
  60. public:
  61. // delete file, 0 for succeed, -1 for failed
  62. static TZ_INT Remove(const std::string & path);
  63. static TZ_INT Rename(const std::string & origin, const std::string & target);
  64. protected:
  65. std::string m_filePath;
  66. TZ_Uint32 m_openMode;
  67. }; // end of class File
  68. class DECLDLL PrimeFile : public File
  69. {
  70. public:
  71. PrimeFile(const std::string &);
  72. PrimeFile();
  73. virtual ~PrimeFile();
  74. virtual TZ_Int32 Open(TZ_Uint32 openMode = OpenModeRead | OpenModeWrite
  75. | OpenModeSync);
  76. virtual TZ_Int32 Close(void);
  77. virtual TZ_Int32 Read(TZ_BYTE *, TZ_Uint32);
  78. virtual TZ_Int32 Write(const TZ_BYTE *, TZ_Uint32);
  79. virtual TZ_Int32 ReadLine(TZ_CHAR *, TZ_Uint32);
  80. virtual TZ_Int32 WriteLine(const TZ_CHAR *);
  81. virtual TZ_Int32 Seek(TZ_Int64, FileSeekPos);
  82. virtual TZ_Int64 Truncate(TZ_Uint64 newSize);
  83. virtual TZ_INT Lock(TZ_BOOL shared = FALSE, TZ_BOOL blocking = FALSE);
  84. virtual TZ_INT Unlock();
  85. virtual TZ_Int64 Tell(void);
  86. private:
  87. #if defined(WIN32) || defined(WINDOWS)
  88. void GetFileAttr(DWORD & access, DWORD & attr, DWORD & flags);
  89. #elif defined(LINUX)
  90. int GetIntFileMode(void);
  91. #endif
  92. FileHandle m_handle;
  93. };// end of class PrimeFile
  94. class DECLDLL StreamFile : public File
  95. {
  96. public:
  97. StreamFile(const std::string &);
  98. StreamFile();
  99. virtual ~StreamFile();
  100. virtual TZ_Int32 Open(TZ_Uint32 openMode = OpenModeRead | OpenModeWrite
  101. | OpenModeSync);
  102. virtual TZ_Int32 Close(void);
  103. virtual TZ_Int32 Read(TZ_BYTE *, TZ_Uint32);
  104. virtual TZ_Int32 Write(const TZ_BYTE *, TZ_Uint32);
  105. virtual TZ_Int32 ReadLine(TZ_CHAR *, TZ_Uint32);
  106. virtual TZ_Int32 WriteLine(const TZ_CHAR *);
  107. virtual TZ_Int32 Seek(TZ_Int64, FileSeekPos);
  108. virtual TZ_Int64 Tell(void);
  109. virtual TZ_Int64 Truncate(TZ_Uint64 newSize);
  110. virtual TZ_INT Lock(TZ_BOOL shared = FALSE, TZ_BOOL blocking = FALSE);
  111. virtual TZ_INT Unlock();
  112. private:
  113. std::string getStrFileMode(void);
  114. FILE * m_file;
  115. };// end of class StreamFile
  116. } // end of namespace tzc
  117. #endif /*-----#ifndef _FILE_H-----*/