Opera 12.15 Source Code
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.

BufferedLowLevelFile.h 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. **
  3. ** Copyright (C) 2004-2011 Opera Software ASA. All rights reserved.
  4. **
  5. ** This file is part of the Opera web browser.
  6. ** It may not be distributed under any circumstances.
  7. */
  8. #ifndef OPBUFFEREDLOWLEVELFILE_H
  9. #define OPBUFFEREDLOWLEVELFILE_H
  10. #include "modules/search_engine/FileWrapper.h"
  11. /** Buffering wrapper class for OpLowLevelFile's.
  12. *
  13. * This class can be wrapped around any OpLowLevelFile to perform buffered
  14. * reads aligned on block boundaries. It may speed up file usage on Windows.
  15. */
  16. class BufferedLowLevelFile
  17. : public FileWrapper
  18. {
  19. public:
  20. /** Create an BufferedLowLevelFile object.
  21. *
  22. * This does not create or open the file (but the supplied wrapped_file
  23. * may be already created or open).
  24. *
  25. * @param wrapped_file The normal, unbuffered OpLowLevelFile that will handle actual I/O.
  26. * @param status set to OK, or error code if the return value is NULL.
  27. * @param buffer_size The size of the buffer. Optimally it should be a power of 2
  28. * @param transfer_ownership If TRUE, on success of this call the ownership of wrapped_file
  29. * is transferred to the new BufferedLowLevelFile, which becomes responsible for
  30. * deallocating it in the destructor. In this case the wrapped_file object must have
  31. * been allocated using OP_NEW.
  32. * @return The new BufferedLowLevelFile, or NULL on error
  33. */
  34. static BufferedLowLevelFile* Create(OpLowLevelFile* wrapped_file,
  35. OP_STATUS& status,
  36. OpFileLength buffer_size,
  37. BOOL transfer_ownership = TRUE);
  38. ~BufferedLowLevelFile();
  39. OP_STATUS Open(int mode);
  40. /** OPPS! Currently not posix compatible. Returns Eof()==TRUE when positioned at end of file,
  41. * not after trying to read past the end. */
  42. BOOL Eof() const;
  43. OP_STATUS Write(const void* data, OpFileLength len);
  44. OP_STATUS Read(void* data,
  45. OpFileLength len,
  46. OpFileLength* bytes_read);
  47. OP_STATUS ReadLine(char** data);
  48. OpLowLevelFile* CreateCopy();
  49. OpLowLevelFile* CreateTempFile(const uni_char* prefix);
  50. OP_STATUS SafeClose();
  51. OP_STATUS GetFilePos(OpFileLength* pos) const;
  52. OP_STATUS GetFileLength(OpFileLength* len) const;
  53. OP_STATUS Close();
  54. OP_STATUS Flush();
  55. /** OPPS! Currently not posix compatible. Does not allow setting a position beyond the end of the file */
  56. OP_STATUS SetFilePos(OpFileLength pos,
  57. OpSeekMode mode = SEEK_FROM_START);
  58. OP_STATUS SetFileLength(OpFileLength len);
  59. private:
  60. BufferedLowLevelFile();
  61. BufferedLowLevelFile(OpLowLevelFile*, OpFileLength, unsigned char*, BOOL);
  62. enum IOop {
  63. IO_unknown = 3,
  64. IO_read = 1,
  65. IO_write = 2
  66. };
  67. unsigned char* const m_buffer;
  68. const OpFileLength m_buffer_size;
  69. mutable OpFileLength m_buffer_start;
  70. mutable OpFileLength m_buffer_end; // non-inclusive
  71. mutable OpFileLength m_physical_file_pos;
  72. mutable OpFileLength m_virtual_file_pos;
  73. mutable OpFileLength m_file_length;
  74. IOop m_last_IO_operation;
  75. CHECK_RESULT(OP_STATUS EnsureValidFileLength() const);
  76. CHECK_RESULT(OP_STATUS EnsureValidVirtualFilePos() const);
  77. CHECK_RESULT(OP_STATUS EnsureValidPhysicalFilePos() const);
  78. CHECK_RESULT(OP_STATUS EnsurePhysicalFilePos(OpFileLength pos, IOop operation));
  79. CHECK_RESULT(OP_STATUS BufferVirtualFilePos());
  80. CHECK_RESULT(OP_STATUS BufferedRead(void* data, OpFileLength len, OpFileLength* bytes_read));
  81. };
  82. #endif // !OPBUFFEREDLOWLEVELFILE_H