paths.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author: Ben Allison [email protected]
  6. ** Created:
  7. **/
  8. #include <windows.h>
  9. #include <shlwapi.h>
  10. #include <strsafe.h>
  11. #include <shlobj.h>
  12. #include <shellapi.h>
  13. #include "../Winamp/in2.h"
  14. extern In_Module mod;
  15. #ifdef __cplusplus
  16. extern "C" BOOL UtilGetSpecialFolderPath(HWND hwnd, TCHAR *path, int folder)
  17. #else
  18. BOOL UtilGetSpecialFolderPath(HWND hwnd, TCHAR *path, int folder)
  19. #endif
  20. {
  21. ITEMIDLIST *pidl; // Shell Item ID List ptr
  22. IMalloc *imalloc; // Shell IMalloc interface ptr
  23. BOOL result; // Return value
  24. if (SHGetSpecialFolderLocation(hwnd, folder, &pidl) != NOERROR)
  25. return FALSE;
  26. result = SHGetPathFromIDList (pidl, path);
  27. if (SHGetMalloc (&imalloc) == NOERROR)
  28. {
  29. #ifdef __cplusplus
  30. imalloc->Free(pidl);
  31. imalloc->Release();
  32. #else
  33. imalloc->lpVtbl->Free(imalloc, pidl);
  34. imalloc->lpVtbl->Release(imalloc);
  35. #endif
  36. }
  37. return result;
  38. }
  39. /*
  40. This ugly function converts our specially coded paths
  41. environment variables are surrounded with %'s and CSIDLs are surrounded with {}
  42. note that CSIDLs need to be DECIMAL!
  43. */
  44. #ifdef __cplusplus
  45. extern "C" void ResolveEnvironmentVariables2(wchar_t *string, wchar_t *destString, size_t stringSize)
  46. #else
  47. void ResolveEnvironmentVariables2(TCHAR *string, TCHAR *destString, size_t stringSize)
  48. #endif
  49. {
  50. //char *saveStart = string;
  51. wchar_t *p = string;
  52. int inPercent = 0, inBrace = 0;
  53. wchar_t environString[MAX_PATH] = {0};
  54. wchar_t *dest = destString;
  55. wchar_t helper[MAX_PATH] = {0};
  56. wchar_t *pEnv = environString;
  57. *dest = 0;
  58. while (p && *p)
  59. {
  60. if (*p == L'%')
  61. {
  62. if (inPercent)
  63. {
  64. *pEnv = 0;
  65. helper[0] = 0;
  66. GetEnvironmentVariableW(environString, helper, MAX_PATH);
  67. StringCchCatW(dest, stringSize, helper);
  68. pEnv = environString;
  69. string = CharNextW(p);
  70. *p = 0;
  71. p = string;
  72. }
  73. else
  74. {
  75. wchar_t *newP = CharNextW(p);
  76. *p = 0;
  77. StringCchCatW(dest, stringSize, string);
  78. string = p = newP;
  79. }
  80. inPercent = !inPercent;
  81. }
  82. else if (*p == L'{')
  83. {
  84. if (inPercent)
  85. {
  86. ptrdiff_t count = CharNextW(p) - p;
  87. while (count-- && (pEnv - environString) < MAX_PATH)
  88. *pEnv++ = *p++;
  89. }
  90. else
  91. {
  92. wchar_t *newP = CharNextW(p);
  93. *p = 0;
  94. StringCchCatW(dest, stringSize, string);
  95. string = p = newP;
  96. inBrace = 1;
  97. }
  98. }
  99. else if (*p == L'}')
  100. {
  101. if (inPercent)
  102. {
  103. ptrdiff_t count = CharNextW(p) - p;
  104. while (count-- && (pEnv - environString) < MAX_PATH)
  105. *pEnv++ = *p++;
  106. }
  107. *pEnv = 0;
  108. //SHGetSpecialFolderPath(NULL, helper, atoi(environString), FALSE);
  109. #if defined(UNICODE) || defined(_UNICODE)
  110. SHGetSpecialFolderPath(NULL, helper, StrToInt(environString), FALSE);
  111. //UtilGetSpecialFolderPath(NULL, helper, _wtoi(environString));
  112. #else
  113. SHGetSpecialFolderPathW(NULL, helper, StrToIntW(environString), FALSE);
  114. //UtilGetSpecialFolderPath(NULL, helper, _aoi(environString));
  115. #endif
  116. StringCchCatW(dest, stringSize, helper);
  117. pEnv = environString;
  118. string = CharNextW(p);
  119. *p = 0;
  120. p = string;
  121. inBrace = 0;
  122. }
  123. else
  124. {
  125. if (inPercent || inBrace)
  126. {
  127. ptrdiff_t count = CharNextW(p) - p;
  128. while (count-- && (pEnv - environString) < MAX_PATH)
  129. *pEnv++ = *p++;
  130. }
  131. else
  132. p = CharNextW(p);
  133. }
  134. }
  135. StringCchCatW(dest, stringSize, string);
  136. }
  137. #ifndef NO_INPLACE_RESOLVE
  138. #include <malloc.h>
  139. // call this function if you want to modify in-place
  140. #include "main.h"
  141. #ifdef __cplusplus
  142. extern "C" void ResolveEnvironmentVariables(wchar_t *string, size_t stringSize) //
  143. #else
  144. void ResolveEnvironmentVariables(wchar_t *string, size_t stringSize) //
  145. #endif
  146. {
  147. wchar_t *dest = (wchar_t *) alloca(stringSize * sizeof(dest[0]));
  148. ResolveEnvironmentVariables2(string, dest, stringSize);
  149. StringCchCopyW(string, stringSize, dest);
  150. }
  151. #endif