blob: 40c87455529878c8a56f241ade644558eff178e9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkOSFile.h"
11
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include <errno.h>
epoger@google.come8ebeb12012-10-29 16:42:11 +000013#include <stdio.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16
17#ifdef _WIN32
18#include <direct.h>
19#include <io.h>
20#else
21#include <unistd.h>
22#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
24SkFILE* sk_fopen(const char path[], SkFILE_Flags flags)
25{
26 char perm[4];
27 char* p = perm;
28
29 if (flags & kRead_SkFILE_Flag)
30 *p++ = 'r';
31 if (flags & kWrite_SkFILE_Flag)
32 *p++ = 'w';
33 *p++ = 'b';
34 *p = 0;
35
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000036 //TODO: on Windows fopen is just ASCII or the current code page,
37 //convert to utf16 and use _wfopen
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 SkFILE* f = (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}
45
humper@google.com18a48c32013-01-14 19:42:08 +000046char* sk_fgets(char* str, int size, SkFILE* f) {
47 return ::fgets(str, size, (FILE *)f);
humper@google.com7af56be2013-01-14 18:49:19 +000048}
49
humper@google.com18a48c32013-01-14 19:42:08 +000050
humper@google.com7af56be2013-01-14 18:49:19 +000051int sk_feof(SkFILE *f) {
humper@google.comd386b0e2013-01-14 22:01:14 +000052 // no :: namespace qualifier because it breaks android
53 return feof((FILE *)f);
humper@google.com7af56be2013-01-14 18:49:19 +000054}
55
reed@android.com8a1c16f2008-12-17 15:59:43 +000056size_t sk_fgetsize(SkFILE* f)
57{
58 SkASSERT(f);
59
vandebo@chromium.org6390c722012-03-28 21:03:22 +000060 long curr = ::ftell((FILE*)f); // remember where we are
61 if (curr < 0) {
62 return 0;
63 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 ::fseek((FILE*)f, 0, SEEK_END); // go to the end
vandebo@chromium.org6390c722012-03-28 21:03:22 +000065 long size = ::ftell((FILE*)f); // record the size
66 if (size < 0) {
67 size = 0;
68 }
69 ::fseek((FILE*)f, curr, SEEK_SET); // go back to our prev loc
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 return size;
71}
72
73bool sk_frewind(SkFILE* f)
74{
75 SkASSERT(f);
76 ::rewind((FILE*)f);
77// ::fseek((FILE*)f, 0, SEEK_SET);
78 return true;
79}
80
81size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f)
82{
83 SkASSERT(f);
84 if (buffer == NULL)
85 {
86 size_t curr = ::ftell((FILE*)f);
87 if ((long)curr == -1) {
88 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof((FILE*)f), ferror((FILE*)f)));
89 return 0;
90 }
91 // ::fseek((FILE*)f, (long)(curr + byteCount), SEEK_SET);
92 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR);
93 if (err != 0) {
94 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));
96 return 0;
97 }
98 return byteCount;
99 }
100 else
101 return ::fread(buffer, 1, byteCount, (FILE*)f);
102}
103
104size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f)
105{
106 SkASSERT(f);
107 return ::fwrite(buffer, 1, byteCount, (FILE*)f);
108}
109
110void sk_fflush(SkFILE* f)
111{
112 SkASSERT(f);
113 ::fflush((FILE*)f);
114}
115
116void sk_fclose(SkFILE* f)
117{
118 SkASSERT(f);
119 ::fclose((FILE*)f);
120}
121
epoger@google.come8ebeb12012-10-29 16:42:11 +0000122bool sk_exists(const char *path)
123{
124#ifdef _WIN32
125 return (0 == _access(path, 0));
126#else
127 return (0 == access(path, 0));
128#endif
129}
130
131bool sk_isdir(const char *path)
132{
133 struct stat status;
134 if (0 != stat(path, &status)) {
135 return false;
136 }
scroggo@google.com6e725162012-11-01 16:28:23 +0000137 return SkToBool(status.st_mode & S_IFDIR);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000138}
139
140bool sk_mkdir(const char* path)
141{
142 if (sk_isdir(path)) {
143 return true;
144 }
145 if (sk_exists(path)) {
146 fprintf(stderr,
147 "sk_mkdir: path '%s' already exists but is not a directory\n",
148 path);
149 return false;
150 }
151
152 int retval;
153#ifdef _WIN32
154 retval = _mkdir(path);
155#else
156 retval = mkdir(path, 0777);
157#endif
158 if (0 == retval) {
159 return true;
160 } else {
161 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path);
162 return false;
163 }
164}