forked from stephaniewm55/WinVi-Minimalist-Text-Hex-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileList.c
208 lines (185 loc) · 5.34 KB
/
FileList.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/********************************************************\
|* *|
|* Program: WinVi *|
|* Author: Raphael Molle *|
|* Compiler: several *|
|* --------------------- -------------------------- *|
|* © Copyright 1994-2007 by Raphael Molle, Berlin *|
|* © Copyleft by GPL See file COPYING for more *|
|* info about terms of use *|
|* *|
\********************************************************/
/* revision log:
* 3-Dec-2000 first publication of source code
* 28-Jul-2007 no removal of '+' parameters in startup phase
*/
#include <windows.h>
#include <string.h>
#include <wchar.h>
#include "resource.h"
#include "winvi.h"
#include "pathexp.h"
#include "myassert.h"
typedef struct tagFILELIST {
struct tagFILELIST *Next;
BYTE Flags;
WCHAR FileName[1];
} FILELIST, *PFILELIST;
PFILELIST pFileList, pLastFile;
int nCurrFileListEntry, nNumFileListEntries;
BOOL RemoveFileListEntry(INT n)
{ PFILELIST p;
HLOCAL hFile;
if (nNumFileListEntries == 0) return FALSE;
--nNumFileListEntries;
if (nCurrFileListEntry) --nCurrFileListEntry;
p = pFileList;
if (p == NULL) {
assert(pFileList != NULL);
nNumFileListEntries =
nCurrFileListEntry = 0;
return FALSE;
}
pFileList = pFileList->Next;
hFile = LocalHandle(p);
LocalUnlock(hFile);
LocalFree(hFile);
return (TRUE);
}
void ClearFileList(void)
{ extern BOOL StartupPhase;
PWSTR lp;
INT i = 0;
while (i < nNumFileListEntries) {
if (!StartupPhase || *(lp = GetFileListEntry(0)) != '+')
RemoveFileListEntry(i);
else ++i;
}
nCurrFileListEntry = i;
}
static int AppendSingle(PCWSTR lp1, PCWSTR lp2, int nLen)
{ HLOCAL hFile;
PFILELIST pFile;
hFile = LocalAlloc(LMEM_FIXED, (wcslen(lp1) + nLen) * sizeof(WCHAR)
+ sizeof(FILELIST));
if (!hFile) return (-1);
pFile = (PFILELIST)LocalLock(hFile);
if (pFile == NULL) {
LocalFree(hFile);
return (-1);
}
pFile->Next = NULL;
pFile->Flags = 0;
wcscpy(pFile->FileName, lp1);
_fmemcpy(pFile->FileName + wcslen(lp1), lp2, nLen*sizeof(WCHAR));
pFile->FileName[wcslen(lp1) + nLen] = '\0';
if (pFileList) pLastFile->Next = pFile;
else pFileList = pFile;
pLastFile = pFile;
return (nNumFileListEntries++);
}
INT AppendToFileList(PCWSTR pwFile, INT nLen, INT Flags)
/*Flags: 1=initial parameter list or next command, expand wildcards
*/
{ WCHAR FNameBuf[260];
WCHAR *p = FNameBuf, *PathEnd = FNameBuf;
PCWSTR pw = pwFile;
INT nRet = -1;
BOOL Expand = FALSE;
while (nLen-- && p-FNameBuf < sizeof(FNameBuf)-1) {
switch (*p++ = *pw++) {
case '"':
--p;
break;
case '*': case '?':
Expand = TRUE;
break;
case '/': case '\\':
PathEnd = p;
Expand = FALSE;
break;
case '\0': /*should not occur*/
nLen = 0;
}
}
*p = '\0';
if (!++nLen && Flags & 1 && Expand) {
FIND_DATA Data;
HANDLE FindHandle;
FindHandle = FIND_FIRST(FNameBuf, &Data);
if (FindHandle != INVALID_HANDLE_VALUE) {
*PathEnd = '\0';
do {
if (!IS_DIRECTORY(&Data) && !IS_HIDDEN(&Data)) {
nRet = AppendSingle(FNameBuf, FOUND_NAME(&Data),
wcslen(FOUND_NAME(&Data)));
if (nRet == -1) break;
}
} while (FIND_NEXT(FindHandle, &Data));
FIND_CLOSE(FindHandle);
return (nRet);
}
}
return (AppendSingle(FNameBuf, pw, nLen));
}
PWSTR GetFileListEntry(INT nEntry)
{ PFILELIST p;
p = pFileList;
if (p == NULL) return (NULL);
while (nEntry--) {
p = p->Next;
if (p == NULL) return (NULL);
}
return (p->FileName);
}
BOOL CALLBACK ArgsCallback(HWND hDlg, UINT uMsg, WPARAM wPar, LPARAM lPar)
{ int i;
PWSTR lp;
HWND hwndListBox;
PARAM_NOT_USED(lPar);
switch (uMsg) {
case WM_INITDIALOG:
i = 0;
hwndListBox = GetDlgItem(hDlg, IDC_LISTBOX);
if (hwndListBox) {
while ((lp = GetFileListEntry(i++)) != NULL)
SendMessageW(hwndListBox, LB_ADDSTRING, 0, (LPARAM)lp);
SendMessage(hwndListBox, LB_SETCURSEL, nCurrFileListEntry, 0);
}
return (TRUE);
case WM_COMMAND:
if (COMMAND == IDC_LISTBOX) {
if (NOTIFICATION == LBN_DBLCLK) wPar = IDOK;
else if (NOTIFICATION == LBN_SELCHANGE)
SendMessage(hDlg, DM_SETDEFID, IDOK, 0);
}
if (COMMAND == IDOK) {
i = (int)SendDlgItemMessage(hDlg,IDC_LISTBOX,LB_GETCURSEL,0,0);
if (i==LB_ERR || ((lp=GetFileListEntry(i)) == NULL)
|| !AskForSave(hDlg, 1))
return (TRUE);
nCurrFileListEntry = i;
Unsafe = FALSE; /*already asked for save*/
Open(lp, 0);
}
if (COMMAND==IDOK || COMMAND==IDCANCEL)
EndDialog(hDlg, COMMAND==IDOK);
return (TRUE);
}
return (FALSE);
}
void ArgsCmd(void)
{ static DLGPROC Callback;
if (!Callback)
Callback = (DLGPROC)MakeProcInstance((FARPROC)ArgsCallback, hInst);
DialogBoxW(hInst, MAKEINTRESW(IDD_ARGS), hwndMain, Callback);
}
INT FindOrAppendToFileList(PCWSTR Fname)
{ int j;
for (j = 0; j < nNumFileListEntries; ++j) {
PWSTR pName;
pName = GetFileListEntry(j);
if (pName != NULL && !wcsicmp(pName, Fname)) return (j);
}
return (AppendToFileList(Fname, wcslen(Fname), 0));
}