StreamFile.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "StreamFile.h"
  29. #include "../../Include/Rocket/Core/FileInterface.h"
  30. #include "../../Include/Rocket/Core.h"
  31. namespace Rocket {
  32. namespace Core {
  33. StreamFile::StreamFile()
  34. {
  35. file_handle = 0;
  36. length = 0;
  37. }
  38. StreamFile::~StreamFile()
  39. {
  40. if (file_handle)
  41. Close();
  42. }
  43. /// Attempts to open the stream pointing at a given URL.
  44. bool StreamFile::Open(const String& path)
  45. {
  46. String url_safe_path = path.Replace(":", "|");
  47. SetStreamDetails(URL(url_safe_path), Stream::MODE_READ);
  48. if (file_handle)
  49. Close();
  50. // Fix the path if a leading colon has been replaced with a pipe.
  51. String fixed_path = path.Replace("|", ":");
  52. file_handle = GetFileInterface()->Open(fixed_path);
  53. if (!file_handle)
  54. {
  55. Log::Message(Log::LT_WARNING, "Unable to open file %s.", fixed_path.CString());
  56. return false;
  57. }
  58. GetLength();
  59. return true;
  60. }
  61. // Closes the stream.
  62. void StreamFile::Close()
  63. {
  64. if (file_handle)
  65. {
  66. GetFileInterface()->Close(file_handle);
  67. file_handle = 0;
  68. }
  69. length = 0;
  70. }
  71. /// Returns the size of this stream (in bytes).
  72. size_t StreamFile::Length() const
  73. {
  74. return length;
  75. }
  76. // Returns the position of the stream pointer (in bytes).
  77. size_t StreamFile::Tell() const
  78. {
  79. return GetFileInterface()->Tell(file_handle);
  80. }
  81. // Sets the stream position (in bytes).
  82. bool StreamFile::Seek(long offset, int origin) const
  83. {
  84. return GetFileInterface()->Seek(file_handle, offset, origin);
  85. }
  86. // Read from the stream.
  87. size_t StreamFile::Read(void* buffer, size_t bytes) const
  88. {
  89. return GetFileInterface()->Read(buffer, bytes, file_handle);
  90. }
  91. // Write to the stream at the current position.
  92. size_t StreamFile::Write(const void* ROCKET_UNUSED_PARAMETER(buffer), size_t ROCKET_UNUSED_PARAMETER(bytes))
  93. {
  94. ROCKET_UNUSED(buffer);
  95. ROCKET_UNUSED(bytes);
  96. ROCKET_ERROR;
  97. return 0;
  98. }
  99. // Truncate the stream to the specified length.
  100. size_t StreamFile::Truncate(size_t ROCKET_UNUSED_PARAMETER(bytes))
  101. {
  102. ROCKET_UNUSED(bytes);
  103. ROCKET_ERROR;
  104. return 0;
  105. }
  106. // Returns true if the stream is ready for reading, false otherwise.
  107. bool StreamFile::IsReadReady()
  108. {
  109. return Tell() < Length();
  110. }
  111. // Returns true if the stream is ready for writing, false otherwise.
  112. bool StreamFile::IsWriteReady()
  113. {
  114. return false;
  115. }
  116. // Determines the length of the stream.
  117. void StreamFile::GetLength()
  118. {
  119. length = GetFileInterface()->Length(file_handle);
  120. }
  121. }
  122. }