source: trunk/kDbg/kDbgHlpCrt.cpp@ 3528

Last change on this file since 3528 was 3528, checked in by bird, 18 years ago

Some refactoring and OS abstraction by instroducing kDbgHlp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/* $Id: kDbgHlpCrt.cpp 3528 2007-08-20 02:43:13Z bird $ */
2/** @file
3 * kDbg - The Debug Info Reader, Helpers, CRT Based Implementation.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with This program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
25/*******************************************************************************
26* Header Files *
27*******************************************************************************/
28#include "kDbgHlp.h"
29#include "kDbg.h"
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <errno.h>
35
36
37
38/**
39 * The stdio base implementation of KDBGHLPFILE.
40 */
41typedef struct KDBGHLPFILE
42{
43 /** Pointer to the stdio file stream. */
44 FILE *pStrm;
45} KDBGHLPFILE;
46
47/** @def HAVE_FSEEKO
48 * Define HAVE_FSEEKO to indicate that fseeko and ftello should be used. */
49#if !defined(_MSC_VER)
50# define HAVE_FSEEKO
51#endif
52
53
54void *kDbgHlpAlloc(size_t cb)
55{
56 return malloc(cb);
57}
58
59
60void *kDbgHlpAllocZ(size_t cb)
61{
62 return calloc(1, cb);
63}
64
65
66void *kDbgHlpAllocDup(const void *pv, size_t cb)
67{
68 void *pvNew = malloc(cb);
69 if (pvNew)
70 memcpy(pvNew, pv, cb);
71 return pvNew;
72}
73
74
75void *kDbgHlpReAlloc(void *pv, size_t cb)
76{
77 return realloc(pv, cb);
78}
79
80
81void kDbgHlpFree(void *pv)
82{
83 free(pv);
84}
85
86
87int kDbgHlpCrtConvErrno(int rc)
88{
89 switch (rc)
90 {
91 case 0: return 0;
92 case EINVAL: return KDBG_ERR_INVALID_PARAMETER;
93 case ENOMEM: return KDBG_ERR_NO_MEMORY;
94 case EISDIR:
95 case ENOENT: return KDBG_ERR_FILE_NOT_FOUND;
96 default: return KDBG_ERR_GENERAL_FAILURE;
97 }
98}
99
100
101int kDbgHlpOpenRO(const char *pszFilename, PKDBGHLPFILE *ppFile)
102{
103 PKDBGHLPFILE pFile = (PKDBGHLPFILE)kDbgHlpAlloc(sizeof(*pFile));
104 if (!pFile)
105 return KDBG_ERR_NO_MEMORY;
106
107 pFile->pStrm = fopen(pszFilename, "rb");
108 if (pFile->pStrm)
109 {
110 *ppFile = pFile;
111 return 0;
112 }
113 return kDbgHlpCrtConvErrno(errno);
114}
115
116
117void kDbgHlpClose(PKDBGHLPFILE pFile)
118{
119 if (pFile)
120 {
121 fclose(pFile->pStrm);
122 pFile->pStrm = NULL;
123 kDbgHlpFree(pFile);
124 }
125}
126
127
128int64_t kDbgHlpFileSize(PKDBGHLPFILE pFile)
129{
130 int64_t cbFile;
131 int64_t offCur = kDbgHlpTell(pFile);
132 if (offCur >= 0)
133 {
134 if (kDbgHlpSeekByEnd(pFile, 0) == 0)
135 cbFile = kDbgHlpTell(pFile);
136 else
137 cbFile = -1;
138 kDbgHlpSeek(pFile, offCur);
139 }
140 return cbFile;
141}
142
143
144int kDbgHlpReadAt(PKDBGHLPFILE pFile, int64_t off, void *pv, size_t cb)
145{
146 int rc = kDbgHlpSeek(pFile, off);
147 if (!rc)
148 rc = kDbgHlpRead(pFile, pv, cb);
149 return rc;
150}
151
152
153int kDbgHlpRead(PKDBGHLPFILE pFile, void *pv, size_t cb)
154{
155 if (fread(pv, cb, 1, pFile->pStrm) == 1)
156 return 0;
157 return -1;
158}
159
160
161int kDbgHlpSeek(PKDBGHLPFILE pFile, int64_t off)
162{
163#ifdef HAVE_FSEEKO
164 if (!fseeko(pFile->pStrm, off, SEEK_SET))
165 return 0;
166#else
167 long l = (long)off;
168 if (l != off)
169 return KDBG_ERR_OUT_OF_RANGE;
170 if (!fseek(pFile->pStrm, l, SEEK_SET))
171 return 0;
172#endif
173 return kDbgHlpCrtConvErrno(errno);
174}
175
176
177int kDbgHlpSeekByCur(PKDBGHLPFILE pFile, int64_t off)
178{
179#ifdef HAVE_FSEEKO
180 if (!fseeko(pFile->pStrm, off, SEEK_CUR))
181 return 0;
182#else
183 long l = (long)off;
184 if (l != off)
185 return KDBG_ERR_OUT_OF_RANGE;
186 if (!fseek(pFile->pStrm, l, SEEK_CUR))
187 return 0;
188#endif
189 return kDbgHlpCrtConvErrno(errno);
190}
191
192
193int kDbgHlpSeekByEnd(PKDBGHLPFILE pFile, int64_t off)
194{
195#ifdef HAVE_FSEEKO
196 if (!fseeko(pFile->pStrm, -off, SEEK_END))
197 return 0;
198#else
199 long l = (long)off;
200 if (l != off)
201 return KDBG_ERR_OUT_OF_RANGE;
202 if (!fseek(pFile->pStrm, -l, SEEK_END))
203 return 0;
204#endif
205 return kDbgHlpCrtConvErrno(errno);
206}
207
208
209int64_t kDbgHlpTell(PKDBGHLPFILE pFile)
210{
211#ifdef HAVE_FSEEKO
212 return ftello(pFile->pStrm);
213#else
214 return ftell(pFile->pStrm);
215#endif
216}
217
Note: See TracBrowser for help on using the repository browser.