source: trunk/dll/mle.c@ 1161

Last change on this file since 1161 was 1161, checked in by John Small, 17 years ago

Ticket 187: Draft 1: Functions only

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