[Gdal-dev] Wide-character filenames with GDAL file IO?

Mateusz Loskot mateusz at loskot.net
Tue Sep 19 18:37:40 EDT 2006


Ben Discoe wrote:
>> -----
>> From: Mateusz Loskot
>> Sent: Tuesday, September 19, 2006 8:46 AM
>>
>> Ben Discoe wrote:
>>> Another option is for me to open the file first, e.g. for Windows:
>>> 	std::wstring fname;
>>> 	FILE *fp = _wfopen(fname, L"rb");
>> --------------------------^^^^^^^
>>
>> A side note, there is no implicit conversion defined
>> You need to use fname.c_str().
> 
> You are correct, sorry i left that out.  The code would be like this:
> 
> FILE *GDALFileOpen(const char *fname_utf8, const char *mode)
> {
> #if WIN32	// Windows
> 	wstring2 fn, mo(mode);
> 	fn.from_utf8(fname_utf8);
> 	return _wfopen(fn.c_str(), mo.c_str());
> #elif __DARWIN_OSX__	// Mac OS X
> 	return fopen(fname_utf8, mode);
> #else	// some other Unix flavor
> 	wstring2 fn, mo(mode);
> 	fn.from_utf8(fname_utf8);
> 	return fopen(fn.mb_str(), mode);
> #endif
> }
> 
> This is using the small "wstring2" class which is just std::wstring plus
> methods for converting to and from utf-8 and mbs.
> 
> The problem i have _not_ solved is what to do on Windows with code which
> uses std::ifstream() instead of fopen().

std::ifstream is a type from the C++ library but fopen() is a function
from the C library.
I'm not sure I understand what's here to be solved and what you mean
"Windows with code which uses".

Simply, you have to decide if you use streams or file descriptor, both
are different approaches used in different languages (C/C++) and are
based on different libraries (C/C++).

> MSDN documents that there is a
> (wchar_t*) version of std::ifstream() constructor
> (http://msdn2.microsoft.com/en-us/library/zek0beca.aspx),

Simply, there are two specializations of basic_ifstream,
one for char -> ifsteram and one for wchar_t -> wifstream.
The idea is the same as behind two specializations of basic_string:
string and wstring.

ifstream and wifstream are typedefs of the basic_ifstream specializations:

typedef basic_ifstream<char> ifstream;
typedef basic_ifstream<wchar_t> wifstream;

> but it does not
> appear in the headers/libs that come with MSVC 7.1.

AFAIR there is <fstream> header in C++ library provided with
Visual C++ 7.1 compiler.
Streams are available with all C++ standard compliant compilers,
Visual C++ 7.1 is considered as the first ISO C++ compliant version.
So, it should work perfectly.

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Gdal-dev mailing list