stream.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you
  4. // may not use this file except in compliance with the License. You
  5. // may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License.
  14. #ifndef WEBGL_LOADER_STREAM_H_
  15. #define WEBGL_LOADER_STREAM_H_
  16. #include <stdio.h>
  17. #include <string>
  18. #include <vector>
  19. #include "base.h"
  20. namespace webgl_loader {
  21. // An abstract interface to allow appending bytes to various streams.
  22. class ByteSinkInterface {
  23. public:
  24. virtual void Put(char c) = 0;
  25. virtual size_t PutN(const char* data, size_t len) = 0;
  26. virtual ~ByteSinkInterface() { }
  27. protected:
  28. ByteSinkInterface() { }
  29. private:
  30. // Disallow copy and assignment.
  31. ByteSinkInterface(const ByteSinkInterface&);
  32. void operator=(const ByteSinkInterface&);
  33. };
  34. // None of the concrete implementations actually own the backing data.
  35. // They should be safe to copy.
  36. class NullSink : public ByteSinkInterface {
  37. public:
  38. NullSink() { }
  39. virtual void Put(char) { }
  40. virtual size_t PutN(const char*, size_t len) { return len; }
  41. };
  42. class FileSink : public ByteSinkInterface {
  43. public:
  44. // |fp| is unowned and must not be NULL.
  45. explicit FileSink(FILE* fp)
  46. : fp_(fp) {
  47. }
  48. virtual void Put(char c) {
  49. PutChar(c, fp_);
  50. }
  51. virtual size_t PutN(const char* data, size_t len) {
  52. return fwrite(data, 1, len, fp_);
  53. }
  54. private:
  55. FILE *fp_; // unowned.
  56. };
  57. class VectorSink : public ByteSinkInterface {
  58. public:
  59. // |vec| is unowned and must not be NULL.
  60. explicit VectorSink(std::vector<char>* vec)
  61. : vec_(vec) {
  62. }
  63. virtual void Put(char c) {
  64. vec_->push_back(c);
  65. }
  66. virtual size_t PutN(const char* data, size_t len) {
  67. vec_->insert(vec_->end(), data, data + len);
  68. return len;
  69. }
  70. private:
  71. std::vector<char>* vec_; // unowned.
  72. };
  73. class StringSink : public ByteSinkInterface {
  74. public:
  75. // |str| is unowned and must not be NULL.
  76. explicit StringSink(std::string* str)
  77. : str_(str) {
  78. DCHECK(str != NULL);
  79. }
  80. virtual void Put(char c) {
  81. str_->push_back(c);
  82. }
  83. virtual size_t PutN(const char* data, size_t len) {
  84. str_->append(data, len);
  85. return len;
  86. }
  87. private:
  88. std::string* str_; // unowned.
  89. };
  90. class ByteHistogramSink : public ByteSinkInterface {
  91. public:
  92. // |sink| in unowned and must not be NULL.
  93. explicit ByteHistogramSink(ByteSinkInterface* sink)
  94. : sink_(sink) {
  95. memset(histo_, 0, sizeof(histo_));
  96. }
  97. virtual void Put(char c) {
  98. histo_[static_cast<uint8>(c)]++;
  99. sink_->Put(c);
  100. }
  101. virtual size_t PutN(const char* data, size_t len) {
  102. const char* const end = data + len;
  103. for (const char* iter = data; iter != end; ++iter) {
  104. histo_[static_cast<uint8>(*iter)]++;
  105. }
  106. return sink_->PutN(data, len);
  107. }
  108. const size_t* histo() const {
  109. return histo_;
  110. }
  111. private:
  112. size_t histo_[256];
  113. ByteSinkInterface* sink_; // unowned.
  114. };
  115. // TODO: does it make sense to have a global enum? How should
  116. // new BufferedInput implementations define new error codes?
  117. enum ErrorCode {
  118. kNoError = 0,
  119. kEndOfFile = 1,
  120. kFileError = 2, // TODO: translate errno.
  121. };
  122. // Adapted from ryg's BufferedStream abstraction:
  123. // http://fgiesen.wordpress.com/2011/11/21/buffer-centric-io/
  124. class BufferedInput {
  125. public:
  126. typedef ErrorCode (*Refiller)(BufferedInput*);
  127. BufferedInput(Refiller refiller = RefillZeroes)
  128. : cursor(NULL),
  129. begin_(NULL),
  130. end_(NULL),
  131. refiller_(refiller),
  132. error_(kNoError) {
  133. }
  134. // InitFromMemory.
  135. BufferedInput(const char* data, size_t length)
  136. : cursor(data),
  137. begin_(data),
  138. end_(data + length),
  139. refiller_(RefillEndOfFile),
  140. error_(kNoError) {
  141. }
  142. const char* begin() const {
  143. return begin_;
  144. }
  145. const char* end() const {
  146. return end_;
  147. }
  148. const char* cursor;
  149. ErrorCode error() const {
  150. DCHECK(begin() <= cursor);
  151. DCHECK(cursor <= end());
  152. return error_;
  153. }
  154. ErrorCode Refill() {
  155. DCHECK(begin() <= cursor);
  156. DCHECK(cursor <= end());
  157. if (cursor == end()) {
  158. error_ = refiller_(this);
  159. }
  160. return error_;
  161. }
  162. protected:
  163. static ErrorCode RefillZeroes(BufferedInput* bi) {
  164. static const char kZeroes[64] = { 0 };
  165. bi->cursor = kZeroes;
  166. bi->begin_ = kZeroes;
  167. bi->end_ = kZeroes + sizeof(kZeroes);
  168. return bi->error_;
  169. }
  170. static ErrorCode RefillEndOfFile(BufferedInput* bi) {
  171. return bi->fail(kEndOfFile);
  172. }
  173. ErrorCode fail(ErrorCode why) {
  174. error_ = why;
  175. refiller_ = RefillZeroes;
  176. return Refill();
  177. }
  178. const char* begin_;
  179. const char* end_;
  180. Refiller refiller_;
  181. ErrorCode error_;
  182. private:
  183. // Disallow copy and assign.
  184. BufferedInput(const BufferedInput&);
  185. void operator=(const BufferedInput&);
  186. };
  187. class BufferedInputStream : public BufferedInput {
  188. public:
  189. BufferedInputStream(FILE* fp, char* buf, size_t size)
  190. : BufferedInput(RefillFread),
  191. fp_(fp),
  192. buf_(buf),
  193. size_(size) {
  194. DCHECK(buf != NULL);
  195. // Disable buffering since we're doing it ourselves.
  196. // TODO check error.
  197. setvbuf(fp_, NULL, _IONBF, 0);
  198. cursor = buf;
  199. begin_ = buf;
  200. end_ = buf;
  201. }
  202. protected:
  203. // TODO: figure out how to automate this casting pattern.
  204. static ErrorCode RefillFread(BufferedInput* bi) {
  205. return static_cast<BufferedInputStream*>(bi)->DoRefillFread();
  206. }
  207. private:
  208. ErrorCode DoRefillFread() {
  209. const size_t bytes_read = fread(buf_, 1, size_, fp_);
  210. cursor = begin_;
  211. end_ = begin_ + bytes_read;
  212. if (bytes_read < size_) {
  213. if (feof(fp_)) {
  214. refiller_ = RefillEndOfFile;
  215. } else if (ferror(fp_)) {
  216. return fail(kFileError);
  217. }
  218. }
  219. return kNoError;
  220. }
  221. FILE* fp_;
  222. char* buf_;
  223. size_t size_;
  224. };
  225. } // namespace webgl_loader
  226. #endif // WEBGL_LOADER_STREAM_H_