source: trunk/dll/mle.c@ 775

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

Minor clean up add comments re recent changes

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