1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #include "nonstd_stdio.h" |
---|
6 | #include "nonstd.h" |
---|
7 | #include "Convert.h" //strTOwstr() |
---|
8 | #include <common/stl-util.h> |
---|
9 | |
---|
10 | #if defined _WIN32 && !defined SHP |
---|
11 | //<unistd.h> not needed for unlink() |
---|
12 | #include "Shlwapi.h" //PathIsRelative() |
---|
13 | #include <sys/stat.h> //_stat |
---|
14 | #else |
---|
15 | #include <unistd.h> |
---|
16 | #endif |
---|
17 | |
---|
18 | bool fileExists(const char* path) |
---|
19 | { |
---|
20 | //lepiej gdyby uzywalo stat bo mfopen mogloby cos niepotrzebnie wczytywac przy otwarciu pliku ale mfopen wiadomo ze zadziala wszedzie tak samo |
---|
21 | MFILE *f=mfopen(path,FOPEN_READ_BINARY); |
---|
22 | if (f==NULL) return false; |
---|
23 | mfclose(f); |
---|
24 | return true; |
---|
25 | } |
---|
26 | |
---|
27 | bool directoryExists(const char* path) |
---|
28 | { |
---|
29 | struct _stat s; |
---|
30 | if (_stat(path,&s)!=0) return false; |
---|
31 | return S_ISDIR(s.st_mode); |
---|
32 | } |
---|
33 | |
---|
34 | bool makeDirectory(const char* path) |
---|
35 | { |
---|
36 | #ifdef _WIN32 |
---|
37 | return mkdir(path)==0; |
---|
38 | #else |
---|
39 | return mkdir(path,0777)==0; |
---|
40 | #endif |
---|
41 | } |
---|
42 | |
---|
43 | bool makeDirectories(const char* path) |
---|
44 | { |
---|
45 | if (directoryExists(path)) return true; |
---|
46 | string parentdir=getFileDir(path); |
---|
47 | if (!makeDirectories(parentdir.c_str())) return false; |
---|
48 | return makeDirectory(path); |
---|
49 | } |
---|
50 | |
---|
51 | int getFileSize(const char* path) |
---|
52 | { |
---|
53 | int size; |
---|
54 | MFILE *f=mfopen(path,FOPEN_READ_BINARY); |
---|
55 | if (f==NULL) return -1; |
---|
56 | size=getFileSize(f); |
---|
57 | mfclose(f); |
---|
58 | return size; |
---|
59 | } |
---|
60 | |
---|
61 | int getFileSize(MFILE *f) |
---|
62 | { |
---|
63 | int saved_pos = mftell(f); |
---|
64 | mfseek(f, 0, SEEK_END); |
---|
65 | int size = mftell(f); |
---|
66 | mfseek(f, saved_pos, SEEK_SET); |
---|
67 | return size; |
---|
68 | } |
---|
69 | |
---|
70 | bool removeFile(const char* path) |
---|
71 | { |
---|
72 | return _unlink(path)==0; //VS: "The POSIX name is deprecated. Instead, use the ISO C++ conformant name: _unlink" |
---|
73 | } |
---|
74 | |
---|
75 | bool isAbsolutePath(const char* fname) |
---|
76 | { |
---|
77 | if (fname == NULL) return false; //SplitFileSystem never passes NULL but this function is public so we never know |
---|
78 | #if defined _WIN32 && !defined SHP |
---|
79 | #ifdef __BORLANDC__ |
---|
80 | return PathIsRelative(fname) == FALSE; //no wide char for old borland compiler |
---|
81 | #else |
---|
82 | return PathIsRelative(Convert::strTOwstr(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx |
---|
83 | #endif |
---|
84 | #else |
---|
85 | return fname[0] == PATH_SEPARATOR_CHAR; |
---|
86 | #endif |
---|
87 | } |
---|
88 | |
---|
89 | #if defined SHP && defined BADA_API_1 |
---|
90 | |
---|
91 | MFILE *mfopen(const char *path, const char *mode) |
---|
92 | { |
---|
93 | Osp::Io::File *f = new Osp::Io::File(); |
---|
94 | result r = f->Construct(path, mode); |
---|
95 | if (IsFailed(r)) |
---|
96 | { |
---|
97 | delete f; |
---|
98 | f = NULL; |
---|
99 | } |
---|
100 | return f; |
---|
101 | } |
---|
102 | |
---|
103 | void mfclose(MFILE *f) |
---|
104 | { |
---|
105 | delete f; |
---|
106 | } |
---|
107 | |
---|
108 | int mfread(void *ptr, int size, int count, MFILE *f) |
---|
109 | { |
---|
110 | int bytes = size * count; |
---|
111 | int przeczytane = f->Read(ptr, bytes); |
---|
112 | return przeczytane != bytes ? przeczytane / size : count; |
---|
113 | } |
---|
114 | |
---|
115 | int mfwrite(const void *ptr, int size, int count, MFILE *f) |
---|
116 | { |
---|
117 | result r = f->Write(ptr, size * count); |
---|
118 | if (IsFailed(r)) |
---|
119 | return 0; //nie mozemy wykryc jesli udalo sie zapisac czêæ |
---|
120 | else |
---|
121 | return count; |
---|
122 | } |
---|
123 | |
---|
124 | int mfputs(const char *txt, MFILE *f) |
---|
125 | { |
---|
126 | int len = strlen(txt); |
---|
127 | int res = mfwrite(txt, len, 1, f); |
---|
128 | return res == 1 ? 1 : EOF; |
---|
129 | } |
---|
130 | |
---|
131 | char* mfgets(char *str, int num, MFILE *f) |
---|
132 | { |
---|
133 | bool err = false; |
---|
134 | int przeczytane = 0; |
---|
135 | num--; //zeby zawsze zostalo miejsce na wpisanie koncz¹cego NULL |
---|
136 | do |
---|
137 | { |
---|
138 | err = f->Read(str, 1) != 1; |
---|
139 | if (!err) |
---|
140 | { |
---|
141 | str++; |
---|
142 | przeczytane++; |
---|
143 | } |
---|
144 | } while (!err && przeczytane<num && *str != '\n'); |
---|
145 | if (*str == '\n' && przeczytane<num) |
---|
146 | *(str + 1) = 0; |
---|
147 | return przeczytane == 0 ? NULL : str; |
---|
148 | } |
---|
149 | |
---|
150 | int mfeof(MFILE *f) |
---|
151 | { |
---|
152 | //brzydkie obejscie zeby w bada wykryc czy FILE jest w stanie EOF |
---|
153 | static char buf[1]; |
---|
154 | int pos = f->Tell(); |
---|
155 | int przeczytane = f->Read(&buf, 1); |
---|
156 | f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN,pos); |
---|
157 | return przeczytane == 1 ? 0 : 1; |
---|
158 | } |
---|
159 | |
---|
160 | int mfseek(MFILE *f, long position, int type) |
---|
161 | { |
---|
162 | result r; |
---|
163 | if (type == SEEK_SET) |
---|
164 | r = f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN, position); |
---|
165 | else if (type == SEEK_CUR) |
---|
166 | r = f->Seek(Osp::Io::FILESEEKPOSITION_CURRENT, position); |
---|
167 | else if (type == SEEK_END) |
---|
168 | r = f->Seek(Osp::Io::FILESEEKPOSITION_END, position); |
---|
169 | else |
---|
170 | return 1; |
---|
171 | return IsFailed(r) ? 1 : 0; |
---|
172 | } |
---|
173 | |
---|
174 | long mftell(MFILE *f) |
---|
175 | { |
---|
176 | return f->Tell(); |
---|
177 | } |
---|
178 | |
---|
179 | #endif |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | #ifdef __ANDROID__ |
---|
186 | #include "framsg.h" |
---|
187 | #include "nonstd.h" |
---|
188 | #include "nonstd_stl.h" |
---|
189 | MFILE *mfopen(const char *path, const char *mode) |
---|
190 | { |
---|
191 | string respath=GET_APP_RESOURCES; //the macro can be char* or std::string, we don't know (nonstd.h, INITIAL_DIR_IS_RES, cwd.cpp) so we convert it to std::string |
---|
192 | //printFM("Opening '%s', mode='%s'",path,mode); |
---|
193 | //printFM("GET_APP_RESOURCES='%s'",respath.c_str()); |
---|
194 | NvFile *rfile=NULL; //can only read |
---|
195 | FILE *rwfile=NULL; |
---|
196 | if (strstr(path,respath.c_str())==path) //opening resource! so we use a dedicated way to read from assets |
---|
197 | { |
---|
198 | path+=respath.length(); //strip the prefix, we need a relative path in assets |
---|
199 | if (strstr(mode,"w")) |
---|
200 | printFM("Warning: attempt to open a read-only resource '%s' in writable mode '%s'",path,mode); |
---|
201 | rfile=NvFOpen(path); //"mode" not supported! can only read |
---|
202 | //printFM("Opened RES file as %p",rfile); |
---|
203 | if (rfile==NULL) return NULL; |
---|
204 | } else //a "normal" access (HOME) |
---|
205 | { |
---|
206 | rwfile=fopen(path,mode); |
---|
207 | //printFM("Opened HOME file as %p",rwfile); |
---|
208 | if (rwfile==NULL) return NULL; |
---|
209 | } |
---|
210 | MFILE *mfile=new MFILE; |
---|
211 | mfile->rfile=rfile; |
---|
212 | mfile->rwfile=rwfile; |
---|
213 | return mfile; |
---|
214 | } |
---|
215 | |
---|
216 | void mfclose(MFILE *f) |
---|
217 | { |
---|
218 | if (f->rfile) |
---|
219 | NvFClose(f->rfile); |
---|
220 | else |
---|
221 | fclose(f->rwfile); |
---|
222 | |
---|
223 | delete f; |
---|
224 | } |
---|
225 | |
---|
226 | int mfread(void *ptr, int size, int count, MFILE *f) |
---|
227 | { |
---|
228 | if (f->rfile) |
---|
229 | return NvFRead(ptr, size, count, f->rfile); //nvidia introduced my corrections in SDK v10.14, so a fix is no longer needed here |
---|
230 | else |
---|
231 | return fread(ptr, size, count, f->rwfile); |
---|
232 | } |
---|
233 | |
---|
234 | int mfwrite(const void *ptr, int size, int count, MFILE *f) |
---|
235 | { |
---|
236 | if (f->rfile) |
---|
237 | return 0; //write not supported in assets using nvidia functions |
---|
238 | else |
---|
239 | return fwrite(ptr, size, count, f->rwfile); |
---|
240 | } |
---|
241 | |
---|
242 | int mfputs(const char *txt, MFILE *f) |
---|
243 | { |
---|
244 | int len = strlen(txt); |
---|
245 | int res = mfwrite(txt, len, 1, f); |
---|
246 | return res == 1 ? 1 : EOF; |
---|
247 | } |
---|
248 | |
---|
249 | char* mfgets(char *str, int num, MFILE *f) |
---|
250 | { |
---|
251 | if (f->rfile) |
---|
252 | { |
---|
253 | char *ret=NvFGets(str, num, f->rfile); |
---|
254 | //fixing nvidia inconsistency... their function never returns NULL (fix submitted) |
---|
255 | if (ret!=NULL && *ret==0 && num>0) //nothing has been read, must have been eof |
---|
256 | return NULL; |
---|
257 | return ret; |
---|
258 | } |
---|
259 | else |
---|
260 | return fgets(str,num,f->rwfile); |
---|
261 | } |
---|
262 | |
---|
263 | int mfeof(MFILE *f) |
---|
264 | { |
---|
265 | if (f->rfile) |
---|
266 | return NvFEOF(f->rfile); |
---|
267 | else |
---|
268 | return feof(f->rwfile); |
---|
269 | } |
---|
270 | |
---|
271 | int mfseek(MFILE *f, long position, int type) |
---|
272 | { |
---|
273 | if (f->rfile) |
---|
274 | return NvFSeek(f->rfile, position, type); //nvidia introduced my corrections in SDK v10.14, so a fix is no longer needed here |
---|
275 | else |
---|
276 | return fseek(f->rwfile, position, type); |
---|
277 | } |
---|
278 | |
---|
279 | long mftell(MFILE *f) |
---|
280 | { |
---|
281 | if (f->rfile) |
---|
282 | return NvFTell(f->rfile); |
---|
283 | else |
---|
284 | return ftell(f->rwfile); |
---|
285 | } |
---|
286 | #endif |
---|