aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-02-25 18:17:12 +0000
committerGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-02-25 18:17:12 +0000
commit1a6880597e88ecb81e19df26fed8fb63f42cb627 (patch)
treec8cf3fd69f6e60c2f143dfb8164c5a84d42ccf49 /src/core
parent12d588a7f28e18f87f5f9a6eb57fc1f5137a8d28 (diff)
move SkFDStream out of images into core
git-svn-id: http://skia.googlecode.com/svn/trunk@7846 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/core')
-rw-r--r--src/core/SkFDStream.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/core/SkFDStream.cpp b/src/core/SkFDStream.cpp
new file mode 100644
index 0000000000..d38813c54d
--- /dev/null
+++ b/src/core/SkFDStream.cpp
@@ -0,0 +1,91 @@
+
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "SkStream.h"
+#include <unistd.h>
+
+//#define TRACE_FDSTREAM
+
+SkFDStream::SkFDStream(int fileDesc, bool closeWhenDone)
+ : fFD(fileDesc), fCloseWhenDone(closeWhenDone) {
+}
+
+SkFDStream::~SkFDStream() {
+ if (fFD >= 0 && fCloseWhenDone) {
+ ::close(fFD);
+ }
+}
+
+bool SkFDStream::rewind() {
+ if (fFD >= 0) {
+ off_t value = ::lseek(fFD, 0, SEEK_SET);
+#ifdef TRACE_FDSTREAM
+ if (value) {
+ SkDebugf("xxxxxxxxxxxxxx rewind failed %d\n", value);
+ }
+#endif
+ return value == 0;
+ }
+ return false;
+}
+
+size_t SkFDStream::read(void* buffer, size_t size) {
+ if (fFD >= 0) {
+ if (buffer == NULL && size == 0) { // request total size
+ off_t curr = ::lseek(fFD, 0, SEEK_CUR);
+ if (curr < 0) {
+#ifdef TRACE_FDSTREAM
+ SkDebugf("xxxxxxxxxxxxx lseek failed 0 CURR\n");
+#endif
+ return 0; // error
+ }
+ off_t size = ::lseek(fFD, 0, SEEK_END);
+ if (size < 0) {
+#ifdef TRACE_FDSTREAM
+ SkDebugf("xxxxxxxxxxxxx lseek failed 0 END\n");
+#endif
+ size = 0; // error
+ }
+ if (::lseek(fFD, curr, SEEK_SET) != curr) {
+ // can't restore, error
+#ifdef TRACE_FDSTREAM
+ SkDebugf("xxxxxxxxxxxxx lseek failed %d SET\n", curr);
+#endif
+ return 0;
+ }
+ return (size_t) size;
+ } else if (NULL == buffer) { // skip
+ off_t oldCurr = ::lseek(fFD, 0, SEEK_CUR);
+ if (oldCurr < 0) {
+#ifdef TRACE_FDSTREAM
+ SkDebugf("xxxxxxxxxxxxx lseek1 failed %d CUR\n", oldCurr);
+#endif
+ return 0; // error;
+ }
+ off_t newCurr = ::lseek(fFD, size, SEEK_CUR);
+ if (newCurr < 0) {
+#ifdef TRACE_FDSTREAM
+ SkDebugf("xxxxxxxxxxxxx lseek2 failed %d CUR\n", newCurr);
+#endif
+ return 0; // error;
+ }
+ // return the actual amount we skipped
+ return (size_t) (newCurr - oldCurr);
+ } else { // read
+ ssize_t actual = ::read(fFD, buffer, size);
+ // our API can't return an error, so we return 0
+ if (actual < 0) {
+#ifdef TRACE_FDSTREAM
+ SkDebugf("xxxxxxxxxxxxx read failed %d actual %d\n", size, actual);
+#endif
+ actual = 0;
+ }
+ return actual;
+ }
+ }
+ return 0;
+}