You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
4.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_
  11. #define RTC_BASE_SYSTEM_FILE_WRAPPER_H_
  12. #include <stddef.h>
  13. #include <stdio.h>
  14. #include "rtc_base/critical_section.h"
  15. // Implementation that can read (exclusive) or write from/to a file.
  16. namespace webrtc {
  17. // This class is a thin wrapper around FILE*. It's main features are that it
  18. // owns the FILE*, calling fclose on destruction, and that on windows, file
  19. // names passed to the open methods are always treated as utf-8, regardless of
  20. // system code page.
  21. // Most of the methods return only a success/fail indication. When needed, an
  22. // optional argument |int* error| should be added to all methods, in the same
  23. // way as for the OpenWriteOnly methods.
  24. class FileWrapper final {
  25. public:
  26. // Opens a file, in read or write mode. Use the is_open() method on the
  27. // returned object to check if the open operation was successful. On failure,
  28. // and if |error| is non-null, the system errno value is stored at |*error|.
  29. // The file is closed by the destructor.
  30. static FileWrapper OpenReadOnly(const char* file_name_utf8);
  31. static FileWrapper OpenReadOnly(const std::string& file_name_utf8);
  32. static FileWrapper OpenWriteOnly(const char* file_name_utf8,
  33. int* error = nullptr);
  34. static FileWrapper OpenWriteOnly(const std::string& file_name_utf8,
  35. int* error = nullptr);
  36. FileWrapper() = default;
  37. // Takes over ownership of |file|, closing it on destruction. Calling with
  38. // null |file| is allowed, and results in a FileWrapper with is_open() false.
  39. explicit FileWrapper(FILE* file) : file_(file) {}
  40. ~FileWrapper() { Close(); }
  41. // Copying is not supported.
  42. FileWrapper(const FileWrapper&) = delete;
  43. FileWrapper& operator=(const FileWrapper&) = delete;
  44. // Support for move semantics.
  45. FileWrapper(FileWrapper&&);
  46. FileWrapper& operator=(FileWrapper&&);
  47. // Returns true if a file has been opened. If the file is not open, no methods
  48. // but is_open and Close may be called.
  49. bool is_open() const { return file_ != nullptr; }
  50. // Closes the file, and implies Flush. Returns true on success, false if
  51. // writing buffered data fails. On failure, the file is nevertheless closed.
  52. // Calling Close on an already closed file does nothing and returns success.
  53. bool Close();
  54. // Write any buffered data to the underlying file. Returns true on success,
  55. // false on write error. Note: Flushing when closing, is not required.
  56. bool Flush();
  57. // Seeks to the beginning of file. Returns true on success, false on failure,
  58. // e.g., if the underlying file isn't seekable.
  59. bool Rewind() { return SeekTo(0); }
  60. // TODO(nisse): The seek functions are used only by the WavReader. If that
  61. // code is demoted to test code, seek functions can be deleted from this
  62. // utility.
  63. // Seek relative to current file position.
  64. bool SeekRelative(int64_t offset);
  65. // Seek to given position.
  66. bool SeekTo(int64_t position);
  67. // Returns number of bytes read. Short count indicates EOF or error.
  68. size_t Read(void* buf, size_t length);
  69. // If the most recent Read() returned a short count, this methods returns true
  70. // if the short count was due to EOF, and false it it was due to some i/o
  71. // error.
  72. bool ReadEof() const;
  73. // Returns true if all data was successfully written (or buffered), or false
  74. // if there was an error. Writing buffered data can fail later, and is
  75. // reported with return value from Flush or Close.
  76. bool Write(const void* buf, size_t length);
  77. private:
  78. FILE* file_ = nullptr;
  79. };
  80. } // namespace webrtc
  81. #endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_