[8003] | 1 | /* $Id: kFileSDF.cpp,v 1.2 2002-02-24 02:47:27 bird Exp $
|
---|
[5534] | 2 | *
|
---|
| 3 | * kFileSDF- Structure Defintion File class Implementation.
|
---|
| 4 | *
|
---|
| 5 | * Copyright (c) 2001 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
| 6 | *
|
---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 8 | *
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | /*******************************************************************************
|
---|
| 13 | * Defined Constants And Macros *
|
---|
| 14 | *******************************************************************************/
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 | /*******************************************************************************
|
---|
| 18 | * Header Files *
|
---|
| 19 | *******************************************************************************/
|
---|
| 20 | #include <string.h>
|
---|
| 21 |
|
---|
[8003] | 22 | #include "kTypes.h"
|
---|
| 23 | #include "kError.h"
|
---|
| 24 | #include "kFileInterfaces.h"
|
---|
[5534] | 25 | #include "kFile.h"
|
---|
| 26 | #include "kFileFormatBase.h"
|
---|
| 27 | #include "kFileSDF.h"
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | /*******************************************************************************
|
---|
| 31 | * Structures and Typedefs *
|
---|
| 32 | *******************************************************************************/
|
---|
| 33 |
|
---|
| 34 |
|
---|
[8003] | 35 |
|
---|
[5534] | 36 | /*******************************************************************************
|
---|
| 37 | * Global Variables *
|
---|
| 38 | *******************************************************************************/
|
---|
[8003] | 39 | #if 0
|
---|
[5534] | 40 | static kFileSDF tst((kFile*)NULL);
|
---|
| 41 | #endif
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Parses the in memory SDF file.
|
---|
| 47 | * @param pvFile Pointer to Filemapping.
|
---|
| 48 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
| 49 | * @remark
|
---|
| 50 | * The layout of a SDF file is something like this:
|
---|
| 51 | *
|
---|
| 52 | * -----------------------
|
---|
| 53 | * | |
|
---|
| 54 | * | SDFHEADER |
|
---|
| 55 | * | |
|
---|
| 56 | * -----------------------
|
---|
| 57 | * | |
|
---|
| 58 | * | Dynamic Array of |
|
---|
| 59 | * | SDFSTRUCTs with |
|
---|
| 60 | * | variable # members. |
|
---|
| 61 | * | |
|
---|
| 62 | * -----------------------
|
---|
| 63 | * | |
|
---|
| 64 | * | Array of SDFTYPE. |
|
---|
| 65 | * | |
|
---|
| 66 | * -----------------------
|
---|
| 67 | *
|
---|
| 68 | */
|
---|
[21916] | 69 | void kFileSDF::parseSDFFile(void *pvFile) throw (kError)
|
---|
[5534] | 70 | {
|
---|
| 71 | /*
|
---|
| 72 | * Sanity check on header.
|
---|
| 73 | */
|
---|
| 74 | pHdr = (PSDFHEADER)pvFile;
|
---|
| 75 | if ( pHdr->cStructs >= 0x8000 /* 32k is a reasonable limit. */
|
---|
| 76 | || pHdr->cTypes >= 0x8000)
|
---|
[8003] | 77 | throw(kError(kError::SDF_TOO_MANY));
|
---|
[5534] | 78 |
|
---|
| 79 | /*
|
---|
| 80 | * Initialize the papStructs array.
|
---|
| 81 | */
|
---|
| 82 | papStructs = NULL;
|
---|
| 83 | PSDFSTRUCT pStruct = (PSDFSTRUCT)(unsigned)(pHdr + 1);
|
---|
| 84 | if (pHdr->cStructs > 0)
|
---|
| 85 | {
|
---|
| 86 | papStructs = new PSDFSTRUCT[pHdr->cStructs];
|
---|
| 87 | for (int i = pHdr->cStructs; i > 0; i--)
|
---|
| 88 | {
|
---|
| 89 | papStructs[i] = pStruct;
|
---|
| 90 | pStruct = (PSDFSTRUCT)&pStruct->aMembers[pStruct->cMembers];
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /*
|
---|
| 95 | * typedefs starts where the structures ends. (it seems)
|
---|
| 96 | */
|
---|
| 97 | paTypes = (PSDFTYPE)pStruct;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Find a typedef.
|
---|
| 104 | * @returns Pointer to the typedef.
|
---|
| 105 | * NULL if not found.
|
---|
| 106 | * @param pszType Name of the type to find.
|
---|
| 107 | * @sketch Linear search.
|
---|
| 108 | * @status Completely implemented.
|
---|
| 109 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
| 110 | * @remark We might concider sorting the type array one day for binary search...
|
---|
| 111 | */
|
---|
| 112 | PSDFTYPE kFileSDF::getType(const char *pszType)
|
---|
| 113 | {
|
---|
| 114 | if (*pszType != '\0')
|
---|
| 115 | {
|
---|
| 116 | int i;
|
---|
| 117 | for (i = 0; i < pHdr->cTypes; i++)
|
---|
| 118 | if (strcmp(paTypes[i].szName, pszType) == 0)
|
---|
| 119 | return &paTypes[i]; /* Found */
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | return NULL; /* Not Found */
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * Find a struct.
|
---|
| 128 | * @returns Pointer to struct.
|
---|
| 129 | * NULL if not found.
|
---|
| 130 | * @param pszStruct Name of the struct to find.
|
---|
| 131 | * @sketch Linear search.
|
---|
| 132 | * @status Completely implemented.
|
---|
| 133 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
| 134 | * @remark We might concider sorting the struct array one day for binary search...
|
---|
| 135 | */
|
---|
| 136 | PSDFSTRUCT kFileSDF::getStruct(const char *pszStruct)
|
---|
| 137 | {
|
---|
| 138 | if (*pszStruct != '\0')
|
---|
| 139 | {
|
---|
| 140 | int i;
|
---|
| 141 | for (i = 0; i < pHdr->cStructs; i++)
|
---|
| 142 | if (strcmp(papStructs[i]->szName, pszStruct) == 0)
|
---|
| 143 | return papStructs[i]; /* Found */
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return NULL; /* Not Found */
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Constructor.
|
---|
| 153 | * @param pFile Pointer to file object to parse.
|
---|
| 154 | * The status of this is very likely to be changed.
|
---|
| 155 | * @sketch Read the file in to memory block.
|
---|
| 156 | * Convert '\r\n' to '\n'.
|
---|
| 157 | * Call parser function which does the rest of the work.
|
---|
| 158 | * @status Completely implemented.
|
---|
| 159 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
| 160 | * @remark Will throw error codes.
|
---|
| 161 | */
|
---|
[21916] | 162 | kFileSDF::kFileSDF(kFile *pFile) throw (kError) :
|
---|
[8003] | 163 | kFileFormatBase(pFile), papStructs(NULL), pHdr(NULL), paTypes(NULL)
|
---|
[5534] | 164 | {
|
---|
| 165 | long cchFile = pFile->getSize();
|
---|
[8003] | 166 | void * pvFile = pFile->mapFile();
|
---|
[5534] | 167 |
|
---|
| 168 | /*
|
---|
| 169 | * apply "rb"->"r" fix
|
---|
| 170 | */
|
---|
| 171 | char * pchW = (char*)pvFile;
|
---|
| 172 | char * pchR = pchW;
|
---|
| 173 | char * pchEnd = pchW + cchFile;
|
---|
| 174 | while (cchFile > 0)
|
---|
| 175 | {
|
---|
| 176 | char ch = *pchR;
|
---|
| 177 | if (ch == '\r' && cchFile >= 2 && pchR[1] == '\n')
|
---|
| 178 | {
|
---|
| 179 | cchFile--;
|
---|
| 180 | pchR++;
|
---|
| 181 | ch = '\n';
|
---|
| 182 | }
|
---|
| 183 | *pchW++ = ch;
|
---|
| 184 | pchR++;
|
---|
| 185 | cchFile--;
|
---|
| 186 | }
|
---|
| 187 | while (pchR < pchW)
|
---|
| 188 | *pchR = 0;
|
---|
| 189 |
|
---|
| 190 | /*
|
---|
| 191 | * Parse the file.
|
---|
| 192 | */
|
---|
| 193 | try
|
---|
| 194 | {
|
---|
| 195 | parseSDFFile(pvFile);
|
---|
| 196 | }
|
---|
| 197 | catch (int err)
|
---|
| 198 | {
|
---|
[8003] | 199 | kFile::mapFree(pvFile);
|
---|
[5534] | 200 | if (papStructs);
|
---|
| 201 | delete papStructs;
|
---|
| 202 | throw(err);
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 |
|
---|
[21916] | 207 | kFileSDF::~kFileSDF() throw (kError)
|
---|
[5534] | 208 | {
|
---|
| 209 | if (papStructs);
|
---|
| 210 | delete papStructs;
|
---|
[8003] | 211 | kFile::mapFree(pHdr);
|
---|
[5534] | 212 | }
|
---|
[8003] | 213 |
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | kDbgTypeEntry * kFileSDF::dbgtypeFindFirst(int flFlags)
|
---|
| 217 | {
|
---|
| 218 | return NULL;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | kDbgTypeEntry * kFileSDF::dbgtypeFindNext(kDbgTypeEntry *kDbgTypeEntry)
|
---|
| 222 | {
|
---|
| 223 | return NULL;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | void kFileSDF::dbgtypeFindClose(kDbgTypeEntry *kDbgTypeEntry)
|
---|
| 227 | {
|
---|
| 228 | return;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | kDbgTypeEntry * kFileSDF::dbgtypeLookup(const char *pszName, int flFlags)
|
---|
| 232 | {
|
---|
| 233 | return NULL;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 | KBOOL kFileSDF::dump(kFile *pOut)
|
---|
| 240 | {
|
---|
| 241 | return FALSE;
|
---|
| 242 | }
|
---|
| 243 |
|
---|