#include "filesystem.h" #if _WIN32 #include #include "../../../libc/native/libc.h" #elif __APPLE__ #include #include #include #include #include #include #include #include #elif __linux #include #include #include #include #include #else #include #include #include #include #include #endif #if BB_ANDROID #include #endif namespace bbFileSystem{ bbString _appDir; bbString _appPath; bbArray _appArgs; struct GCRoot : public bbGCRoot{ void gcMark(){ bbGCMark( _appArgs ); } }; GCRoot root; void init(){ static bool done; if( done ) return; done=true; _appArgs=bbArray( bb_argc ); for( int i=0;i0 && i appArgs(){ init(); return _appArgs; } bbBool copyFile( bbString srcPath,bbString dstPath ){ #if _WIN32 // return CopyFileW( bbWString( srcPath ),bbWString( dstPath ),FALSE ); return CopyFileW( widen_utf8( bbCString( srcPath ) ),widen_utf8( bbCString( dstPath ) ),FALSE ); #elif __APPLE__ int ret=copyfile( bbCString( srcPath ),bbCString( dstPath ),0,COPYFILE_ALL ); if( ret>=0 ) return true; // printf( "copyfile failed, ret=%i\n",ret ); // printf( "src=%s\n",srcPath.c_str() ); // printf( "dst=%s\n",dstPath.c_str() ); return false; #else //TODO: use sendfile() here? // int err=-1; if( FILE *srcp=fopen( bbCString( srcPath ),"rb" ) ){ err=-2; if( FILE *dstp=fopen( bbCString( dstPath ),"wb" ) ){ err=0; char buf[1024]; while( int n=fread( buf,1,1024,srcp ) ){ if( fwrite( buf,1,n,dstp )!=n ){ err=-3; break; } } fclose( dstp ); }else{ // printf( "FOPEN 'wb' for CopyFile(%s,%s) failed\n",C_STR(srcpath),C_STR(dstpath) ); fflush( stdout ); } fclose( srcp ); }else{ // printf( "FOPEN 'rb' for CopyFile(%s,%s) failed\n",C_STR(srcpath),C_STR(dstpath) ); fflush( stdout ); } return err==0; #endif } #if BB_ANDROID int android_read( void *cookie,char *buf,int size ){ return AAsset_read( (AAsset*)cookie,buf,size ); } int android_write( void *cookie,const char* buf,int size ){ return EACCES; // can't provide write access to the apk } fpos_t android_seek( void *cookie,fpos_t offset,int whence ){ return AAsset_seek( (AAsset*)cookie,offset,whence ); } int android_close(void* cookie) { AAsset_close( (AAsset*)cookie ); return 0; } FILE *fopenAsset( void *asset ){ return funopen( asset,android_read,android_write,android_seek,android_close ); } #endif /* FILE *fopen( const char *path,const char *mode ){ #if BB_ANDROID if( !strncmp( path,"asset::",7 ) ){ AAssetManager *assetManager=Android_JNI_GetAssetManager(); if( !assetManager ) return 0; AAsset* asset=AAssetManager_open( assetManager,path+7,0 ); if( !asset ) return 0; return fopenAsset( asset ); } #endif return ::fopen( path,mode ); } */ }