source: trunk/dll/mle.c@ 814

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

DosSleep(1) in loops changed to (0) to enhance performance

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