hfile.h 2.9 KB

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