| [395] | 1 |  | 
|---|
|  | 2 | /*********************************************************************** | 
|---|
|  | 3 |  | 
|---|
|  | 4 | $Id: wrappers.c 551 2007-02-28 01:33:51Z gyoung $ | 
|---|
|  | 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 | 
|---|
| [438] | 12 | 18 Aug 06 SHL Correct Runtime_Error line number report | 
|---|
| [395] | 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 |  | 
|---|
| [551] | 28 | PSZ xfgets(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile, | 
|---|
|  | 29 | UINT uiLineNumber) | 
|---|
| [400] | 30 | { | 
|---|
| [551] | 31 | PSZ psz = fgets(pszBuf, cMaxBytes, fp); | 
|---|
|  | 32 |  | 
|---|
| [400] | 33 | if (!psz) { | 
|---|
|  | 34 | if (ferror(fp)) | 
|---|
|  | 35 | Runtime_Error(pszSrcFile, uiLineNumber, "fgets"); | 
|---|
|  | 36 | } | 
|---|
|  | 37 | else { | 
|---|
|  | 38 | size_t c = strlen(psz); | 
|---|
| [551] | 39 |  | 
|---|
| [400] | 40 | if (c + 1 > cMaxBytes) | 
|---|
|  | 41 | Runtime_Error(pszSrcFile, uiLineNumber, "buffer overflow"); | 
|---|
| [551] | 42 | else if (!c || (psz[c - 1] != '\n' && psz[c - 1] != '\r')) | 
|---|
| [400] | 43 | Runtime_Error(pszSrcFile, uiLineNumber, "missing EOL"); | 
|---|
|  | 44 | } | 
|---|
|  | 45 | return psz; | 
|---|
|  | 46 | } | 
|---|
|  | 47 |  | 
|---|
| [551] | 48 | PSZ xfgets_bstripcr(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile, | 
|---|
|  | 49 | UINT uiLineNumber) | 
|---|
| [400] | 50 | { | 
|---|
| [551] | 51 | PSZ psz = xfgets(pszBuf, cMaxBytes, fp, pszSrcFile, uiLineNumber); | 
|---|
|  | 52 |  | 
|---|
| [400] | 53 | if (psz) | 
|---|
|  | 54 | bstripcr(psz); | 
|---|
|  | 55 | return psz; | 
|---|
|  | 56 | } | 
|---|
|  | 57 |  | 
|---|
| [551] | 58 | FILE *xfopen(PCSZ pszFileName, PCSZ pszMode, PCSZ pszSrcFile, | 
|---|
|  | 59 | UINT uiLineNumber) | 
|---|
| [395] | 60 | { | 
|---|
| [551] | 61 | FILE *fp = fopen(pszFileName, pszMode); | 
|---|
|  | 62 |  | 
|---|
| [395] | 63 | if (!fp) | 
|---|
|  | 64 | Runtime_Error(pszSrcFile, uiLineNumber, "fopen"); | 
|---|
|  | 65 | return fp; | 
|---|
|  | 66 | } | 
|---|
|  | 67 |  | 
|---|
| [551] | 68 | FILE *xfsopen(PCSZ pszFileName, PCSZ pszMode, INT fSharemode, PCSZ pszSrcFile, | 
|---|
|  | 69 | UINT uiLineNumber) | 
|---|
| [395] | 70 | { | 
|---|
| [551] | 71 | FILE *fp = _fsopen((PSZ) pszFileName, (PSZ) pszMode, fSharemode); | 
|---|
|  | 72 |  | 
|---|
| [395] | 73 | if (!fp) | 
|---|
|  | 74 | Runtime_Error(pszSrcFile, uiLineNumber, "_fsopen"); | 
|---|
|  | 75 | return fp; | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
|  | 78 | //== xfree - safe free == | 
|---|
|  | 79 |  | 
|---|
| [551] | 80 | VOID xfree(PVOID pv) | 
|---|
| [395] | 81 | { | 
|---|
|  | 82 | if (pv) | 
|---|
|  | 83 | free(pv); | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
|  | 86 | //== xmalloc() malloc with error checking == | 
|---|
|  | 87 |  | 
|---|
|  | 88 | PVOID xmalloc(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber) | 
|---|
|  | 89 | { | 
|---|
|  | 90 | PVOID pv = malloc(cBytes); | 
|---|
|  | 91 |  | 
|---|
|  | 92 | if (!pv) | 
|---|
| [551] | 93 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY)); | 
|---|
| [395] | 94 |  | 
|---|
|  | 95 | return pv; | 
|---|
|  | 96 | } | 
|---|
|  | 97 |  | 
|---|
|  | 98 | //== xmallocz() malloc and zero with error checking == | 
|---|
|  | 99 |  | 
|---|
|  | 100 | PVOID xmallocz(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber) | 
|---|
|  | 101 | { | 
|---|
|  | 102 | PVOID pv = malloc(cBytes); | 
|---|
|  | 103 |  | 
|---|
|  | 104 | if (!pv) | 
|---|
| [551] | 105 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY)); | 
|---|
| [395] | 106 | else | 
|---|
|  | 107 | memset(pv, 0, cBytes); | 
|---|
|  | 108 |  | 
|---|
|  | 109 | return pv; | 
|---|
|  | 110 | } | 
|---|
|  | 111 |  | 
|---|
|  | 112 | //== xrealloc() realloc with error checking == | 
|---|
|  | 113 |  | 
|---|
|  | 114 | PVOID xrealloc(PVOID pvIn, size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber) | 
|---|
|  | 115 | { | 
|---|
|  | 116 | PVOID pv = realloc(pvIn, cBytes); | 
|---|
|  | 117 |  | 
|---|
|  | 118 | if (!pv && cBytes) | 
|---|
| [551] | 119 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY)); | 
|---|
| [395] | 120 |  | 
|---|
|  | 121 | return pv; | 
|---|
|  | 122 |  | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
|  | 125 | //== xstrdup() strdup with error checking == | 
|---|
|  | 126 |  | 
|---|
|  | 127 | PVOID xstrdup(PCSZ pszIn, PCSZ pszSrcFile, UINT uiLineNumber) | 
|---|
|  | 128 | { | 
|---|
|  | 129 | PSZ psz = strdup(pszIn); | 
|---|
|  | 130 |  | 
|---|
|  | 131 | if (!psz) | 
|---|
| [551] | 132 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY)); | 
|---|
| [395] | 133 |  | 
|---|
|  | 134 | return psz; | 
|---|
|  | 135 | } | 
|---|