// This file is a part of the Framsticks GDK. // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. // Refer to http://www.framsticks.com/ for further information. #include "nonstd_stdio.h" #if defined _WIN32 && !defined SHP //tu nie trzeba includów zeby dzialal unlink() #else #include #endif #include "nonstd.h" bool fileExists(const char* path) { //lepiej gdyby uzywalo stat bo mfopen mogloby cos niepotrzebnie wczytywac przy otwarciu pliku ale mfopen wiadomo ze zadziala wszedzie tak samo MFILE *f=mfopen(path,FOPEN_READ_BINARY); if (f==NULL) return false; mfclose(f); return true; } bool removeFile(const char* path) { return _unlink(path)==0; //VS: "The POSIX name is deprecated. Instead, use the ISO C++ conformant name: _unlink" } #if defined SHP && defined BADA_API_1 MFILE *mfopen(const char *path, const char *mode) { Osp::Io::File *f = new Osp::Io::File(); result r = f->Construct(path, mode); if (IsFailed(r)) { delete f; f = NULL; } return f; } void mfclose(MFILE *f) { delete f; } int mfread(void *ptr, int size, int count, MFILE *f) { int bytes = size * count; int przeczytane = f->Read(ptr, bytes); return przeczytane != bytes ? przeczytane / size : count; } int mfwrite(const void *ptr, int size, int count, MFILE *f) { result r = f->Write(ptr, size * count); if (IsFailed(r)) return 0; //nie mozemy wykryc jesli udalo sie zapisac część else return count; } int mfputs(const char *txt, MFILE *f) { int len = strlen(txt); int res = mfwrite(txt, len, 1, f); return res == 1 ? 1 : EOF; } char* mfgets(char *str, int num, MFILE *f) { bool err = false; int przeczytane = 0; num--; //zeby zawsze zostalo miejsce na wpisanie konczącego NULL do { err = f->Read(str, 1) != 1; if (!err) { str++; przeczytane++; } } while (!err && przeczytaneTell(); int przeczytane = f->Read(&buf, 1); f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN,pos); return przeczytane == 1 ? 0 : 1; } int mfseek(MFILE *f, long position, int type) { result r; if (type == SEEK_SET) r = f->Seek(Osp::Io::FILESEEKPOSITION_BEGIN, position); else if (type == SEEK_CUR) r = f->Seek(Osp::Io::FILESEEKPOSITION_CURRENT, position); else if (type == SEEK_END) r = f->Seek(Osp::Io::FILESEEKPOSITION_END, position); else return 1; return IsFailed(r) ? 1 : 0; } long mftell(MFILE *f) { return f->Tell(); } #endif #ifdef __ANDROID__ //#include "framsg.h" #include "nonstd.h" MFILE *mfopen(const char *path, const char *mode) { NvFile *rfile=NULL; //umie tylko czytac FILE *rwfile=NULL; if (strstr(path,GET_APP_RESOURCES)==path) //otwieramy resource! wiec uzywamy czytania z assets { 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 rfile=NvFOpen(path); //"mode" not supported! umie tylko czytac if (rfile==NULL) return NULL; } else //"normalny" dostep (do HOME) { rwfile=fopen(path,mode); if (rwfile==NULL) return NULL; } MFILE *mfile=new MFILE; mfile->rfile=rfile; mfile->rwfile=rwfile; return mfile; } void mfclose(MFILE *f) { if (f->rfile) NvFClose(f->rfile); else fclose(f->rwfile); delete f; } int mfread(void *ptr, int size, int count, MFILE *f) { if (f->rfile) return size==0?0:NvFRead(ptr, size, count, f->rfile)/size; //blad nvidii w interpretacji zwracanej wartosci - zwraca liczbe bajtow zamiast liczbe porcji else return fread(ptr, size, count, f->rwfile); } int mfwrite(const void *ptr, int size, int count, MFILE *f) { if (f->rfile) return 0; //write not supported funkcjami nvidii else return fwrite(ptr, size, count, f->rwfile); } int mfputs(const char *txt, MFILE *f) { int len = strlen(txt); int res = mfwrite(txt, len, 1, f); return res == 1 ? 1 : EOF; } char* mfgets(char *str, int num, MFILE *f) { if (f->rfile) return NvFGets(str, num, f->rfile); else return fgets(str,num,f->rwfile); } int mfeof(MFILE *f) { if (f->rfile) return NvFEOF(f->rfile); else return feof(f->rwfile); } int mfseek(MFILE *f, long position, int type) { if (f->rfile) 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. else return fseek(f->rwfile, position, type); } long mftell(MFILE *f) { if (f->rfile) return NvFTell(f->rfile); else return ftell(f->rwfile); } #endif