source: trunk/dll/mle.c@ 578

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

Rot13 update to fix compiler warning

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