Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » A5.1.8 's al_get_standard_path has some problem

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
A5.1.8 's al_get_standard_path has some problem
vkensou
Member #15,546
March 2014

In windows al_get_standard_path() return ANICODE string,not utf-8. This may cause confusion in actual use.
this is my patch:
in file <wsystem.c> replace _al_win_get_path()

#SelectExpand
1ALLEGRO_PATH *_al_win_get_path(int id) 2{ 3 char path[MAX_PATH]; 4 uint32_t csidl = 0; 5 HRESULT ret = 0; 6 ALLEGRO_PATH *cisdl_path = NULL; 7 8 switch (id) { 9 case ALLEGRO_TEMP_PATH: { 10 /* Check: TMP, TMPDIR, TEMP or TEMPDIR */ 11 wchar_t pathw[MAX_PATH]; 12 DWORD ret = GetTempPathW(MAX_PATH, pathw); 13 WideCharToMultiByte(CP_UTF8, 0, pathw, -1, path, wcslen(pathw) + 1, NULL, NULL); 14 if (ret > MAX_PATH) { 15 /* should this ever happen, windows is more broken than I ever thought */ 16 return NULL; 17 } 18 19 return al_create_path_for_directory(path); 20 21 } break; 22 23 case ALLEGRO_RESOURCES_PATH: { /* where the program is in */ 24 HANDLE process = GetCurrentProcess(); 25 char *ptr; 26 wchar_t pathw[MAX_PATH]; 27 GetModuleFileNameExW(process, NULL, pathw, MAX_PATH); 28 WideCharToMultiByte(CP_UTF8, 0, pathw, -1, path, wcslen(pathw) + 1, NULL, NULL); 29 ptr = strrchr(path, '\\'); 30 if (!ptr) { /* shouldn't happen */ 31 return NULL; 32 } 33 34 /* chop off everything including and after the last slash */ 35 /* should this not chop the slash? */ 36 *ptr = '\0'; 37 38 return al_create_path_for_directory(path); 39 } break; 40 41 case ALLEGRO_USER_DATA_PATH: /* CSIDL_APPDATA */ 42 case ALLEGRO_USER_SETTINGS_PATH: 43 csidl = CSIDL_APPDATA; 44 break; 45 46 case ALLEGRO_USER_HOME_PATH: /* CSIDL_PROFILE */ 47 csidl = CSIDL_PROFILE; 48 break; 49 50 case ALLEGRO_USER_DOCUMENTS_PATH: /* CSIDL_PERSONAL */ 51 csidl = CSIDL_PERSONAL; 52 break; 53 54 case ALLEGRO_EXENAME_PATH: { /* full path to the exe including its name */ 55 HANDLE process = GetCurrentProcess(); 56 wchar_t pathw[MAX_PATH]; 57 GetModuleFileNameExW(process, NULL, pathw, MAX_PATH); 58 WideCharToMultiByte(CP_UTF8, 0, pathw, -1, path, wcslen(pathw) + 1, NULL, NULL); 59 60 return al_create_path(path); 61 } break; 62 63 default: 64 return NULL; 65 } 66 wchar_t pathw[MAX_PATH]; 67 ret = SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, pathw); 68 if (ret != S_OK) { 69 return NULL; 70 } 71 WideCharToMultiByte(CP_UTF8, 0, pathw, -1, path, wcslen(pathw) + 1, NULL, NULL); 72 73 cisdl_path = al_create_path_for_directory(path); 74 if (!cisdl_path) 75 return NULL; 76 77 if (csidl == CSIDL_APPDATA) { 78 const char *org_name = al_get_org_name(); 79 const char *app_name = al_get_app_name(); 80 81 if (!app_name || !app_name[0]) { 82 /* this shouldn't ever happen. */ 83 al_destroy_path(cisdl_path); 84 return NULL; 85 } 86 87 if (org_name && org_name[0]) { 88 al_append_path_component(cisdl_path, org_name); 89 } 90 91 al_append_path_component(cisdl_path, app_name); 92 } 93 94 return cisdl_path; 95}

:D

this is the patch

