common.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "./common.h"
  2. #include "./wasabi.h"
  3. #include "./main.h"
  4. #include "../winamp/wa_ipc.h"
  5. #include <strsafe.h>
  6. LPWSTR Plugin_MallocString(size_t cchLen)
  7. {
  8. return (LPWSTR)malloc(sizeof(WCHAR) * cchLen);
  9. }
  10. void Plugin_FreeString(LPWSTR pszString)
  11. {
  12. if (NULL != pszString)
  13. {
  14. free(pszString);
  15. }
  16. }
  17. LPWSTR Plugin_ReAllocString(LPWSTR pszString, size_t cchLen)
  18. {
  19. return (LPWSTR)realloc(pszString, sizeof(WCHAR) * cchLen);
  20. }
  21. LPWSTR Plugin_CopyString(LPCWSTR pszSource)
  22. {
  23. if (NULL == pszSource)
  24. return NULL;
  25. INT cchSource = lstrlenW(pszSource) + 1;
  26. LPWSTR copy = Plugin_MallocString(cchSource);
  27. if (NULL != copy)
  28. {
  29. CopyMemory(copy, pszSource, sizeof(WCHAR) * cchSource);
  30. }
  31. return copy;
  32. }
  33. LPSTR Plugin_MallocAnsiString(size_t cchLen)
  34. {
  35. return (LPSTR)malloc(sizeof(CHAR) * cchLen);
  36. }
  37. LPSTR Plugin_CopyAnsiString(LPCSTR pszSource)
  38. {
  39. if (NULL == pszSource)
  40. return NULL;
  41. INT cchSource = lstrlenA(pszSource) + 1;
  42. LPSTR copy = Plugin_MallocAnsiString(cchSource);
  43. if (NULL != copy)
  44. {
  45. CopyMemory(copy, pszSource, sizeof(CHAR) * cchSource);
  46. }
  47. return copy;
  48. }
  49. void Plugin_FreeAnsiString(LPSTR pszString)
  50. {
  51. Plugin_FreeString((LPWSTR)pszString);
  52. }
  53. LPSTR Plugin_WideCharToMultiByte(UINT codePage, DWORD dwFlags, LPCWSTR lpWideCharStr, INT cchWideChar, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar)
  54. {
  55. INT cchBuffer = WideCharToMultiByte(codePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
  56. if (0 == cchBuffer) return NULL;
  57. LPSTR buffer = Plugin_MallocAnsiString(cchBuffer);
  58. if (NULL == buffer) return NULL;
  59. if (0 == WideCharToMultiByte(codePage, dwFlags, lpWideCharStr, cchWideChar, buffer, cchBuffer, lpDefaultChar, lpUsedDefaultChar))
  60. {
  61. Plugin_FreeAnsiString(buffer);
  62. return NULL;
  63. }
  64. return buffer;
  65. }
  66. LPWSTR Plugin_MultiByteToWideChar(UINT codePage, DWORD dwFlags, LPCSTR lpMultiByteStr, INT cbMultiByte)
  67. {
  68. if (NULL == lpMultiByteStr) return NULL;
  69. INT cchBuffer = MultiByteToWideChar(codePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
  70. if (NULL == cchBuffer) return NULL;
  71. if (cbMultiByte > 0) cchBuffer++;
  72. LPWSTR buffer = Plugin_MallocString(cchBuffer);
  73. if (NULL == buffer) return NULL;
  74. if (0 == MultiByteToWideChar(codePage, dwFlags, lpMultiByteStr, cbMultiByte, buffer, cchBuffer))
  75. {
  76. Plugin_FreeString(buffer);
  77. return NULL;
  78. }
  79. if (cbMultiByte > 0)
  80. {
  81. buffer[cchBuffer - 1] = L'\0';
  82. }
  83. return buffer;
  84. }
  85. LPWSTR Plugin_DuplicateResString(LPCWSTR pszResource)
  86. {
  87. return (IS_INTRESOURCE(pszResource)) ?
  88. (LPWSTR)pszResource :
  89. Plugin_CopyString(pszResource);
  90. }
  91. void Plugin_FreeResString(LPWSTR pszResource)
  92. {
  93. if (!IS_INTRESOURCE(pszResource))
  94. Plugin_FreeString(pszResource);
  95. }
  96. HRESULT Plugin_CopyResString(LPWSTR pszBuffer, INT cchBufferMax, LPCWSTR pszString)
  97. {
  98. if (NULL == pszBuffer)
  99. return E_INVALIDARG;
  100. HRESULT hr = S_OK;
  101. if (NULL == pszString)
  102. {
  103. pszBuffer[0] = L'\0';
  104. }
  105. else if (IS_INTRESOURCE(pszString))
  106. {
  107. if (NULL == WASABI_API_LNG)
  108. hr = E_FAIL;
  109. else
  110. WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)pszString, pszBuffer, cchBufferMax);
  111. }
  112. else
  113. {
  114. hr = StringCchCopy(pszBuffer, cchBufferMax, pszString);
  115. }
  116. return hr;
  117. }
  118. void Plugin_SafeRelease(IUnknown *pUnk)
  119. {
  120. if (NULL != pUnk)
  121. pUnk->Release();
  122. }
  123. HRESULT Plugin_MakeResourcePath(LPWSTR pszBuffer, INT cchBufferMax, LPCWSTR pszType, LPCWSTR pszName, UINT flags)
  124. {
  125. HINSTANCE hInstance = WASABI_API_LNG_HINST;
  126. if (NULL == hInstance || NULL == FindResource(hInstance, pszName, pszType))
  127. hInstance = Plugin_GetInstance();
  128. if (NULL == OMUTILITY)
  129. return E_UNEXPECTED;
  130. return OMUTILITY->MakeResourcePath(pszBuffer, cchBufferMax, hInstance, pszType, pszName, flags);
  131. }
  132. HWND Plugin_GetDialogOwner(void)
  133. {
  134. HWND hOwner= Plugin_GetLibrary();
  135. if (NULL == hOwner || FALSE == IsWindowVisible(hOwner) ||
  136. FALSE == IsWindowEnabled(hOwner))
  137. {
  138. hOwner = Plugin_GetWinamp();
  139. if (NULL != hOwner)
  140. {
  141. HWND hDlgParent = (HWND)SENDWAIPC(hOwner, IPC_GETDIALOGBOXPARENT, 0L);
  142. if (NULL != hDlgParent)
  143. hOwner = hDlgParent;
  144. }
  145. }
  146. return hOwner;
  147. }
  148. HRESULT Plugin_AppendFileFilter(LPTSTR pszBuffer, size_t cchBufferMax, LPCTSTR pName, LPCTSTR pFilter, LPTSTR *ppBufferOut, size_t *pRemaining, BOOL bShowFilter)
  149. {
  150. HRESULT hr;
  151. LPTSTR pCursor = pszBuffer;
  152. if (NULL != ppBufferOut)
  153. *ppBufferOut = pszBuffer;
  154. if (NULL != pRemaining)
  155. *pRemaining = cchBufferMax;
  156. if (NULL == pszBuffer || NULL == pName || NULL == pFilter)
  157. return E_INVALIDARG;
  158. pszBuffer[0] = TEXT('\0');
  159. hr = StringCchCopyEx(pCursor, cchBufferMax, pName, &pCursor, &cchBufferMax,
  160. STRSAFE_IGNORE_NULLS | STRSAFE_NULL_ON_FAILURE);
  161. if (bShowFilter && SUCCEEDED(hr))
  162. {
  163. LPTSTR p = pCursor;
  164. hr = StringCchPrintfEx(pCursor, cchBufferMax, &pCursor, &cchBufferMax,
  165. STRSAFE_IGNORE_NULLS | STRSAFE_NULL_ON_FAILURE, TEXT(" (%s)"), pFilter);
  166. if (SUCCEEDED(hr) && p != pCursor)
  167. CharLowerBuff(p, (INT)(INT_PTR)(pCursor - p));
  168. }
  169. if (SUCCEEDED(hr))
  170. {
  171. pCursor++;
  172. cchBufferMax--;
  173. hr = StringCchCopyEx(pCursor, cchBufferMax, pFilter, &pCursor, &cchBufferMax,
  174. STRSAFE_IGNORE_NULLS | STRSAFE_NULL_ON_FAILURE);
  175. }
  176. if (cchBufferMax < 1)
  177. hr = STRSAFE_E_INSUFFICIENT_BUFFER;
  178. pCursor++;
  179. cchBufferMax--;
  180. if (SUCCEEDED(hr))
  181. {
  182. pCursor[0] = TEXT('\0');
  183. if (NULL != ppBufferOut)
  184. *ppBufferOut = pCursor;
  185. if (NULL != pRemaining)
  186. *pRemaining = cchBufferMax;
  187. }
  188. else
  189. {
  190. pszBuffer[0] = TEXT('\0');
  191. pszBuffer[1] = TEXT('\0');
  192. }
  193. return hr;
  194. }