Skip to content

Commit d48a6ec

Browse files
committed
Fixed dbf (DBFCreate_MW and DBFOpen_MW) to use correct utf-8 path.
1 parent 69e0d1d commit d48a6ec

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

src/Shapefile/dbf.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,41 @@
1212

1313
DBFHandle SHPAPI_CALL DBFOpen_MW( CStringW pszFilename, const char * pszAccess )
1414
{
15-
CStringA nameA = Utility::ConvertToAnsi1252(pszFilename);
15+
CStringA nameA = Utility::ConvertToUtf8(pszFilename);
16+
17+
// Save current locale, then switch CRT to UTF-8 so fopen() inside DBFOpen
18+
// interprets the path bytes as UTF-8 instead of the system ANSI code page.
19+
// Requires Windows 10 1803 or later.
20+
CStringA prevLocale = setlocale(LC_ALL, nullptr);
21+
setlocale(LC_ALL, ".UTF8");
22+
1623
m_globalSettings.SetGdalUtf8(true);
1724
DBFHandle handle = DBFOpen(nameA, pszAccess);
1825
m_globalSettings.SetGdalUtf8(false);
26+
27+
// Restore previous locale
28+
setlocale(LC_ALL, prevLocale);
29+
1930
return handle;
2031
}
2132

2233
DBFHandle SHPAPI_CALL DBFCreate_MW( CStringW nameW )
2334
{
24-
CStringA nameA = Utility::ConvertToAnsi1252(nameW);
35+
CStringA nameA = Utility::ConvertToUtf8(nameW);
36+
37+
// Save current locale, then switch CRT to UTF-8 so fopen() inside DBFCreate
38+
// interprets the path bytes as UTF-8 instead of the system ANSI code page.
39+
// Requires Windows 10 1803 or later.
40+
CStringA prevLocale = setlocale(LC_ALL, nullptr);
41+
setlocale(LC_ALL, ".UTF8");
42+
2543
m_globalSettings.SetGdalUtf8(true);
2644
DBFHandle handle = DBFCreate(nameA);
2745
m_globalSettings.SetGdalUtf8(false);
46+
47+
// Restore previous locale
48+
setlocale(LC_ALL, prevLocale);
49+
2850
return handle;
2951
}
3052

0 commit comments

Comments
 (0)