source: trunk/dll/mle.c@ 814

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

DosSleep(1) in loops changed to (0) to enhance performance

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