Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: src/core/SkFDStream.cpp

Issue 15298009: Change SkStream. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Clean up, address comments. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/core/SkData.cpp ('k') | src/core/SkStream.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
OLDNEW
« no previous file with comments | « src/core/SkData.cpp ('k') | src/core/SkStream.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698