source: trunk/dll/rename.c@ 1036

Last change on this file since 1036 was 907, checked in by Steven Levine, 18 years ago

Avoid out of memory traps in Compare Directories
Rework Compare Directories progress display for 2 second update rate
Start refactoring to reduce dependence on fm3dll.h
Add timer services (IsITimerExpired etc.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1
2/***********************************************************************
3
4 $Id: rename.c 907 2008-01-06 07:26:17Z stevenhl $
5
6 Copyright (c) 1993-98 M. Kimes
7 Copyright (c) 2004, 2007 Steven H.Levine
8
9 Revisions
10 01 Aug 04 SHL - Rework lstrip/rstrip usage
11 22 Mar 07 GKY Use QWL_USER
12 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
13 27 Sep 07 SHL Correct ULONGLONG size formatting
14 30 Dec 07 GKY Use TestFDates for comparing dates
15
16***********************************************************************/
17
18#include <string.h>
19
20#define INCL_WIN
21#define INCL_LONGLONG
22
23#include "fm3dlg.h"
24#include "fm3str.h"
25#include "errutil.h" // Dos_Error...
26#include "strutil.h" // GetPString
27#include "fm3dll.h"
28
29MRESULT EXPENTRY RenameProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
30{
31 MOVEIT *mv;
32
33 switch (msg) {
34 case WM_INITDLG:
35 mv = (MOVEIT *) mp2;
36 WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mv);
37 if (!mv || !mv->source) {
38 WinDismissDlg(hwnd, 0);
39 break;
40 }
41 WinSendDlgItemMsg(hwnd,
42 REN_SOURCE,
43 EM_SETTEXTLIMIT, MPFROM2SHORT(CCHMAXPATH, 0), MPVOID);
44 WinSendDlgItemMsg(hwnd,
45 REN_TARGET,
46 EM_SETTEXTLIMIT, MPFROM2SHORT(CCHMAXPATH, 0), MPVOID);
47 if (!*mv->target)
48 strcpy(mv->target, mv->source);
49 WinSendMsg(hwnd, UM_RESCAN, MPVOID, MPVOID);
50 if (mv->rename || !stricmp(mv->target, mv->source)) {
51
52 CHAR *p = strrchr(mv->target, '\\');
53 if (p) {
54
55 USHORT sello, selhi;
56
57 sello = p - mv->target + 1;
58 selhi = strlen(mv->target);
59
60 WinSendDlgItemMsg(hwnd,
61 REN_TARGET,
62 EM_SETSEL, MPFROM2SHORT(sello, selhi), MPVOID);
63 }
64 WinShowWindow(WinWindowFromID(hwnd, REN_OVEROLD), FALSE);
65 WinShowWindow(WinWindowFromID(hwnd, REN_OVERNEW), FALSE);
66 }
67 break;
68
69 case UM_RESCAN:
70 {
71 mv = WinQueryWindowPtr(hwnd, QWL_USER);
72 if (mv) {
73
74 FILESTATUS3L fs1, fs2;
75 CHAR s[CCHMAXPATH * 2], *p, chkname[CCHMAXPATH], szCmmaFmtFileSize[81];
76 INT sourceexists = 0, targetexists = 0,
77 sourcenewer = 0, sourcesmaller = 0;
78
79 p = mv->target;
80 while (*p) {
81 if (*p == '/')
82 *p = '\\';
83 p++;
84 }
85 if (!MakeFullName(mv->target))
86 WinSetDlgItemText(hwnd, REN_TARGET, mv->target);
87 if (!MakeFullName(mv->source))
88 WinSetDlgItemText(hwnd, REN_SOURCE, mv->source);
89 if (!DosQueryPathInfo(mv->source, FIL_STANDARDL, &fs1, sizeof(fs1))) {
90 CommaFmtULL(szCmmaFmtFileSize,
91 sizeof(szCmmaFmtFileSize), fs1.cbFile, ' ');
92 sprintf(s,
93 " %s%s %ss %04u/%02u/%02u %02u:%02u:%02u",
94 fs1.attrFile & FILE_DIRECTORY ?
95 GetPString(IDS_DIRBRKTTEXT) : NullStr,
96 szCmmaFmtFileSize,
97 GetPString(IDS_BYTETEXT),
98 fs1.fdateLastWrite.year + 1980,
99 fs1.fdateLastWrite.month,
100 fs1.fdateLastWrite.day,
101 fs1.ftimeLastWrite.hours,
102 fs1.ftimeLastWrite.minutes, fs1.ftimeLastWrite.twosecs * 2);
103 WinSetDlgItemText(hwnd, REN_SOURCEINFO, s);
104 sourceexists = 1;
105 if (fs1.attrFile & FILE_DIRECTORY)
106 sourceexists = 3;
107 }
108 else
109 WinSetDlgItemText(hwnd,
110 REN_SOURCEINFO, GetPString(IDS_DOESNTEXIST2TEXT));
111 strcpy(chkname, mv->target);
112 p = strrchr(s, '\\');
113 if (p && (strchr(p, '*') || strchr(p, '?'))) {
114 if (!AdjustWildcardName(mv->target, chkname))
115 strcpy(chkname, mv->target);
116 }
117 if (!DosQueryPathInfo(chkname, FIL_STANDARDL, &fs2, sizeof(fs2))) {
118 CommaFmtULL(szCmmaFmtFileSize,
119 sizeof(szCmmaFmtFileSize), fs2.cbFile, ' ');
120 sprintf(s,
121 " %s%s %ss %04u/%02u/%02u %02u:%02u:%02u",
122 fs2.attrFile & FILE_DIRECTORY ?
123 GetPString(IDS_DIRBRKTTEXT) : NullStr,
124 szCmmaFmtFileSize,
125 GetPString(IDS_BYTETEXT),
126 fs2.fdateLastWrite.year + 1980,
127 fs2.fdateLastWrite.month,
128 fs2.fdateLastWrite.day,
129 fs2.ftimeLastWrite.hours,
130 fs2.ftimeLastWrite.minutes, fs2.ftimeLastWrite.twosecs * 2);
131 WinSetDlgItemText(hwnd, REN_TARGETINFO, s);
132 targetexists = 1;
133 if (fs2.attrFile & (FILE_DIRECTORY))
134 targetexists = 3;
135 WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), TRUE);
136 }
137 else {
138 WinSetDlgItemText(hwnd,
139 REN_TARGETINFO, GetPString(IDS_DOESNTEXIST2TEXT));
140 WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), FALSE);
141 }
142 *s = 0;
143 if (sourceexists)
144 sprintf(s,
145 GetPString(IDS_SOURCEISATEXT),
146 sourceexists & 2 ? GetPString(IDS_DIRECTORYTEXT) :
147 GetPString(IDS_FILETEXT));
148 {
149 FILE *fp = NULL;
150 if (~sourceexists & 2)
151 fp = fopen(mv->source, "ab");
152 if ((!fp && ~sourceexists & 2) || !sourceexists)
153 strcpy(s, GetPString(IDS_CANTACCESSSOURCETEXT));
154 if (fp)
155 fclose(fp);
156 }
157 if (targetexists && stricmp(mv->source, mv->target))
158 sprintf(&s[strlen(s)],
159 GetPString(IDS_TARGETEXISTSISATEXT),
160 targetexists & 2 ? GetPString(IDS_DIRECTORYTEXT) :
161 GetPString(IDS_FILETEXT));
162 if (targetexists && stricmp(mv->source, mv->target))
163 strcpy(&s[strlen(s)], GetPString(IDS_CLICKOVERWRITETEXT));
164 else if (targetexists && !stricmp(mv->source, mv->target))
165 strcpy(&s[strlen(s)], GetPString(IDS_ENTERNEWTARGETTEXT));
166 WinEnableWindow(WinWindowFromID(hwnd, REN_OVERWRITE),
167 stricmp(mv->target, mv->source) &&
168 (!mv->rename || strcmp(mv->target, mv->source)));
169
170 if (targetexists == 1 && sourceexists == 1) {
171 sourcenewer = TestFDates(NULL, NULL,
172 &fs1.fdateLastWrite, &fs1.ftimeLastWrite,
173 &fs2.fdateLastWrite, &fs2.ftimeLastWrite);
174 sourcesmaller = (fs1.cbFile < fs2.cbFile) ? -1 :
175 (fs1.cbFile > fs2.cbFile) ? 1 : 0;
176 sprintf(&s[strlen(s)], GetPString(IDS_SOURCEISTEXT),
177 (sourcenewer == -1) ? GetPString(IDS_NEWERTEXT) :
178 (sourcenewer == 1) ? GetPString(IDS_OLDERTEXT) :
179 GetPString(IDS_SAMEDATETEXT),
180 (sourcesmaller == -1) ? GetPString(IDS_SMALLERTEXT) :
181 (sourcesmaller == 1) ? GetPString(IDS_LARGERTEXT) :
182 GetPString(IDS_SAMESIZETEXT));
183 }
184 WinSetDlgItemText(hwnd, REN_INFORMATION, s);
185 if (targetexists && stricmp(mv->source, mv->target)) {
186 if (WinQueryButtonCheckstate(hwnd, REN_DONTASK))
187 return 0;
188 return MRFROM2SHORT(1, 0);
189 }
190 else if (targetexists && !stricmp(mv->source, mv->target)) {
191 if (mv->rename && strcmp(mv->source, mv->target))
192 return 0;
193 WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), FALSE);
194 return MRFROM2SHORT(2, 0);
195 }
196 }
197 }
198 return 0;
199
200 case WM_COMMAND:
201 switch (SHORT1FROMMP(mp1)) {
202 case DID_CANCEL:
203 WinDismissDlg(hwnd, 0);
204 break;
205
206 case IDM_HELP:
207 if (hwndHelp)
208 WinSendMsg(hwndHelp,
209 HM_DISPLAY_HELP,
210 MPFROM2SHORT(HELP_RENAME, 0), MPFROMSHORT(HM_RESOURCEID));
211 break;
212
213 case REN_SKIP:
214 mv = WinQueryWindowPtr(hwnd, QWL_USER);
215 if (mv) {
216 mv->skip = TRUE;
217 *mv->target = 0;
218 WinDismissDlg(hwnd, 2);
219 }
220 else
221 WinDismissDlg(hwnd, 0);
222 break;
223
224 case REN_RENEXIST:
225 mv = WinQueryWindowPtr(hwnd, QWL_USER);
226 if (mv) {
227
228 CHAR newexist[CCHMAXPATH], fullname[CCHMAXPATH];
229 INT was;
230 APIRET rc;
231
232 *newexist = 0;
233 WinQueryDlgItemText(hwnd, REN_TARGET, CCHMAXPATH, newexist);
234 if (*newexist) {
235 if (DosQueryPathInfo(newexist,
236 FIL_QUERYFULLNAME, fullname, sizeof(fullname)))
237 strcpy(fullname, newexist);
238 was = IsFile(fullname);
239 if (was == -1) {
240 rc = docopyf(MOVE, mv->target, "%s", fullname);
241 if (rc) {
242 if ((LONG) rc > 0)
243 Dos_Error(MB_CANCEL,
244 rc,
245 hwnd,
246 __FILE__,
247 __LINE__,
248 GetPString(IDS_COMPMOVEFAILEDTEXT),
249 mv->target, fullname);
250 else
251 saymsg(MB_CANCEL | MB_ICONEXCLAMATION,
252 hwnd,
253 GetPString(IDS_SORRYTEXT),
254 GetPString(IDS_COMPMOVEFAILEDTEXT),
255 mv->target, fullname);
256 }
257 else
258 saymsg(MB_ENTER,
259 hwnd,
260 GetPString(IDS_SUCCESSTEXT),
261 GetPString(IDS_WASMOVEDTOTEXT), mv->target, fullname);
262 }
263 else
264 saymsg(MB_CANCEL | MB_ICONEXCLAMATION,
265 hwnd,
266 GetPString(IDS_SORRYTEXT),
267 GetPString(IDS_EXISTSASATEXT),
268 fullname,
269 (was) ?
270 GetPString(IDS_FILETEXT) : GetPString(IDS_DIRECTORYTEXT));
271 }
272 WinSetDlgItemText(hwnd, REN_TARGET, mv->target);
273 }
274 break;
275
276 case REN_OVERWRITE:
277 case DID_OK:
278 mv = WinQueryWindowPtr(hwnd, QWL_USER);
279 if (mv) {
280
281 MRESULT mr;
282
283 if (WinQueryButtonCheckstate(hwnd, REN_DONTASK))
284 mv->dontask = TRUE;
285 if (WinQueryButtonCheckstate(hwnd, REN_OVEROLD))
286 mv->overold = TRUE;
287 if (WinQueryButtonCheckstate(hwnd, REN_OVERNEW))
288 mv->overnew = TRUE;
289 *mv->target = 0;
290 WinQueryDlgItemText(hwnd, REN_TARGET, CCHMAXPATH, mv->target);
291 bstrip(mv->target);
292 mr = WinSendMsg(hwnd, UM_RESCAN, MPVOID, MPVOID);
293 if (!mr ||
294 (SHORT1FROMMR(mr) != 2 && SHORT1FROMMP(mp1) == REN_OVERWRITE)) {
295
296 CHAR path[CCHMAXPATH], *p;
297
298 mv->overwrite = (SHORT1FROMMP(mp1) == REN_OVERWRITE);
299 strcpy(path, mv->target);
300 p = strrchr(path, '\\');
301 if (p) {
302 p++;
303 *p = 0;
304 if (SetDir(WinQueryWindow(WinQueryWindow(hwnd, QW_PARENT),
305 QW_OWNER), hwnd, path, 0)) {
306 DosBeep(250, 100);
307 WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, REN_TARGET));
308 break;
309 }
310 }
311 WinDismissDlg(hwnd, 1);
312 }
313 else {
314 DosBeep(250, 100);
315 WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, REN_TARGET));
316 }
317 }
318 break;
319 }
320 return 0;
321 }
322 return WinDefDlgProc(hwnd, msg, mp1, mp2);
323}
324
325#pragma alloc_text(FMRENAME,RenameProc)
Note: See TracBrowser for help on using the repository browser.