source: trunk/dll/mle.c@ 551

Last change on this file since 551 was 551, checked in by Gregg Young, 19 years ago

Indentation cleanup

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