[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__ |
---|
[227] | 141 | #include "framsg.h" |
---|
[109] | 142 | #include "nonstd.h" |
---|
[227] | 143 | #include "nonstd_stl.h" |
---|
[109] | 144 | MFILE *mfopen(const char *path, const char *mode) |
---|
| 145 | { |
---|
[227] | 146 | string respath=GET_APP_RESOURCES; //the macro can be char* or std::string, we don't know (nonstd.h, INITIAL_DIR_IS_RES, cwd.cpp) so we convert it to std::string |
---|
| 147 | //printFM("Opening '%s', mode='%s'",path,mode); |
---|
| 148 | //printFM("GET_APP_RESOURCES='%s'",respath.c_str()); |
---|
| 149 | NvFile *rfile=NULL; //can only read |
---|
[109] | 150 | FILE *rwfile=NULL; |
---|
[227] | 151 | if (strstr(path,respath.c_str())==path) //opening resource! so we use a dedicated way to read from assets |
---|
[109] | 152 | { |
---|
[227] | 153 | path+=respath.length(); //strip the prefix, we need a relative path in assets |
---|
| 154 | if (strstr(mode,"w")) |
---|
| 155 | printFM("Warning: attempt to open a read-only resource '%s' in writable mode '%s'",path,mode); |
---|
| 156 | rfile=NvFOpen(path); //"mode" not supported! can only read |
---|
| 157 | //printFM("Opened RES file as %p",rfile); |
---|
[109] | 158 | if (rfile==NULL) return NULL; |
---|
[227] | 159 | } else //a "normal" access (HOME) |
---|
[109] | 160 | { |
---|
| 161 | rwfile=fopen(path,mode); |
---|
[227] | 162 | //printFM("Opened HOME file as %p",rwfile); |
---|
[109] | 163 | if (rwfile==NULL) return NULL; |
---|
| 164 | } |
---|
| 165 | MFILE *mfile=new MFILE; |
---|
| 166 | mfile->rfile=rfile; |
---|
| 167 | mfile->rwfile=rwfile; |
---|
| 168 | return mfile; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void mfclose(MFILE *f) |
---|
| 172 | { |
---|
| 173 | if (f->rfile) |
---|
| 174 | NvFClose(f->rfile); |
---|
| 175 | else |
---|
| 176 | fclose(f->rwfile); |
---|
| 177 | |
---|
| 178 | delete f; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | int mfread(void *ptr, int size, int count, MFILE *f) |
---|
| 182 | { |
---|
| 183 | if (f->rfile) |
---|
[227] | 184 | return NvFRead(ptr, size, count, f->rfile); //nvidia introduced my corrections in SDK v10.14, so a fix is no longer needed here |
---|
[109] | 185 | else |
---|
| 186 | return fread(ptr, size, count, f->rwfile); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | int mfwrite(const void *ptr, int size, int count, MFILE *f) |
---|
| 190 | { |
---|
| 191 | if (f->rfile) |
---|
[227] | 192 | return 0; //write not supported in assets using nvidia functions |
---|
[109] | 193 | else |
---|
| 194 | return fwrite(ptr, size, count, f->rwfile); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | int mfputs(const char *txt, MFILE *f) |
---|
| 198 | { |
---|
| 199 | int len = strlen(txt); |
---|
| 200 | int res = mfwrite(txt, len, 1, f); |
---|
| 201 | return res == 1 ? 1 : EOF; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | char* mfgets(char *str, int num, MFILE *f) |
---|
| 205 | { |
---|
| 206 | if (f->rfile) |
---|
[227] | 207 | { |
---|
| 208 | char *ret=NvFGets(str, num, f->rfile); |
---|
| 209 | //fixing nvidia inconsistency... their function never returns NULL (fix submitted) |
---|
| 210 | if (ret!=NULL && *ret==0 && num>0) //nothing has been read, must have been eof |
---|
| 211 | return NULL; |
---|
| 212 | return ret; |
---|
| 213 | } |
---|
[109] | 214 | else |
---|
| 215 | return fgets(str,num,f->rwfile); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | int mfeof(MFILE *f) |
---|
| 219 | { |
---|
| 220 | if (f->rfile) |
---|
| 221 | return NvFEOF(f->rfile); |
---|
| 222 | else |
---|
| 223 | return feof(f->rwfile); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | int mfseek(MFILE *f, long position, int type) |
---|
| 227 | { |
---|
| 228 | if (f->rfile) |
---|
[227] | 229 | return NvFSeek(f->rfile, position, type); //nvidia introduced my corrections in SDK v10.14, so a fix is no longer needed here |
---|
[109] | 230 | else |
---|
| 231 | return fseek(f->rwfile, position, type); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | long mftell(MFILE *f) |
---|
| 235 | { |
---|
| 236 | if (f->rfile) |
---|
| 237 | return NvFTell(f->rfile); |
---|
| 238 | else |
---|
| 239 | return ftell(f->rwfile); |
---|
| 240 | } |
---|
| 241 | #endif |
---|