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