lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Both LuaCheia and GluaX provide the kind of service you mention, and it works for Linux, Win32 and OS X. From gluax 'sys_module.c' (returns a table using gluax macros):

    //---
    // [tbl]= listdir( [path_str [,mask_str]] )
    //
    // Returns: { "file1.ext", "file2.ext", "dir1/", ... }
    //          { }     (if no files matching the 'mask')
    //          nil     (if no such directory)
    //
    // Note:    Tree recursion etc. are done in a Lua wrapper.
    //
    GLUA_FUNC( listdir )
    {
    const char* path= glua_getString(1);
    const char* mask= glua_getString(2);
    DIR* dir;
    struct dirent* entry;
    uint n=0;

        if (!path || !path[0])
            path= ".";  // default (current dir)

        dir= opendir( path );
        //
        // NULL on failure, 'errno' gives more info:
        //
        //      ENOENT:     No such directory
        //      EINVAL:     Invalid argument or directory name
        //      ENOMEM:     Not enough memory to perform the operation

        if (!dir) break;    // jumps to 'GLUA_END', returns nil.

        glua_pushTable();
            {
            char buf[ PATH_MAX ];

            while( (entry= readdir(dir)) != NULL )
                {
                const char* name= entry->d_name;

                if ( (strcmp(name,".")==0) || (strcmp(name,"..")==0) )
                    continue;   // skip "." and ".." always

                if (mask && !Loc_MaskMatch(name,mask))
                    continue;   // masked out

                if (Loc_IsDir(path,name))
                    {
                    name= strcpy_safe( buf, name, sizeof(buf) );
                    strcat_safe( buf, "/", sizeof(buf) );
                    }

                glua_setTable_str_i( ++n, name );
                }
            }

        closedir( dir );
    }
    GLUA_END



Andy Bushnell kirjoittaa maanantaina, 29. syyskuuta 2003, kello 10:18:

Hi,

Please excuse my ignorance (i'm a Lua newbie).

I have a 'C' function (getFileListing) that has been registered with Lua. It compiles a list of files in the current working directory. Everything works
fine until I try *returning* this info to Lua. How can I do it?

My Lua script (right now) makes the call...

getFileListing()

Nothing happens but printf's in the getFileListing function show that its being called & doing the right thing. So now I have an array of strings (the file names) in the 'C' side but I want to pass this info back to the script. I think I need to use a "Table" to pass this info to Lua, but tables are new to me and I cant see an example which does what I want (which, as a newbie,
im looking for!).

I'd like my script (I think) to look something like ...

t = getFileListing() -- where t is the returned table

-- check for table size

-- iterate through table getting the file listing names

(im not sure of the exact table iteration syntax - but I can work that out
later).

I hope this isnt too simple a question - but any help would greatly
appreciated.

Regards,

Andy