source: trunk/dll/mle.c@ 1161

Last change on this file since 1161 was 1161, checked in by John Small, 17 years ago

Ticket 187: Draft 1: Functions only

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