[122] | 1 | // This file is a part of the Framsticks GDK. |
---|
[197] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[122] | 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
[109] | 5 | #include "nonstd_stdio.h" |
---|
[201] | 6 | #include "nonstd.h" |
---|
| 7 | |
---|
[109] | 8 | #if defined _WIN32 && !defined SHP |
---|
[201] | 9 | //<unistd.h> not needed for unlink() |
---|
| 10 | #include "Shlwapi.h" //PathIsRelative() |
---|
| 11 | #include "Util.h" //strTOwstr() |
---|
[109] | 12 | #else |
---|
[201] | 13 | #include <unistd.h> |
---|
[109] | 14 | #endif |
---|
| 15 | |
---|
| 16 | bool 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 |
---|
| 19 | MFILE *f=mfopen(path,FOPEN_READ_BINARY); |
---|
| 20 | if (f==NULL) return false; |
---|
| 21 | mfclose(f); |
---|
| 22 | return true; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | bool 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 | |
---|
[201] | 30 | bool 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 |
---|
[215] | 34 | #ifdef __BORLANDC__ |
---|
| 35 | return PathIsRelative(fname) == FALSE; //no wide char for old borland compiler |
---|
| 36 | #else |
---|
[202] | 37 | return PathIsRelative(Util::strTOwstr(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx |
---|
[215] | 38 | #endif |
---|
[201] | 39 | #else |
---|
| 40 | return fname[0] == PATH_SEPARATOR_CHAR; |
---|
| 41 | #endif |
---|
| 42 | } |
---|
[109] | 43 | |
---|
| 44 | #if defined SHP && defined BADA_API_1 |
---|
| 45 | |
---|
| 46 | MFILE *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 | |
---|
| 58 | void mfclose(MFILE *f) |
---|
| 59 | { |
---|
| 60 | delete f; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | int 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 | |
---|
| 70 | int 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 | |
---|
| 79 | int 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 | |
---|
| 86 | char* 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 | |
---|
| 105 | int 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 | |
---|
| 115 | int 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 | |
---|
| 129 | long 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" |
---|
| 143 | MFILE *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 | |
---|
| 163 | void mfclose(MFILE *f) |
---|
| 164 | { |
---|
| 165 | if (f->rfile) |
---|
| 166 | NvFClose(f->rfile); |
---|
| 167 | else |
---|
| 168 | fclose(f->rwfile); |
---|
| 169 | |
---|
| 170 | delete f; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | int 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 | |
---|
| 181 | int 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 | |
---|
| 189 | int 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 | |
---|
| 196 | char* 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 | |
---|
| 204 | int mfeof(MFILE *f) |
---|
| 205 | { |
---|
| 206 | if (f->rfile) |
---|
| 207 | return NvFEOF(f->rfile); |
---|
| 208 | else |
---|
| 209 | return feof(f->rwfile); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | int 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 | |
---|
| 220 | long mftell(MFILE *f) |
---|
| 221 | { |
---|
| 222 | if (f->rfile) |
---|
| 223 | return NvFTell(f->rfile); |
---|
| 224 | else |
---|
| 225 | return ftell(f->rwfile); |
---|
| 226 | } |
---|
| 227 | #endif |
---|