I have a (not Irrlicht related) problem: I need to list files in a directory and write the list to a file. Everything works fine, except for files with special characters, e.g. £ ¼ © and so on. I alredy searched for a solution: I should use wchar_t* or stringw for the filenames. However, the given methods in the sample only work for windows...
This is what I have so far:
Code: Select all
int listDirectory(const char* _path, vector<string> &_return_files, const vector<string> *_allowedExtensions, int _type)
{
int fileCount = 0;
DIR *searchDir = opendir(_path);
if(searchDir != NULL)
{
struct dirent *entry;
while((entry=readdir(searchDir))!=NULL)
{
if(entry->d_type == _type)
{
if(string(entry->d_name)!="." && string(entry->d_name)!="..")
{
bool fileTypeIsAlowed = false;
if(!_allowedExtensions)
fileTypeIsAlowed = true;
else
{
string curFilename = entry->d_name;
for(unsigned int i=0;i<curFilename.length();i++)
{
curFilename[i]=(tolower(curFilename[i]));
}
for(vector<string>::const_iterator it_ext = _allowedExtensions->begin();it_ext!=_allowedExtensions->end();it_ext++)
{
string extension = (*it_ext);
if(curFilename.find(extension)!=string::npos)
{
fileTypeIsAlowed = true;
break; //Found the right extension
}
}
}
if(fileTypeIsAlowed)
{
cout<<"found "<<entry->d_name<<"\n";
_return_files.push_back(entry->d_name);
fileCount++;
}
}
}
}
}
return fileCount;
};
Code: Select all
FILE *file = fopen("somefile.txt","w");
vector<string> vector_extensions;
vector_extensions.push_back(".jpg");
vector<string> vector_entries;
if(listDirectory(sourceFolder.c_str(),vector_entries,&vector_extensions)>0)
{
unsigned int i=1;
for(vector<string>::const_iterator it_entry = _vector_entries.begin(); it_entry != _vector_entries.end();it_entry++)
{
string tmpEntry = (*it_entry);
fprintf(file,"\"%s\n\"",tmpEntry.c_str());
i++;
}
}
fclose(file);
The solution should be to use stringw/wchar_t* with WDIR and wopendir, which aren't available for me (mac os x, xcode, gcc).
Anyone with experience on this problem? What should I do? (Oh, yeah, I can't just change the filenames, since I won't be naming the files, but I have to try to make it as userfriendly as possible)
greetings