Changeset 350 for trunk/dll/mle.c


Ignore:
Timestamp:
Jul 26, 2006, 9:01:20 PM (19 years ago)
Author:
root
Message:

Use Runtime_Error

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dll/mle.c

    r301 r350  
    77
    88  Copyright (c) 1993-97 M. Kimes
    9   Copyright (c) 2004 Steven H.Levine
     9  Copyright (c) 2004, 2006 Steven H.Levine
    1010
    1111  01 Aug 04 SHL Rework lstrip/rstrip usage
    1212  16 Apr 06 SHL MLEexportfile: rework to avoid wrap problems
     13  14 Jul 06 SHL Use Runtime_Error
    1314
    1415***********************************************************************/
    15 
    16 /* MLE support functions */
    1716
    1817#define INCL_DOS
    1918#define INCL_WIN
    20 
    2119#include <os2.h>
     20
    2221#include <stdlib.h>
    2322#include <stdio.h>
     
    2524#include <ctype.h>
    2625#include <share.h>
     26
    2727#include "fm3dll.h"
    2828#include "fm3dlg.h"
    2929#include "mle.h"
    3030#include "fm3str.h"
     31
     32static PSZ pszSrcFile = __FILE__;
    3133
    3234#pragma alloc_text(FMMLE,MLEgetlinetext,MLEdeleteline,MLEdeletecurline,MLEdeletetoeol)
     
    152154  IPT   ancpos,curpos,here;
    153155  LONG  len,oldlen;
     156  APIRET rc;
    154157
    155158  len = MLEsizeofsel(h);
    156159  len = min(2048,len);
    157160  oldlen = len;
    158   if(len) {
     161  if (len) {
    159162    len++;
    160     if(!DosAllocMem((PVOID)&temp,4096,
    161                     PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE) && temp) {
     163    rc = DosAllocMem((PVOID)&temp,4096,
     164                     PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
     165    if (rc || !temp)
     166      Dos_Error(MB_CANCEL,rc,h,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
     167    else {
    162168      ancpos = MLEancpos(h);
    163169      curpos = MLEcurpos(h);
     
    165171      WinSendMsg(h,MLM_SETIMPORTEXPORT,MPFROMP(temp),MPFROMLONG(len));
    166172      len = (LONG)WinSendMsg(h,MLM_EXPORT,MPFROMP(&here),MPFROMP(&len));
    167       if(len > 1) {
     173      if (len <= 1)
     174        Runtime_Error(pszSrcFile, __LINE__, "len <= 1");
     175      else {
    168176        if(len > oldlen)
    169177          len--;
     
    189197        }
    190198      }
    191       else
    192         DosBeep(50,100);
    193199      DosFreeMem(temp);
    194200    }
     
    205211  IPT  ancpos,curpos,here;
    206212  LONG sellen,oldlen;
     213  APIRET rc;
    207214
    208215  oldlen = MLEsizeofsel(h);
    209216  if(!oldlen)
    210217    return TRUE;
    211   sel = malloc((INT)(oldlen + 2));
    212   if(!sel) {
    213     DosBeep(50,100);
     218  sel = xmallocz((size_t)(oldlen + 2),pszSrcFile,__LINE__);
     219  if (!sel)
    214220    return FALSE;
    215   }
    216   memset(sel,0,(INT)(oldlen + 2));
    217   if(DosAllocMem((PVOID)&temp,32768L,
    218                   PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE) ||
    219      !temp) {
    220     DosBeep(50,100);
     221  rc = DosAllocMem((PVOID)&temp,32768L,
     222                   PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
     223  if (rc || !temp) {
     224    Dos_Error(MB_CANCEL,rc,h,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
    221225    free(sel);
    222226    DosPostEventSem(CompactSem);
     
    233237    WinSendMsg(h,MLM_SETIMPORTEXPORT,MPFROMP(temp),MPFROMLONG(sellen));
    234238    sellen = (LONG)WinSendMsg(h,MLM_EXPORT,MPFROMP(&here),MPFROMP(&sellen));
    235     if(sellen < 1) {
    236       DosBeep(50,100);
     239    if (sellen < 1) {
     240      Runtime_Error(pszSrcFile, __LINE__, "len < 1");
    237241      free(sel);
    238242      DosPostEventSem(CompactSem);
     
    260264        fp = fopen(filename,"a+");
    261265        if(!fp)
    262           fp = fopen(filename,"w");
    263         if(fp) {
     266          fp = xfopen(filename,"w",pszSrcFile,__LINE__);
     267        if (fp) {
    264268          fseek(fp,0L,SEEK_END);
    265269          fwrite(sel,1,strlen(sel),fp);
     
    267271        }
    268272#ifdef __DEBUG_ALLOC__
    269     _heap_check();
     273        _heap_check();
    270274#endif
    271275        DosFreeMem(temp);
     
    365369                              MPFROMP(&here),
    366370                              MPFROMLONG(sellen));
    367     if(!sellen) {
    368       DosBeep(50,100);
     371    if (!sellen) {
     372      Runtime_Error(pszSrcFile, __LINE__, "sellen 0");
    369373      break;
    370374    }
     
    373377    if(oldlen && *p == '\n' /* && *(p - 1) == '\r' */)
    374378      p--;
    375   }
     379  } // while
    376380  WinSendMsg(h,MLM_SETSEL,MPFROMLONG(ancpos),MPFROMLONG(curpos));
    377381  MLEenable(h);
     
    454458  XMLEWNDPTR *vw;
    455459  HFILE       handle;
     460  APIRET      rc;
    456461
    457462  *titletext = 0;
     
    463468  *titletext = 0;
    464469  WinQueryWindowText(grandpa,512,titletext);
    465   if(!DosOpen(filename,&handle,&action,0L,0L,
    466               OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
    467               OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT |
    468               OPEN_FLAGS_SEQUENTIAL | OPEN_SHARE_DENYNONE |
    469               OPEN_ACCESS_READONLY,0L)) {
     470  rc = DosOpen(filename,&handle,&action,0L,0L,
     471               OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
     472               OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT |
     473               OPEN_FLAGS_SEQUENTIAL | OPEN_SHARE_DENYNONE |
     474               OPEN_ACCESS_READONLY,0L);
     475  if (rc) {
     476    ret = FALSE;
     477  }
     478  else {
    470479    DosChgFilePtr(handle, 0L, FILE_END, &len);
    471480    DosChgFilePtr(handle, 0L, FILE_BEGIN, &action);
    472     if(len) {
    473       if(!DosAllocMem((PVOID)&hexbuff,50001L,
    474                        PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE) &&
    475          hexbuff) {
    476         buffer = malloc(10000);
    477         if(buffer) {
     481    if (len) {
     482      rc = DosAllocMem((PVOID)&hexbuff,50001L,
     483                       PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
     484      if (rc || !hexbuff) {
     485        Dos_Error(MB_CANCEL,rc,h,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
     486        ret = FALSE;
     487      }
     488      else {
     489        buffer = xmalloc(10000,pszSrcFile,__LINE__);
     490        if (!buffer)
     491          ret = FALSE;
     492        else {
    478493          MLEclearall(h);
    479494          WinSendMsg(h,MLM_SETIMPORTEXPORT,MPFROMP(hexbuff),
     
    540555              }
    541556              sprintf(s,
    542                       GetPString(IDS_LOADING2TEXT),
     557                      GetPString(IDS_LOADINGMLETEXT),
    543558                      len);
    544559              WinSetWindowText(grandpa,s);
     
    550565          free(buffer);
    551566        }
    552         else {
    553           saymsg(MB_CANCEL | MB_ICONEXCLAMATION,
    554                  HWND_DESKTOP,
    555                  GetPString(IDS_ARGHTEXT),
    556                  GetPString(IDS_OUTOFMEMORY));
    557           ret = FALSE;
    558         }
    559567        DosFreeMem(hexbuff);
    560       }
    561       else {
    562         saymsg(MB_CANCEL | MB_ICONEXCLAMATION,
    563                HWND_DESKTOP,
    564                GetPString(IDS_ARGHTEXT),
    565                GetPString(IDS_OUTOFMEMORY));
    566         ret = FALSE;
    567568      }
    568569    }
     
    571572    DosClose(handle);
    572573  }
    573   else
    574     ret = FALSE;
    575574  if(!first) {
    576575    WinEnableWindowUpdate(h,TRUE);
     
    582581
    583582
     583//== MLEinsertfile() insert a file into the current position in the MLE ==
     584
    584585BOOL MLEinsertfile (HWND h,CHAR *filename)
    585586{
    586   /* insert a file into the current position in the MLE */
    587587
    588588  HAB   hab;
     
    609609                     titletext);
    610610  fp = _fsopen(filename,"r",SH_DENYNO);
    611   if(fp) {
     611  if (!fp)
     612    ret = FALSE;
     613  else {
    612614    setvbuf(fp,NULL,_IONBF,0);
    613615    fseek(fp,0L,SEEK_END);
     
    615617    fseek(fp,0L,SEEK_SET);
    616618    if(len && len != -1) {
    617       if(!DosAllocMem((PVOID)&buffer,
     619      rc = DosAllocMem((PVOID)&buffer,
    618620                       50000L,
    619                        PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE) &&
    620          buffer) {
     621                       PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
     622      if (rc || !buffer) {
     623        Dos_Error(MB_CANCEL,rc,h,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
     624        ret = FALSE;
     625      }
     626      else {
    621627        WinSendMsg(h,
    622628                   MLM_SETIMPORTEXPORT,
     
    716722            }
    717723            sprintf(s,
    718                     GetPString(IDS_LOADING2TEXT),
     724                    GetPString(IDS_LOADINGMLETEXT),
    719725                    len);
    720726            WinSetWindowText(grandpa,s);
     
    724730        DosFreeMem(buffer);
    725731      }
    726       else {
    727         saymsg(MB_CANCEL | MB_ICONEXCLAMATION,
    728                HWND_DESKTOP,
    729                GetPString(IDS_ARGHTEXT),
    730                GetPString(IDS_OUTOFMEMORY));
    731         ret = FALSE;
    732       }
    733732    }
    734733    if(WinIsWindow(hab,h))
     
    736735    fclose(fp);
    737736  }
    738   else
    739     ret = FALSE;
    740737  if(!first) {
    741738    WinEnableWindowUpdate(h,TRUE);
     
    798795
    799796INT MLEbackgroundload (HWND hwndReport,ULONG msg,HWND h,CHAR *filename,
    800                        INT hex) {
    801 
    802   /* load a file into the MLE in the background (via a separate thread) */
     797                       INT hex)
     798{
     799  /* load a file into the MLE in the background (via a separate thread)
     800   * return _beginthread status
     801   */
    803802
    804803  BKGLOAD *bkg;
    805 
    806   bkg = malloc(sizeof(BKGLOAD));
    807   if(bkg) {
    808     memset(bkg,0,sizeof(BKGLOAD));
    809     bkg->size = sizeof(BKGLOAD);
    810     bkg->hex = (USHORT)hex;
    811     bkg->hwndReport = hwndReport;
    812     bkg->msg = msg;
    813     bkg->h = h;
    814     strcpy(bkg->filename,filename);
    815     return _beginthread(LoadThread,NULL,65536,bkg);
    816   }
    817   return -1;
     804  INT rc;
     805
     806  bkg = xmallocz(sizeof(BKGLOAD),pszSrcFile,__LINE__);
     807  if (!bkg)
     808    return -1;
     809  bkg->size = sizeof(BKGLOAD);
     810  bkg->hex = (USHORT)hex;
     811  bkg->hwndReport = hwndReport;
     812  bkg->msg = msg;
     813  bkg->h = h;
     814  strcpy(bkg->filename,filename);
     815  rc = _beginthread(LoadThread,NULL,65536,bkg);
     816  if (rc == -1)
     817    Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_COULDNTSTARTTHREADTEXT));
     818  return rc;
    818819}
    819820
     
    854855  INT  blanklines = 0;
    855856  BOOL fWrap = MLEgetwrap(h);
     857  APIRET rc;
    856858
    857859  // saymsg(MB_ENTER,h,DEBUG_STRING,"len = %ld",MLEgetlen(h));
     
    881883  }
    882884
    883   if(!DosAllocMem((PVOID)&buffer,4096L,
    884                    PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE) &&
    885      buffer) {
     885  rc = DosAllocMem((PVOID)&buffer,4096L,
     886                   PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
     887  if (rc || !buffer) {
     888    Dos_Error(MB_CANCEL,rc,h,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
     889    ok = FALSE;
     890  }
     891  else
     892  {
    886893    fp = fopen(filename,"a+");
    887     if(!fp)
    888       fp = fopen(filename,"w");
    889     if(fp) {
    890 
     894    if (!fp)
     895      fp = xfopen(filename,"w",pszSrcFile,__LINE__);
     896    if (!fp)
     897      ok = FALSE;
     898    else {
    891899      LONG numlines,ret,len,wl,temp;
    892900      IPT  s;
     
    954962      } // for lines
    955963    }
    956     else
    957       ok = FALSE;
    958   }
    959   else
    960     ok = FALSE;
     964  }
    961965
    962966  MLEsetwrap(h, fWrap);         // Restore
Note: See TracChangeset for help on using the changeset viewer.