source: trunk/dll/mle.c@ 1225

Last change on this file since 1225 was 1225, checked in by John Small, 17 years ago

Ticket 187: Moved typedef's and some #define's from fm3dll.h

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