source: trunk/dll/mle.c@ 578

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

Rot13 update to fix compiler warning

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