source: trunk/dll/mle.c@ 574

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

Use QWL_USER; Replace doesn't move the command and Okay on cmd dialog removed error on unchanged command

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