source: trunk/dll/mle.c@ 1844

Last change on this file since 1844 was 1673, checked in by Gregg Young, 13 years ago

Update to Doxygen comment style Ticket 55. Also some minor code cleanup.

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