FileHelper.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "FuncHelper.h"
  25. #include "StringT.h"
  26. #include <unistd.h>
  27. #include <sys/uio.h>
  28. #include <sys/mman.h>
  29. #define INVALID_MAP_ADDR ((PBYTE)(MAP_FAILED))
  30. CString GetCurrentDirectory();
  31. CString GetModuleFileName(pid_t pid = 0);
  32. BOOL SetCurrentPathToModulePath(pid_t pid = 0);
  33. class CFile
  34. {
  35. public:
  36. BOOL Open(LPCTSTR lpszFilePath, int iFlag, mode_t iMode = 0);
  37. BOOL Close();
  38. BOOL Stat(struct stat& st);
  39. BOOL GetSize(SIZE_T& dwSize);
  40. SSIZE_T Read(PVOID pBuffer, SIZE_T dwCount)
  41. {return read(m_fd, pBuffer, dwCount);}
  42. SSIZE_T Write(PVOID pBuffer, SIZE_T dwCount)
  43. {return write(m_fd, pBuffer, dwCount);}
  44. SSIZE_T PRead(PVOID pBuffer, SIZE_T dwCount, SIZE_T dwOffset)
  45. {return pread(m_fd, pBuffer, dwCount, dwOffset);}
  46. SSIZE_T PWrite(PVOID pBuffer, SIZE_T dwCount, SIZE_T dwOffset)
  47. {return pwrite(m_fd, pBuffer, dwCount, dwOffset);}
  48. SSIZE_T ReadV(const iovec* pVec, int iVecCount)
  49. {return readv(m_fd, pVec, iVecCount);}
  50. SSIZE_T WriteV(const iovec* pVec, int iVecCount)
  51. {return writev(m_fd, pVec, iVecCount);}
  52. SSIZE_T Seek(SSIZE_T lOffset, int iWhence)
  53. {return lseek(m_fd, lOffset, iWhence);}
  54. BOOL IsValid() {return IS_VALID_FD(m_fd);}
  55. operator FD () {return m_fd;}
  56. BOOL IsExist() {return IsValid();}
  57. BOOL IsDirectory();
  58. BOOL IsFile();
  59. static BOOL IsExist(LPCTSTR lpszFilePath);
  60. static BOOL IsDirectory(LPCTSTR lpszFilePath);
  61. static BOOL IsFile(LPCTSTR lpszFilePath);
  62. static BOOL IsLink(LPCTSTR lpszFilePath);
  63. public:
  64. CFile(LPCTSTR lpszFilePath = nullptr, int iFlag = O_RDONLY, mode_t iMode = 0)
  65. : m_fd(INVALID_FD)
  66. {
  67. if(lpszFilePath != nullptr)
  68. Open(lpszFilePath, iFlag, iMode);
  69. }
  70. ~CFile()
  71. {
  72. if(IsValid())
  73. Close();
  74. }
  75. private:
  76. FD m_fd;
  77. };
  78. class CFileMapping
  79. {
  80. public:
  81. BOOL Map(LPCTSTR lpszFilePath, SIZE_T dwSize = 0, SIZE_T dwOffset = 0, int iProtected = PROT_READ, int iFlag = MAP_PRIVATE);
  82. BOOL Map(FD fd, SIZE_T dwSize = 0, SIZE_T dwOffset = 0, int iProtected = PROT_READ, int iFlag = MAP_PRIVATE);
  83. BOOL Unmap();
  84. BOOL MSync(int iFlag = MS_SYNC, SIZE_T dwSize = 0);
  85. BOOL IsValid () {return m_pv != INVALID_MAP_ADDR;}
  86. SIZE_T Size () {return m_dwSize;}
  87. LPBYTE Ptr () {return m_pv;}
  88. operator LPBYTE () {return Ptr();}
  89. public:
  90. CFileMapping()
  91. : m_pv(INVALID_MAP_ADDR)
  92. , m_dwSize(0)
  93. {
  94. }
  95. ~CFileMapping()
  96. {
  97. if(IsValid())
  98. Unmap();
  99. }
  100. private:
  101. PBYTE m_pv;
  102. SIZE_T m_dwSize;
  103. };