source: cpp/common/nonstd_stdio.cpp @ 215

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

Support for Borland compiler and non-wide char API

  • Property svn:eol-style set to native
File size: 5.2 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
8#if defined _WIN32 && !defined SHP
9 //<unistd.h> not needed for unlink()
10 #include "Shlwapi.h" //PathIsRelative()
11 #include "Util.h" //strTOwstr()
12#else
13 #include <unistd.h>
14#endif
15
16bool fileExists(const char* path)
17{
18//lepiej gdyby uzywalo stat bo mfopen mogloby cos niepotrzebnie wczytywac przy otwarciu pliku ale mfopen wiadomo ze zadziala wszedzie tak samo
19MFILE *f=mfopen(path,FOPEN_READ_BINARY);
20if (f==NULL) return false;
21mfclose(f);
22return true;
23}
24
25bool removeFile(const char* path)
26{
27  return _unlink(path)==0; //VS: "The POSIX name is deprecated. Instead, use the ISO C++ conformant name: _unlink"
28}
29
30bool isAbsolutePath(const char* fname)
31{
32        if (fname == NULL) return false; //SplitFileSystem never passes NULL but this function is public so we never know
33#if defined _WIN32 && !defined SHP
34 #ifdef __BORLANDC__
35        return PathIsRelative(fname) == FALSE; //no wide char for old borland compiler
36 #else
37        return PathIsRelative(Util::strTOwstr(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx
38 #endif
39#else
40        return fname[0] == PATH_SEPARATOR_CHAR;
41#endif
42}
43
44#if defined SHP && defined BADA_API_1
45
46MFILE *mfopen(const char *path, const char *mode)
47{
48        Osp::Io::File *f = new Osp::Io::File();
49        result r = f->Construct(path, mode);
50        if (IsFailed(r))
51        {
52                delete f;
53                f = NULL;
54        }
55        return f;
56}
57
58void mfclose(MFILE *f)
59{
60        delete f;
61}
62
63int mfread(void *ptr, int size, int count, MFILE *f)
64{
65        int bytes = size * count;
66        int przeczytane = f->Read(ptr, bytes);
67        return przeczytane != bytes ? przeczytane / size : count;
68}
69
70int mfwrite(const void *ptr, int size, int count, MFILE *f)
71{
72        result r = f->Write(ptr, size * count);
73        if (IsFailed(r))
74                return 0; //nie mozemy wykryc jesli udalo sie zapisac czêœæ
75        else
76                return count;
77}
78
79int mfputs(const char *txt, MFILE *f)
80{
81        int len = strlen(txt);
82        int res = mfwrite(txt, len, 1, f);
83        return res == 1 ? 1 : EOF;
84}
85
86char* mfgets(char *str, int num, MFILE *f)
87{
88        bool err = false;
89        int przeczytane = 0;
90        num--; //zeby zawsze zostalo miejsce na wpisanie koncz¹cego NULL
91        do
92        {
93                err = f->Read(str, 1) != 1;
94                if (!err)
95                {
96                        str++;
97                        przeczytane++;
98                }
99        } while (!err && przeczytane<num && *str != '\n');
100        if (*str == '\n' && przeczytane<num)
101                *(str + 1) = 0;
102        return przeczytane == 0 ? NULL : str;
103}
104
105int mfeof(MFILE *f)
106{
107        //brzydkie obejscie zeby w bada wykryc czy FILE jest w stanie EOF
108        static char buf[1];
109        int pos = f->Tell();
110        int przeczytane = f->Read(&buf, 1);
111        f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN,pos);
112        return przeczytane == 1 ? 0 : 1;
113}
114
115int mfseek(MFILE *f, long position, int type)
116{
117        result r;
118        if (type == SEEK_SET)
119                r = f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN, position);
120        else if (type == SEEK_CUR)
121                r = f->Seek(Osp::Io::FILESEEKPOSITION_CURRENT, position);
122        else if (type == SEEK_END)
123                r = f->Seek(Osp::Io::FILESEEKPOSITION_END, position);
124        else
125                return 1;
126        return IsFailed(r) ? 1 : 0;
127}
128
129long mftell(MFILE *f)
130{
131        return f->Tell();
132}
133
134#endif
135
136
137
138
139
140#ifdef __ANDROID__
141//#include "framsg.h"
142#include "nonstd.h"
143MFILE *mfopen(const char *path, const char *mode)
144{
145        NvFile *rfile=NULL; //umie tylko czytac
146        FILE *rwfile=NULL;
147        if (strstr(path,GET_APP_RESOURCES)==path) //otwieramy resource! wiec uzywamy czytania z assets
148        {
149                if (path[0]=='.' && path[1]=='/') path+=2; //nie rozpoznaje sciezek "./costam", a w sailorze zrobiles tak ze nie moze byc pusty path bo to jest znacznik ze jest niezainicjowany, dlatego uzywasz "./" i musimy je tu obcinac
150                rfile=NvFOpen(path); //"mode" not supported! umie tylko czytac
151                if (rfile==NULL) return NULL;
152        } else //"normalny" dostep (do HOME)
153        {
154                rwfile=fopen(path,mode);
155                if (rwfile==NULL) return NULL;
156        }
157        MFILE *mfile=new MFILE;
158        mfile->rfile=rfile;
159        mfile->rwfile=rwfile;
160        return mfile;
161}
162
163void mfclose(MFILE *f)
164{
165        if (f->rfile)
166                NvFClose(f->rfile);
167        else
168                fclose(f->rwfile);
169
170        delete f;
171}
172
173int mfread(void *ptr, int size, int count, MFILE *f)
174{
175        if (f->rfile)
176                return size==0?0:NvFRead(ptr, size, count, f->rfile)/size; //blad nvidii w interpretacji zwracanej wartosci - zwraca liczbe bajtow zamiast liczbe porcji
177        else
178                return fread(ptr, size, count, f->rwfile);
179}
180
181int mfwrite(const void *ptr, int size, int count, MFILE *f)
182{
183        if (f->rfile)
184                return 0; //write not supported funkcjami nvidii
185        else
186                return fwrite(ptr, size, count, f->rwfile);
187}
188
189int mfputs(const char *txt, MFILE *f)
190{
191        int len = strlen(txt);
192        int res = mfwrite(txt, len, 1, f);
193        return res == 1 ? 1 : EOF;
194}
195
196char* mfgets(char *str, int num, MFILE *f)
197{
198        if (f->rfile)
199                return NvFGets(str, num, f->rfile);
200        else
201                return fgets(str,num,f->rwfile);
202}
203
204int mfeof(MFILE *f)
205{
206        if (f->rfile)
207                return NvFEOF(f->rfile);
208        else
209                return feof(f->rwfile);
210}
211
212int mfseek(MFILE *f, long position, int type)
213{
214        if (f->rfile)
215                return NvFSeek(f->rfile, position, type)==-1; // off_t AAsset_seek(AAsset* asset, off_t offset, int whence): Returns the new position on success, or (off_t) -1 on error.
216        else
217                return fseek(f->rwfile, position, type);
218}
219
220long mftell(MFILE *f)
221{
222        if (f->rfile)
223                return NvFTell(f->rfile);
224        else
225                return ftell(f->rwfile);
226}
227#endif
Note: See TracBrowser for help on using the repository browser.