hfile.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef HV_FILE_H_
  2. #define HV_FILE_H_
  3. #include <string> // for std::string
  4. #include "hplatform.h" // for stat
  5. #include "hbuf.h" // for HBuf
  6. class HFile {
  7. public:
  8. HFile() {
  9. filepath[0] = '\0';
  10. fp = NULL;
  11. }
  12. ~HFile() {
  13. close();
  14. }
  15. int open(const char* filepath, const char* mode) {
  16. close();
  17. strncpy(this->filepath, filepath, MAX_PATH - 1);
  18. fp = fopen(filepath, mode);
  19. return fp ? 0 : errno;
  20. }
  21. void close() {
  22. if (fp) {
  23. fclose(fp);
  24. fp = NULL;
  25. }
  26. }
  27. bool isopen() {
  28. return fp != NULL;
  29. }
  30. int remove() {
  31. close();
  32. return ::remove(filepath);
  33. }
  34. int rename(const char* newpath) {
  35. close();
  36. return ::rename(filepath, newpath);
  37. }
  38. size_t read(void* ptr, size_t len) {
  39. return fread(ptr, 1, len, fp);
  40. }
  41. size_t write(const void* ptr, size_t len) {
  42. return fwrite(ptr, 1, len, fp);
  43. }
  44. size_t write(const std::string& str) {
  45. return write(str.c_str(), str.length());
  46. }
  47. int seek(size_t offset, int whence = SEEK_SET) {
  48. return fseek(fp, offset, whence);
  49. }
  50. int tell() {
  51. return ftell(fp);
  52. }
  53. int flush() {
  54. return fflush(fp);
  55. }
  56. static size_t size(const char* filepath) {
  57. struct stat st;
  58. memset(&st, 0, sizeof(st));
  59. stat(filepath, &st);
  60. return st.st_size;
  61. }
  62. size_t size() {
  63. return HFile::size(filepath);
  64. }
  65. size_t readall(HBuf& buf) {
  66. size_t filesize = size();
  67. if (filesize == 0) return 0;
  68. buf.resize(filesize);
  69. return fread(buf.base, 1, filesize, fp);
  70. }
  71. size_t readall(std::string& str) {
  72. size_t filesize = size();
  73. if (filesize == 0) return 0;
  74. str.resize(filesize);
  75. return fread((void*)str.data(), 1, filesize, fp);
  76. }
  77. bool readline(std::string& str) {
  78. str.clear();
  79. char ch;
  80. while (fread(&ch, 1, 1, fp)) {
  81. if (ch == '\n') {
  82. // unix: LF
  83. return true;
  84. }
  85. if (ch == '\r') {
  86. // dos: CRLF
  87. // read LF
  88. if (fread(&ch, 1, 1, fp) && ch != '\n') {
  89. // mac: CR
  90. fseek(fp, -1, SEEK_CUR);
  91. }
  92. return true;
  93. }
  94. str += ch;
  95. }
  96. return str.length() != 0;
  97. }
  98. int readrange(std::string& str, size_t from = 0, size_t to = 0) {
  99. size_t filesize = size();
  100. if (filesize == 0) return 0;
  101. if (to == 0 || to >= filesize) to = filesize - 1;
  102. size_t readbytes = to - from + 1;
  103. str.resize(readbytes);
  104. fseek(fp, from, SEEK_SET);
  105. return fread((void*)str.data(), 1, readbytes, fp);
  106. }
  107. public:
  108. char filepath[MAX_PATH];
  109. FILE* fp;
  110. };
  111. #endif // HV_FILE_H_