source: trunk/dll/mle.c@ 771

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

Increase subject to 1024 reduce DosSleep times

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