source: trunk/dll/mle.c@ 551

Last change on this file since 551 was 551, checked in by Gregg Young, 19 years ago

Indentation cleanup

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