#SelectExpand
1diff --git "a/C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\TortoiseGit\\wsy2995.tmp\\wsystem-830bcab-left.c" "b/E:\\myroom\\libraries\\allegro\\src\\win\\wsystem.c" 2index 68fb78e..6c72b08 100644 3--- "a/C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\TortoiseGit\\wsy2995.tmp\\wsystem-830bcab-left.c" 4+++ "b/E:\\myroom\\libraries\\allegro\\src\\win\\wsystem.c" 5@@ -450,7 +450,9 @@ ALLEGRO_PATH *_al_win_get_path(int id) 6 switch (id) { 7 case ALLEGRO_TEMP_PATH: { 8 /* Check: TMP, TMPDIR, TEMP or TEMPDIR */ 9- DWORD ret = GetTempPath(MAX_PATH, path); 10+ wchar_t pathw[MAX_PATH]; 11+ DWORD ret = GetTempPathW(MAX_PATH, pathw); 12+ WideCharToMultiByte(CP_UTF8, 0, pathw, -1, path, wcslen(pathw) + 1, NULL, NULL); 13 if (ret > MAX_PATH) { 14 /* should this ever happen, windows is more broken than I ever thought */ 15 return NULL; 16@@ -463,7 +465,9 @@ ALLEGRO_PATH *_al_win_get_path(int id) 17 case ALLEGRO_RESOURCES_PATH: { /* where the program is in */ 18 HANDLE process = GetCurrentProcess(); 19 char *ptr; 20- GetModuleFileNameEx(process, NULL, path, MAX_PATH); 21+ wchar_t pathw[MAX_PATH]; 22+ GetModuleFileNameExW(process, NULL, pathw, MAX_PATH); 23+ WideCharToMultiByte(CP_UTF8, 0, pathw, -1, path, wcslen(pathw) + 1, NULL, NULL); 24 ptr = strrchr(path, '\\'); 25 if (!ptr) { /* shouldn't happen */ 26 return NULL; 27@@ -491,7 +495,9 @@ ALLEGRO_PATH *_al_win_get_path(int id) 28 29 case ALLEGRO_EXENAME_PATH: { /* full path to the exe including its name */ 30 HANDLE process = GetCurrentProcess(); 31- GetModuleFileNameEx(process, NULL, path, MAX_PATH); 32+ wchar_t pathw[MAX_PATH]; 33+ GetModuleFileNameExW(process, NULL, pathw, MAX_PATH); 34+ WideCharToMultiByte(CP_UTF8, 0, pathw, -1, path, wcslen(pathw) + 1, NULL, NULL); 35 36 return al_create_path(path); 37 } break; 38@@ -500,7 +506,9 @@ ALLEGRO_PATH *_al_win_get_path(int id) 39 return NULL; 40 } 41 42- ret = SHGetFolderPath(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, path); 43+ wchar_t pathw[MAX_PATH]; 44+ ret = SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, pathw); 45+ WideCharToMultiByte(CP_UTF8, 0, pathw, -1, path, wcslen(pathw) + 1, NULL, NULL); 46 if (ret != S_OK) { 47 return NULL; 48 }

Edgar Reynaldo
Member #8,592
May 2007
avatar

vkensou
Member #15,546
March 2014

to Edgar Reynaldo:

thanks your suggestion:D

Elias
Member #358
May 2000

Thanks! I slightly modified the patch to the below one. (Mainly use our own utf16 conversion instead of the Windows one.) With which name and email do you want to appear in the commit message?

#SelectExpand
1diff --git a/src/win/wsystem.c b/src/win/wsystem.c 2index 68fb78e..5348bcf 100644 3--- a/src/win/wsystem.c 4+++ b/src/win/wsystem.c 5@@ -443,6 +443,8 @@ static ALLEGRO_TOUCH_INPUT_DRIVER *win_get_touch_input_driver(void) 6 ALLEGRO_PATH *_al_win_get_path(int id) 7 { 8 char path[MAX_PATH]; 9+ wchar_t pathw[MAX_PATH]; 10+ ALLEGRO_USTR *pathu; 11 uint32_t csidl = 0; 12 HRESULT ret = 0; 13 ALLEGRO_PATH *cisdl_path = NULL; 14@@ -450,12 +452,15 @@ ALLEGRO_PATH *_al_win_get_path(int id) 15 switch (id) { 16 case ALLEGRO_TEMP_PATH: { 17 /* Check: TMP, TMPDIR, TEMP or TEMPDIR */ 18- DWORD ret = GetTempPath(MAX_PATH, path); 19+ 20+ DWORD ret = GetTempPathW(MAX_PATH, pathw); 21 if (ret > MAX_PATH) { 22 /* should this ever happen, windows is more broken than I ever thought */ 23 return NULL; 24 } 25- 26+ pathu = al_ustr_new_from_utf16(pathw); 27+ al_ustr_to_buffer(pathu, path, sizeof path); 28+ al_ustr_free(pathu); 29 return al_create_path_for_directory(path); 30 31 } break; 32@@ -463,7 +468,11 @@ ALLEGRO_PATH *_al_win_get_path(int id) 33 case ALLEGRO_RESOURCES_PATH: { /* where the program is in */ 34 HANDLE process = GetCurrentProcess(); 35 char *ptr; 36- GetModuleFileNameEx(process, NULL, path, MAX_PATH); 37+ 38+ GetModuleFileNameExW(process, NULL, pathw, MAX_PATH); 39+ pathu = al_ustr_new_from_utf16(pathw); 40+ al_ustr_to_buffer(pathu, path, sizeof path); 41+ al_ustr_free(pathu); 42 ptr = strrchr(path, '\\'); 43 if (!ptr) { /* shouldn't happen */ 44 return NULL; 45@@ -491,7 +500,11 @@ ALLEGRO_PATH *_al_win_get_path(int id) 46 47 case ALLEGRO_EXENAME_PATH: { /* full path to the exe including its name */ 48 HANDLE process = GetCurrentProcess(); 49- GetModuleFileNameEx(process, NULL, path, MAX_PATH); 50+ 51+ GetModuleFileNameExW(process, NULL, pathw, MAX_PATH); 52+ pathu = al_ustr_new_from_utf16(pathw); 53+ al_ustr_to_buffer(pathu, path, sizeof path); 54+ al_ustr_free(pathu); 55 56 return al_create_path(path); 57 } break; 58@@ -500,11 +513,15 @@ ALLEGRO_PATH *_al_win_get_path(int id) 59 return NULL; 60 } 61 62- ret = SHGetFolderPath(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, path); 63+ ret = SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, pathw); 64 if (ret != S_OK) { 65 return NULL; 66 } 67 68+ pathu = al_ustr_new_from_utf16(pathw); 69+ al_ustr_to_buffer(pathu, path, sizeof path); 70+ al_ustr_free(pathu); 71+ 72 cisdl_path = al_create_path_for_directory(path); 73 if (!cisdl_path) 74 return NULL;

--
"Either help out or stop whining" - Evert

vkensou
Member #15,546
March 2014

:D

to Elias:
I just looked at the forum today. Patch has been applied to the latest version.
use name: vkensou and email:boywanyx@hotmail.com
is it too late to tell you?:D

Go to: