source: trunk/dll/mle.c@ 2

Last change on this file since 2 was 2, checked in by root, 23 years ago

Initial revision

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