source: trunk/dll/mle.c@ 1400

Last change on this file since 1400 was 1400, checked in by Gregg Young, 16 years ago

Remainder of changes to rename commafmt.h/c (Ticket 28, 82); Additional strings moved to PCSZs in init.c (Ticket 6); Added WriteDetailsSwitches used it and LoadDetailsSwitches to consolidate inline code (Ticket 343, 344)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.1 KB
Line 
1
2/***********************************************************************
3
4 $Id: mle.c 1400 2009-03-08 17:50:25Z gyoung $
5
6 MLE text editor/viewer
7
8 Copyright (c) 1993-97 M. Kimes
9 Copyright (c) 2004, 2008 Steven H.Levine
10
11 01 Aug 04 SHL Rework lstrip/rstrip usage
12 16 Apr 06 SHL MLEexportfile: rework to avoid wrap problems
13 14 Jul 06 SHL Use Runtime_Error
14 03 Nov 06 SHL Count thread usage
15 22 Mar 07 GKY Use QWL_USER
16 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
17 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
18 26 Aug 07 GKY DosSleep(1) in loops changed to (0)
19 17 Dec 07 GKY Make WPURLDEFAULTSETTINGS the fall back for ftp/httprun
20 29 Feb 08 GKY Refactor global command line variables to notebook.h
21 22 Jun 08 GKY Fixed memory buffer access after it had been freed
22 06 Jul 08 GKY Rework LoadThread logic with Steven's help
23 10 Dec 08 SHL Integrate exception handler support
24 08 Mar 09 GKY Additional strings move to PCSZs in init.c
25
26***********************************************************************/
27
28#include <stdlib.h>
29#include <string.h>
30#include <ctype.h>
31#include <share.h>
32// #include <process.h> // _beginthread
33
34#define INCL_DOS
35#define INCL_WIN
36#define INCL_LONGLONG
37
38#include "fm3dll.h"
39#include "fm3dll2.h" // #define's for UM_*, control id's, etc.
40#include "mle.h"
41#include "init.h" // Data declaration(s)
42#include "mainwnd.h" // Data declaration(s)
43#include "newview.h" // Data declarations
44#include "fm3dlg.h"
45#include "fm3str.h"
46#include "errutil.h" // Dos_Error...
47#include "strutil.h" // GetPString
48#include "notebook.h" // httprun etc
49#include "autoview.h" // CreateHexDump
50#include "saveclip.h" // SaveToClip
51#include "common.h" // DecrThreadUsage, IncrThreadUsage
52#include "chklist.h" // PosOverOkay
53#include "valid.h" // TestBinary
54#include "strips.h" // bstrip
55#include "systemf.h" // runemf2
56#include "wrappers.h" // xfopen
57#include "misc.h" // PostMsg
58#include "fortify.h"
59#include "excputil.h" // xbeginthread
60
61static PSZ pszSrcFile = __FILE__;
62
63#define FAKEROT 1
64#define DOROT13(c) (!isalpha((c)))?(c):((((c) >= (char) 'A') && \
65 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
66 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
67 ((c) - (char) 0xd):(c)
68
69/*((FAKEROT==0)?(c):(FAKEROT==1)?(!isalpha((c)))?(c):((((c) >= (char) 'A') && \
70 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
71 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
72 ((c) - (char) 0xd):(c):((c) >= (char) '!') ? ((((c) + (char) 47) > (char) '~') ? ((c) - (char) 47) :\
73 ((c) + (char) 47)) : (c))*/
74
75LONG MLEgetlinetext(HWND h, LONG l, CHAR * buf, INT maxlen)
76{
77 /* get text of line l from MLE */
78
79 IPT s, e;
80
81 s = MLEstartofline(h, l);
82 e = MLElinelenleft(h, s);
83 return MLEtextatpos(h, s, buf, min((INT) e, maxlen));
84}
85
86LONG MLEdeleteline(HWND h, LONG l)
87{
88 /* delete line l from MLE */
89
90 IPT s, e;
91
92 s = MLEstartofline(h, l);
93 e = MLElinelenleft(h, s);
94 return MLEdelete(h, s, e);
95}
96
97LONG MLEdeletecurline(HWND h)
98{
99 /* delete current line from MLE */
100
101 LONG l;
102
103 l = MLEcurline(h);
104 return MLEdeleteline(h, l);
105}
106
107LONG MLEdeletetoeol(HWND h)
108{
109 /* delete from cursor pos to end of line */
110
111 IPT s, e;
112
113 s = MLEcurpos(h);
114 e = MLEcurlenleft(h);
115 return MLEdelete(h, s, e);
116}
117
118VOID MLEclearall(HWND h)
119{
120 /* remove all text from MLE */
121 LONG len;
122
123 len = MLEgetlen(h);
124 if (len)
125 MLEdelete(h, 0, len);
126}
127
128LONG MLEtextatcursor(HWND h, CHAR * buffer, INT buflen)
129{
130 /* place up to buflen chars of text from cursor pos into buffer
131 * return # of chars imported
132 */
133
134 IPT i;
135
136 i = MLEcurpos(h);
137 return MLEtextatpos(h, i, buffer, buflen);
138}
139
140LONG MLEtextatpos(HWND h, IPT i, CHAR * buffer, INT buflen)
141{
142 /* place up to buflen chars of text from pos i in buffer
143 * return # of chars imported
144 */
145
146 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer),
147 MPFROMLONG((LONG) buflen));
148 return (LONG) WinSendMsg(h, MLM_EXPORT,
149 MPFROMP(&i), MPFROMLONG((PLONG) & buflen));
150}
151
152LONG MLEsizeofsel(HWND h)
153{
154 /* return length of selected text */
155
156 IPT cursor, anchor, test;
157
158 cursor = MLEcurpos(h);
159 anchor = MLEancpos(h);
160 test = min(cursor, anchor);
161 /* MLE fakes us out; get real length in bytes */
162 return (LONG) WinSendMsg(h, MLM_QUERYFORMATTEXTLENGTH,
163 MPFROMLONG(test),
164 MPFROMLONG((LONG) ((cursor < anchor) ?
165 (anchor - cursor) :
166 (cursor - anchor))));
167}
168
169VOID MLEinternet(HWND h, BOOL ftp)
170{
171 CHAR *temp = NULL;
172 IPT ancpos, curpos, here;
173 LONG len, oldlen;
174 APIRET rc;
175 ULONG size;
176
177 len = MLEsizeofsel(h);
178 len = min(2048, len);
179 oldlen = len;
180 if (len) {
181 len++;
182 rc = DosAllocMem((PVOID) & temp, 4096,
183 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
184 if (rc || !temp)
185 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
186 GetPString(IDS_OUTOFMEMORY));
187 else {
188 ancpos = MLEancpos(h);
189 curpos = MLEcurpos(h);
190 here = min(curpos, ancpos);
191 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(len));
192 len = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&here), MPFROMP(&len));
193 if (len <= 1)
194 Runtime_Error(pszSrcFile, __LINE__, "len <= 1");
195 else {
196 if (len > oldlen)
197 len--;
198 temp[len] = 0;
199 bstripcr(temp);
200 if (*temp) {
201 if (ftp) {
202 if (fFtpRunWPSDefault) {
203 CHAR WPSDefaultFtpRun[CCHMAXPATH], WPSDefaultFtpRunDir[CCHMAXPATH];
204
205 size = sizeof(WPSDefaultFtpRun);
206 PrfQueryProfileData(HINI_USERPROFILE, PCSZ_WPURLDEFAULTSETTINGS,
207 "DefaultBrowserExe", WPSDefaultFtpRun, &size);
208 size = sizeof(WPSDefaultFtpRunDir);
209 PrfQueryProfileData(HINI_USERPROFILE, PCSZ_WPURLDEFAULTSETTINGS,
210 "DefaultWorkingDir", WPSDefaultFtpRunDir, &size);
211 runemf2(SEPARATE | WINDOWED,
212 h, pszSrcFile, __LINE__,
213 WPSDefaultFtpRunDir,
214 fLibPathStrictFtpRun ? pLibPathStrict : NULL,
215 "%s %s", WPSDefaultFtpRun, temp);
216 }
217 else
218 runemf2(SEPARATE | WINDOWED,
219 h, pszSrcFile, __LINE__,
220 ftprundir, NULL, "%s %s", ftprun, temp);
221 }
222 else
223 if (fHttpRunWPSDefault) {
224 CHAR WPSDefaultHttpRun[CCHMAXPATH], WPSDefaultHttpRunDir[CCHMAXPATH];
225
226 size = sizeof(WPSDefaultHttpRun);
227 PrfQueryProfileData(HINI_USERPROFILE, PCSZ_WPURLDEFAULTSETTINGS,
228 "DefaultBrowserExe", WPSDefaultHttpRun, &size);
229 size = sizeof(WPSDefaultHttpRunDir);
230 PrfQueryProfileData(HINI_USERPROFILE, PCSZ_WPURLDEFAULTSETTINGS,
231 "DefaultWorkingDir", WPSDefaultHttpRunDir, &size);
232 runemf2(SEPARATE | WINDOWED,
233 h, pszSrcFile, __LINE__,
234 WPSDefaultHttpRunDir,
235 fLibPathStrictHttpRun ? pLibPathStrict : NULL,
236 "%s %s", WPSDefaultHttpRun, temp);
237 }
238 else
239 runemf2(SEPARATE | WINDOWED,
240 h, pszSrcFile, __LINE__,
241 httprundir, NULL, "%s %s", httprun, temp);
242 }
243 }
244 DosFreeMem(temp);
245 }
246 }
247}
248
249BOOL MLEdoblock(HWND h, INT action, CHAR * filename)
250{
251 /* perform action on text in selection */
252
253 register CHAR *p;
254 CHAR *sel, *temp = NULL;
255 IPT ancpos, curpos, here;
256 LONG sellen, oldlen;
257 APIRET rc;
258
259 oldlen = MLEsizeofsel(h);
260 if (!oldlen)
261 return TRUE;
262 sel = xmallocz((size_t) (oldlen + 2), pszSrcFile, __LINE__);
263 if (!sel)
264 return FALSE;
265 rc = DosAllocMem((PVOID) & temp, 32768L,
266 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
267 if (rc || !temp) {
268 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
269 GetPString(IDS_OUTOFMEMORY));
270 free(sel);
271# ifdef FORTIFY
272 Fortify_LeaveScope();
273# endif
274 DosPostEventSem(CompactSem);
275 return FALSE;
276 }
277
278 ancpos = MLEancpos(h);
279 curpos = MLEcurpos(h);
280 here = min(curpos, ancpos);
281 p = sel;
282 MLEdisable(h);
283 while (oldlen > 0) {
284 sellen = min(oldlen + 1, 32701);
285 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(sellen));
286 sellen =
287 (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&here), MPFROMP(&sellen));
288 if (sellen < 1) {
289 Runtime_Error(pszSrcFile, __LINE__, "len < 1");
290 free(sel);
291# ifdef FORTIFY
292 Fortify_LeaveScope();
293# endif
294 DosPostEventSem(CompactSem);
295 return FALSE;
296 }
297 if (sellen > min(oldlen, 32700))
298 sellen--;
299 memcpy(p, temp, sellen);
300 p += sellen;
301 oldlen -= sellen;
302 }
303 switch (action) {
304 case APPENDCLIP:
305 SaveToClip(h, sel, TRUE);
306 DosFreeMem(temp);
307 free(sel);
308# ifdef FORTIFY
309 Fortify_LeaveScope();
310# endif
311 MLEenable(h);
312 DosPostEventSem(CompactSem);
313 return TRUE;
314
315 case WRITE:
316 {
317 FILE *fp;
318
319 fp = fopen(filename, "a+");
320 if (!fp)
321 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
322 if (fp) {
323 fseek(fp, 0L, SEEK_END);
324 fwrite(sel, 1, strlen(sel), fp);
325 fclose(fp);
326 }
327#ifdef __DEBUG_ALLOC__
328 _heap_check();
329#endif
330 DosFreeMem(temp);
331 free(sel);
332# ifdef FORTIFY
333 Fortify_LeaveScope();
334# endif
335 MLEenable(h);
336 DosPostEventSem(CompactSem);
337 return TRUE;
338 }
339
340 case UPPERCASE:
341 p = sel;
342 while (*p) {
343 if (isalpha(*p))
344 *p = toupper(*p);
345 p++;
346 }
347 break;
348
349 case LOWERCASE:
350 p = sel;
351 while (*p) {
352 if (isalpha(*p))
353 *p = tolower(*p);
354 p++;
355 }
356 break;
357
358 case TOGGLECASE:
359 p = sel;
360 while (*p) {
361 if (isalpha(*p)) {
362 if (islower(*p))
363 *p = toupper(*p);
364 else
365 *p = tolower(*p);
366 }
367 p++;
368 }
369 break;
370
371 case ROT13:
372 p = sel;
373 while (*p) {
374 *p = DOROT13(*p); // fixme condition both true and false?
375 p++;
376 }
377 break;
378
379 case XOR:
380 p = sel;
381 while (*p) {
382 *p = (~*p);
383 p++;
384 }
385 break;
386
387 case FORMAT:
388 p = sel;
389 while (*p) {
390 if (*p == '\r') {
391 memmove(p, p + 1, strlen(p));
392 continue;
393 }
394 if (*p == '\n') {
395 *p = ' ';
396 continue;
397 }
398 p++;
399 }
400 break;
401
402 default: /* unknown action */
403#ifdef __DEBUG_ALLOC__
404 _heap_check();
405#endif
406 DosFreeMem(temp);
407 free(sel);
408# ifdef FORTIFY
409 Fortify_LeaveScope();
410# endif
411 DosPostEventSem(CompactSem);
412 MLEenable(h);
413 return FALSE;
414 }
415
416 /* replace selection with altered text */
417 p = sel;
418 here = min(curpos, ancpos);
419 MLEclear(h); /* delete current selection */
420 sellen = oldlen = strlen(sel); /* actual number of bytes */
421 while (oldlen > 0) {
422 sellen = min(oldlen, 32700);
423 memcpy(temp, p, sellen);
424 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(sellen));
425 sellen = (LONG) WinSendMsg(h,
426 MLM_IMPORT,
427 MPFROMP(&here), MPFROMLONG(sellen));
428 if (!sellen) {
429 Runtime_Error(pszSrcFile, __LINE__, "sellen 0");
430 break;
431 }
432 p += sellen;
433 oldlen -= sellen;
434 if (oldlen && *p == '\n' /* && *(p - 1) == '\r' */ )
435 p--;
436 } // while
437 WinSendMsg(h, MLM_SETSEL, MPFROMLONG(ancpos), MPFROMLONG(curpos));
438 MLEenable(h);
439#ifdef __DEBUG_ALLOC__
440 _heap_check();
441#endif
442 DosFreeMem(temp);
443 free(sel);
444# ifdef FORTIFY
445 Fortify_LeaveScope();
446# endif
447 DosPostEventSem(CompactSem);
448 return TRUE;
449}
450
451BOOL MLEquotepara(HWND h, CHAR * initials, BOOL fQuoteOld)
452{
453 LONG num;
454 CHAR lineend[2], line[8], *p;
455
456 if (!initials || !*initials)
457 initials = " > ";
458 num = MLEcurline(h);
459 while (MLElinelen(h, num) < 3L && MLEnumlines(h) >= num)
460 num++;
461 while (MLElinelen(h, num) > 2L && MLEnumlines(h) >= num) {
462 memset(line, 0, 8);
463 MLEgetlinetext(h, num, line, 7L);
464 line[7] = 0;
465 if ((p = strchr(line, '>')) == NULL) {
466 MLEsetcurpos(h, MLEstartofline(h, num));
467 MLEinsert(h, initials);
468 MLEsetcurpos(h, (MLEstartofline(h, num) + MLElinelen(h, num)) - 1L);
469 MLEtextatcursor(h, lineend, 2L);
470 if (*lineend != '\r' && *lineend != '\n')
471 MLEinsert(h, "\n");
472 }
473 else if (fQuoteOld) {
474 while (isspace(line[strlen(line) - 1]))
475 line[strlen(line) - 1] = 0;
476 MLEsetcurpos(h, MLEstartofline(h, num) + (p - line));
477 MLEinsert(h, ">");
478 }
479 num++;
480 }
481 MLEsetcurpos(h, MLEstartofline(h, num));
482 return TRUE;
483}
484
485BOOL MLEAutoLoad(HWND h, CHAR * filename)
486{
487 XMLEWNDPTR *vw;
488
489 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
490 if (vw && vw->size != sizeof(XMLEWNDPTR))
491 vw = NULL;
492 if (TestBinary(filename)) {
493 if (vw)
494 vw->hex = 1;
495 return MLEHexLoad(h, filename);
496 }
497 if (vw)
498 vw->hex = 2;
499 return MLEloadfile(h, filename);
500}
501
502BOOL MLEHexLoad(HWND h, CHAR * filename)
503{
504 /* insert a file into the current position in the MLE */
505
506 HAB hab;
507 CHAR *buffer = NULL, *hexbuff = NULL;
508 IPT iptOffset = -1;
509 ULONG numread, howmuch, numimport, action, len, left = 0;
510 BOOL ret = TRUE, first = TRUE;
511 CHAR titletext[512];
512 HWND grandpa;
513 XMLEWNDPTR *vw;
514 HFILE handle;
515 APIRET rc;
516
517 *titletext = 0;
518 hab = WinQueryAnchorBlock(h);
519 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
520 if (vw && vw->size != sizeof(XMLEWNDPTR))
521 vw = NULL;
522 grandpa = GrandparentOf(h);
523 *titletext = 0;
524 WinQueryWindowText(grandpa, 512, titletext);
525 rc = DosOpen(filename, &handle, &action, 0, 0,
526 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
527 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT |
528 OPEN_FLAGS_SEQUENTIAL | OPEN_SHARE_DENYNONE |
529 OPEN_ACCESS_READONLY, 0);
530 if (rc) {
531 ret = FALSE;
532 }
533 else {
534 DosChgFilePtr(handle, 0, FILE_END, &len);
535 DosChgFilePtr(handle, 0, FILE_BEGIN, &action);
536 if (len) {
537 rc = DosAllocMem((PVOID) & hexbuff, 50001,
538 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
539 if (rc || !hexbuff) {
540 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
541 GetPString(IDS_OUTOFMEMORY));
542 ret = FALSE;
543 }
544 else {
545 buffer = xmalloc(10000, pszSrcFile, __LINE__);
546 if (!buffer)
547 ret = FALSE;
548 else {
549 MLEclearall(h);
550 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(hexbuff),
551 MPFROMLONG(50000L));
552 if (!DosRead(handle, buffer, min(10000, len), &numread) && numread) {
553
554 CHAR s[81];
555
556 MLEsetwrap(h, FALSE);
557 WinSetSysValue(HWND_DESKTOP, SV_INSERTMODE, FALSE);
558 *hexbuff = 0;
559 numimport = CreateHexDump(buffer,
560 numread, hexbuff, 50000, left, TRUE);
561 while (len && numimport) { /* import entire file */
562 left += numread;
563 len -= numread;
564 if (!WinIsWindow(hab, h) || (vw && vw->killme))
565 break;
566 howmuch = (INT) WinSendMsg(h,
567 MLM_IMPORT,
568 MPFROMP(&iptOffset),
569 MPFROMLONG(numimport));
570 if (first && len) {
571 MLEdisable(h);
572 WinEnableWindowUpdate(h, FALSE);
573 first = FALSE;
574 }
575// fprintf(stderr,"%d bytes of %d imported\n",howmuch,numimport);
576 if (howmuch < 1) {
577 numimport = 0;
578 break;
579 }
580 while (howmuch < numimport) {
581 numimport -= howmuch;
582 memmove(hexbuff, hexbuff + howmuch, numimport);
583 DosSleep(0); //26 Aug 07 GKY 1
584 if (!WinIsWindow(hab, h) || (vw && vw->killme))
585 break;
586 howmuch = (INT) WinSendMsg(h,
587 MLM_IMPORT,
588 MPFROMP(&iptOffset),
589 MPFROMLONG((LONG) numimport));
590 if (howmuch < 1)
591 break;
592 }
593 if (DosRead(handle, buffer, min(10000, len), &numread)
594 || !numread) {
595 numimport = 0;
596 break;
597 }
598 *hexbuff = 0;
599 numimport =
600 CreateHexDump(buffer, numread, hexbuff, 50000, left, TRUE);
601 if (numimport < 1 || !WinIsWindow(hab, h) || (vw && vw->killme)) {
602 numimport = 0;
603 break;
604 }
605 sprintf(s, GetPString(IDS_LOADINGMLETEXT), len);
606 WinSetWindowText(grandpa, s);
607 }
608 DosSleep(1);
609 }
610 else
611 ret = FALSE;
612 free(buffer);
613# ifdef FORTIFY
614 Fortify_LeaveScope();
615# endif
616 }
617 DosFreeMem(hexbuff);
618 }
619 }
620 if (WinIsWindow(hab, h))
621 WinSetWindowText(grandpa, titletext);
622 DosClose(handle);
623 }
624 if (!first) {
625 WinEnableWindowUpdate(h, TRUE);
626 MLEenable(h);
627 }
628 MLEsetchanged(h, FALSE);
629 return ret;
630}
631
632//== MLEinsertfile() insert a file into the current position in the MLE ==
633
634BOOL MLEinsertfile(HWND h, CHAR * filename)
635{
636
637 HAB hab;
638 FILE *fp;
639 CHAR *buffer = NULL;
640 INT len;
641 IPT iptOffset = -1L;
642 INT numread, howmuch, tempnum, x;
643 BOOL ret = TRUE, first = TRUE, once = FALSE, dont = FALSE;
644 CHAR titletext[512];
645 HWND grandpa;
646 XMLEWNDPTR *vw;
647 APIRET rc;
648
649 *titletext = 0;
650 hab = WinQueryAnchorBlock(h);
651 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
652 if (vw && vw->size != sizeof(XMLEWNDPTR))
653 vw = NULL;
654 grandpa = GrandparentOf(h);
655 *titletext = 0;
656 WinQueryWindowText(grandpa, 512, titletext);
657 fp = _fsopen(filename, "r", SH_DENYNO);
658 if (!fp)
659 ret = FALSE;
660 else {
661 setvbuf(fp, NULL, _IONBF, 0);
662 fseek(fp, 0L, SEEK_END);
663 len = (INT) ftell(fp);
664 fseek(fp, 0L, SEEK_SET);
665 if (len && len != -1) {
666 rc = DosAllocMem((PVOID) & buffer,
667 50000L, PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
668 if (rc || !buffer) {
669 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
670 GetPString(IDS_OUTOFMEMORY));
671 ret = FALSE;
672 }
673 else {
674 WinSendMsg(h,
675 MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(50000L));
676 numread = fread(buffer, 1, min(50000, len), fp);
677 if (numread < 1)
678 ret = FALSE;
679 while (len && numread > 0) { /* here we go... */
680
681 CHAR s[81];
682
683 while (numread > 0) { /* import entire file */
684 if (!WinIsWindow(hab, h) || (vw && vw->killme))
685 break;
686 if (strlen(buffer) < numread) {
687 if (!once && !dont)
688 rc = saymsg(MB_YESNOCANCEL,
689 HWND_DESKTOP,
690 GetPString(IDS_WARNINGTEXT),
691 GetPString(IDS_TEXTNULSTEXT));
692 else if (once)
693 rc = MBID_YES;
694 else if (dont)
695 rc = MBID_NO;
696 if (rc == MBID_YES) {
697 once = FALSE;
698 for (x = 0; x < numread; x++) {
699 if (!buffer[x])
700 buffer[x] = ' ';
701 }
702 }
703 else if (rc == MBID_CANCEL) {
704 len = 0;
705 numread = 0;
706 saymsg(MB_ENTER,
707 HWND_DESKTOP,
708 GetPString(IDS_OBEYTEXT),
709 GetPString(IDS_LOADCANCELLEDTEXT));
710 break;
711 }
712 else if (rc == MBID_NO)
713 dont = TRUE;
714 }
715 howmuch = (INT) WinSendMsg(h,
716 MLM_IMPORT,
717 MPFROMP(&iptOffset),
718 MPFROMLONG((LONG) numread));
719 if (first && !feof(fp)) {
720 MLEdisable(h);
721 WinEnableWindowUpdate(h, FALSE);
722 first = FALSE;
723 }
724// fprintf(stderr,"%d bytes of %d imported\n",howmuch,numread);
725 if (howmuch < 1) {
726 numread = 0;
727 break;
728 }
729 len -= howmuch;
730 if (howmuch < numread) {
731 numread -= howmuch;
732 memmove(buffer, buffer + howmuch, numread);
733 if (numread && len) {
734 tempnum = numread;
735 numread = fread(buffer + tempnum,
736 1, min(50000 - tempnum, len), fp);
737 if (numread > 1)
738 numread += tempnum;
739 else
740 numread = tempnum;
741 }
742 DosSleep(0); //26 Aug 07 GKY 1
743 }
744 else
745 numread = fread(buffer, 1, min(50000, len), fp);
746 if (numread < 1 || !WinIsWindow(hab, h) || (vw && vw->killme)) {
747 numread = 0;
748 break;
749 }
750 sprintf(s, GetPString(IDS_LOADINGMLETEXT), len);
751 WinSetWindowText(grandpa, s);
752 }
753 DosSleep(0); //26 Aug 07 GKY 1
754 }
755 DosFreeMem(buffer);
756 }
757 }
758 if (WinIsWindow(hab, h))
759 WinSetWindowText(grandpa, titletext);
760 fclose(fp);
761 }
762 if (!first) {
763 WinEnableWindowUpdate(h, TRUE);
764 MLEenable(h);
765 }
766 return ret;
767}
768
769typedef struct
770{
771 USHORT size;
772 USHORT hex;
773 HWND h;
774 CHAR filename[CCHMAXPATH];
775 HWND hwndReport;
776 HWND msg;
777}
778BKGLOAD;
779
780VOID LoadThread(VOID * arg)
781{
782 BKGLOAD *bkg;
783 BOOL fSuccess;
784 HAB thab;
785 HMQ thmq;
786
787 DosError(FERR_DISABLEHARDERR);
788
789# ifdef FORTIFY
790 Fortify_EnterScope();
791# endif
792
793 bkg = (BKGLOAD *) arg;
794 if (bkg) {
795 thab = WinInitialize(0);
796 if (thab) {
797 thmq = WinCreateMsgQueue(thab, 0);
798 if (thmq) {
799 WinCancelShutdown(thmq, TRUE);
800 IncrThreadUsage();
801 priority_normal();
802 if (bkg->hex == 1)
803 fSuccess = MLEHexLoad(bkg->h, bkg->filename);
804 else if (bkg->hex == 2)
805 fSuccess = MLEloadfile(bkg->h, bkg->filename);
806 else
807 fSuccess = MLEAutoLoad(bkg->h, bkg->filename);
808 priority_bumped();
809 if (bkg->hwndReport && WinIsWindow(thab, bkg->hwndReport))
810 PostMsg(bkg->hwndReport, bkg->msg, MPFROMLONG(fSuccess),
811 MPFROMP(bkg->filename));
812#ifdef __DEBUG_ALLOC__
813 _heap_check();
814#endif
815 WinDestroyMsgQueue(thmq);
816 }
817 else
818 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
819 DecrThreadUsage();
820 WinTerminate(thab);
821 }
822 else {
823 // fixme to be gone? - can not PostMsg without HAB
824 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
825 }
826 free(bkg);
827 } // if bkg
828# ifdef FORTIFY
829 Fortify_LeaveScope();
830# endif
831 // _endthread(); // 10 Dec 08 SHL
832}
833
834INT MLEbackgroundload(HWND hwndReport, ULONG msg, HWND h, CHAR * filename,
835 INT hex)
836{
837 /* load a file into the MLE in the background (via a separate thread)
838 * return _beginthread status
839 */
840
841 BKGLOAD *bkg;
842
843 bkg = xmallocz(sizeof(BKGLOAD), pszSrcFile, __LINE__);
844 if (!bkg)
845 return -1;
846 bkg->size = sizeof(BKGLOAD);
847 bkg->hex = (USHORT) hex;
848 bkg->hwndReport = hwndReport;
849 bkg->msg = msg;
850 bkg->h = h;
851 strcpy(bkg->filename, filename);
852 return xbeginthread(LoadThread,
853 65536,
854 bkg,
855 pszSrcFile,
856 __LINE__);
857}
858
859BOOL MLEloadfile(HWND h, CHAR * filename)
860{
861 /* load a file into the MLE, getting rid of whatever was already
862 * there. Note this returns without erasing existing text if the
863 * file to load does not exist
864 */
865
866 FILESTATUS3 fsa;
867 BOOL ret;
868
869 if (!DosQueryPathInfo(filename, FIL_STANDARD, &fsa, sizeof(fsa)) &&
870 ~fsa.attrFile & FILE_DIRECTORY) {
871 MLEclearall(h);
872 ret = MLEinsertfile(h, filename);
873 MLEsetchanged(h, FALSE);
874 return ret;
875 }
876 return FALSE;
877}
878
879BOOL MLEexportfile(HWND h, CHAR * filename, INT tabspaces,
880 BOOL striptraillines, BOOL striptrailspaces)
881{
882 /* save the MLE contents as a file. Format the output so that
883 * the file is CR/LF terminated as presented in the MLE.
884 */
885
886 FILE *fp = NULL;
887 CHAR *buffer = NULL;
888 CHAR *p;
889 BOOL ok = TRUE;
890 INT blanklines = 0;
891 BOOL fWrap = MLEgetwrap(h);
892 APIRET rc;
893
894 // saymsg(MB_ENTER,h,DEBUG_STRING,"len = %ld",MLEgetlen(h));
895 if (!MLEgetlen(h)) /* nothing to save; forget it */
896 return TRUE;
897
898 MLEsetwrap(h, FALSE); // Need wrap off to export MLFIE_NOTRANS
899
900 if (striptraillines) {
901
902 register LONG x;
903 LONG numlines;
904
905 numlines = MLEnumlines(h);
906 for (x = numlines; x; x--) {
907 if (MLElinelen(h, x - 1L) < 2)
908 MLEdeleteline(h, x - 1L);
909 else
910 break;
911 }
912 if (!MLEgetlen(h)) {
913 /* nothing to save; forget it */
914 MLEsetwrap(h, fWrap); // Restore
915 return TRUE;
916 }
917 }
918
919 rc = DosAllocMem((PVOID) & buffer, 4096L,
920 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
921 if (rc || !buffer) {
922 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
923 GetPString(IDS_OUTOFMEMORY));
924 ok = FALSE;
925 }
926 else {
927 fp = fopen(filename, "a+");
928 if (!fp)
929 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
930 if (!fp)
931 ok = FALSE;
932 else {
933 LONG numlines, ret, len, wl, temp;
934 IPT s;
935
936 fseek(fp, 0L, SEEK_END);
937 numlines = MLEnumlines(h);
938
939 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(4095L));
940 for (temp = 0; temp < numlines; temp++) {
941 s = MLEstartofline(h, temp);
942 wl = len = (LONG) MLElinelenleft(h, s);
943 ret = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&s), MPFROMP(&len));
944 if (ret < 0)
945 break;
946 wl = min(wl, ret);
947 buffer[wl] = 0;
948 if (*buffer) {
949 p = buffer + strlen(buffer) - 1;
950 while (p >= buffer && (*p == '\n' || *p == '\r')) {
951 *p = 0;
952 p--;
953 }
954 }
955 if (tabspaces) {
956 p = buffer;
957 while (*p) {
958 if (*p == '\t') {
959 *p = ' ';
960 memmove((p + tabspaces) - 1, p, strlen(p) + 1);
961 memset(p, ' ', tabspaces);
962 }
963 p++;
964 }
965 }
966 if (striptrailspaces && *buffer) {
967 p = buffer + strlen(buffer) - 1;
968 while (p >= buffer && (*p == ' ' || *p == '\t')) {
969 *p = 0;
970 p--;
971 }
972 }
973 if (striptraillines) {
974 if (!*buffer) {
975 blanklines++;
976 continue;
977 }
978 else {
979 while (blanklines) {
980 fwrite("\n", 1, 1, fp);
981 blanklines--;
982 }
983 }
984 }
985 strcat(buffer, "\n");
986 // buffer = translate_out(buffer,4095,h,filename);
987 if (fwrite(buffer, 1, strlen(buffer), fp) < 1) {
988 saymsg(MB_ENTER,
989 h, GetPString(IDS_ARGHTEXT), GetPString(IDS_WRITEERRORTEXT));
990 break;
991 }
992 } // for lines
993 }
994 }
995
996 MLEsetwrap(h, fWrap); // Restore
997
998 if (fp)
999 fclose(fp);
1000 if (buffer)
1001 DosFreeMem(buffer);
1002
1003 return ok;
1004}
1005
1006MRESULT EXPENTRY SandRDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
1007{
1008 /* initiate search(/replace)s in edit mode */
1009
1010 SRCHPTR *vw;
1011
1012 if (msg != WM_INITDLG)
1013 vw = (SRCHPTR *) WinQueryWindowPtr(hwnd, QWL_USER);
1014 else
1015 vw = NULL;
1016
1017 switch (msg) {
1018 case WM_INITDLG:
1019 vw = (SRCHPTR *) mp2;
1020 if (!vw) {
1021 WinDismissDlg(hwnd, 0);
1022 break;
1023 }
1024 WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mp2);
1025 WinSendDlgItemMsg(hwnd, SRCH_SEARCH, EM_SETTEXTLIMIT,
1026 MPFROM2SHORT(256, 0), MPVOID);
1027 WinSendDlgItemMsg(hwnd, SRCH_REPLACE, EM_SETTEXTLIMIT,
1028 MPFROM2SHORT(256, 0), MPVOID);
1029 if (*vw->search)
1030 WinSetDlgItemText(hwnd, SRCH_SEARCH, vw->search);
1031 if (!MLEgetreadonly(vw->hwndmle)) {
1032 if (*vw->replace)
1033 WinSetDlgItemText(hwnd, SRCH_REPLACE, vw->replace);
1034 WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_SETCHECK,
1035 MPFROM2SHORT(vw->sandr, 0), MPVOID);
1036 WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_SETCHECK,
1037 MPFROM2SHORT(vw->rall, 0), MPVOID);
1038 }
1039 else {
1040 WinEnableWindow(WinWindowFromID(hwnd, SRCH_SANDR), FALSE);
1041 WinEnableWindow(WinWindowFromID(hwnd, SRCH_RALL), FALSE);
1042 WinEnableWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
1043 WinShowWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
1044 *vw->replace = 0;
1045 vw->sandr = FALSE;
1046 vw->rall = FALSE;
1047 }
1048 memset(&vw->se, 0, sizeof(MLE_SEARCHDATA));
1049 vw->se.cb = sizeof(MLE_SEARCHDATA);
1050 vw->se.pchFind = (PCHAR) vw->search;
1051 vw->se.cchFind = (SHORT) strlen(vw->search);
1052 vw->se.pchReplace = (PCHAR) vw->replace;
1053 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1054 vw->se.iptStart = MLEcurpos(vw->hwndmle);
1055 vw->se.iptStop = -1L;
1056 vw->se.cchFound = 0;
1057 PosOverOkay(hwnd);
1058 break;
1059
1060 case WM_CONTROL:
1061 return 0;
1062
1063 case WM_COMMAND:
1064 switch (SHORT1FROMMP(mp1)) {
1065 case IDM_HELP:
1066 saymsg(MB_ENTER | MB_ICONASTERISK,
1067 hwnd,
1068 NullStr,
1069 GetPString(IDS_ENTERSEARCHSTRINGTEXT),
1070 (MLEgetreadonly(vw->hwndmle)) ?
1071 "." : GetPString(IDS_REPLACESTRINGTEXT));
1072 break;
1073
1074 case DID_CANCEL:
1075 WinDismissDlg(hwnd, 0);
1076 break;
1077
1078 case DID_OK:
1079 WinShowWindow(hwnd, FALSE);
1080 {
1081 CHAR temp[257];
1082 APIRET ret;
1083
1084 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_QUERYCHECK,
1085 MPVOID, MPVOID))
1086 vw->sandr = TRUE;
1087 else
1088 vw->sandr = FALSE;
1089 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_QUERYCHECK,
1090 MPVOID, MPVOID))
1091 vw->rall = TRUE;
1092 else
1093 vw->rall = FALSE;
1094 *vw->replace = 0;
1095 WinQueryDlgItemText(hwnd, SRCH_REPLACE, 256, vw->replace);
1096 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1097 WinQueryDlgItemText(hwnd, SRCH_SEARCH, 256, temp);
1098 if (*temp) {
1099 strcpy(vw->search, temp);
1100 vw->se.cchFind = (SHORT) strlen(vw->search);
1101 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1102 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1103 (MLFSEARCH_CASESENSITIVE *
1104 (vw->fInsensitive ==
1105 FALSE)) | (MLFSEARCH_CHANGEALL *
1106 (vw->rall != 0))),
1107 MPFROMP(&vw->se))) {
1108 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1109 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1110 WinDismissDlg(hwnd, 0);
1111 break;
1112 }
1113 else if (vw->sandr && !vw->rall) {
1114 ret = saymsg(MB_YESNOCANCEL,
1115 hwnd,
1116 NullStr,
1117 GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1118 if (ret == MBID_YES)
1119 MLEinsert(vw->hwndmle, vw->replace);
1120 else if (ret == MBID_CANCEL) {
1121 WinDismissDlg(hwnd, 0);
1122 break;
1123 }
1124 WinDismissDlg(hwnd, 1);
1125 break;
1126 }
1127 }
1128 WinDismissDlg(hwnd, 0);
1129 }
1130 break;
1131 }
1132 return 0;
1133
1134 case WM_CLOSE:
1135 break;
1136 }
1137
1138 return WinDefDlgProc(hwnd, msg, mp1, mp2);
1139}
1140
1141BOOL MLEfindfirst(HWND hwnd, SRCHPTR * vw)
1142{
1143 if (MLEsizeofsel(vw->hwndmle) < 256L) {
1144 MLEgetseltext(vw->hwndmle, vw->search);
1145 vw->search[255] = 0;
1146 }
1147 if (WinDlgBox(HWND_DESKTOP, hwnd, SandRDlgProc, FM3ModHandle,
1148 SRCH_FRAME, MPFROMP(vw)) && *vw->search)
1149 return TRUE;
1150 return FALSE;
1151}
1152
1153INT MLEfindnext(HWND hwnd, SRCHPTR * vw)
1154{
1155 if (!*vw->search)
1156 return -1;
1157 else {
1158 vw->se.iptStart++;
1159 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1160 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1161 (MLFSEARCH_CASESENSITIVE *
1162 (vw->fInsensitive ==
1163 FALSE)) | (MLFSEARCH_CHANGEALL *
1164 (vw->rall))), MPFROMP(&vw->se)))
1165 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1166 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1167 else if (vw->sandr && !vw->rall) {
1168
1169 APIRET ret;
1170
1171 ret = saymsg(MB_YESNOCANCEL,
1172 hwnd,
1173 NullStr, GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1174 if (ret == MBID_YES)
1175 MLEinsert(vw->hwndmle, vw->replace);
1176 if (ret != MBID_CANCEL)
1177 return 1;
1178 }
1179 }
1180 return 0;
1181}
1182
1183#pragma alloc_text(FMMLE,MLEgetlinetext,MLEdeleteline,MLEdeletecurline,MLEdeletetoeol)
1184#pragma alloc_text(FMMLE,MLEclearall,MLEtextatcursor,MLEtextatpos,MLEsizeofsel)
1185#pragma alloc_text(FMMLE3,MLEdoblock,MLEquotepara,MLEinternet)
1186#pragma alloc_text(FMMLE4,MLEAutoLoad,MLEHexLoad,MLEinsertfile,LoadThread,MLEbackgroundload)
1187#pragma alloc_text(FMMLE5,MLEloadfile,MLEexportfile)
1188#pragma alloc_text(FMMLE3,MLEfindfirst,MLEfindnext,SandRDlgProc)
Note: See TracBrowser for help on using the repository browser.