source: cpp/common/nonstd_stdio.cpp @ 145

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

(c)opyright information added

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