source: trunk/dll/mle.c@ 888

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

runemf2 now quotes executable strings if needed (Ticket 180); it also reports where it was called from on errors

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.4 KB
RevLine 
[2]1
[123]2/***********************************************************************
3
4 $Id: mle.c 888 2007-12-22 22:02:11Z 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)
[888]19 17 Dec 07 GKY Make WPURLDEFAULTSETTINGS the fall back for ftp/httprun
[123]20
21***********************************************************************/
22
[2]23#define INCL_DOS
24#define INCL_WIN
[841]25#define INCL_LONGLONG
[350]26#include <os2.h>
[2]27
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <ctype.h>
32#include <share.h>
[689]33#include <process.h> // _beginthread
[350]34
[2]35#include "fm3dll.h"
36#include "fm3dlg.h"
37#include "mle.h"
38#include "fm3str.h"
39
[350]40static PSZ pszSrcFile = __FILE__;
41
[2]42#define FAKEROT 1
[578]43#define DOROT13(c) (!isalpha((c)))?(c):((((c) >= (char) 'A') && \
[2]44 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
45 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
[578]46 ((c) - (char) 0xd):(c)
47
48/*((FAKEROT==0)?(c):(FAKEROT==1)?(!isalpha((c)))?(c):((((c) >= (char) 'A') && \
49 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
50 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
[2]51 ((c) - (char) 0xd):(c):((c) >= (char) '!') ? ((((c) + (char) 47) > (char) '~') ? ((c) - (char) 47) :\
[578]52 ((c) + (char) 47)) : (c))*/
[2]53
[551]54LONG MLEgetlinetext(HWND h, LONG l, CHAR * buf, INT maxlen)
[301]55{
[2]56 /* get text of line l from MLE */
57
[551]58 IPT s, e;
[2]59
[551]60 s = MLEstartofline(h, l);
61 e = MLElinelenleft(h, s);
62 return MLEtextatpos(h, s, buf, min((INT) e, maxlen));
[2]63}
64
[551]65LONG MLEdeleteline(HWND h, LONG l)
[301]66{
[2]67 /* delete line l from MLE */
68
[551]69 IPT s, e;
[2]70
[551]71 s = MLEstartofline(h, l);
72 e = MLElinelenleft(h, s);
73 return MLEdelete(h, s, e);
[2]74}
75
[551]76LONG MLEdeletecurline(HWND h)
[301]77{
[2]78 /* delete current line from MLE */
79
80 LONG l;
81
82 l = MLEcurline(h);
[551]83 return MLEdeleteline(h, l);
[2]84}
85
[551]86LONG MLEdeletetoeol(HWND h)
[301]87{
[2]88 /* delete from cursor pos to end of line */
89
[551]90 IPT s, e;
[2]91
92 s = MLEcurpos(h);
93 e = MLEcurlenleft(h);
[551]94 return MLEdelete(h, s, e);
[2]95}
96
[551]97VOID MLEclearall(HWND h)
[301]98{
[2]99 /* remove all text from MLE */
100 LONG len;
101
102 len = MLEgetlen(h);
[551]103 if (len)
104 MLEdelete(h, 0, len);
[2]105}
106
[551]107LONG MLEtextatcursor(HWND h, CHAR * buffer, INT buflen)
[301]108{
[2]109 /* place up to buflen chars of text from cursor pos into buffer
110 * return # of chars imported
111 */
112
[551]113 IPT i;
[2]114
115 i = MLEcurpos(h);
[551]116 return MLEtextatpos(h, i, buffer, buflen);
[2]117}
118
[551]119LONG MLEtextatpos(HWND h, IPT i, CHAR * buffer, INT buflen)
[301]120{
[2]121 /* place up to buflen chars of text from pos i in buffer
122 * return # of chars imported
123 */
124
[551]125 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer),
126 MPFROMLONG((LONG) buflen));
127 return (LONG) WinSendMsg(h, MLM_EXPORT,
128 MPFROMP(&i), MPFROMLONG((PLONG) & buflen));
[2]129}
130
[551]131LONG MLEsizeofsel(HWND h)
[301]132{
[2]133 /* return length of selected text */
134
[551]135 IPT cursor, anchor, test;
[2]136
137 cursor = MLEcurpos(h);
138 anchor = MLEancpos(h);
[551]139 test = min(cursor, anchor);
[2]140 /* MLE fakes us out; get real length in bytes */
[551]141 return (LONG) WinSendMsg(h, MLM_QUERYFORMATTEXTLENGTH,
142 MPFROMLONG(test),
143 MPFROMLONG((LONG) ((cursor < anchor) ?
144 (anchor - cursor) :
145 (cursor - anchor))));
[2]146}
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,
[888]181 h, pszSrcFile, __LINE__,
182 ftprund, NULL, "%s %s", ftprun, temp);
[551]183 else if (!ftp && *httprun)
184 runemf2(SEPARATE | WINDOWED,
[888]185 h, pszSrcFile, __LINE__,
186 httprund, NULL, "%s %s", httprun, temp);
[551]187 }
[2]188 }
189 DosFreeMem(temp);
190 }
191 }
192}
193
[551]194BOOL MLEdoblock(HWND h, INT action, CHAR * filename)
[301]195{
[2]196 /* perform action on text in selection */
197
198 register CHAR *p;
[551]199 CHAR *sel, *temp = NULL;
200 IPT ancpos, curpos, here;
201 LONG sellen, oldlen;
[350]202 APIRET rc;
[2]203
204 oldlen = MLEsizeofsel(h);
[551]205 if (!oldlen)
[2]206 return TRUE;
[551]207 sel = xmallocz((size_t) (oldlen + 2), pszSrcFile, __LINE__);
[350]208 if (!sel)
[2]209 return FALSE;
[551]210 rc = DosAllocMem((PVOID) & temp, 32768L,
211 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]212 if (rc || !temp) {
[551]213 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
214 GetPString(IDS_OUTOFMEMORY));
[2]215 free(sel);
216 DosPostEventSem(CompactSem);
217 return FALSE;
218 }
219
220 ancpos = MLEancpos(h);
221 curpos = MLEcurpos(h);
[551]222 here = min(curpos, ancpos);
[2]223 p = sel;
224 MLEdisable(h);
[551]225 while (oldlen > 0) {
226 sellen = min(oldlen + 1, 32701);
227 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(sellen));
228 sellen =
229 (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&here), MPFROMP(&sellen));
[350]230 if (sellen < 1) {
231 Runtime_Error(pszSrcFile, __LINE__, "len < 1");
[2]232 free(sel);
233 DosPostEventSem(CompactSem);
234 return FALSE;
235 }
[551]236 if (sellen > min(oldlen, 32700))
[2]237 sellen--;
[551]238 memcpy(p, temp, sellen);
[2]239 p += sellen;
240 oldlen -= sellen;
241 }
[551]242 switch (action) {
243 case APPENDCLIP:
244 SaveToClip(h, sel, TRUE);
245 DosFreeMem(temp);
246 free(sel);
247 MLEenable(h);
248 DosPostEventSem(CompactSem);
249 return TRUE;
250
251 case WRITE:
252 {
253 FILE *fp;
254
255 fp = fopen(filename, "a+");
256 if (!fp)
257 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
258 if (fp) {
259 fseek(fp, 0L, SEEK_END);
260 fwrite(sel, 1, strlen(sel), fp);
261 fclose(fp);
262 }
263#ifdef __DEBUG_ALLOC__
264 _heap_check();
265#endif
[2]266 DosFreeMem(temp);
267 free(sel);
268 MLEenable(h);
269 DosPostEventSem(CompactSem);
270 return TRUE;
[551]271 }
[2]272
[551]273 case UPPERCASE:
274 p = sel;
275 while (*p) {
276 if (isalpha(*p))
277 *p = toupper(*p);
278 p++;
279 }
280 break;
[2]281
[551]282 case LOWERCASE:
283 p = sel;
284 while (*p) {
285 if (isalpha(*p))
286 *p = tolower(*p);
287 p++;
288 }
289 break;
[2]290
[551]291 case TOGGLECASE:
292 p = sel;
293 while (*p) {
294 if (isalpha(*p)) {
295 if (islower(*p))
296 *p = toupper(*p);
297 else
298 *p = tolower(*p);
[2]299 }
[551]300 p++;
301 }
302 break;
[2]303
[551]304 case ROT13:
305 p = sel;
306 while (*p) {
307 *p = DOROT13(*p); // fixme condition both true and false?
308 p++;
309 }
310 break;
[2]311
[551]312 case XOR:
313 p = sel;
314 while (*p) {
315 *p = (~*p);
316 p++;
317 }
318 break;
[2]319
[551]320 case FORMAT:
321 p = sel;
322 while (*p) {
323 if (*p == '\r') {
324 memmove(p, p + 1, strlen(p));
325 continue;
[2]326 }
[551]327 if (*p == '\n') {
328 *p = ' ';
329 continue;
[2]330 }
[551]331 p++;
332 }
333 break;
[2]334
[551]335 default: /* unknown action */
[2]336#ifdef __DEBUG_ALLOC__
337 _heap_check();
338#endif
[551]339 DosFreeMem(temp);
340 free(sel);
341 DosPostEventSem(CompactSem);
342 MLEenable(h);
343 return FALSE;
[2]344 }
345
346 /* replace selection with altered text */
347 p = sel;
[551]348 here = min(curpos, ancpos);
349 MLEclear(h); /* delete current selection */
350 sellen = oldlen = strlen(sel); /* actual number of bytes */
351 while (oldlen > 0) {
352 sellen = min(oldlen, 32700);
353 memcpy(temp, p, sellen);
354 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(sellen));
355 sellen = (LONG) WinSendMsg(h,
356 MLM_IMPORT,
357 MPFROMP(&here), MPFROMLONG(sellen));
[350]358 if (!sellen) {
359 Runtime_Error(pszSrcFile, __LINE__, "sellen 0");
[2]360 break;
361 }
362 p += sellen;
363 oldlen -= sellen;
[551]364 if (oldlen && *p == '\n' /* && *(p - 1) == '\r' */ )
[2]365 p--;
[551]366 } // while
367 WinSendMsg(h, MLM_SETSEL, MPFROMLONG(ancpos), MPFROMLONG(curpos));
[2]368 MLEenable(h);
369#ifdef __DEBUG_ALLOC__
[551]370 _heap_check();
[2]371#endif
372 DosFreeMem(temp);
373 free(sel);
374 DosPostEventSem(CompactSem);
375 return TRUE;
376}
377
[551]378BOOL MLEquotepara(HWND h, CHAR * initials, BOOL fQuoteOld)
[301]379{
[2]380 LONG num;
[551]381 CHAR lineend[2], line[8], *p;
[2]382
[551]383 if (!initials || !*initials)
[2]384 initials = " > ";
385 num = MLEcurline(h);
[551]386 while (MLElinelen(h, num) < 3L && MLEnumlines(h) >= num)
[2]387 num++;
[551]388 while (MLElinelen(h, num) > 2L && MLEnumlines(h) >= num) {
389 memset(line, 0, 8);
390 MLEgetlinetext(h, num, line, 7L);
[2]391 line[7] = 0;
[551]392 if ((p = strchr(line, '>')) == NULL) {
393 MLEsetcurpos(h, MLEstartofline(h, num));
394 MLEinsert(h, initials);
395 MLEsetcurpos(h, (MLEstartofline(h, num) + MLElinelen(h, num)) - 1L);
396 MLEtextatcursor(h, lineend, 2L);
397 if (*lineend != '\r' && *lineend != '\n')
398 MLEinsert(h, "\n");
[2]399 }
[551]400 else if (fQuoteOld) {
401 while (isspace(line[strlen(line) - 1]))
402 line[strlen(line) - 1] = 0;
403 MLEsetcurpos(h, MLEstartofline(h, num) + (p - line));
404 MLEinsert(h, ">");
[2]405 }
406 num++;
407 }
[551]408 MLEsetcurpos(h, MLEstartofline(h, num));
[2]409 return TRUE;
410}
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;
[841]435 IPT iptOffset = -1;
436 ULONG numread, howmuch, numimport, action, len, left = 0;
[551]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);
[844]452 rc = DosOpen(filename, &handle, &action, 0, 0,
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, 0);
[350]457 if (rc) {
458 ret = FALSE;
459 }
460 else {
[841]461 DosChgFilePtr(handle, 0, FILE_END, &len);
462 DosChgFilePtr(handle, 0, FILE_BEGIN, &action);
[350]463 if (len) {
[841]464 rc = DosAllocMem((PVOID) & hexbuff, 50001,
[551]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);
[814]510 DosSleep(0); //26 Aug 07 GKY 1
[551]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 }
[771]535 DosSleep(1);
[551]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 }
[814]666 DosSleep(0); //26 Aug 07 GKY 1
[551]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 }
[814]677 DosSleep(0); //26 Aug 07 GKY 1
[551]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
[551]773BOOL MLEloadfile(HWND h, CHAR * filename)
[301]774{
[2]775 /* load a file into the MLE, getting rid of whatever was already
776 * there. Note this returns without erasing existing text if the
777 * file to load does not exist
778 */
779
[843]780 FILESTATUS3 fsa;
[551]781 BOOL ret;
[2]782
[843]783 if (!DosQueryPathInfo(filename, FIL_STANDARD, &fsa, sizeof(fsa)) &&
784 ~fsa.attrFile & FILE_DIRECTORY) {
[2]785 MLEclearall(h);
[551]786 ret = MLEinsertfile(h, filename);
787 MLEsetchanged(h, FALSE);
[2]788 return ret;
789 }
790 return FALSE;
791}
792
[551]793BOOL MLEexportfile(HWND h, CHAR * filename, INT tabspaces,
794 BOOL striptraillines, BOOL striptrailspaces)
[301]795{
[2]796 /* save the MLE contents as a file. Format the output so that
797 * the file is CR/LF terminated as presented in the MLE.
798 */
799
[301]800 FILE *fp = NULL;
801 CHAR *buffer = NULL;
802 CHAR *p;
803 BOOL ok = TRUE;
[551]804 INT blanklines = 0;
[301]805 BOOL fWrap = MLEgetwrap(h);
[350]806 APIRET rc;
[2]807
[301]808 // saymsg(MB_ENTER,h,DEBUG_STRING,"len = %ld",MLEgetlen(h));
[551]809 if (!MLEgetlen(h)) /* nothing to save; forget it */
[2]810 return TRUE;
811
[551]812 MLEsetwrap(h, FALSE); // Need wrap off to export MLFIE_NOTRANS
[301]813
[551]814 if (striptraillines) {
[2]815
816 register LONG x;
[551]817 LONG numlines;
[2]818
819 numlines = MLEnumlines(h);
[551]820 for (x = numlines; x; x--) {
821 if (MLElinelen(h, x - 1L) < 2)
822 MLEdeleteline(h, x - 1L);
[2]823 else
[551]824 break;
[2]825 }
[551]826 if (!MLEgetlen(h)) {
[301]827 /* nothing to save; forget it */
828 MLEsetwrap(h, fWrap); // Restore
[2]829 return TRUE;
[301]830 }
[2]831 }
832
[551]833 rc = DosAllocMem((PVOID) & buffer, 4096L,
834 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]835 if (rc || !buffer) {
[551]836 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
837 GetPString(IDS_OUTOFMEMORY));
[350]838 ok = FALSE;
839 }
[551]840 else {
841 fp = fopen(filename, "a+");
[350]842 if (!fp)
[551]843 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
[350]844 if (!fp)
845 ok = FALSE;
846 else {
[551]847 LONG numlines, ret, len, wl, temp;
848 IPT s;
[2]849
[551]850 fseek(fp, 0L, SEEK_END);
[2]851 numlines = MLEnumlines(h);
852
[551]853 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(4095L));
854 for (temp = 0; temp < numlines; temp++) {
855 s = MLEstartofline(h, temp);
856 wl = len = (LONG) MLElinelenleft(h, s);
857 ret = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&s), MPFROMP(&len));
858 if (ret < 0)
859 break;
860 wl = min(wl, ret);
861 buffer[wl] = 0;
862 if (*buffer) {
863 p = buffer + strlen(buffer) - 1;
864 while (p >= buffer && (*p == '\n' || *p == '\r')) {
865 *p = 0;
866 p--;
867 }
868 }
869 if (tabspaces) {
870 p = buffer;
871 while (*p) {
872 if (*p == '\t') {
873 *p = ' ';
874 memmove((p + tabspaces) - 1, p, strlen(p) + 1);
875 memset(p, ' ', tabspaces);
876 }
877 p++;
878 }
879 }
880 if (striptrailspaces && *buffer) {
881 p = buffer + strlen(buffer) - 1;
882 while (p >= buffer && (*p == ' ' || *p == '\t')) {
883 *p = 0;
884 p--;
885 }
886 }
887 if (striptraillines) {
888 if (!*buffer) {
889 blanklines++;
890 continue;
891 }
892 else {
893 while (blanklines) {
894 fwrite("\n", 1, 1, fp);
895 blanklines--;
896 }
897 }
898 }
899 strcat(buffer, "\n");
900 // buffer = translate_out(buffer,4095,h,filename);
901 if (fwrite(buffer, 1, strlen(buffer), fp) < 1) {
902 saymsg(MB_ENTER,
903 h, GetPString(IDS_ARGHTEXT), GetPString(IDS_WRITEERRORTEXT));
904 break;
905 }
906 } // for lines
[2]907 }
908 }
[301]909
[551]910 MLEsetwrap(h, fWrap); // Restore
[301]911
912 if (fp)
913 fclose(fp);
914 if (buffer)
915 DosFreeMem(buffer);
916
917 return ok;
[2]918}
919
[551]920MRESULT EXPENTRY SandRDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[301]921{
[2]922 /* initiate search(/replace)s in edit mode */
923
924 SRCHPTR *vw;
925
[551]926 if (msg != WM_INITDLG)
[574]927 vw = (SRCHPTR *) WinQueryWindowPtr(hwnd, QWL_USER);
[2]928 else
929 vw = NULL;
930
[551]931 switch (msg) {
932 case WM_INITDLG:
933 vw = (SRCHPTR *) mp2;
934 if (!vw) {
935 WinDismissDlg(hwnd, 0);
[2]936 break;
[551]937 }
[574]938 WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mp2);
[551]939 WinSendDlgItemMsg(hwnd, SRCH_SEARCH, EM_SETTEXTLIMIT,
940 MPFROM2SHORT(256, 0), MPVOID);
941 WinSendDlgItemMsg(hwnd, SRCH_REPLACE, EM_SETTEXTLIMIT,
942 MPFROM2SHORT(256, 0), MPVOID);
943 if (*vw->search)
944 WinSetDlgItemText(hwnd, SRCH_SEARCH, vw->search);
945 if (!MLEgetreadonly(vw->hwndmle)) {
946 if (*vw->replace)
947 WinSetDlgItemText(hwnd, SRCH_REPLACE, vw->replace);
948 WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_SETCHECK,
949 MPFROM2SHORT(vw->sandr, 0), MPVOID);
950 WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_SETCHECK,
951 MPFROM2SHORT(vw->rall, 0), MPVOID);
952 }
953 else {
954 WinEnableWindow(WinWindowFromID(hwnd, SRCH_SANDR), FALSE);
955 WinEnableWindow(WinWindowFromID(hwnd, SRCH_RALL), FALSE);
956 WinEnableWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
957 WinShowWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
958 *vw->replace = 0;
959 vw->sandr = FALSE;
960 vw->rall = FALSE;
961 }
962 memset(&vw->se, 0, sizeof(MLE_SEARCHDATA));
963 vw->se.cb = sizeof(MLE_SEARCHDATA);
964 vw->se.pchFind = (PCHAR) vw->search;
965 vw->se.cchFind = (SHORT) strlen(vw->search);
966 vw->se.pchReplace = (PCHAR) vw->replace;
967 vw->se.cchReplace = (SHORT) strlen(vw->replace);
968 vw->se.iptStart = MLEcurpos(vw->hwndmle);
969 vw->se.iptStop = -1L;
970 vw->se.cchFound = 0;
971 PosOverOkay(hwnd);
972 break;
[2]973
[551]974 case WM_CONTROL:
975 return 0;
[2]976
[551]977 case WM_COMMAND:
978 switch (SHORT1FROMMP(mp1)) {
979 case IDM_HELP:
980 saymsg(MB_ENTER | MB_ICONASTERISK,
981 hwnd,
982 NullStr,
983 GetPString(IDS_ENTERSEARCHSTRINGTEXT),
984 (MLEgetreadonly(vw->hwndmle)) ?
985 "." : GetPString(IDS_REPLACESTRINGTEXT));
986 break;
[2]987
[551]988 case DID_CANCEL:
989 WinDismissDlg(hwnd, 0);
990 break;
[2]991
[551]992 case DID_OK:
993 WinShowWindow(hwnd, FALSE);
994 {
995 CHAR temp[257];
996 APIRET ret;
[2]997
[551]998 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_QUERYCHECK,
999 MPVOID, MPVOID))
1000 vw->sandr = TRUE;
1001 else
1002 vw->sandr = FALSE;
1003 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_QUERYCHECK,
1004 MPVOID, MPVOID))
1005 vw->rall = TRUE;
1006 else
1007 vw->rall = FALSE;
1008 *vw->replace = 0;
1009 WinQueryDlgItemText(hwnd, SRCH_REPLACE, 256, vw->replace);
1010 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1011 WinQueryDlgItemText(hwnd, SRCH_SEARCH, 256, temp);
1012 if (*temp) {
1013 strcpy(vw->search, temp);
1014 vw->se.cchFind = (SHORT) strlen(vw->search);
1015 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1016 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1017 (MLFSEARCH_CASESENSITIVE *
1018 (vw->fInsensitive ==
1019 FALSE)) | (MLFSEARCH_CHANGEALL *
1020 (vw->rall != 0))),
1021 MPFROMP(&vw->se))) {
1022 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1023 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1024 WinDismissDlg(hwnd, 0);
1025 break;
1026 }
1027 else if (vw->sandr && !vw->rall) {
1028 ret = saymsg(MB_YESNOCANCEL,
1029 hwnd,
1030 NullStr,
1031 GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1032 if (ret == MBID_YES)
1033 MLEinsert(vw->hwndmle, vw->replace);
1034 else if (ret == MBID_CANCEL) {
1035 WinDismissDlg(hwnd, 0);
1036 break;
1037 }
1038 WinDismissDlg(hwnd, 1);
1039 break;
1040 }
1041 }
1042 WinDismissDlg(hwnd, 0);
[2]1043 }
[551]1044 break;
1045 }
1046 return 0;
[2]1047
[551]1048 case WM_CLOSE:
1049 break;
[2]1050 }
1051
[551]1052 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]1053}
1054
[551]1055BOOL MLEfindfirst(HWND hwnd, SRCHPTR * vw)
[301]1056{
[551]1057 if (MLEsizeofsel(vw->hwndmle) < 256L) {
1058 MLEgetseltext(vw->hwndmle, vw->search);
[2]1059 vw->search[255] = 0;
1060 }
[551]1061 if (WinDlgBox(HWND_DESKTOP, hwnd, SandRDlgProc, FM3ModHandle,
1062 SRCH_FRAME, MPFROMP(vw)) && *vw->search)
[2]1063 return TRUE;
1064 return FALSE;
1065}
1066
[551]1067INT MLEfindnext(HWND hwnd, SRCHPTR * vw)
[301]1068{
[551]1069 if (!*vw->search)
[2]1070 return -1;
1071 else {
1072 vw->se.iptStart++;
[551]1073 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1074 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1075 (MLFSEARCH_CASESENSITIVE *
1076 (vw->fInsensitive ==
1077 FALSE)) | (MLFSEARCH_CHANGEALL *
1078 (vw->rall))), MPFROMP(&vw->se)))
1079 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1080 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1081 else if (vw->sandr && !vw->rall) {
[2]1082
1083 APIRET ret;
1084
1085 ret = saymsg(MB_YESNOCANCEL,
[551]1086 hwnd,
1087 NullStr, GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1088 if (ret == MBID_YES)
1089 MLEinsert(vw->hwndmle, vw->replace);
1090 if (ret != MBID_CANCEL)
1091 return 1;
[2]1092 }
1093 }
1094 return 0;
1095}
[793]1096
1097#pragma alloc_text(FMMLE,MLEgetlinetext,MLEdeleteline,MLEdeletecurline,MLEdeletetoeol)
1098#pragma alloc_text(FMMLE,MLEclearall,MLEtextatcursor,MLEtextatpos,MLEsizeofsel)
1099#pragma alloc_text(FMMLE3,MLEdoblock,MLEquotepara,MLEinternet)
1100#pragma alloc_text(FMMLE4,MLEAutoLoad,MLEHexLoad,MLEinsertfile,LoadThread,MLEbackgroundload)
1101#pragma alloc_text(FMMLE5,MLEloadfile,MLEexportfile)
1102#pragma alloc_text(FMMLE3,MLEfindfirst,MLEfindnext,SandRDlgProc)
Note: See TracBrowser for help on using the repository browser.