source: cpp/common/virtfile/stdiofile.cpp @ 1321

Last change on this file since 1321 was 820, checked in by Maciej Komosinski, 6 years ago

More complete implementation of directories in the virtual filesystem for Android

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "stdiofile.h"
6#include <common/nonstd_dir.h>
7#include <common/nonstd_stdio.h>
8#include <common/log.h>
9#include <common/Convert.h>
10#ifdef __ANDROID__
11#include <common/dirs.h>
12#include <common/platform/android/AndroidAPK_DIR.h>
13#endif
14
15VirtFILE* StdioFileSystem::Vfopen(const char *path, const char *mode)
16{
17        //log_printf("Vfopen %s %s",path,mode);
18#if defined USE_MFILE || defined _WIN32
19        MFILE *f = mfopen(path, mode);
20#else
21        FILE *f = fopen(path, mode);
22#endif
23        //log_printf("%p",f);
24        if (f) return new StdioFILE(f, path); else return NULL;
25}
26
27VirtDIR* StdioFileSystem::Vopendir(const char* path)
28{
29        //log_printf("Vopendir %s",path);
30#ifdef __ANDROID__
31        int resources_prefix_length = getAppResourcesDir().length();
32        if (strncmp(path, getAppResourcesDir().c_str(), resources_prefix_length) == 0) //it is a resources dir
33        {
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.
35                return vd;
36        }
37#endif
38
39#ifdef _WIN32
40        DIRTYPE *d = wopendir(Convert::utf8ToUtf16(path).c_str());
41#else
42        DIR *d = opendir(path);
43#endif
44        //log_printf("%p",d);
45        if (d) return new StdioDIR(d); else return NULL;
46}
47
48bool StdioFileSystem::Vfexists(const char* path)
49{
50        return fileExists(path);
51}
52
53bool 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
81#ifndef NO_STD_IN_OUT_ERR
82void 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);
90}
91#endif
92
93dirent* StdioDIR::Vreaddir()
94{
95        //log_printf("Vreaddir %s",dir);
96#ifdef _WIN32
97        wdirent *wde = wreaddir(dir);
98        if (wde == NULL) return NULL;
99        strcpy(de.d_name, Convert::wstrToUtf8(wde->d_name).c_str());
100        return &de;
101#else
102        return readdir(dir);
103#endif
104}
Note: See TracBrowser for help on using the repository browser.