source: cpp/common/nonstd_stdio.cpp @ 271

Last change on this file since 271 was 247, checked in by Maciej Komosinski, 10 years ago

Sources support both 32-bit and 64-bit, and more compilers

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
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
18bool 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
21MFILE *f=mfopen(path,FOPEN_READ_BINARY);
22if (f==NULL) return false;
23mfclose(f);
24return true;
25}
26
27bool directoryExists(const char* path)
28{
29struct _stat s;
30if (_stat(path,&s)!=0) return false;
31return S_ISDIR(s.st_mode);
32}
33
34bool makeDirectory(const char* path)
35{
36#ifdef _WIN32
37return mkdir(path)==0;
38#else
39return mkdir(path,0777)==0;
40#endif
41}
42
43bool makeDirectories(const char* path)
44{
45if (directoryExists(path)) return true;
46string parentdir=getFileDir(path);
47if (!makeDirectories(parentdir.c_str())) return false;
48return makeDirectory(path);
49}
50
51int getFileSize(const char* path)
52{
53int size;
54MFILE *f=mfopen(path,FOPEN_READ_BINARY);
55if (f==NULL) return -1;
56size=getFileSize(f);
57mfclose(f);
58return size;
59}
60
61int 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
70bool 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
75bool 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
91MFILE *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
103void mfclose(MFILE *f)
104{
105        delete f;
106}
107
108int 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
115int 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
124int 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
131char* 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
150int 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
160int 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
174long 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"
189MFILE *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
216void mfclose(MFILE *f)
217{
218        if (f->rfile)
219                NvFClose(f->rfile);
220        else
221                fclose(f->rwfile);
222
223        delete f;
224}
225
226int 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
234int 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
242int 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
249char* 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
263int mfeof(MFILE *f)
264{
265        if (f->rfile)
266                return NvFEOF(f->rfile);
267        else
268                return feof(f->rwfile);
269}
270
271int 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
279long mftell(MFILE *f)
280{
281        if (f->rfile)
282                return NvFTell(f->rfile);
283        else
284                return ftell(f->rwfile);
285}
286#endif
Note: See TracBrowser for help on using the repository browser.