source: trunk/dll/mle.c@ 771

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

Increase subject to 1024 reduce DosSleep times

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