source: trunk/dll/mle.c@ 793

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

Move #pragma alloc_text to end for OpenWatcom compat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.1 KB
Line 
1
2/***********************************************************************
3
4 $Id: mle.c 793 2007-08-21 02:53:38Z 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 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
17 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
18
19***********************************************************************/
20
21#define INCL_DOS
22#define INCL_WIN
23#include <os2.h>
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <ctype.h>
29#include <share.h>
30#include <process.h> // _beginthread
31
32#include "fm3dll.h"
33#include "fm3dlg.h"
34#include "mle.h"
35#include "fm3str.h"
36
37static PSZ pszSrcFile = __FILE__;
38
39#define FAKEROT 1
40#define DOROT13(c) (!isalpha((c)))?(c):((((c) >= (char) 'A') && \
41 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
42 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
43 ((c) - (char) 0xd):(c)
44
45/*((FAKEROT==0)?(c):(FAKEROT==1)?(!isalpha((c)))?(c):((((c) >= (char) 'A') && \
46 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
47 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
48 ((c) - (char) 0xd):(c):((c) >= (char) '!') ? ((((c) + (char) 47) > (char) '~') ? ((c) - (char) 47) :\
49 ((c) + (char) 47)) : (c))*/
50
51LONG MLEgetlinetext(HWND h, LONG l, CHAR * buf, INT maxlen)
52{
53 /* get text of line l from MLE */
54
55 IPT s, e;
56
57 s = MLEstartofline(h, l);
58 e = MLElinelenleft(h, s);
59 return MLEtextatpos(h, s, buf, min((INT) e, maxlen));
60}
61
62LONG MLEdeleteline(HWND h, LONG l)
63{
64 /* delete line l from MLE */
65
66 IPT s, e;
67
68 s = MLEstartofline(h, l);
69 e = MLElinelenleft(h, s);
70 return MLEdelete(h, s, e);
71}
72
73LONG MLEdeletecurline(HWND h)
74{
75 /* delete current line from MLE */
76
77 LONG l;
78
79 l = MLEcurline(h);
80 return MLEdeleteline(h, l);
81}
82
83LONG MLEdeletetoeol(HWND h)
84{
85 /* delete from cursor pos to end of line */
86
87 IPT s, e;
88
89 s = MLEcurpos(h);
90 e = MLEcurlenleft(h);
91 return MLEdelete(h, s, e);
92}
93
94VOID MLEclearall(HWND h)
95{
96 /* remove all text from MLE */
97 LONG len;
98
99 len = MLEgetlen(h);
100 if (len)
101 MLEdelete(h, 0, len);
102}
103
104LONG MLEtextatcursor(HWND h, CHAR * buffer, INT buflen)
105{
106 /* place up to buflen chars of text from cursor pos into buffer
107 * return # of chars imported
108 */
109
110 IPT i;
111
112 i = MLEcurpos(h);
113 return MLEtextatpos(h, i, buffer, buflen);
114}
115
116LONG MLEtextatpos(HWND h, IPT i, CHAR * buffer, INT buflen)
117{
118 /* place up to buflen chars of text from pos i in buffer
119 * return # of chars imported
120 */
121
122 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer),
123 MPFROMLONG((LONG) buflen));
124 return (LONG) WinSendMsg(h, MLM_EXPORT,
125 MPFROMP(&i), MPFROMLONG((PLONG) & buflen));
126}
127
128LONG MLEsizeofsel(HWND h)
129{
130 /* return length of selected text */
131
132 IPT cursor, anchor, test;
133
134 cursor = MLEcurpos(h);
135 anchor = MLEancpos(h);
136 test = min(cursor, anchor);
137 /* MLE fakes us out; get real length in bytes */
138 return (LONG) WinSendMsg(h, MLM_QUERYFORMATTEXTLENGTH,
139 MPFROMLONG(test),
140 MPFROMLONG((LONG) ((cursor < anchor) ?
141 (anchor - cursor) :
142 (cursor - anchor))));
143}
144
145VOID MLEinternet(HWND h, BOOL ftp)
146{
147 CHAR *temp = NULL;
148 IPT ancpos, curpos, here;
149 LONG len, oldlen;
150 APIRET rc;
151
152 len = MLEsizeofsel(h);
153 len = min(2048, len);
154 oldlen = len;
155 if (len) {
156 len++;
157 rc = DosAllocMem((PVOID) & temp, 4096,
158 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
159 if (rc || !temp)
160 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
161 GetPString(IDS_OUTOFMEMORY));
162 else {
163 ancpos = MLEancpos(h);
164 curpos = MLEcurpos(h);
165 here = min(curpos, ancpos);
166 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(len));
167 len = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&here), MPFROMP(&len));
168 if (len <= 1)
169 Runtime_Error(pszSrcFile, __LINE__, "len <= 1");
170 else {
171 if (len > oldlen)
172 len--;
173 temp[len] = 0;
174 bstripcr(temp);
175 if (*temp) {
176 if (ftp && *ftprun)
177 runemf2(SEPARATE | WINDOWED,
178 h, NULL, NULL, "%s %s", ftprun, temp);
179 else if (!ftp && *httprun)
180 runemf2(SEPARATE | WINDOWED,
181 h, NULL, NULL, "%s %s", httprun, temp);
182 }
183 }
184 DosFreeMem(temp);
185 }
186 }
187}
188
189BOOL MLEdoblock(HWND h, INT action, CHAR * filename)
190{
191 /* perform action on text in selection */
192
193 register CHAR *p;
194 CHAR *sel, *temp = NULL;
195 IPT ancpos, curpos, here;
196 LONG sellen, oldlen;
197 APIRET rc;
198
199 oldlen = MLEsizeofsel(h);
200 if (!oldlen)
201 return TRUE;
202 sel = xmallocz((size_t) (oldlen + 2), pszSrcFile, __LINE__);
203 if (!sel)
204 return FALSE;
205 rc = DosAllocMem((PVOID) & temp, 32768L,
206 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
207 if (rc || !temp) {
208 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
209 GetPString(IDS_OUTOFMEMORY));
210 free(sel);
211 DosPostEventSem(CompactSem);
212 return FALSE;
213 }
214
215 ancpos = MLEancpos(h);
216 curpos = MLEcurpos(h);
217 here = min(curpos, ancpos);
218 p = sel;
219 MLEdisable(h);
220 while (oldlen > 0) {
221 sellen = min(oldlen + 1, 32701);
222 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(sellen));
223 sellen =
224 (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&here), MPFROMP(&sellen));
225 if (sellen < 1) {
226 Runtime_Error(pszSrcFile, __LINE__, "len < 1");
227 free(sel);
228 DosPostEventSem(CompactSem);
229 return FALSE;
230 }
231 if (sellen > min(oldlen, 32700))
232 sellen--;
233 memcpy(p, temp, sellen);
234 p += sellen;
235 oldlen -= sellen;
236 }
237 switch (action) {
238 case APPENDCLIP:
239 SaveToClip(h, sel, TRUE);
240 DosFreeMem(temp);
241 free(sel);
242 MLEenable(h);
243 DosPostEventSem(CompactSem);
244 return TRUE;
245
246 case WRITE:
247 {
248 FILE *fp;
249
250 fp = fopen(filename, "a+");
251 if (!fp)
252 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
253 if (fp) {
254 fseek(fp, 0L, SEEK_END);
255 fwrite(sel, 1, strlen(sel), fp);
256 fclose(fp);
257 }
258#ifdef __DEBUG_ALLOC__
259 _heap_check();
260#endif
261 DosFreeMem(temp);
262 free(sel);
263 MLEenable(h);
264 DosPostEventSem(CompactSem);
265 return TRUE;
266 }
267
268 case UPPERCASE:
269 p = sel;
270 while (*p) {
271 if (isalpha(*p))
272 *p = toupper(*p);
273 p++;
274 }
275 break;
276
277 case LOWERCASE:
278 p = sel;
279 while (*p) {
280 if (isalpha(*p))
281 *p = tolower(*p);
282 p++;
283 }
284 break;
285
286 case TOGGLECASE:
287 p = sel;
288 while (*p) {
289 if (isalpha(*p)) {
290 if (islower(*p))
291 *p = toupper(*p);
292 else
293 *p = tolower(*p);
294 }
295 p++;
296 }
297 break;
298
299 case ROT13:
300 p = sel;
301 while (*p) {
302 *p = DOROT13(*p); // fixme condition both true and false?
303 p++;
304 }
305 break;
306
307 case XOR:
308 p = sel;
309 while (*p) {
310 *p = (~*p);
311 p++;
312 }
313 break;
314
315 case FORMAT:
316 p = sel;
317 while (*p) {
318 if (*p == '\r') {
319 memmove(p, p + 1, strlen(p));
320 continue;
321 }
322 if (*p == '\n') {
323 *p = ' ';
324 continue;
325 }
326 p++;
327 }
328 break;
329
330 default: /* unknown action */
331#ifdef __DEBUG_ALLOC__
332 _heap_check();
333#endif
334 DosFreeMem(temp);
335 free(sel);
336 DosPostEventSem(CompactSem);
337 MLEenable(h);
338 return FALSE;
339 }
340
341 /* replace selection with altered text */
342 p = sel;
343 here = min(curpos, ancpos);
344 MLEclear(h); /* delete current selection */
345 sellen = oldlen = strlen(sel); /* actual number of bytes */
346 while (oldlen > 0) {
347 sellen = min(oldlen, 32700);
348 memcpy(temp, p, sellen);
349 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(sellen));
350 sellen = (LONG) WinSendMsg(h,
351 MLM_IMPORT,
352 MPFROMP(&here), MPFROMLONG(sellen));
353 if (!sellen) {
354 Runtime_Error(pszSrcFile, __LINE__, "sellen 0");
355 break;
356 }
357 p += sellen;
358 oldlen -= sellen;
359 if (oldlen && *p == '\n' /* && *(p - 1) == '\r' */ )
360 p--;
361 } // while
362 WinSendMsg(h, MLM_SETSEL, MPFROMLONG(ancpos), MPFROMLONG(curpos));
363 MLEenable(h);
364#ifdef __DEBUG_ALLOC__
365 _heap_check();
366#endif
367 DosFreeMem(temp);
368 free(sel);
369 DosPostEventSem(CompactSem);
370 return TRUE;
371}
372
373BOOL MLEquotepara(HWND h, CHAR * initials, BOOL fQuoteOld)
374{
375 LONG num;
376 CHAR lineend[2], line[8], *p;
377
378 if (!initials || !*initials)
379 initials = " > ";
380 num = MLEcurline(h);
381 while (MLElinelen(h, num) < 3L && MLEnumlines(h) >= num)
382 num++;
383 while (MLElinelen(h, num) > 2L && MLEnumlines(h) >= num) {
384 memset(line, 0, 8);
385 MLEgetlinetext(h, num, line, 7L);
386 line[7] = 0;
387 if ((p = strchr(line, '>')) == NULL) {
388 MLEsetcurpos(h, MLEstartofline(h, num));
389 MLEinsert(h, initials);
390 MLEsetcurpos(h, (MLEstartofline(h, num) + MLElinelen(h, num)) - 1L);
391 MLEtextatcursor(h, lineend, 2L);
392 if (*lineend != '\r' && *lineend != '\n')
393 MLEinsert(h, "\n");
394 }
395 else if (fQuoteOld) {
396 while (isspace(line[strlen(line) - 1]))
397 line[strlen(line) - 1] = 0;
398 MLEsetcurpos(h, MLEstartofline(h, num) + (p - line));
399 MLEinsert(h, ">");
400 }
401 num++;
402 }
403 MLEsetcurpos(h, MLEstartofline(h, num));
404 return TRUE;
405}
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(1);
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(1);
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(1);
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(1);
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
768BOOL MLEloadfile(HWND h, CHAR * filename)
769{
770 /* load a file into the MLE, getting rid of whatever was already
771 * there. Note this returns without erasing existing text if the
772 * file to load does not exist
773 */
774
775 FILESTATUS3 fsa;
776 BOOL ret;
777
778 if (!DosQueryPathInfo(filename, FIL_STANDARD, &fsa, (ULONG) sizeof(fsa)) &&
779 !(fsa.attrFile & FILE_DIRECTORY)) {
780 MLEclearall(h);
781 ret = MLEinsertfile(h, filename);
782 MLEsetchanged(h, FALSE);
783 return ret;
784 }
785 return FALSE;
786}
787
788BOOL MLEexportfile(HWND h, CHAR * filename, INT tabspaces,
789 BOOL striptraillines, BOOL striptrailspaces)
790{
791 /* save the MLE contents as a file. Format the output so that
792 * the file is CR/LF terminated as presented in the MLE.
793 */
794
795 FILE *fp = NULL;
796 CHAR *buffer = NULL;
797 CHAR *p;
798 BOOL ok = TRUE;
799 INT blanklines = 0;
800 BOOL fWrap = MLEgetwrap(h);
801 APIRET rc;
802
803 // saymsg(MB_ENTER,h,DEBUG_STRING,"len = %ld",MLEgetlen(h));
804 if (!MLEgetlen(h)) /* nothing to save; forget it */
805 return TRUE;
806
807 MLEsetwrap(h, FALSE); // Need wrap off to export MLFIE_NOTRANS
808
809 if (striptraillines) {
810
811 register LONG x;
812 LONG numlines;
813
814 numlines = MLEnumlines(h);
815 for (x = numlines; x; x--) {
816 if (MLElinelen(h, x - 1L) < 2)
817 MLEdeleteline(h, x - 1L);
818 else
819 break;
820 }
821 if (!MLEgetlen(h)) {
822 /* nothing to save; forget it */
823 MLEsetwrap(h, fWrap); // Restore
824 return TRUE;
825 }
826 }
827
828 rc = DosAllocMem((PVOID) & buffer, 4096L,
829 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
830 if (rc || !buffer) {
831 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
832 GetPString(IDS_OUTOFMEMORY));
833 ok = FALSE;
834 }
835 else {
836 fp = fopen(filename, "a+");
837 if (!fp)
838 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
839 if (!fp)
840 ok = FALSE;
841 else {
842 LONG numlines, ret, len, wl, temp;
843 IPT s;
844
845 fseek(fp, 0L, SEEK_END);
846 numlines = MLEnumlines(h);
847
848 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(4095L));
849 for (temp = 0; temp < numlines; temp++) {
850 s = MLEstartofline(h, temp);
851 wl = len = (LONG) MLElinelenleft(h, s);
852 ret = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&s), MPFROMP(&len));
853 if (ret < 0)
854 break;
855 wl = min(wl, ret);
856 buffer[wl] = 0;
857 if (*buffer) {
858 p = buffer + strlen(buffer) - 1;
859 while (p >= buffer && (*p == '\n' || *p == '\r')) {
860 *p = 0;
861 p--;
862 }
863 }
864 if (tabspaces) {
865 p = buffer;
866 while (*p) {
867 if (*p == '\t') {
868 *p = ' ';
869 memmove((p + tabspaces) - 1, p, strlen(p) + 1);
870 memset(p, ' ', tabspaces);
871 }
872 p++;
873 }
874 }
875 if (striptrailspaces && *buffer) {
876 p = buffer + strlen(buffer) - 1;
877 while (p >= buffer && (*p == ' ' || *p == '\t')) {
878 *p = 0;
879 p--;
880 }
881 }
882 if (striptraillines) {
883 if (!*buffer) {
884 blanklines++;
885 continue;
886 }
887 else {
888 while (blanklines) {
889 fwrite("\n", 1, 1, fp);
890 blanklines--;
891 }
892 }
893 }
894 strcat(buffer, "\n");
895 // buffer = translate_out(buffer,4095,h,filename);
896 if (fwrite(buffer, 1, strlen(buffer), fp) < 1) {
897 saymsg(MB_ENTER,
898 h, GetPString(IDS_ARGHTEXT), GetPString(IDS_WRITEERRORTEXT));
899 break;
900 }
901 } // for lines
902 }
903 }
904
905 MLEsetwrap(h, fWrap); // Restore
906
907 if (fp)
908 fclose(fp);
909 if (buffer)
910 DosFreeMem(buffer);
911
912 return ok;
913}
914
915MRESULT EXPENTRY SandRDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
916{
917 /* initiate search(/replace)s in edit mode */
918
919 SRCHPTR *vw;
920
921 if (msg != WM_INITDLG)
922 vw = (SRCHPTR *) WinQueryWindowPtr(hwnd, QWL_USER);
923 else
924 vw = NULL;
925
926 switch (msg) {
927 case WM_INITDLG:
928 vw = (SRCHPTR *) mp2;
929 if (!vw) {
930 WinDismissDlg(hwnd, 0);
931 break;
932 }
933 WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mp2);
934 WinSendDlgItemMsg(hwnd, SRCH_SEARCH, EM_SETTEXTLIMIT,
935 MPFROM2SHORT(256, 0), MPVOID);
936 WinSendDlgItemMsg(hwnd, SRCH_REPLACE, EM_SETTEXTLIMIT,
937 MPFROM2SHORT(256, 0), MPVOID);
938 if (*vw->search)
939 WinSetDlgItemText(hwnd, SRCH_SEARCH, vw->search);
940 if (!MLEgetreadonly(vw->hwndmle)) {
941 if (*vw->replace)
942 WinSetDlgItemText(hwnd, SRCH_REPLACE, vw->replace);
943 WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_SETCHECK,
944 MPFROM2SHORT(vw->sandr, 0), MPVOID);
945 WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_SETCHECK,
946 MPFROM2SHORT(vw->rall, 0), MPVOID);
947 }
948 else {
949 WinEnableWindow(WinWindowFromID(hwnd, SRCH_SANDR), FALSE);
950 WinEnableWindow(WinWindowFromID(hwnd, SRCH_RALL), FALSE);
951 WinEnableWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
952 WinShowWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
953 *vw->replace = 0;
954 vw->sandr = FALSE;
955 vw->rall = FALSE;
956 }
957 memset(&vw->se, 0, sizeof(MLE_SEARCHDATA));
958 vw->se.cb = sizeof(MLE_SEARCHDATA);
959 vw->se.pchFind = (PCHAR) vw->search;
960 vw->se.cchFind = (SHORT) strlen(vw->search);
961 vw->se.pchReplace = (PCHAR) vw->replace;
962 vw->se.cchReplace = (SHORT) strlen(vw->replace);
963 vw->se.iptStart = MLEcurpos(vw->hwndmle);
964 vw->se.iptStop = -1L;
965 vw->se.cchFound = 0;
966 PosOverOkay(hwnd);
967 break;
968
969 case WM_CONTROL:
970 return 0;
971
972 case WM_COMMAND:
973 switch (SHORT1FROMMP(mp1)) {
974 case IDM_HELP:
975 saymsg(MB_ENTER | MB_ICONASTERISK,
976 hwnd,
977 NullStr,
978 GetPString(IDS_ENTERSEARCHSTRINGTEXT),
979 (MLEgetreadonly(vw->hwndmle)) ?
980 "." : GetPString(IDS_REPLACESTRINGTEXT));
981 break;
982
983 case DID_CANCEL:
984 WinDismissDlg(hwnd, 0);
985 break;
986
987 case DID_OK:
988 WinShowWindow(hwnd, FALSE);
989 {
990 CHAR temp[257];
991 APIRET ret;
992
993 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_QUERYCHECK,
994 MPVOID, MPVOID))
995 vw->sandr = TRUE;
996 else
997 vw->sandr = FALSE;
998 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_QUERYCHECK,
999 MPVOID, MPVOID))
1000 vw->rall = TRUE;
1001 else
1002 vw->rall = FALSE;
1003 *vw->replace = 0;
1004 WinQueryDlgItemText(hwnd, SRCH_REPLACE, 256, vw->replace);
1005 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1006 WinQueryDlgItemText(hwnd, SRCH_SEARCH, 256, temp);
1007 if (*temp) {
1008 strcpy(vw->search, temp);
1009 vw->se.cchFind = (SHORT) strlen(vw->search);
1010 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1011 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1012 (MLFSEARCH_CASESENSITIVE *
1013 (vw->fInsensitive ==
1014 FALSE)) | (MLFSEARCH_CHANGEALL *
1015 (vw->rall != 0))),
1016 MPFROMP(&vw->se))) {
1017 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1018 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1019 WinDismissDlg(hwnd, 0);
1020 break;
1021 }
1022 else if (vw->sandr && !vw->rall) {
1023 ret = saymsg(MB_YESNOCANCEL,
1024 hwnd,
1025 NullStr,
1026 GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1027 if (ret == MBID_YES)
1028 MLEinsert(vw->hwndmle, vw->replace);
1029 else if (ret == MBID_CANCEL) {
1030 WinDismissDlg(hwnd, 0);
1031 break;
1032 }
1033 WinDismissDlg(hwnd, 1);
1034 break;
1035 }
1036 }
1037 WinDismissDlg(hwnd, 0);
1038 }
1039 break;
1040 }
1041 return 0;
1042
1043 case WM_CLOSE:
1044 break;
1045 }
1046
1047 return WinDefDlgProc(hwnd, msg, mp1, mp2);
1048}
1049
1050BOOL MLEfindfirst(HWND hwnd, SRCHPTR * vw)
1051{
1052 if (MLEsizeofsel(vw->hwndmle) < 256L) {
1053 MLEgetseltext(vw->hwndmle, vw->search);
1054 vw->search[255] = 0;
1055 }
1056 if (WinDlgBox(HWND_DESKTOP, hwnd, SandRDlgProc, FM3ModHandle,
1057 SRCH_FRAME, MPFROMP(vw)) && *vw->search)
1058 return TRUE;
1059 return FALSE;
1060}
1061
1062INT MLEfindnext(HWND hwnd, SRCHPTR * vw)
1063{
1064 if (!*vw->search)
1065 return -1;
1066 else {
1067 vw->se.iptStart++;
1068 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1069 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1070 (MLFSEARCH_CASESENSITIVE *
1071 (vw->fInsensitive ==
1072 FALSE)) | (MLFSEARCH_CHANGEALL *
1073 (vw->rall))), MPFROMP(&vw->se)))
1074 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1075 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1076 else if (vw->sandr && !vw->rall) {
1077
1078 APIRET ret;
1079
1080 ret = saymsg(MB_YESNOCANCEL,
1081 hwnd,
1082 NullStr, GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1083 if (ret == MBID_YES)
1084 MLEinsert(vw->hwndmle, vw->replace);
1085 if (ret != MBID_CANCEL)
1086 return 1;
1087 }
1088 }
1089 return 0;
1090}
1091
1092#pragma alloc_text(FMMLE,MLEgetlinetext,MLEdeleteline,MLEdeletecurline,MLEdeletetoeol)
1093#pragma alloc_text(FMMLE,MLEclearall,MLEtextatcursor,MLEtextatpos,MLEsizeofsel)
1094#pragma alloc_text(FMMLE3,MLEdoblock,MLEquotepara,MLEinternet)
1095#pragma alloc_text(FMMLE4,MLEAutoLoad,MLEHexLoad,MLEinsertfile,LoadThread,MLEbackgroundload)
1096#pragma alloc_text(FMMLE5,MLEloadfile,MLEexportfile)
1097#pragma alloc_text(FMMLE3,MLEfindfirst,MLEfindnext,SandRDlgProc)
Note: See TracBrowser for help on using the repository browser.