1
0

fileview_compare.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "main.h"
  2. #include "./fileview.h"
  3. #include "./fileview_internal.h"
  4. #include "./resource.h"
  5. #include <strsafe.h>
  6. typedef struct _TYPEORDERINTERNAL
  7. {
  8. UINT index;
  9. WCHAR szName[64];
  10. } TYPEORDERINTERNAL;
  11. typedef INT (CALLBACK *SHLWAPI_STRCMPLOGICALW)(LPCWSTR, LPCWSTR);
  12. static SHLWAPI_STRCMPLOGICALW fnStrCmpLogicalW = NULL;
  13. static UINT szFileTypesSort[FVFT_LAST + 1];
  14. static FILEDATA *g_pCompareData = NULL;
  15. #define FILEREC(__idx) (g_pCompareData->pRec[(*(size_t*)(__idx))])
  16. #define FILEINFO(__idx) (FILEREC(__idx).Info)
  17. #define COMPARE_META(__elem1, __elem2)\
  18. {\
  19. if (NULL == FILEREC(__elem1).pMeta) FileViewMeta_Discover(g_pCompareData->szPath, &FILEREC(__elem1), NULL, NULL, 0);\
  20. if (NULL == FILEREC(__elem2).pMeta) FileViewMeta_Discover(g_pCompareData->szPath, &FILEREC(__elem2), NULL, NULL, 0);\
  21. if (NULL == FILEREC(__elem1).pMeta || NULL == FILEREC(__elem2).pMeta)\
  22. return ((INT)(ULONG_PTR)(FILEREC(__elem1).pMeta - FILEREC(__elem2).pMeta));\
  23. }
  24. #define COMPARE_STR_I(__str1, __str2)\
  25. ((NULL == (__str1) || NULL == (__str2)) ? ((INT)(ULONG_PTR)((__str1) - (__str2))) :\
  26. (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, (__str1), -1, (__str2), -1) - 2))
  27. #define COMPARE_META_STR_I(__elem1, __elem2, __metaField)\
  28. {\
  29. LPCWSTR s1, s2;\
  30. BOOL b1, b2;\
  31. COMPARE_META(__elem1, __elem2);\
  32. b1 = FileViewMeta_GetString(FILEREC(__elem1).pMeta, (__metaField), &s1);\
  33. b2 = FileViewMeta_GetString(FILEREC(__elem2).pMeta, (__metaField), &s2);\
  34. return (!b1 || !b2) ? (b1 - b2) : COMPARE_STR_I(s1, s2);\
  35. }
  36. #define COMPARE_META_INT(__elem1, __elem2, __metaField)\
  37. {\
  38. INT i1, i2;\
  39. BOOL b1, b2;\
  40. COMPARE_META(__elem1, __elem2);\
  41. b1 = FileViewMeta_GetInt(FILEREC(__elem1).pMeta, (__metaField), &i1);\
  42. b2 = FileViewMeta_GetInt(FILEREC(__elem2).pMeta, (__metaField), &i2);\
  43. return (!b1 || !b2) ? (b1 - b2) : (i1 - i2);\
  44. }
  45. __inline static int __cdecl FileRecord_CompareByName(const void *elem1, const void *elem2)
  46. {
  47. return (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  48. FILEINFO(elem1).cFileName, -1, FILEINFO(elem2).cFileName, -1) - 2);
  49. }
  50. __inline static int __cdecl FileRecord_CompareByNameLogical(const void *elem1, const void *elem2)
  51. {
  52. return fnStrCmpLogicalW(FILEINFO(elem1).cFileName, FILEINFO(elem2).cFileName);
  53. }
  54. __inline static int __cdecl FileRecord_CompareBySize(const void *elem1, const void *elem2)
  55. {
  56. return ((FILEINFO(elem1).nFileSizeHigh != FILEINFO(elem2).nFileSizeHigh) ?
  57. (FILEINFO(elem1).nFileSizeHigh - FILEINFO(elem2).nFileSizeHigh) :
  58. (FILEINFO(elem1).nFileSizeLow - FILEINFO(elem2).nFileSizeLow));
  59. }
  60. __inline static int __cdecl FileRecord_CompareByLastWriteTime(const void *elem1, const void *elem2)
  61. {
  62. return CompareFileTime(&FILEINFO(elem1).ftLastWriteTime, &FILEINFO(elem2).ftLastWriteTime);
  63. }
  64. __inline static int __cdecl FileRecord_CompareByType(const void *elem1, const void *elem2)
  65. {
  66. return szFileTypesSort[FILEREC(elem1).fileType] - szFileTypesSort[FILEREC(elem2).fileType];
  67. }
  68. __inline static int __cdecl FileRecord_CompareByAttributes(const void *elem1, const void *elem2)
  69. {
  70. wchar_t szTest1[32] = {0}, szTest2[32] = {0};
  71. FileView_FormatAttributes(FILEINFO(elem1).dwFileAttributes, szTest1, sizeof(szTest1)/sizeof(szTest1[0]));
  72. FileView_FormatAttributes(FILEINFO(elem2).dwFileAttributes, szTest2, sizeof(szTest2)/sizeof(szTest2[0]));
  73. return (CompareStringW(LOCALE_USER_DEFAULT, 0, szTest1, -1, szTest2, -1) - 2);
  74. }
  75. __inline static int __cdecl FileRecord_CompareByExtension(const void *elem1, const void *elem2)
  76. {
  77. if (0 == FILEREC(elem1).extOffset || 0 == FILEREC(elem2).extOffset)
  78. return ((INT)(FILEREC(elem1).extOffset - (INT)FILEREC(elem2).extOffset));
  79. return (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  80. FILEINFO(elem1).cFileName + FILEREC(elem1).extOffset, -1,
  81. FILEINFO(elem2).cFileName + FILEREC(elem2).extOffset, -1) - 2);
  82. }
  83. __inline static int __cdecl FileRecord_CompareByCreationTime(const void *elem1, const void *elem2)
  84. {
  85. return CompareFileTime(&FILEINFO(elem1).ftCreationTime, &FILEINFO(elem1).ftCreationTime);
  86. }
  87. __inline static int __cdecl FileRecord_CompareByAlbum(const void *elem1, const void *elem2)
  88. {
  89. COMPARE_META_STR_I(elem1, elem2, MF_ALBUM);
  90. }
  91. __inline static int __cdecl FileRecord_CompareByArtist(const void *elem1, const void *elem2)
  92. {
  93. COMPARE_META_STR_I(elem1, elem2, MF_ARTIST);
  94. }
  95. __inline static int __cdecl FileRecord_CompareByTitle(const void *elem1, const void *elem2)
  96. {
  97. COMPARE_META_STR_I(elem1, elem2, MF_TITLE);
  98. }
  99. __inline static int __cdecl FileRecord_CompareByMLDB(const void *elem1, const void *elem2)
  100. {
  101. COMPARE_META_INT(elem1, elem2, MF_SOURCE);
  102. }
  103. __inline static int __cdecl FileRecord_CompareByGenre(const void *elem1, const void *elem2)
  104. {
  105. COMPARE_META_STR_I(elem1, elem2, MF_GENRE);
  106. }
  107. __inline static int __cdecl FileRecord_CompareByYear(const void *elem1, const void *elem2)
  108. {
  109. COMPARE_META_INT(elem1, elem2, MF_YEAR);
  110. }
  111. __inline static int __cdecl FileRecord_CompareByLength(const void *elem1, const void *elem2)
  112. {
  113. COMPARE_META_INT(elem1, elem2, MF_LENGTH);
  114. }
  115. __inline static int __cdecl FileRecord_CompareByBitrate(const void *elem1, const void *elem2)
  116. {
  117. COMPARE_META_INT(elem1, elem2, MF_BITRATE);
  118. }
  119. __inline static int __cdecl FileRecord_CompareByTrack(const void *elem1, const void *elem2)
  120. {
  121. COMPARE_META_INT(elem1, elem2, MF_TRACKNUM);
  122. }
  123. __inline static int __cdecl FileRecord_CompareByDisc(const void *elem1, const void *elem2)
  124. {
  125. COMPARE_META_INT(elem1, elem2, MF_DISCNUM);
  126. }
  127. __inline static int __cdecl FileRecord_CompareByComment(const void *elem1, const void *elem2)
  128. {
  129. COMPARE_META_STR_I(elem1, elem2, MF_COMMENT);
  130. }
  131. __inline static int __cdecl FileRecord_CompareByPublisher(const void *elem1, const void *elem2)
  132. {
  133. COMPARE_META_STR_I(elem1, elem2, MF_PUBLISHER);
  134. }
  135. __inline static int __cdecl FileRecord_CompareByComposer(const void *elem1, const void *elem2)
  136. {
  137. COMPARE_META_STR_I(elem1, elem2, MF_COMPOSER);
  138. }
  139. __inline static int __cdecl FileRecord_CompareByAlbumArtist(const void *elem1, const void *elem2)
  140. {
  141. COMPARE_META_STR_I(elem1, elem2, MF_ALBUMARTIST);
  142. }
  143. __inline static int __cdecl CompareTypeOrderInternal(const void *elem1, const void *elem2)
  144. {
  145. return (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, ((TYPEORDERINTERNAL*)elem1)->szName, -1, ((TYPEORDERINTERNAL*)elem2)->szName, -1) - 2);
  146. }
  147. void FileView_SortByColumnEx(FILEDATA *pFileData, UINT uColumn, size_t *pOrder, size_t count)
  148. {
  149. if (pFileData && pFileData->pRec && pOrder && count > 1)
  150. {
  151. static BOOL bLoadFailed = FALSE;
  152. int (__cdecl *fnComparer)(const void *, const void *) = NULL;
  153. switch(uColumn)
  154. {
  155. case FVCOLUMN_NAME:
  156. if (NULL == fnStrCmpLogicalW && !bLoadFailed)
  157. {
  158. UINT prevErrorMode;
  159. HMODULE hModule;
  160. prevErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  161. hModule = LoadLibraryW(L"Shlwapi.dll");
  162. SetErrorMode(prevErrorMode);
  163. if (hModule)
  164. {
  165. fnStrCmpLogicalW = (SHLWAPI_STRCMPLOGICALW)GetProcAddress(hModule, "StrCmpLogicalW");
  166. FreeLibrary(hModule);
  167. }
  168. bLoadFailed = FALSE;
  169. }
  170. fnComparer = (fnStrCmpLogicalW) ? FileRecord_CompareByNameLogical : FileRecord_CompareByName;
  171. break;
  172. case FVCOLUMN_SIZE: fnComparer = FileRecord_CompareBySize; break;
  173. case FVCOLUMN_MODIFIED: fnComparer = FileRecord_CompareByLastWriteTime; break;
  174. case FVCOLUMN_TYPE:
  175. {
  176. TYPEORDERINTERNAL szOrder[sizeof(szFileTypesSort)/sizeof(szFileTypesSort[0])];
  177. for (int i = 0; i < sizeof(szFileTypesSort)/sizeof(szFileTypesSort[0]); i++)
  178. {
  179. szOrder[i].index = i;
  180. WASABI_API_LNGSTRINGW_BUF(i, szOrder[i].szName, sizeof(szOrder[i].szName)/sizeof(szOrder[i].szName[0]));
  181. }
  182. qsort(szOrder, sizeof(szFileTypesSort)/sizeof(szFileTypesSort[0]), sizeof(TYPEORDERINTERNAL), CompareTypeOrderInternal);
  183. for (int i = 0; i < sizeof(szFileTypesSort)/sizeof(szFileTypesSort[0]); i++) szFileTypesSort[szOrder[i].index] = i;
  184. }
  185. fnComparer = FileRecord_CompareByType;
  186. break;
  187. case FVCOLUMN_CREATED: fnComparer = FileRecord_CompareByCreationTime; break;
  188. case FVCOLUMN_ATTRIBUTES: fnComparer = FileRecord_CompareByAttributes; break;
  189. case FVCOLUMN_EXTENSION: fnComparer = FileRecord_CompareByExtension; break;
  190. case FVCOLUMN_ARTIST: fnComparer = FileRecord_CompareByArtist; break;
  191. case FVCOLUMN_ALBUM: fnComparer = FileRecord_CompareByAlbum; break;
  192. case FVCOLUMN_TITLE: fnComparer = FileRecord_CompareByTitle; break;
  193. case FVCOLUMN_INMLDB: fnComparer = FileRecord_CompareByMLDB; break;
  194. case FVCOLUMN_GENRE: fnComparer = FileRecord_CompareByGenre; break;
  195. case FVCOLUMN_YEAR: fnComparer = FileRecord_CompareByYear; break;
  196. case FVCOLUMN_LENGTH: fnComparer = FileRecord_CompareByLength; break;
  197. case FVCOLUMN_BITRATE: fnComparer = FileRecord_CompareByBitrate; break;
  198. case FVCOLUMN_TRACK: fnComparer = FileRecord_CompareByTrack; break;
  199. case FVCOLUMN_DISC: fnComparer = FileRecord_CompareByDisc; break;
  200. case FVCOLUMN_COMMENT: fnComparer = FileRecord_CompareByComment; break;
  201. case FVCOLUMN_PUBLISHER: fnComparer = FileRecord_CompareByPublisher; break;
  202. case FVCOLUMN_COMPOSER: fnComparer = FileRecord_CompareByComposer; break;
  203. case FVCOLUMN_ALBUMARTIST: fnComparer = FileRecord_CompareByAlbumArtist; break;
  204. }
  205. if (fnComparer)
  206. {
  207. g_pCompareData = pFileData;
  208. qsort(pOrder, count, sizeof(size_t), fnComparer);
  209. g_pCompareData = NULL;
  210. }
  211. }
  212. }
  213. void FileView_SortByColumn(FILEDATA *pFileData, UINT uColumn)
  214. {
  215. if (!pFileData) return;
  216. FileView_SortByColumnEx(pFileData, uColumn, pFileData->pSort, pFileData->count);
  217. }