source: cpp/common/nonstd_stdio.cpp @ 202

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

Absolute/relative had a reversed condition

  • Property svn:eol-style set to native
File size: 5.1 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        return PathIsRelative(Util::strTOwstr(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx
35#else
36        return fname[0] == PATH_SEPARATOR_CHAR;
37#endif
38}
39
40#if defined SHP && defined BADA_API_1
41
42MFILE *mfopen(const char *path, const char *mode)
43{
44        Osp::Io::File *f = new Osp::Io::File();
45        result r = f->Construct(path, mode);
46        if (IsFailed(r))
47        {
48                delete f;
49                f = NULL;
50        }
51        return f;
52}
53
54void mfclose(MFILE *f)
55{
56        delete f;
57}
58
59int mfread(void *ptr, int size, int count, MFILE *f)
60{
61        int bytes = size * count;
62        int przeczytane = f->Read(ptr, bytes);
63        return przeczytane != bytes ? przeczytane / size : count;
64}
65
66int mfwrite(const void *ptr, int size, int count, MFILE *f)
67{
68        result r = f->Write(ptr, size * count);
69        if (IsFailed(r))
70                return 0; //nie mozemy wykryc jesli udalo sie zapisac czêœæ
71        else
72                return count;
73}
74
75int mfputs(const char *txt, MFILE *f)
76{
77        int len = strlen(txt);
78        int res = mfwrite(txt, len, 1, f);
79        return res == 1 ? 1 : EOF;
80}
81
82char* mfgets(char *str, int num, MFILE *f)
83{
84        bool err = false;
85        int przeczytane = 0;
86        num--; //zeby zawsze zostalo miejsce na wpisanie koncz¹cego NULL
87        do
88        {
89                err = f->Read(str, 1) != 1;
90                if (!err)
91                {
92                        str++;
93                        przeczytane++;
94                }
95        } while (!err && przeczytane<num && *str != '\n');
96        if (*str == '\n' && przeczytane<num)
97                *(str + 1) = 0;
98        return przeczytane == 0 ? NULL : str;
99}
100
101int mfeof(MFILE *f)
102{
103        //brzydkie obejscie zeby w bada wykryc czy FILE jest w stanie EOF
104        static char buf[1];
105        int pos = f->Tell();
106        int przeczytane = f->Read(&buf, 1);
107        f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN,pos);
108        return przeczytane == 1 ? 0 : 1;
109}
110
111int mfseek(MFILE *f, long position, int type)
112{
113        result r;
114        if (type == SEEK_SET)
115                r = f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN, position);
116        else if (type == SEEK_CUR)
117                r = f->Seek(Osp::Io::FILESEEKPOSITION_CURRENT, position);
118        else if (type == SEEK_END)
119                r = f->Seek(Osp::Io::FILESEEKPOSITION_END, position);
120        else
121                return 1;
122        return IsFailed(r) ? 1 : 0;
123}
124
125long mftell(MFILE *f)
126{
127        return f->Tell();
128}
129
130#endif
131
132
133
134
135
136#ifdef __ANDROID__
137//#include "framsg.h"
138#include "nonstd.h"
139MFILE *mfopen(const char *path, const char *mode)
140{
141        NvFile *rfile=NULL; //umie tylko czytac
142        FILE *rwfile=NULL;
143        if (strstr(path,GET_APP_RESOURCES)==path) //otwieramy resource! wiec uzywamy czytania z assets
144        {
145                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
146                rfile=NvFOpen(path); //"mode" not supported! umie tylko czytac
147                if (rfile==NULL) return NULL;
148        } else //"normalny" dostep (do HOME)
149        {
150                rwfile=fopen(path,mode);
151                if (rwfile==NULL) return NULL;
152        }
153        MFILE *mfile=new MFILE;
154        mfile->rfile=rfile;
155        mfile->rwfile=rwfile;
156        return mfile;
157}
158
159void mfclose(MFILE *f)
160{
161        if (f->rfile)
162                NvFClose(f->rfile);
163        else
164                fclose(f->rwfile);
165
166        delete f;
167}
168
169int mfread(void *ptr, int size, int count, MFILE *f)
170{
171        if (f->rfile)
172                return size==0?0:NvFRead(ptr, size, count, f->rfile)/size; //blad nvidii w interpretacji zwracanej wartosci - zwraca liczbe bajtow zamiast liczbe porcji
173        else
174                return fread(ptr, size, count, f->rwfile);
175}
176
177int mfwrite(const void *ptr, int size, int count, MFILE *f)
178{
179        if (f->rfile)
180                return 0; //write not supported funkcjami nvidii
181        else
182                return fwrite(ptr, size, count, f->rwfile);
183}
184
185int mfputs(const char *txt, MFILE *f)
186{
187        int len = strlen(txt);
188        int res = mfwrite(txt, len, 1, f);
189        return res == 1 ? 1 : EOF;
190}
191
192char* mfgets(char *str, int num, MFILE *f)
193{
194        if (f->rfile)
195                return NvFGets(str, num, f->rfile);
196        else
197                return fgets(str,num,f->rwfile);
198}
199
200int mfeof(MFILE *f)
201{
202        if (f->rfile)
203                return NvFEOF(f->rfile);
204        else
205                return feof(f->rwfile);
206}
207
208int mfseek(MFILE *f, long position, int type)
209{
210        if (f->rfile)
211                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.
212        else
213                return fseek(f->rwfile, position, type);
214}
215
216long mftell(MFILE *f)
217{
218        if (f->rfile)
219                return NvFTell(f->rfile);
220        else
221                return ftell(f->rwfile);
222}
223#endif
Note: See TracBrowser for help on using the repository browser.