Skip to content

Instantly share code, notes, and snippets.

@zwetan
Last active August 29, 2015 13:57
Show Gist options
  • Save zwetan/9378352 to your computer and use it in GitHub Desktop.
Save zwetan/9378352 to your computer and use it in GitHub Desktop.
All about listing files and directories with redtamarin
import C.dirent.*;
import C.sys.stat.*;
import C.unistd.*;
/**
* Returns a list of files.
*/
public function getfiles( path:String = "." ):Array
{
var files:Array = [];
var d:DIR = opendir( path );
if( d == null )
{
return files;
}
var dp:dirent;
var statbuf:status;
var ffd:int;
while( (dp = readdir(d)) != null )
{
if( (dp.d_name == ".") || (dp.d_name == "..") )
{
continue;
}
statbuf = new status();
ffd = stat( path + "/" + dp.d_name, statbuf );
if( S_ISREG( statbuf.st_mode ) )
{
files.push( dp.d_name );
}
}
closedir(d);
files.sort( Array.DESCENDING );
return files;
}
/**
* Returns a list of folders.
*/
public function getfolders( path:String = "." ):Array
{
var folders:Array = [];
var d:DIR = opendir( path );
if( d == null )
{
return folders;
}
var dp:dirent;
var statbuf:status;
var ffd:int;
while( (dp = readdir(d)) != null )
{
if( (dp.d_name == ".") || (dp.d_name == "..") )
{
continue;
}
statbuf = new status();
ffd = stat( path + "/" + dp.d_name, statbuf );
if( S_ISDIR( statbuf.st_mode ) )
{
folders.push( dp.d_name );
}
}
closedir(d);
folders.sort( Array.DESCENDING );
return folders;
}
/*
Now that we can list files and folders,
lets say we have the following structure
tree:
├── foo
│   ├── abc
│   │   └── def.txt
│   ├── bar
│   │   ├── baz.txt
│   │   └── blah.txt
│   └── bbb.txt
├── qaz
│   ├── lll
│   │   └── ggg.txt
│   └── zaq.txt
└── xyz.txt
listing by depth:
/xyz.txt
/foo/bbb.txt
/foo/abc/def.txt
/foo/bar/blah.txt
/foo/bar/baz.txt
/qaz/zaq.txt
/qaz/lll/ggg.txt
listing by breadth:
/xyz.txt
/qaz/zaq.txt
/foo/bbb.txt
/qaz/lll/ggg.txt
/foo/bar/blah.txt
/foo/bar/baz.txt
/foo/abc/def.txt
see bellow for the implementations
based on the excellent blog post by Eric Lippert
"Breadth is sometimes better than depth"
http://blogs.msdn.com/b/ericlippert/archive/2004/09/27/breadth-is-sometimes-better-than-depth.aspx
*/
/**
* Returns a list of files and folders ordered by depth.
*/
public function getListByDepth( path:String, includePath:Boolean = false ):Array
{
var list:Array = [];
var stack:Array = [];
stack.push( path );
while( stack.length > 0 )
{
var folder:String = stack.pop();
var subfolders:Array = getfolders( folder );
for( var i:uint = 0; i< subfolders.length; i++ )
{
stack.push( folder + "/" + subfolders[i] );
}
var files:Array = getfiles( folder );
for( var j:uint = 0; j < files.length; j++ )
{
if( includePath )
{
list.push( folder + "/" + files[j] );
}
else
{
list.push( folder.substr(path.length) + "/" + files[j] );
}
}
}
return list;
}
/**
* Returns a list of files and folders ordered by breadth.
*/
public function getListByBreadth( path:String, includePath:Boolean = false ):Array
{
var list:Array = [];
var queue:Array = [];
queue[queue.length] = path;
var counter:uint = 0;
while(counter < queue.length)
{
var folder:String = queue[counter];
var subfolders:Array = getfolders( folder );
for( var i:uint = 0; i< subfolders.length; i++ )
{
queue[queue.length] = folder + "/" + subfolders[i];
}
var files:Array = getfiles( folder );
for( var j:uint = 0; j < files.length; j++ )
{
if( includePath )
{
list.push( folder + "/" + files[j] );
}
else
{
list.push( folder.substr(path.length) + "/" + files[j] );
}
}
queue[counter] = null;
counter++;
}
return list;
}
/* basic usage */
trace( "--------" );
trace( getListByDepth( "." ).join("\n") );
trace( "--------" );
trace( getListByBreadth( "." ).join("\n") );
trace( "--------" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment