[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[820] | 2 | // Copyright (C) 1999-2018 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include "stdiofile.h" |
---|
| 6 | #include <common/nonstd_dir.h> |
---|
| 7 | #include <common/nonstd_stdio.h> |
---|
[375] | 8 | #include <common/log.h> |
---|
[281] | 9 | #include <common/Convert.h> |
---|
[425] | 10 | #ifdef __ANDROID__ |
---|
| 11 | #include <common/dirs.h> |
---|
| 12 | #include <common/platform/android/AndroidAPK_DIR.h> |
---|
| 13 | #endif |
---|
[109] | 14 | |
---|
[281] | 15 | VirtFILE* StdioFileSystem::Vfopen(const char *path, const char *mode) |
---|
[109] | 16 | { |
---|
[375] | 17 | //log_printf("Vfopen %s %s",path,mode); |
---|
[374] | 18 | #if defined USE_MFILE || defined _WIN32 |
---|
[281] | 19 | MFILE *f = mfopen(path, mode); |
---|
[206] | 20 | #else |
---|
[281] | 21 | FILE *f = fopen(path, mode); |
---|
[206] | 22 | #endif |
---|
[375] | 23 | //log_printf("%p",f); |
---|
[298] | 24 | if (f) return new StdioFILE(f, path); else return NULL; |
---|
[109] | 25 | } |
---|
| 26 | |
---|
| 27 | VirtDIR* StdioFileSystem::Vopendir(const char* path) |
---|
| 28 | { |
---|
[375] | 29 | //log_printf("Vopendir %s",path); |
---|
[425] | 30 | #ifdef __ANDROID__ |
---|
[820] | 31 | int resources_prefix_length = getAppResourcesDir().length(); |
---|
[425] | 32 | if (strncmp(path, getAppResourcesDir().c_str(), resources_prefix_length) == 0) //it is a resources dir |
---|
| 33 | { |
---|
[820] | 34 | VirtDIR *vd = AndroidAPK_DIR::opendir(path + resources_prefix_length + 1); //+1 because we also skip '/' and start with a "relative" dir, otherwise it does not work. |
---|
[425] | 35 | return vd; |
---|
| 36 | } |
---|
| 37 | #endif |
---|
| 38 | |
---|
[281] | 39 | #ifdef _WIN32 |
---|
| 40 | DIRTYPE *d = wopendir(Convert::utf8ToUtf16(path).c_str()); |
---|
| 41 | #else |
---|
| 42 | DIR *d = opendir(path); |
---|
| 43 | #endif |
---|
[375] | 44 | //log_printf("%p",d); |
---|
[298] | 45 | if (d) return new StdioDIR(d); else return NULL; |
---|
[109] | 46 | } |
---|
| 47 | |
---|
[295] | 48 | bool StdioFileSystem::Vfexists(const char* path) |
---|
[109] | 49 | { |
---|
| 50 | return fileExists(path); |
---|
| 51 | } |
---|
| 52 | |
---|
[820] | 53 | bool StdioFileSystem::Vdirexists(const char* path, bool is_writable) |
---|
| 54 | { |
---|
| 55 | #ifdef __ANDROID__ |
---|
| 56 | int resources_prefix_length = getAppResourcesDir().length(); |
---|
| 57 | if (strncmp(path, getAppResourcesDir().c_str(), resources_prefix_length) == 0) //it is a resources dir |
---|
| 58 | { |
---|
| 59 | if (is_writable) |
---|
| 60 | return false; |
---|
| 61 | VirtDIR *vd = AndroidAPK_DIR::opendir(path + resources_prefix_length + 1); //+1 because we also skip '/' and start with a "relative" dir, otherwise it does not work. |
---|
| 62 | if (vd != NULL) |
---|
| 63 | { |
---|
| 64 | delete vd; |
---|
| 65 | return true; |
---|
| 66 | } |
---|
| 67 | else |
---|
| 68 | { |
---|
| 69 | return false; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | #endif |
---|
| 73 | return directoryExists(path, is_writable); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
[425] | 81 | #ifndef NO_STD_IN_OUT_ERR |
---|
[109] | 82 | void StdioFILE::setStdio() |
---|
| 83 | { |
---|
| 84 | static StdioFILEDontClose si(stdin); |
---|
| 85 | static StdioFILEDontClose so(stdout); |
---|
| 86 | static StdioFILEDontClose se(stderr); |
---|
| 87 | setVstdin(&si); |
---|
| 88 | setVstdout(&so); |
---|
| 89 | setVstderr(&se); |
---|
[425] | 90 | } |
---|
[227] | 91 | #endif |
---|
[109] | 92 | |
---|
| 93 | dirent* StdioDIR::Vreaddir() |
---|
| 94 | { |
---|
[375] | 95 | //log_printf("Vreaddir %s",dir); |
---|
[281] | 96 | #ifdef _WIN32 |
---|
[820] | 97 | wdirent *wde = wreaddir(dir); |
---|
| 98 | if (wde == NULL) return NULL; |
---|
[281] | 99 | strcpy(de.d_name, Convert::wstrToUtf8(wde->d_name).c_str()); |
---|
| 100 | return &de; |
---|
| 101 | #else |
---|
[109] | 102 | return readdir(dir); |
---|
[281] | 103 | #endif |
---|
[109] | 104 | } |
---|