ganado / redtamarin

Automatically exported from code.google.com/p/redtamarin
Other
2 stars 0 forks source link

Filesystem API #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
copy the one from AIR

but keep things low-level

Original issue reported on code.google.com by zwetan on 26 Apr 2009 at 8:28

GoogleCodeExporter commented 9 years ago
the filesystem API is started but we're not just copy AIR, we gonna do better :)

So far we consider native everything that is compiled in the 
*shell_toplevel.abc*
 * everything declared in the *avmplus* package
 * everything declared in the *clib* (C.unistd, C.stdlib, etc.)

How are we gonna deal with the filesystem then ?

On one side we will have a "dumb" implementation for the FPAPI in *avmglue*
{{{
package flash.filesystem
{
    public class File
    {
        private var _path:String;
        public function File( path:String = null )
        {
            _path = path;
        }

        public function get isDirectory():Boolean
        {
            return false;
        }

        public function resolvePath( path:String ):File
        {
            return new File( _path + path );
        }
    }
}
}}}

then we add new functionality in *avmplus.File*
{{{
package avmplus
{

    [native(cls="::avmshell::FileClass", methods="auto")]
    public class File
    {
        public native static function exists( filename:String ):Boolean;
        public native static function read( filename:String ):String;
        public native static function write( filename:String, data:String ):void;

        public native static function isDirectory( filename:String ):Boolean;

    }
}
}}}

and we also add new functionality in *C.stdlib*
{{{
package C.stdlib
{

    [native(cls="::avmshell::StdlibClass", methods="auto")]
    internal class __stdlib
    {

        public native static function realpath( path:String ):String;  //char *realpath(char const *path, char *resolvedPath);
    }

    public function realpath( path:String ):String
    {
        return __stdlib.realpath( path );
    }

}
}}}

and with that we can implement an API :)

{{{
package flash.filesystem
{
    import avmplus.File;
    import C.stdlib.realpath;

    REDTAMARIN::API
    public class File
    {
        private var _path:String;
        public function File( path:String = null )
        {
            _path = path;
        }

        public function get isDirectory():Boolean
        {
            return avmplus.File.isDirectory( _path );
        }

        public function resolvePath( path:String ):File
        {
            return new File( realpath( _path, path ) );
        }
    }
}
}}}

we will keep a mock implementation of the FPAPI no matter what

we will use the Conditional Compilation *REDTAMARIN::API* for the real 
implementation part

but this is one API based on what can be found in flash.filesystem.*, we could 
decide to provide another API, based on Python for example
{{{
package os.path
{
    import avmplus.File;
    import C.stdlib.realpath;

    public function isdir( path:String ):Boolean
    {
        return File.isDirectory( path );
    }

    public function realpath( filename:String ):String
    {
        return realpath( filename );
    }

}
}}}

Original comment by zwetan on 9 Aug 2010 at 11:19

GoogleCodeExporter commented 9 years ago
about naming, when we refer to a file reference, wether it is a directory, 
regular file, etc.
we should refer to a *filename*

see here http://en.wikipedia.org/wiki/Filename

a *path*, for example, is sub-component of a filename

for the URI parts, http://en.wikipedia.org/wiki/Uniform_Resource_Identifier
we will use the core.uri class from maashaack

Original comment by zwetan on 9 Aug 2010 at 1:12

GoogleCodeExporter commented 9 years ago
taking a biiig shortcut for listing files and directories
under POSIX it's quite easy, you just use <dirent.h> and use opendir() readdir()

under WIN32, you have the equivalent with _findfirst() _findnext() etc.
but we can not really add that in the VMPI

we need to be able to iterate trough directories inside the native class

so the goal is to emulate dirent.h for WIN32 so you could do that

----
        ArrayObject *list = toplevel->arrayClass->newArray();
        DIR *root = opendir(filenameUTF8.c_str());
        struct dirent *entry;
        int count = 0;

        if( root ) {
            do {
                entry = readdir(root);
                if(entry) {
                    if(directory == (entry->d_type==DT_DIR)) {
                        list->setUintProperty(count++, core->newStringUTF8(entry->d_name)->atom());
                    }
                }
            } while(entry != NULL);
        }

        closedir(root);
        return list;
----

Original comment by zwetan on 10 Aug 2010 at 5:40

GoogleCodeExporter commented 9 years ago

Original comment by zwetan on 23 Jan 2011 at 3:23