1 /* libs/graphics/ports/SkOSFile_stdio.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include "SkOSFile.h"
19
20 #ifndef SK_BUILD_FOR_BREW
21
22 #include <stdio.h>
23 #include <errno.h>
24
sk_fopen(const char path[],SkFILE_Flags flags)25 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags)
26 {
27 char perm[4];
28 char* p = perm;
29
30 if (flags & kRead_SkFILE_Flag)
31 *p++ = 'r';
32 if (flags & kWrite_SkFILE_Flag)
33 *p++ = 'w';
34 *p++ = 'b';
35 *p = 0;
36
37 SkFILE* f = (SkFILE*)::fopen(path, perm);
38 #if 0
39 if (NULL == f)
40 SkDebugf("sk_fopen failed for %s (%s), errno=%s\n", path, perm, strerror(errno));
41 #endif
42 return f;
43 }
44
sk_fgetsize(SkFILE * f)45 size_t sk_fgetsize(SkFILE* f)
46 {
47 SkASSERT(f);
48
49 size_t curr = ::ftell((FILE*)f); // remember where we are
50 ::fseek((FILE*)f, 0, SEEK_END); // go to the end
51 size_t size = ::ftell((FILE*)f); // record the size
52 ::fseek((FILE*)f, (long)curr, SEEK_SET); // go back to our prev loc
53 return size;
54 }
55
sk_frewind(SkFILE * f)56 bool sk_frewind(SkFILE* f)
57 {
58 SkASSERT(f);
59 ::rewind((FILE*)f);
60 // ::fseek((FILE*)f, 0, SEEK_SET);
61 return true;
62 }
63
sk_fread(void * buffer,size_t byteCount,SkFILE * f)64 size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f)
65 {
66 SkASSERT(f);
67 if (buffer == NULL)
68 {
69 size_t curr = ::ftell((FILE*)f);
70 if ((long)curr == -1) {
71 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof((FILE*)f), ferror((FILE*)f)));
72 return 0;
73 }
74 // ::fseek((FILE*)f, (long)(curr + byteCount), SEEK_SET);
75 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR);
76 if (err != 0) {
77 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n",
78 byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err));
79 return 0;
80 }
81 return byteCount;
82 }
83 else
84 return ::fread(buffer, 1, byteCount, (FILE*)f);
85 }
86
sk_fwrite(const void * buffer,size_t byteCount,SkFILE * f)87 size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f)
88 {
89 SkASSERT(f);
90 return ::fwrite(buffer, 1, byteCount, (FILE*)f);
91 }
92
sk_fflush(SkFILE * f)93 void sk_fflush(SkFILE* f)
94 {
95 SkASSERT(f);
96 ::fflush((FILE*)f);
97 }
98
sk_fclose(SkFILE * f)99 void sk_fclose(SkFILE* f)
100 {
101 SkASSERT(f);
102 ::fclose((FILE*)f);
103 }
104
105 #endif
106
107