source: trunk/dll/mle.c@ 775

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

Minor clean up add comments re recent changes

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