source: trunk/dll/mle.c@ 892

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

Dialog and menu changes for mailrun and use of WPURLDEFAULTSETTINGS for runftp and runhttp if not supplied by user (libpathstrict option added)

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