Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UNICODE
- #define UNICODE
- #endif
- #include <Windows.h>
- #include <CommCtrl.h>
- #include <string>
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <fstream>
- #include <ctime>
- #define LoadFilesButtonClicked 0x1
- #define SortFilesButtonClicked 0x2
- struct FileData {
- std::wstring fileName;
- std::wstring fileSize;
- };
- //g for global
- std::vector<FileData> gFilesVector;
- std::wofstream gErrorLogFile;
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
- HWND hEdit, hListViewLeft, hListViewRight, hLoadButton, hSortButton;
- void AddColumns(HWND hListView)
- {
- LVCOLUMN lvCol = { };
- lvCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
- lvCol.cx = 50;
- lvCol.pszText = (LPWSTR)L"Index";
- ListView_InsertColumn(hListView, 0, &lvCol);
- lvCol.cx = 300;
- lvCol.pszText = (LPWSTR)L"File Name";
- ListView_InsertColumn(hListView, 1, &lvCol);
- lvCol.cx = 150;
- lvCol.pszText = (LPWSTR)L"File Size";
- ListView_InsertColumn(hListView, 2, &lvCol);
- }
- void AddItemToListView(HWND hListView, FileData* fileData)
- {
- LVITEM lvItem = { };
- lvItem.mask = LVIF_TEXT;
- int index = ListView_GetItemCount(hListView);
- lvItem.iItem = index; //index should start with the next Item
- std::wstring indexStr = std::to_wstring(index + 1);
- lvItem.pszText = (LPWSTR)indexStr.c_str();
- ListView_InsertItem(hListView, &lvItem);
- ListView_SetItemText(hListView, index, 1, (LPWSTR)fileData->fileName.c_str());
- ListView_SetItemText(hListView, index, 2, (LPWSTR)fileData->fileSize.c_str());
- }
- std::wstring GenerateUniqueFileName(const std::wstring& baseName,
- std::unordered_map<std::wstring, int>& gNameCount)
- {
- std::wstring uniqueName = baseName;
- int count = gNameCount[baseName];
- if (count > 0) {
- uniqueName = baseName + L"[" + std::to_wstring(count) + L"]";
- }
- gNameCount[baseName]++;
- return uniqueName;
- }
- void LogError(std::wofstream& logFile, const std::wstring& message) {
- std::time_t currentTime = std::time(nullptr);
- std::tm localTime;
- localtime_s(&localTime, ¤tTime);
- wchar_t timeBuffer[80];
- wcsftime(timeBuffer, sizeof(timeBuffer), L"%Y-%m-%d %H:%M:%S", &localTime);
- logFile << L"[" << timeBuffer << L"] " << message << std::endl;
- }
- void LoadFilesData(HWND hListView, const std::wstring directoryPath,
- std::vector<FileData>& files, std::unordered_map<std::wstring, int> &gNameCount,
- std::wofstream &errorLogFile)
- {
- WIN32_FIND_DATA data;
- std::wstring searchPath = directoryPath + L"//*";
- HANDLE hFind = FindFirstFile(searchPath.c_str(), &data);
- if (hFind == INVALID_HANDLE_VALUE) {
- DWORD errorCode = GetLastError();
- std::wstring errorMessage = L"Error when getting files from directory: " + directoryPath +
- L". Error Code: " + std::to_wstring(errorCode);
- //std::wcout << errorMessage << std::endl;
- LogError(errorLogFile, errorMessage);
- return;
- }
- do {
- if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- if (wcscmp(data.cFileName, L".") != 0 && wcscmp(data.cFileName, L"..") != 0) {
- const std::wstring newDirPath = directoryPath + L"//" + data.cFileName;
- LoadFilesData(hListView, newDirPath, files, gNameCount, errorLogFile);
- }
- }
- else {
- FileData fileData;
- std::wstring baseName = data.cFileName;
- fileData.fileName = GenerateUniqueFileName(baseName, gNameCount);
- ULONGLONG fileSize = ((ULONGLONG)data.nFileSizeHigh << 32) + data.nFileSizeLow;
- fileData.fileSize = std::to_wstring(fileSize) + L" bytes";
- gFilesVector.push_back(fileData);
- }
- } while (FindNextFile(hFind, &data) != 0);
- FindClose(hFind);
- }
- void Swap(int i, int j)
- {
- auto temp = gFilesVector[i];
- gFilesVector[i] = gFilesVector[j];
- gFilesVector[j] = temp;
- }
- int Partition(int left, int right)
- {
- auto pivot = gFilesVector[left].fileSize.c_str();
- int j = left;
- for (int i = left + 1; i <= right; i++)
- {
- if (wcscmp(gFilesVector[i].fileSize.c_str(), pivot) <= 0)
- {
- j++;
- Swap(i, j);
- }
- }
- Swap(left, j);
- return j;
- }
- void QSort(int left, int right)
- {
- while (left < right)
- {
- int pivotIndex = Partition(left, right);
- if (pivotIndex - left <= right - pivotIndex)
- {
- QSort(left, pivotIndex - 1);
- left = pivotIndex + 1;
- }
- else
- {
- QSort(pivotIndex + 1, right);
- right = pivotIndex - 1;
- }
- }
- }
- void SortListView(HWND hListView)
- {
- if (gFilesVector.size() > 0) {
- ListView_DeleteAllItems(hListView);
- QSort(0, gFilesVector.size() - 1);
- SendMessage(hListView, WM_SETREDRAW, FALSE, 0);
- int size = gFilesVector.size();
- for (int i = 0; i < size; i++) {
- AddItemToListView(hListView, &gFilesVector[i]);
- }
- SendMessage(hListView, WM_SETREDRAW, TRUE, 0);
- }
- else {
- MessageBox(hListView, L"Fill your left table!", L"Error", MB_ICONERROR);
- }
- }
- void PrepareListView(HWND hListView)
- {
- wchar_t directoryPath[MAX_PATH]{ };
- GetWindowText(hEdit, directoryPath, MAX_PATH);
- ListView_DeleteAllItems(hListViewLeft);
- std::wofstream errorLogFile(L"D://errors.log.txt", std::ios::app);
- gFilesVector = { };
- std::unordered_map<std::wstring, int> gNameCount = { };
- SendMessage(hListViewLeft, WM_SETREDRAW, FALSE, 0);
- LoadFilesData(hListViewLeft, directoryPath, gFilesVector, gNameCount, errorLogFile);
- int size = gFilesVector.size();
- for (int i = 0; i < size; i++) {
- AddItemToListView(hListViewLeft, &gFilesVector[i]);
- }
- SendMessage(hListViewLeft, WM_SETREDRAW, TRUE, 0);
- }
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
- {
- /*if (AllocConsole()) {
- FILE* fp;
- freopen_s(&fp, "CONOUT$", "w", stdout);
- }*/
- const wchar_t CLASS_NAME[] = L"WINAPI_LAB1";
- WNDCLASS wc = { };
- wc.lpfnWndProc = WindowProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = CLASS_NAME;
- RegisterClass(&wc);
- HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"WINAPI_LAB1", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, 900, 600,
- NULL, NULL, hInstance, NULL
- );
- if (hwnd == NULL)
- {
- return 0;
- }
- ShowWindow(hwnd, nCmdShow);
- hEdit = CreateWindowEx(0, WC_EDIT, L"",
- WS_CHILD | WS_VISIBLE | WS_BORDER,
- 10, 10, 600, 20,
- hwnd, NULL, NULL, NULL);
- ShowWindow(hEdit, TRUE);
- hListViewLeft = CreateWindowEx(0, WC_LISTVIEW, L"",
- WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT,
- 10, 40, 420, 500,
- hwnd, NULL, NULL, NULL);
- ShowWindow(hListViewLeft, TRUE);
- hListViewRight = CreateWindowEx(0, WC_LISTVIEW, L"",
- WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT,
- 440, 40, 420, 500,
- hwnd, NULL, NULL, NULL);
- ShowWindow(hListViewRight, TRUE);
- AddColumns(hListViewLeft);
- AddColumns(hListViewRight);
- hLoadButton = CreateWindowEx(0, WC_BUTTON, L"Load Files",
- WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
- 630, 10, 80, 25,
- hwnd, (HMENU)LoadFilesButtonClicked, NULL, NULL);
- ShowWindow(hLoadButton, TRUE);
- hSortButton = CreateWindowEx(0, WC_BUTTON, L"Sort Files",
- WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
- 780, 10, 80, 25,
- hwnd, (HMENU)SortFilesButtonClicked, NULL, NULL);
- ShowWindow(hLoadButton, TRUE);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0) > 0)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hwnd, &ps);
- // All painting occurs here, between BeginPaint and EndPaint.
- FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
- EndPaint(hwnd, &ps);
- }
- return 0;
- case WM_CREATE:
- {
- }
- break;
- case WM_COMMAND:
- {
- switch (LOWORD(wParam))
- {
- case LoadFilesButtonClicked:
- PrepareListView(hListViewLeft);
- break;
- case SortFilesButtonClicked:
- SortListView(hListViewRight);
- break;
- }
- }
- break;
- case WM_CLOSE:
- /*if (MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL) == IDOK) {
- DestroyWindow(hwnd);
- }*/
- PostQuitMessage(0);
- break;
- case WM_DESTROY:
- return 0;
- default:
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement