| [395] | 1 | 
 | 
|---|
 | 2 | /***********************************************************************
 | 
|---|
 | 3 | 
 | 
|---|
 | 4 |   $Id: wrappers.c 400 2006-07-29 19:51:30Z root $
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 |   Wrappers with error checking
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 |   Copyright (c) 2006 Steven H.Levine
 | 
|---|
 | 9 | 
 | 
|---|
 | 10 |   22 Jul 06 SHL Baseline
 | 
|---|
| [400] | 11 |   29 Jul 06 SHL Add xgets_stripped
 | 
|---|
| [395] | 12 | 
 | 
|---|
 | 13 | ***********************************************************************/
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | #define INCL_WIN
 | 
|---|
 | 16 | #include <os2.h>
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | #include <stdio.h>
 | 
|---|
 | 19 | #include <stdlib.h>
 | 
|---|
 | 20 | #include <string.h>
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include "fm3dll.h"
 | 
|---|
 | 23 | #include "fm3str.h"
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | #pragma alloc_text(WRAPPERS1,xfree,xfopen,xfsopen,xmalloc,xrealloc)
 | 
|---|
 | 26 | 
 | 
|---|
| [400] | 27 | PSZ xfgets(PSZ pszBuf, size_t cMaxBytes, FILE *fp, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
 | 28 | {
 | 
|---|
 | 29 |   PSZ psz = fgets(pszBuf,cMaxBytes,fp);
 | 
|---|
 | 30 |   if (!psz) {
 | 
|---|
 | 31 |     if (ferror(fp))
 | 
|---|
 | 32 |       Runtime_Error(pszSrcFile, uiLineNumber, "fgets");
 | 
|---|
 | 33 |   }
 | 
|---|
 | 34 |   else {
 | 
|---|
 | 35 |     size_t c = strlen(psz);
 | 
|---|
 | 36 |     if (c + 1 > cMaxBytes)
 | 
|---|
 | 37 |       Runtime_Error(pszSrcFile, uiLineNumber, "buffer overflow");
 | 
|---|
 | 38 |     else if (!c || (psz[c-1] != '\n' && psz[c-1] != '\r'))
 | 
|---|
 | 39 |       Runtime_Error(pszSrcFile, uiLineNumber, "missing EOL");
 | 
|---|
 | 40 |   }
 | 
|---|
 | 41 |   return psz;
 | 
|---|
 | 42 | }
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | PSZ xfgets_bstripcr(PSZ pszBuf, size_t cMaxBytes, FILE *fp, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
 | 45 | {
 | 
|---|
 | 46 |   PSZ psz = xfgets(pszBuf,cMaxBytes,fp,pszSrcFile,__LINE__);
 | 
|---|
 | 47 |   if (psz)
 | 
|---|
 | 48 |     bstripcr(psz);
 | 
|---|
 | 49 |   return psz;
 | 
|---|
 | 50 | }
 | 
|---|
 | 51 | 
 | 
|---|
| [395] | 52 | FILE *xfopen(PCSZ pszFileName, PCSZ pszMode, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
 | 53 | {
 | 
|---|
 | 54 |   FILE* fp = fopen(pszFileName, pszMode);
 | 
|---|
 | 55 |   if (!fp)
 | 
|---|
 | 56 |     Runtime_Error(pszSrcFile, uiLineNumber, "fopen");
 | 
|---|
 | 57 |   return fp;
 | 
|---|
 | 58 | }
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 | FILE *xfsopen(PCSZ pszFileName,PCSZ pszMode,INT fSharemode, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
 | 61 | {
 | 
|---|
 | 62 |   FILE* fp = _fsopen((PSZ)pszFileName, (PSZ)pszMode, fSharemode);
 | 
|---|
 | 63 |   if (!fp)
 | 
|---|
 | 64 |     Runtime_Error(pszSrcFile, uiLineNumber, "_fsopen");
 | 
|---|
 | 65 |   return fp;
 | 
|---|
 | 66 | }
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 | //== xfree - safe free ==
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | VOID xfree (PVOID pv)
 | 
|---|
 | 72 | {
 | 
|---|
 | 73 |   if (pv)
 | 
|---|
 | 74 |     free(pv);
 | 
|---|
 | 75 | }
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | //== xmalloc() malloc with error checking ==
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | PVOID xmalloc(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
 | 80 | {
 | 
|---|
 | 81 |   PVOID pv = malloc(cBytes);
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 |   if (!pv)
 | 
|---|
 | 84 |     Runtime_Error(pszSrcFile,uiLineNumber,GetPString(IDS_OUTOFMEMORY));
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |   return pv;
 | 
|---|
 | 87 | }
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 | //== xmallocz() malloc and zero with error checking ==
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 | PVOID xmallocz(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
 | 92 | {
 | 
|---|
 | 93 |   PVOID pv = malloc(cBytes);
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 |   if (!pv)
 | 
|---|
 | 96 |     Runtime_Error(pszSrcFile,uiLineNumber,GetPString(IDS_OUTOFMEMORY));
 | 
|---|
 | 97 |   else
 | 
|---|
 | 98 |     memset(pv, 0, cBytes);
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |   return pv;
 | 
|---|
 | 101 | }
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 | //== xrealloc() realloc with error checking ==
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 | PVOID xrealloc(PVOID pvIn, size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
 | 106 | {
 | 
|---|
 | 107 |   PVOID pv = realloc(pvIn, cBytes);
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |   if (!pv && cBytes)
 | 
|---|
 | 110 |     Runtime_Error(pszSrcFile,uiLineNumber,GetPString(IDS_OUTOFMEMORY));
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 |   return pv;
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 | }
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 | //== xstrdup() strdup with error checking ==
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | PVOID xstrdup(PCSZ pszIn, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
 | 119 | {
 | 
|---|
 | 120 |   PSZ psz = strdup(pszIn);
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 |   if (!psz)
 | 
|---|
 | 123 |     Runtime_Error(pszSrcFile, uiLineNumber,GetPString(IDS_OUTOFMEMORY));
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |   return psz;
 | 
|---|
 | 126 | }
 | 
|---|
 | 127 | 
 | 
|---|