source: trunk/dll/mle.c@ 886

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

Make WPURLDEFAULTSETTINGS the fall back for ftprun Ticket 182

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