OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 | |
10 #include "SkOSFile.h" | 8 #include "SkOSFile.h" |
11 | 9 |
12 #include <errno.h> | 10 #include <errno.h> |
13 #include <stdio.h> | 11 #include <stdio.h> |
14 #include <sys/stat.h> | 12 #include <sys/stat.h> |
15 #include <sys/types.h> | 13 #include <sys/types.h> |
16 | 14 |
17 #ifdef _WIN32 | 15 #ifdef _WIN32 |
18 #include <direct.h> | 16 #include <direct.h> |
19 #include <io.h> | 17 #include <io.h> |
20 #else | 18 #else |
21 #include <unistd.h> | 19 #include <unistd.h> |
22 #endif | 20 #endif |
23 | 21 |
24 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) | 22 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) { |
25 { | |
26 char perm[4]; | 23 char perm[4]; |
27 char* p = perm; | 24 char* p = perm; |
28 | 25 |
29 if (flags & kRead_SkFILE_Flag) | 26 if (flags & kRead_SkFILE_Flag) { |
30 *p++ = 'r'; | 27 *p++ = 'r'; |
31 if (flags & kWrite_SkFILE_Flag) | 28 } |
| 29 if (flags & kWrite_SkFILE_Flag) { |
32 *p++ = 'w'; | 30 *p++ = 'w'; |
| 31 } |
33 *p++ = 'b'; | 32 *p++ = 'b'; |
34 *p = 0; | 33 *p = 0; |
35 | 34 |
36 //TODO: on Windows fopen is just ASCII or the current code page, | 35 //TODO: on Windows fopen is just ASCII or the current code page, |
37 //convert to utf16 and use _wfopen | 36 //convert to utf16 and use _wfopen |
38 SkFILE* f = (SkFILE*)::fopen(path, perm); | 37 return (SkFILE*)::fopen(path, perm); |
39 #if 0 | |
40 if (NULL == f) | |
41 SkDebugf("sk_fopen failed for %s (%s), errno=%s\n", path, perm, strerror
(errno)); | |
42 #endif | |
43 return f; | |
44 } | 38 } |
45 | 39 |
46 char* sk_fgets(char* str, int size, SkFILE* f) { | 40 char* sk_fgets(char* str, int size, SkFILE* f) { |
47 return ::fgets(str, size, (FILE *)f); | 41 return ::fgets(str, size, (FILE *)f); |
48 } | 42 } |
49 | 43 |
50 | |
51 int sk_feof(SkFILE *f) { | 44 int sk_feof(SkFILE *f) { |
52 // no :: namespace qualifier because it breaks android | 45 // no :: namespace qualifier because it breaks android |
53 return feof((FILE *)f); | 46 return feof((FILE *)f); |
54 } | 47 } |
55 | 48 |
56 size_t sk_fgetsize(SkFILE* f) | 49 size_t sk_fgetsize(SkFILE* f) { |
57 { | |
58 SkASSERT(f); | 50 SkASSERT(f); |
59 | 51 |
60 long curr = ::ftell((FILE*)f); // remember where we are | 52 long curr = ::ftell((FILE*)f); // remember where we are |
61 if (curr < 0) { | 53 if (curr < 0) { |
62 return 0; | 54 return 0; |
63 } | 55 } |
64 ::fseek((FILE*)f, 0, SEEK_END); // go to the end | 56 |
65 long size = ::ftell((FILE*)f); // record the size | 57 ::fseek((FILE*)f, 0, SEEK_END); // go to the end |
| 58 long size = ::ftell((FILE*)f); // record the size |
66 if (size < 0) { | 59 if (size < 0) { |
67 size = 0; | 60 size = 0; |
68 } | 61 } |
69 ::fseek((FILE*)f, curr, SEEK_SET); // go back to our prev loc | 62 |
| 63 ::fseek((FILE*)f, curr, SEEK_SET); // go back to our prev location |
70 return size; | 64 return size; |
71 } | 65 } |
72 | 66 |
73 bool sk_frewind(SkFILE* f) | 67 bool sk_frewind(SkFILE* f) { |
74 { | |
75 SkASSERT(f); | 68 SkASSERT(f); |
76 ::rewind((FILE*)f); | 69 ::rewind((FILE*)f); |
77 // ::fseek((FILE*)f, 0, SEEK_SET); | |
78 return true; | 70 return true; |
79 } | 71 } |
80 | 72 |
81 size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f) | 73 size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f) { |
82 { | |
83 SkASSERT(f); | 74 SkASSERT(f); |
84 if (buffer == NULL) | 75 if (buffer == NULL) { |
85 { | |
86 size_t curr = ::ftell((FILE*)f); | 76 size_t curr = ::ftell((FILE*)f); |
87 if ((long)curr == -1) { | 77 if ((long)curr == -1) { |
88 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f,
feof((FILE*)f), ferror((FILE*)f))); | 78 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f,
feof((FILE*)f), ferror((FILE*)f))); |
89 return 0; | 79 return 0; |
90 } | 80 } |
91 // ::fseek((FILE*)f, (long)(curr + byteCount), SEEK_SET); | |
92 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR); | 81 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR); |
93 if (err != 0) { | 82 if (err != 0) { |
94 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d
returned:%d\n", | 83 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d
returned:%d\n", |
95 byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err))
; | 84 byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err))
; |
96 return 0; | 85 return 0; |
97 } | 86 } |
98 return byteCount; | 87 return byteCount; |
99 } | 88 } |
100 else | 89 else |
101 return ::fread(buffer, 1, byteCount, (FILE*)f); | 90 return ::fread(buffer, 1, byteCount, (FILE*)f); |
102 } | 91 } |
103 | 92 |
104 size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) | 93 size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) { |
105 { | |
106 SkASSERT(f); | 94 SkASSERT(f); |
107 return ::fwrite(buffer, 1, byteCount, (FILE*)f); | 95 return ::fwrite(buffer, 1, byteCount, (FILE*)f); |
108 } | 96 } |
109 | 97 |
110 void sk_fflush(SkFILE* f) | 98 void sk_fflush(SkFILE* f) { |
111 { | |
112 SkASSERT(f); | 99 SkASSERT(f); |
113 ::fflush((FILE*)f); | 100 ::fflush((FILE*)f); |
114 } | 101 } |
115 | 102 |
116 void sk_fclose(SkFILE* f) | 103 bool sk_fseek(SkFILE* f, size_t byteCount) { |
117 { | 104 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_SET); |
| 105 return err == 0; |
| 106 } |
| 107 |
| 108 bool sk_fmove(SkFILE* f, long byteCount) { |
| 109 int err = ::fseek((FILE*)f, byteCount, SEEK_CUR); |
| 110 return err == 0; |
| 111 } |
| 112 |
| 113 size_t sk_ftell(SkFILE* f) { |
| 114 long curr = ::ftell((FILE*)f); |
| 115 if (curr < 0) { |
| 116 return 0; |
| 117 } |
| 118 return curr; |
| 119 } |
| 120 |
| 121 void sk_fclose(SkFILE* f) { |
118 SkASSERT(f); | 122 SkASSERT(f); |
119 ::fclose((FILE*)f); | 123 ::fclose((FILE*)f); |
120 } | 124 } |
121 | 125 |
122 bool sk_exists(const char *path) | 126 bool sk_exists(const char *path) { |
123 { | |
124 #ifdef _WIN32 | 127 #ifdef _WIN32 |
125 return (0 == _access(path, 0)); | 128 return (0 == _access(path, 0)); |
126 #else | 129 #else |
127 return (0 == access(path, 0)); | 130 return (0 == access(path, 0)); |
128 #endif | 131 #endif |
129 } | 132 } |
130 | 133 |
131 bool sk_isdir(const char *path) | 134 bool sk_isdir(const char *path) { |
132 { | |
133 struct stat status; | 135 struct stat status; |
134 if (0 != stat(path, &status)) { | 136 if (0 != stat(path, &status)) { |
135 return false; | 137 return false; |
136 } | 138 } |
137 return SkToBool(status.st_mode & S_IFDIR); | 139 return SkToBool(status.st_mode & S_IFDIR); |
138 } | 140 } |
139 | 141 |
140 bool sk_mkdir(const char* path) | 142 bool sk_mkdir(const char* path) { |
141 { | |
142 if (sk_isdir(path)) { | 143 if (sk_isdir(path)) { |
143 return true; | 144 return true; |
144 } | 145 } |
145 if (sk_exists(path)) { | 146 if (sk_exists(path)) { |
146 fprintf(stderr, | 147 fprintf(stderr, |
147 "sk_mkdir: path '%s' already exists but is not a directory\n", | 148 "sk_mkdir: path '%s' already exists but is not a directory\n", |
148 path); | 149 path); |
149 return false; | 150 return false; |
150 } | 151 } |
151 | 152 |
152 int retval; | 153 int retval; |
153 #ifdef _WIN32 | 154 #ifdef _WIN32 |
154 retval = _mkdir(path); | 155 retval = _mkdir(path); |
155 #else | 156 #else |
156 retval = mkdir(path, 0777); | 157 retval = mkdir(path, 0777); |
157 #endif | 158 #endif |
158 if (0 == retval) { | 159 if (0 == retval) { |
159 return true; | 160 return true; |
160 } else { | 161 } else { |
161 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); | 162 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); |
162 return false; | 163 return false; |
163 } | 164 } |
164 } | 165 } |
OLD | NEW |