Advertisement
Old_But_Gold

Untitled

Sep 4th, 2024 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.21 KB | None | 0 0
  1. #ifndef UNICODE
  2. #define UNICODE
  3. #endif
  4.  
  5. #include <Windows.h>
  6. #include <CommCtrl.h>
  7. #include <string>
  8. #include <iostream>
  9. #include <vector>
  10. #include <unordered_map>
  11. #include <fstream>
  12. #include <ctime>
  13.  
  14. #define LoadFilesButtonClicked 0x1
  15. #define SortFilesButtonClicked 0x2
  16.  
  17. struct FileData {
  18. std::wstring fileName;
  19. std::wstring fileSize;
  20. };
  21.  
  22. //g for global
  23. std::vector<FileData> gFilesVector;
  24.  
  25. std::wofstream gErrorLogFile;
  26.  
  27. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  28.  
  29. HWND hEdit, hListViewLeft, hListViewRight, hLoadButton, hSortButton;
  30.  
  31. void AddColumns(HWND hListView)
  32. {
  33. LVCOLUMN lvCol = { };
  34. lvCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
  35.  
  36. lvCol.cx = 50;
  37. lvCol.pszText = (LPWSTR)L"Index";
  38. ListView_InsertColumn(hListView, 0, &lvCol);
  39.  
  40. lvCol.cx = 300;
  41. lvCol.pszText = (LPWSTR)L"File Name";
  42. ListView_InsertColumn(hListView, 1, &lvCol);
  43.  
  44. lvCol.cx = 150;
  45. lvCol.pszText = (LPWSTR)L"File Size";
  46. ListView_InsertColumn(hListView, 2, &lvCol);
  47. }
  48.  
  49. void AddItemToListView(HWND hListView, FileData* fileData)
  50. {
  51. LVITEM lvItem = { };
  52. lvItem.mask = LVIF_TEXT;
  53.  
  54. int index = ListView_GetItemCount(hListView);
  55. lvItem.iItem = index; //index should start with the next Item
  56.  
  57. std::wstring indexStr = std::to_wstring(index + 1);
  58. lvItem.pszText = (LPWSTR)indexStr.c_str();
  59. ListView_InsertItem(hListView, &lvItem);
  60.  
  61. ListView_SetItemText(hListView, index, 1, (LPWSTR)fileData->fileName.c_str());
  62. ListView_SetItemText(hListView, index, 2, (LPWSTR)fileData->fileSize.c_str());
  63. }
  64.  
  65.  
  66. std::wstring GenerateUniqueFileName(const std::wstring& baseName,
  67. std::unordered_map<std::wstring, int>& gNameCount)
  68. {
  69. std::wstring uniqueName = baseName;
  70. int count = gNameCount[baseName];
  71.  
  72. if (count > 0) {
  73. uniqueName = baseName + L"[" + std::to_wstring(count) + L"]";
  74. }
  75.  
  76. gNameCount[baseName]++;
  77.  
  78. return uniqueName;
  79. }
  80.  
  81. void LogError(std::wofstream& logFile, const std::wstring& message) {
  82. std::time_t currentTime = std::time(nullptr);
  83. std::tm localTime;
  84. localtime_s(&localTime, &currentTime);
  85.  
  86. wchar_t timeBuffer[80];
  87. wcsftime(timeBuffer, sizeof(timeBuffer), L"%Y-%m-%d %H:%M:%S", &localTime);
  88.  
  89. logFile << L"[" << timeBuffer << L"] " << message << std::endl;
  90. }
  91.  
  92. void LoadFilesData(HWND hListView, const std::wstring directoryPath,
  93. std::vector<FileData>& files, std::unordered_map<std::wstring, int> &gNameCount,
  94. std::wofstream &errorLogFile)
  95. {
  96. WIN32_FIND_DATA data;
  97.  
  98. std::wstring searchPath = directoryPath + L"//*";
  99.  
  100. HANDLE hFind = FindFirstFile(searchPath.c_str(), &data);
  101.  
  102. if (hFind == INVALID_HANDLE_VALUE) {
  103. DWORD errorCode = GetLastError();
  104. std::wstring errorMessage = L"Error when getting files from directory: " + directoryPath +
  105. L". Error Code: " + std::to_wstring(errorCode);
  106. //std::wcout << errorMessage << std::endl;
  107. LogError(errorLogFile, errorMessage);
  108. return;
  109. }
  110.  
  111. do {
  112. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  113. if (wcscmp(data.cFileName, L".") != 0 && wcscmp(data.cFileName, L"..") != 0) {
  114. const std::wstring newDirPath = directoryPath + L"//" + data.cFileName;
  115. LoadFilesData(hListView, newDirPath, files, gNameCount, errorLogFile);
  116. }
  117. }
  118. else {
  119. FileData fileData;
  120. std::wstring baseName = data.cFileName;
  121.  
  122. fileData.fileName = GenerateUniqueFileName(baseName, gNameCount);
  123.  
  124. ULONGLONG fileSize = ((ULONGLONG)data.nFileSizeHigh << 32) + data.nFileSizeLow;
  125. fileData.fileSize = std::to_wstring(fileSize) + L" bytes";
  126.  
  127. gFilesVector.push_back(fileData);
  128. }
  129. } while (FindNextFile(hFind, &data) != 0);
  130.  
  131. FindClose(hFind);
  132. }
  133.  
  134. void Swap(int i, int j)
  135. {
  136. auto temp = gFilesVector[i];
  137. gFilesVector[i] = gFilesVector[j];
  138. gFilesVector[j] = temp;
  139. }
  140.  
  141. int Partition(int left, int right)
  142. {
  143. auto pivot = gFilesVector[left].fileSize.c_str();
  144. int j = left;
  145.  
  146. for (int i = left + 1; i <= right; i++)
  147. {
  148. if (wcscmp(gFilesVector[i].fileSize.c_str(), pivot) <= 0)
  149. {
  150. j++;
  151. Swap(i, j);
  152. }
  153. }
  154.  
  155. Swap(left, j);
  156. return j;
  157. }
  158.  
  159. void QSort(int left, int right)
  160. {
  161. while (left < right)
  162. {
  163. int pivotIndex = Partition(left, right);
  164. if (pivotIndex - left <= right - pivotIndex)
  165. {
  166. QSort(left, pivotIndex - 1);
  167. left = pivotIndex + 1;
  168. }
  169. else
  170. {
  171. QSort(pivotIndex + 1, right);
  172. right = pivotIndex - 1;
  173. }
  174. }
  175. }
  176.  
  177. void SortListView(HWND hListView)
  178. {
  179. if (gFilesVector.size() > 0) {
  180. ListView_DeleteAllItems(hListView);
  181.  
  182. QSort(0, gFilesVector.size() - 1);
  183. SendMessage(hListView, WM_SETREDRAW, FALSE, 0);
  184.  
  185. int size = gFilesVector.size();
  186.  
  187. for (int i = 0; i < size; i++) {
  188. AddItemToListView(hListView, &gFilesVector[i]);
  189. }
  190.  
  191. SendMessage(hListView, WM_SETREDRAW, TRUE, 0);
  192. }
  193. else {
  194. MessageBox(hListView, L"Fill your left table!", L"Error", MB_ICONERROR);
  195. }
  196. }
  197.  
  198. void PrepareListView(HWND hListView)
  199. {
  200. wchar_t directoryPath[MAX_PATH]{ };
  201. GetWindowText(hEdit, directoryPath, MAX_PATH);
  202. ListView_DeleteAllItems(hListViewLeft);
  203.  
  204. std::wofstream errorLogFile(L"D://errors.log.txt", std::ios::app);
  205.  
  206. gFilesVector = { };
  207. std::unordered_map<std::wstring, int> gNameCount = { };
  208.  
  209. SendMessage(hListViewLeft, WM_SETREDRAW, FALSE, 0);
  210.  
  211. LoadFilesData(hListViewLeft, directoryPath, gFilesVector, gNameCount, errorLogFile);
  212.  
  213. int size = gFilesVector.size();
  214.  
  215. for (int i = 0; i < size; i++) {
  216. AddItemToListView(hListViewLeft, &gFilesVector[i]);
  217. }
  218.  
  219. SendMessage(hListViewLeft, WM_SETREDRAW, TRUE, 0);
  220. }
  221.  
  222. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  223. {
  224. /*if (AllocConsole()) {
  225. FILE* fp;
  226. freopen_s(&fp, "CONOUT$", "w", stdout);
  227. }*/
  228.  
  229. const wchar_t CLASS_NAME[] = L"WINAPI_LAB1";
  230.  
  231. WNDCLASS wc = { };
  232. wc.lpfnWndProc = WindowProc;
  233. wc.hInstance = hInstance;
  234. wc.lpszClassName = CLASS_NAME;
  235.  
  236. RegisterClass(&wc);
  237.  
  238. HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"WINAPI_LAB1", WS_OVERLAPPEDWINDOW,
  239. CW_USEDEFAULT, CW_USEDEFAULT, 900, 600,
  240. NULL, NULL, hInstance, NULL
  241. );
  242.  
  243. if (hwnd == NULL)
  244. {
  245. return 0;
  246. }
  247.  
  248. ShowWindow(hwnd, nCmdShow);
  249.  
  250. hEdit = CreateWindowEx(0, WC_EDIT, L"",
  251. WS_CHILD | WS_VISIBLE | WS_BORDER,
  252. 10, 10, 600, 20,
  253. hwnd, NULL, NULL, NULL);
  254. ShowWindow(hEdit, TRUE);
  255.  
  256. hListViewLeft = CreateWindowEx(0, WC_LISTVIEW, L"",
  257. WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT,
  258. 10, 40, 420, 500,
  259. hwnd, NULL, NULL, NULL);
  260. ShowWindow(hListViewLeft, TRUE);
  261.  
  262. hListViewRight = CreateWindowEx(0, WC_LISTVIEW, L"",
  263. WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT,
  264. 440, 40, 420, 500,
  265. hwnd, NULL, NULL, NULL);
  266. ShowWindow(hListViewRight, TRUE);
  267.  
  268. AddColumns(hListViewLeft);
  269. AddColumns(hListViewRight);
  270.  
  271. hLoadButton = CreateWindowEx(0, WC_BUTTON, L"Load Files",
  272. WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
  273. 630, 10, 80, 25,
  274. hwnd, (HMENU)LoadFilesButtonClicked, NULL, NULL);
  275.  
  276. ShowWindow(hLoadButton, TRUE);
  277.  
  278. hSortButton = CreateWindowEx(0, WC_BUTTON, L"Sort Files",
  279. WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
  280. 780, 10, 80, 25,
  281. hwnd, (HMENU)SortFilesButtonClicked, NULL, NULL);
  282.  
  283. ShowWindow(hLoadButton, TRUE);
  284.  
  285. MSG msg;
  286. while (GetMessage(&msg, NULL, 0, 0) > 0)
  287. {
  288. TranslateMessage(&msg);
  289. DispatchMessage(&msg);
  290. }
  291.  
  292. return 0;
  293. }
  294.  
  295. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  296. {
  297. switch (uMsg)
  298. {
  299. case WM_PAINT:
  300. {
  301. PAINTSTRUCT ps;
  302. HDC hdc = BeginPaint(hwnd, &ps);
  303.  
  304. // All painting occurs here, between BeginPaint and EndPaint.
  305.  
  306. FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
  307. EndPaint(hwnd, &ps);
  308. }
  309. return 0;
  310.  
  311. case WM_CREATE:
  312. {
  313.  
  314. }
  315. break;
  316.  
  317. case WM_COMMAND:
  318. {
  319.  
  320. switch (LOWORD(wParam))
  321. {
  322. case LoadFilesButtonClicked:
  323. PrepareListView(hListViewLeft);
  324. break;
  325.  
  326. case SortFilesButtonClicked:
  327. SortListView(hListViewRight);
  328. break;
  329. }
  330. }
  331. break;
  332.  
  333. case WM_CLOSE:
  334. /*if (MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL) == IDOK) {
  335. DestroyWindow(hwnd);
  336. }*/
  337. PostQuitMessage(0);
  338. break;
  339.  
  340. case WM_DESTROY:
  341. return 0;
  342.  
  343. default:
  344. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  345. }
  346. return 0;
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement