OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #ifndef EMBEDDERS_OPENGLUI_EMULATOR_EMULATOR_RESOURCE_H_ |
| 6 #define EMBEDDERS_OPENGLUI_EMULATOR_EMULATOR_RESOURCE_H_ |
| 7 |
| 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> |
| 11 |
| 12 #include "embedders/openglui/common/log.h" |
| 13 #include "embedders/openglui/common/resource.h" |
| 14 |
| 15 class EmulatorResource : public Resource { |
| 16 public: |
| 17 explicit EmulatorResource(const char* path) |
| 18 : base(path), |
| 19 fd_(-1) { |
| 20 } |
| 21 |
| 22 int32_t descriptor() { |
| 23 if (fd_ < 0) { |
| 24 Open(); |
| 25 } |
| 26 return fd_; |
| 27 } |
| 28 |
| 29 off_t length() { |
| 30 if (length_ < 0) { |
| 31 length_ = lseek(fd), 0, SEEK_END); |
| 32 lseek(fd), 0, SEEK_START); |
| 33 } |
| 34 return length_; |
| 35 } |
| 36 |
| 37 int32_t Open() { |
| 38 fd_ = open(path_, 0); |
| 39 if (fd_ >= 0) { |
| 40 return 0; |
| 41 } |
| 42 LOGE("Could not open asset %s", path_); |
| 43 return -1; |
| 44 } |
| 45 |
| 46 void Close() { |
| 47 if (fd_ >= 0) { |
| 48 close(fd_); |
| 49 fd_ = -1; |
| 50 } |
| 51 } |
| 52 |
| 53 int32_t Read(void* buffer, size_t count) { |
| 54 size_t actual = read(asset_, buffer, count); |
| 55 return (actual == count) ? 0 : -1; |
| 56 } |
| 57 |
| 58 private: |
| 59 int fd_; |
| 60 }; |
| 61 |
| 62 #endif // EMBEDDERS_OPENGLUI_EMULATOR_EMULATOR_RESOURCE_H_ |
| 63 |
OLD | NEW |