Listing and Writing Filenames with Unicode (mac)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
freetimecoder
Posts: 226
Joined: Fri Aug 22, 2008 8:50 pm
Contact:

Listing and Writing Filenames with Unicode (mac)

Post by freetimecoder »

Hi,

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);

Strangely "cout<<"found "<<entry->d_name<<"\n";" always prints the right filename, e.g. "© 134511-101.JPG" but the text in the file is "¬© 134511-101.JPG"

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
Nalin
Posts: 194
Joined: Thu Mar 30, 2006 12:34 am
Location: Lacey, WA, USA
Contact:

Re: Listing and Writing Filenames with Unicode (mac)

Post by Nalin »

freetimecoder wrote: The solution should be to use stringw/wchar_t* with WDIR and wopendir, which aren't available for me (mac os x, xcode, gcc).
Pretty sure it is returning things in UTF-8 format. Is it writing UTF-8 file names into your text file? Or is it just prefixing "garbage"? From your example, it looks like it is just prefixing stuff.

Why not try opening your text file in UTF-8 mode?
freetimecoder
Posts: 226
Joined: Fri Aug 22, 2008 8:50 pm
Contact:

Post by freetimecoder »

Yea, prefixing garbage names it. How do I open the text file in UTF-8 mode?
I only found the option to open it in binary mode ("wb") but that gives me the same errors.

greetings
Post Reply