OLD | NEW |
| (Empty) |
1 | |
2 /* | |
3 * Copyright 2011 Google Inc. | |
4 * | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 */ | |
8 #include "SkStream.h" | |
9 | |
10 #ifdef SK_BUILD_FOR_WIN | |
11 | |
12 // -1 means isValid() will return false | |
13 SkFDStream::SkFDStream(int, bool) : fFD(-1), fCloseWhenDone(false) {} | |
14 SkFDStream::~SkFDStream() {} | |
15 bool SkFDStream::rewind() { return false; } | |
16 size_t SkFDStream::read(void*, size_t) { return 0; } | |
17 | |
18 #else | |
19 | |
20 #include <unistd.h> | |
21 | |
22 //#define TRACE_FDSTREAM | |
23 | |
24 SkFDStream::SkFDStream(int fileDesc, bool closeWhenDone) | |
25 : fFD(fileDesc), fCloseWhenDone(closeWhenDone) { | |
26 } | |
27 | |
28 SkFDStream::~SkFDStream() { | |
29 if (fFD >= 0 && fCloseWhenDone) { | |
30 ::close(fFD); | |
31 } | |
32 } | |
33 | |
34 bool SkFDStream::rewind() { | |
35 if (fFD >= 0) { | |
36 off_t value = ::lseek(fFD, 0, SEEK_SET); | |
37 #ifdef TRACE_FDSTREAM | |
38 if (value) { | |
39 SkDebugf("xxxxxxxxxxxxxx rewind failed %d\n", value); | |
40 } | |
41 #endif | |
42 return value == 0; | |
43 } | |
44 return false; | |
45 } | |
46 | |
47 size_t SkFDStream::read(void* buffer, size_t size) { | |
48 if (fFD >= 0) { | |
49 if (buffer == NULL && size == 0) { // request total size | |
50 off_t curr = ::lseek(fFD, 0, SEEK_CUR); | |
51 if (curr < 0) { | |
52 #ifdef TRACE_FDSTREAM | |
53 SkDebugf("xxxxxxxxxxxxx lseek failed 0 CURR\n"); | |
54 #endif | |
55 return 0; // error | |
56 } | |
57 off_t size = ::lseek(fFD, 0, SEEK_END); | |
58 if (size < 0) { | |
59 #ifdef TRACE_FDSTREAM | |
60 SkDebugf("xxxxxxxxxxxxx lseek failed 0 END\n"); | |
61 #endif | |
62 size = 0; // error | |
63 } | |
64 if (::lseek(fFD, curr, SEEK_SET) != curr) { | |
65 // can't restore, error | |
66 #ifdef TRACE_FDSTREAM | |
67 SkDebugf("xxxxxxxxxxxxx lseek failed %d SET\n", curr); | |
68 #endif | |
69 return 0; | |
70 } | |
71 return (size_t) size; | |
72 } else if (NULL == buffer) { // skip | |
73 off_t oldCurr = ::lseek(fFD, 0, SEEK_CUR); | |
74 if (oldCurr < 0) { | |
75 #ifdef TRACE_FDSTREAM | |
76 SkDebugf("xxxxxxxxxxxxx lseek1 failed %d CUR\n", oldCurr); | |
77 #endif | |
78 return 0; // error; | |
79 } | |
80 off_t newCurr = ::lseek(fFD, size, SEEK_CUR); | |
81 if (newCurr < 0) { | |
82 #ifdef TRACE_FDSTREAM | |
83 SkDebugf("xxxxxxxxxxxxx lseek2 failed %d CUR\n", newCurr); | |
84 #endif | |
85 return 0; // error; | |
86 } | |
87 // return the actual amount we skipped | |
88 return (size_t) (newCurr - oldCurr); | |
89 } else { // read | |
90 ssize_t actual = ::read(fFD, buffer, size); | |
91 // our API can't return an error, so we return 0 | |
92 if (actual < 0) { | |
93 #ifdef TRACE_FDSTREAM | |
94 SkDebugf("xxxxxxxxxxxxx read failed %d actual %d\n", size, actua
l); | |
95 #endif | |
96 actual = 0; | |
97 } | |
98 return actual; | |
99 } | |
100 } | |
101 return 0; | |
102 } | |
103 | |
104 #endif | |
OLD | NEW |