source: trunk/dll/mle.c@ 123

Last change on this file since 123 was 123, checked in by root, 21 years ago

Rework lstrip/rstrip usage

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