source: trunk/tools/common/kFileFormatBase.cpp@ 5053

Last change on this file since 5053 was 5053, checked in by bird, 25 years ago

Corrected kFileLX and kFilePE constructors.
Made kDump.exe work again.
Added import module dump to kFilePE.

File size: 2.1 KB
Line 
1/* $Id: kFileFormatBase.cpp,v 1.3 2001-02-02 08:45:41 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 "kFile.h"
23#include "kFileFormatBase.h"
24
25
26
27
28/**
29 * Creates a memory buffer for a binary file.
30 * @returns Pointer to file memoryblock. NULL on error.
31 * @param pszFilename Pointer to filename string.
32 * @remark This function is the one using most of the execution
33 * time (DosRead + DosOpen) - about 70% of the execution time!
34 */
35void *kFileFormatBase::readfile(const char *pszFilename)
36{
37 void *pvFile = NULL;
38 FILE *phFile;
39
40 phFile = fopen(pszFilename, "rb");
41 if (phFile != NULL)
42 {
43 long int cbFile;
44
45 if (ftell(phFile) < 0
46 ||
47 fseek(phFile, 0, SEEK_END) != 0
48 ||
49 (cbFile = ftell(phFile)) < 0
50 ||
51 fseek(phFile, 0, SEEK_SET) != 0
52 )
53 cbFile = -1;
54
55 if (cbFile > 0)
56 {
57 pvFile = malloc((size_t)cbFile + 1);
58 if (pvFile != NULL)
59 {
60 memset(pvFile, 0, (size_t)cbFile + 1);
61 if (fread(pvFile, 1, (size_t)cbFile, phFile) == 0)
62 { /* failed! */
63 free(pvFile);
64 pvFile = NULL;
65 }
66 }
67 else
68 fprintf(stderr, "warning/error: failed to open file %s\n", pszFilename);
69 }
70 fclose(phFile);
71 }
72 return pvFile;
73}
74
75/**
76 * Dump function.
77 * @returns Successindicator.
78 * @param pOut Output file.
79 */
80BOOL kFileFormatBase::dump(kFile *pOut)
81{
82 pOut->printf("Sorry, dump() is not implemented for this file format.\n");
83 return FALSE;
84}
85
Note: See TracBrowser for help on using the repository browser.