| 1 | /* $Id: kFileFormatBase.cpp,v 1.1 2000-03-27 10:18:40 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * kFileFormatBase - Base class for kFile<format> classes.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 1999-2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
|---|
| 6 | *
|
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | /*******************************************************************************
|
|---|
| 14 | * Header Files *
|
|---|
| 15 | *******************************************************************************/
|
|---|
| 16 | #include <os2.h>
|
|---|
| 17 |
|
|---|
| 18 | #include <malloc.h>
|
|---|
| 19 | #include <string.h>
|
|---|
| 20 | #include <stdio.h>
|
|---|
| 21 |
|
|---|
| 22 | #include "kFileFormatBase.h"
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Creates a memory buffer for a binary file.
|
|---|
| 29 | * @returns Pointer to file memoryblock. NULL on error.
|
|---|
| 30 | * @param pszFilename Pointer to filename string.
|
|---|
| 31 | * @remark This function is the one using most of the execution
|
|---|
| 32 | * time (DosRead + DosOpen) - about 70% of the execution time!
|
|---|
| 33 | */
|
|---|
| 34 | void *kFileFormatBase::readfile(const char *pszFilename)
|
|---|
| 35 | {
|
|---|
| 36 | void *pvFile = NULL;
|
|---|
| 37 | FILE *phFile;
|
|---|
| 38 |
|
|---|
| 39 | phFile = fopen(pszFilename, "rb");
|
|---|
| 40 | if (phFile != NULL)
|
|---|
| 41 | {
|
|---|
| 42 | long int cbFile;
|
|---|
| 43 |
|
|---|
| 44 | if (ftell(phFile) < 0
|
|---|
| 45 | ||
|
|---|
| 46 | fseek(phFile, 0, SEEK_END) != 0
|
|---|
| 47 | ||
|
|---|
| 48 | (cbFile = ftell(phFile)) < 0
|
|---|
| 49 | ||
|
|---|
| 50 | fseek(phFile, 0, SEEK_SET) != 0
|
|---|
| 51 | )
|
|---|
| 52 | cbFile = -1;
|
|---|
| 53 |
|
|---|
| 54 | if (cbFile > 0)
|
|---|
| 55 | {
|
|---|
| 56 | pvFile = malloc((size_t)cbFile + 1);
|
|---|
| 57 | if (pvFile != NULL)
|
|---|
| 58 | {
|
|---|
| 59 | memset(pvFile, 0, (size_t)cbFile + 1);
|
|---|
| 60 | if (fread(pvFile, 1, (size_t)cbFile, phFile) == 0)
|
|---|
| 61 | { /* failed! */
|
|---|
| 62 | free(pvFile);
|
|---|
| 63 | pvFile = NULL;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | else
|
|---|
| 67 | fprintf(stderr, "warning/error: failed to open file %s\n", pszFilename);
|
|---|
| 68 | }
|
|---|
| 69 | fclose(phFile);
|
|---|
| 70 | }
|
|---|
| 71 | return pvFile;
|
|---|
| 72 | }
|
|---|