Sign in to follow this  

[C++] Lightweight cross platform file system

This topic is 3664 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

Hi, Does anyone know of a lightweight cross-platform file system library? I'd like to do a cross-platform "find-file" ("dir/ls" will do too.) I know of boost::filesystem, but I do not wish to include the whole boost package just to add this functionality. Thanks.

Share this post


Link to post
Share on other sites
Hi,

why don't you write this find-functionality yourself? It is less than 100 lines of code, including a Windows and a Linux version :)

Try something like this:
Linux Version:

void ceLinuxFileManager::SearchFiles(const ce_string &searchString,
vector<ce_string> &filesFound)
{
ce_string::size_type sepPos = searchString.find_last_of(".");
if(sepPos == -1 || sepPos == 0)
{
string warnMsg = string(__FUNCTION__) + ": Invalid search string: " + searchString;
cerr << warnMsg << endl;
return;
}

// search string should now be: *.extension
ce_string internalSearchString = searchString.substr(sepPos, searchString.length());
// and now only the point with extension.

// go to that directory
char buffer[200];
getcwd(buffer, 200);
// If any directory was given, extract it
ce_string directory = string(buffer) + string("/") + searchString.substr(0, sepPos-1);

cout << "Searching directory %s for files with extension " << directory <<
internalSearchString << endl;

// go to the requested directory
DIR *dir = opendir(directory.c_str());
if(dir)
{
struct dirent *pDir=NULL;
while((pDir = readdir(dir)) != NULL)
{
// check if the file has the correct extension
ce_string filename = (*pDir).d_name;
ce_string::size_type nameSepPos = filename.find_last_of(".");
if (nameSepPos != string::npos)
{
// The file name has an extension, look at it.
ce_string extension = filename.substr(nameSepPos, filename.length());

// if both have the same file extension, add them to the list
if(extension == internalSearchString)
{
filesFound.push_back(filename);
}
} // TODO: else?
}
closedir(dir);
}
else
{
ce_string warnMsg = string(__FUNCTION__) + ": could not open directory: " + directory;
cerr << warnMsg << endl;
}
}


Windows Version:

void ceWin32FileManager::SearchFiles(
const ce_string &searchString,
vector<ce_string> &filesFound)
{
ce_string::size_type sepPos = searchString.find_last_of(".");
if(sepPos == -1 || sepPos == 0)
{
ce_string warnMsg = string(__FUNCTION__) + ": Invalid search string: "+searchString;
cerr << warnMsg << endl;
return;
}

// search string should now be: *.extension
ce_string internalSearchString = searchString.substr(sepPos, searchString.length());
// and now only the point with extension.

// go to that directory
char buffer[200];
_getcwd(buffer, 200);
// If any directory was given, extract it
ce_string directory = string(buffer) + string("\\") + searchString.substr(0, sepPos-1);

//ce_string work_directory(buffer);
//ce_string directory = string(buffer) + string("\\") + searchString.substr(0, sepPos);

// change to the directory
if(_chdir(directory.c_str()) != -1)
{
struct _finddata_t fileinfo;

// findfirst() needs a regular expression of this type: *.extension
// build this from the search string. Try to get a first file
intptr_t handle = _findfirst( (string("*")+internalSearchString).c_str(), &fileinfo);

// was at least one file found?
if(handle != -1)
{
// yes...
filesFound.push_back(directory+string("\\") + fileinfo.name);
while(true)
{
if(_findnext(handle, &fileinfo) != 0)
break;
filesFound.push_back(directory+string("\\")+fileinfo.name);
}
}
}
else
{
ce_string warnMsg = string(__FUNCTION__) + ": couldnt open directory: " + directory;
cerr << warnMsg << endl;
}
}


Both methods will return a list of filenames for a search request like "/usr/lib/*.so" :)

Share this post


Link to post
Share on other sites
Quote:

why don't you write this find-functionality yourself?

I was doing this, and then I figured someone else must have done it before.. :)

Thanks for your code snippets!

Share this post


Link to post
Share on other sites
boost::filesystem

what's the problem? "I don't want to add boost to my include paths" is hardly a valid problem. I think filesystem requires building the lib, too.

Share this post


Link to post
Share on other sites

This topic is 3664 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this