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