• Advertisement
Sign in to follow this  

PHYSFS how to read a directory...

This topic is 1650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to use PHYSFS to read a directory and it's files to determine how many files are in each directory, but can't figure out how to do it with PHYSFS. I don't want to use BOOST, but if I can use something new in C++11 or the new STL? I would be ok to doing that if possible...

 

class FileSystem
{
private:
    static FileSystem fs;
    std::string s;

    FileSystem() : s("NA"){}
    
public:
    ~FileSystem(){}
    void Kill()
    {
        PHYSFS_deinit();
    }
    void Init(const char* path)
    {
        if(!PHYSFS_isInit())
        {    
            PHYSFS_init(path);
            s = PHYSFS_getBaseDir();
            s.erase(s.find_last_of("\\"));
            s.erase(s.find_last_of("\\")+1);
        }
    }
    const std::string& GetPath()
    {
        return s;
    }
    static FileSystem& Get()
    {
        return fs;
    }
    void FillDirectory(const char* path)
    {
        //not working...
          char** rc = PHYSFS_enumerateFiles(path);
        char** i;
        int file_count = 0;
        for(i = rc, file_count = 0; *i != NULL; i++, file_count++)
            printf("%s\n", *i);
         printf("\n total (%d) files.\n", file_count);

        if(rc == nullptr)
        {
            //throw error here
        }
        else
        {
            for(i = rc; *i != nullptr; ++i)
            {
                const int len = strlen(path) + strlen(*i) + 2;
                char* fname = new char[len];
                const char* origdir = path;
                if(strcmp(origdir, "/") == 0)
                    origdir = "";
                snprintf(fname, len, "%s/%s", origdir, *i);
    
                if(PHYSFS_isDirectory(fname))
                {
                    std::cout << "DIR = " << fname << std::endl;
                }
                else if(PHYSFS_isSymbolicLink(fname))
                {
                    std::cout << "SymbolicLink = " << fname << std::endl;
                }
                else
                {
                    //file
                }
                delete[] fname;
            }
            PHYSFS_freeList(rc);
        }
    }
};

 

Share this post


Link to post
Share on other sites
Advertisement
Guest Hiwas


void Init(const char* path)
    {
        if(!PHYSFS_isInit())
        {    
            PHYSFS_init(path);
            s = PHYSFS_getBaseDir();
            s.erase(s.find_last_of("\\"));
            s.erase(s.find_last_of("\\")+1);
        }
    }

 

This is likely the source of the problem.  PHYSFS_init does not setup the paths in the virtual file system, it simply does some basic initialization.  So, at this point, any call to access a file will fail since there is nothing available.  You need at least one call to PHYSFS_addToSearchPath before anything can be discovered in the file system.  Better than adding a path though, you can use PHYSFS_setSaneConfig which will do some additional work and give you a nice layout.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement