forked from Nilcm01/flips_pct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.cpp
More file actions
171 lines (138 loc) · 4.62 KB
/
tools.cpp
File metadata and controls
171 lines (138 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <shlobj.h>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <io.h> // For _finddata_t, _wfindfirst, _wfindnext, _findclose
#include <sys/stat.h> // For _S_IFDIR
#include <Windows.h>
#include <Shlwapi.h>
using namespace std;
#include "tools.h"
vector<wstring> FindPatchFiles(const wchar_t* folder_path) {
vector<wstring> patch_files;
// Open the directory using _wfindfirst and _wfindnext
_wfinddata_t file_info;
intptr_t handle = _wfindfirst((wstring(folder_path) + L"\\*.*").c_str(), &file_info);
if (handle != -1) {
do {
// Check if the file is a regular file
if (!(file_info.attrib & _A_SUBDIR)) {
// Get the file extension
wstring extension = wstring(file_info.name).substr(wstring(file_info.name).find_last_of(L'.') + 1);
// Check if the extension matches "*.ips", "*.bps", or "*.ups"
if (extension == L"ips" || extension == L"bps" || extension == L"ups") {
// Add the file to the list of patch files
patch_files.push_back(wstring(folder_path) + L"\\" + file_info.name);
}
}
} while (_wfindnext(handle, &file_info) == 0);
// Close the directory handle
_findclose(handle);
}
// Sort the patch files alphabetically
sort(patch_files.begin(), patch_files.end());
return patch_files;
}
/*static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if(uMsg == BFFM_INITIALIZED)
{
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
return 0;
}*/
bool BrowseFolder(WCHAR* path, WCHAR* message)
{
/*WCHAR current[MAX_PATH];
wcscpy(current, L"");*/
BROWSEINFOW bi = { 0 };
bi.lpszTitle = message;
bi.ulFlags = BIF_RETURNONLYFSDIRS || BIF_USENEWUI || BIF_BROWSEINCLUDEURLS;
/*bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM) current;*/
LPITEMIDLIST pidl = SHBrowseForFolderW ( &bi );
if (pidl != 0)
{
// get the name of the folder and put it in path
SHGetPathFromIDListW ( pidl, path );
// free memory used
IMalloc * imalloc = 0;
if (SUCCEEDED( SHGetMalloc ( &imalloc )))
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
return true;
}
return false;
}
bool FindMatchingFile(const WCHAR* patchname, const WCHAR* destinationPath, WCHAR* matchingFile, bool removeExtension)
{
WCHAR patchfilename[MAX_PATH];
wcscpy(patchfilename, patchname);
// Remove the path to the patch file, if any
WCHAR* patchnameonly = wcsrchr(patchfilename, L'\\');
if (patchnameonly != NULL)
{
patchnameonly++;
}
else
{
patchnameonly = patchfilename;
}
// Remove the extension from the patch filename
if (removeExtension) {
WCHAR* patchext = wcsrchr(patchnameonly, L'.');
if (patchext != NULL)
{
*patchext = L'\0';
}
} else {
WCHAR* patchext = wcsrchr(patchnameonly, L'.');
if (patchext != NULL)
{
patchext = wcslwr(patchext);
}
}
// Open the directory using _wfindfirst and _wfindnext
_wfinddata_t file_info;
intptr_t handle = _wfindfirst((wstring(destinationPath) + L"\\*.*").c_str(), &file_info);
if (handle != -1) {
do {
// Check if the file is a regular file
if (!(file_info.attrib & _A_SUBDIR)) {
WCHAR* filename = new WCHAR[MAX_PATH];
wcscpy(filename, file_info.name);
// Remove the extension from the file name
if (!removeExtension) {
WCHAR* fileext = wcsrchr(filename, L'.');
if (fileext != NULL)
{
fileext = wcslwr(fileext);
}
}
if (lstrcmpiW(filename, patchnameonly) == 0)
{
swprintf(matchingFile, MAX_PATH, L"%S\\%S", destinationPath, filename);
// Close the directory handle
_findclose(handle);
return true;
}
}
} while (_wfindnext(handle, &file_info) == 0);
// Close the directory handle
_findclose(handle);
}
return false;
}
string WStringToString(const wstring& wstr)
{
string str;
size_t size;
str.resize(wstr.length());
wcstombs_s(&size, &str[0], str.size() + 1, wstr.c_str(), wstr.size());
return str;
}