Add a directory listing command to nasal. Given a directory name, return
the file list as an array.
This commit is contained in:
parent
ee065ee6ef
commit
71d0001f3d
1 changed files with 18 additions and 0 deletions
|
@ -299,6 +299,23 @@ static naRef f_screenPrint(naContext c, naRef me, int argc, naRef* args)
|
||||||
return naNil();
|
return naNil();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return an array listing of all files in a directory
|
||||||
|
static naRef f_directory(naContext c, naRef me, int argc, naRef* args)
|
||||||
|
{
|
||||||
|
if(argc != 1 || !naIsString(args[0]))
|
||||||
|
naRuntimeError(c, "bad arguments to directory()");
|
||||||
|
naRef ldir = args[0];
|
||||||
|
ulDir* dir = ulOpenDir(naStr_data(args[0]));
|
||||||
|
if(!dir) return naNil();
|
||||||
|
naRef result = naNewVector(c);
|
||||||
|
ulDirEnt* dent;
|
||||||
|
while((dent = ulReadDir(dir)))
|
||||||
|
naVec_append(result, naStr_fromdata(naNewString(c), dent->d_name,
|
||||||
|
strlen(dent->d_name)));
|
||||||
|
ulCloseDir(dir);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// Table of extension functions. Terminate with zeros.
|
// Table of extension functions. Terminate with zeros.
|
||||||
static struct { char* name; naCFunction func; } funcs[] = {
|
static struct { char* name; naCFunction func; } funcs[] = {
|
||||||
{ "getprop", f_getprop },
|
{ "getprop", f_getprop },
|
||||||
|
@ -311,6 +328,7 @@ static struct { char* name; naCFunction func; } funcs[] = {
|
||||||
{ "_interpolate", f_interpolate },
|
{ "_interpolate", f_interpolate },
|
||||||
{ "rand", f_rand },
|
{ "rand", f_rand },
|
||||||
{ "screenPrint", f_screenPrint },
|
{ "screenPrint", f_screenPrint },
|
||||||
|
{ "directory", f_directory },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue