source: trunk/dll/mle.c@ 574

Last change on this file since 574 was 574, checked in by Gregg Young, 18 years ago

Use QWL_USER; Replace doesn't move the command and Okay on cmd dialog removed error on unchanged command

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