source: cpp/common/nonstd_stdio.cpp @ 109

Last change on this file since 109 was 109, checked in by sz, 10 years ago

source reorganization (see README)
new feature added: part/joint shapes (see frams/_demos/part_shapes.cpp)

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