1
0

util.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "main.h"
  2. #include "./util.h"
  3. #include "api__ml_wire.h"
  4. #include <strsafe.h>
  5. LPWSTR Plugin_MallocString( size_t cchLen )
  6. {
  7. return (LPWSTR)calloc( cchLen, sizeof( WCHAR ) );
  8. }
  9. void Plugin_FreeString( LPWSTR pszString )
  10. {
  11. if ( NULL != pszString )
  12. {
  13. free( pszString );
  14. }
  15. }
  16. LPWSTR Plugin_ReAllocString( LPWSTR pszString, size_t cchLen )
  17. {
  18. return (LPWSTR)realloc( pszString, sizeof( WCHAR ) * cchLen );
  19. }
  20. LPWSTR Plugin_CopyString( LPCWSTR pszSource )
  21. {
  22. if ( NULL == pszSource )
  23. return NULL;
  24. //INT cchSource = lstrlenW( pszSource ) + 1;
  25. //LPWSTR copy = Plugin_MallocString( cchSource );
  26. //if ( NULL != copy )
  27. //{
  28. // CopyMemory( copy, pszSource, sizeof( WCHAR ) * cchSource );
  29. //}
  30. //
  31. return _wcsdup( pszSource );
  32. }
  33. LPSTR Plugin_MallocAnsiString( size_t cchLen )
  34. {
  35. return (LPSTR)calloc( cchLen, sizeof( CHAR ) );
  36. }
  37. LPSTR Plugin_CopyAnsiString( LPCSTR pszSource )
  38. {
  39. if ( NULL == pszSource )
  40. return NULL;
  41. //INT cchSource = lstrlenA(pszSource) + 1;
  42. //
  43. //LPSTR copy = Plugin_MallocAnsiString(cchSource);
  44. //if (NULL != copy)
  45. //{
  46. // CopyMemory(copy, pszSource, sizeof(CHAR) * cchSource);
  47. //}
  48. return _strdup( pszSource );
  49. }
  50. void Plugin_FreeAnsiString( LPSTR pszString )
  51. {
  52. Plugin_FreeString( (LPWSTR)pszString );
  53. }
  54. LPSTR Plugin_WideCharToMultiByte( UINT codePage, DWORD dwFlags, LPCWSTR lpWideCharStr, INT cchWideChar, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar )
  55. {
  56. INT cchBuffer = WideCharToMultiByte( codePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar );
  57. if ( 0 == cchBuffer )
  58. return NULL;
  59. LPSTR buffer = Plugin_MallocAnsiString( cchBuffer );
  60. if ( NULL == buffer )
  61. return NULL;
  62. if ( 0 == WideCharToMultiByte( codePage, dwFlags, lpWideCharStr, cchWideChar, buffer, cchBuffer, lpDefaultChar, lpUsedDefaultChar ) )
  63. {
  64. Plugin_FreeAnsiString( buffer );
  65. return NULL;
  66. }
  67. return buffer;
  68. }
  69. LPWSTR Plugin_MultiByteToWideChar( UINT codePage, DWORD dwFlags, LPCSTR lpMultiByteStr, INT cbMultiByte )
  70. {
  71. if ( NULL == lpMultiByteStr )
  72. return NULL;
  73. INT cchBuffer = MultiByteToWideChar( codePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0 );
  74. if ( NULL == cchBuffer )
  75. return NULL;
  76. if ( cbMultiByte > 0 )
  77. cchBuffer++;
  78. LPWSTR buffer = Plugin_MallocString( cchBuffer );
  79. if ( NULL == buffer )
  80. return NULL;
  81. if ( 0 == MultiByteToWideChar( codePage, dwFlags, lpMultiByteStr, cbMultiByte, buffer, cchBuffer ) )
  82. {
  83. Plugin_FreeString( buffer );
  84. return NULL;
  85. }
  86. if ( cbMultiByte > 0 )
  87. {
  88. buffer[ cchBuffer - 1 ] = L'\0';
  89. }
  90. return buffer;
  91. }
  92. LPWSTR Plugin_DuplicateResString( LPCWSTR pszResource )
  93. {
  94. return ( IS_INTRESOURCE( pszResource ) ) ? (LPWSTR)pszResource : Plugin_CopyString( pszResource );
  95. }
  96. void Plugin_FreeResString( LPWSTR pszResource )
  97. {
  98. if ( !IS_INTRESOURCE( pszResource ) )
  99. Plugin_FreeString( pszResource );
  100. }
  101. HRESULT Plugin_CopyResString( LPWSTR pszBuffer, INT cchBufferMax, LPCWSTR pszString )
  102. {
  103. if ( NULL == pszBuffer )
  104. return E_INVALIDARG;
  105. HRESULT hr = S_OK;
  106. if ( NULL == pszString )
  107. {
  108. pszBuffer[ 0 ] = L'\0';
  109. }
  110. else if ( IS_INTRESOURCE( pszString ) )
  111. {
  112. if ( NULL == WASABI_API_LNG )
  113. hr = E_FAIL;
  114. else
  115. WASABI_API_LNGSTRINGW_BUF( (INT)(INT_PTR)pszString, pszBuffer, cchBufferMax );
  116. }
  117. else
  118. {
  119. hr = StringCchCopy( pszBuffer, cchBufferMax, pszString );
  120. }
  121. return hr;
  122. }
  123. void Plugin_SafeRelease( IUnknown *pUnk )
  124. {
  125. if ( NULL != pUnk )
  126. pUnk->Release();
  127. }
  128. HRESULT Plugin_FileExtensionFromUrl( LPWSTR pszBuffer, INT cchBufferMax, LPCWSTR pszUrl, LPCWSTR defaultExtension )
  129. {
  130. LPCWSTR cursor = pszUrl;
  131. while ( L'\0' != *cursor && L'?' != *cursor )
  132. cursor = CharNext( cursor );
  133. LPCWSTR end = cursor;
  134. while ( cursor != pszUrl && L'/' != *cursor && L'\\' != *cursor && L'.' != *cursor )
  135. cursor = CharPrev( pszUrl, cursor );
  136. if ( L'.' == *cursor && cursor != pszUrl )
  137. {
  138. if ( CharNext( cursor ) < end )
  139. {
  140. INT cchExtension = (INT)(INT_PTR)( end - cursor );
  141. return StringCchCopyN( pszBuffer, cchBufferMax, cursor, cchExtension );
  142. }
  143. }
  144. return StringCchCopy( pszBuffer, cchBufferMax, defaultExtension );
  145. }
  146. void Plugin_ReplaceBadPathChars( LPWSTR pszPath )
  147. {
  148. if ( NULL == pszPath )
  149. return;
  150. while ( L'\0' != *pszPath )
  151. {
  152. switch ( *pszPath )
  153. {
  154. case L'?':
  155. case L'/':
  156. case L'\\':
  157. case L':':
  158. case L'*':
  159. case L'\"':
  160. case L'<':
  161. case L'>':
  162. case L'|':
  163. *pszPath = L'_';
  164. break;
  165. default:
  166. if ( *pszPath < 32 )
  167. *pszPath = L'_';
  168. break;
  169. }
  170. pszPath = CharNextW( pszPath );
  171. }
  172. }
  173. INT Plugin_CleanDirectory( LPWSTR pszPath )
  174. {
  175. if ( NULL == pszPath )
  176. return 0;
  177. INT cchPath = lstrlen( pszPath );
  178. LPWSTR cursor = pszPath + cchPath;
  179. while ( cursor-- != pszPath && ( L' ' == *cursor || L'.' == *cursor ) )
  180. *cursor = L'\0';
  181. return ( cchPath - (INT)(INT_PTR)( cursor - pszPath ) - 1 );
  182. }
  183. HRESULT Plugin_EnsurePathExist( LPCWSTR pszDirectory )
  184. {
  185. DWORD ec = ERROR_SUCCESS;
  186. UINT errorMode = SetErrorMode( SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS );
  187. if ( 0 == CreateDirectory( pszDirectory, NULL ) )
  188. {
  189. ec = GetLastError();
  190. if ( ERROR_PATH_NOT_FOUND == ec )
  191. {
  192. LPCWSTR pszBlock = pszDirectory;
  193. WCHAR szBuffer[ MAX_PATH ] = { 0 };
  194. LPCTSTR pszCursor = PathFindNextComponent( pszBlock );
  195. ec = ( pszCursor == pszBlock || S_OK != StringCchCopyN( szBuffer, ARRAYSIZE( szBuffer ), pszBlock, ( pszCursor - pszBlock ) ) ) ? ERROR_INVALID_NAME : ERROR_SUCCESS;
  196. pszBlock = pszCursor;
  197. while ( ERROR_SUCCESS == ec && NULL != ( pszCursor = PathFindNextComponent( pszBlock ) ) )
  198. {
  199. if ( pszCursor == pszBlock || S_OK != StringCchCatN( szBuffer, ARRAYSIZE( szBuffer ), pszBlock, ( pszCursor - pszBlock ) ) )
  200. ec = ERROR_INVALID_NAME;
  201. if ( ERROR_SUCCESS == ec && !CreateDirectory( szBuffer, NULL ) )
  202. {
  203. ec = GetLastError();
  204. if ( ERROR_ALREADY_EXISTS == ec )
  205. ec = ERROR_SUCCESS;
  206. }
  207. pszBlock = pszCursor;
  208. }
  209. }
  210. if ( ERROR_ALREADY_EXISTS == ec )
  211. ec = ERROR_SUCCESS;
  212. }
  213. SetErrorMode( errorMode );
  214. SetLastError( ec );
  215. return HRESULT_FROM_WIN32( ec );
  216. }