source: trunk/tools/common/kFileSDF.cpp@ 5534

Last change on this file since 5534 was 5534, checked in by bird, 24 years ago

Preinitial coding of SDF file format class.

File size: 5.5 KB
Line 
1/* $Id: kFileSDF.cpp,v 1.1 2001-04-17 04:16:03 bird Exp $
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 <os2.h>
21
22#include <string.h>
23#include <malloc.h>
24
25#include "kInterfaces.h"
26#include "kFile.h"
27#include "kFileFormatBase.h"
28#include "kFileSDF.h"
29
30
31/*******************************************************************************
32* Structures and Typedefs *
33*******************************************************************************/
34
35
36/*******************************************************************************
37* Global Variables *
38*******************************************************************************/
39#if 1
40static 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 */
69void kFileSDF::parseSDFFile(void *pvFile) throw(int)
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)
77 throw(1);
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 */
112PSDFTYPE 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 */
136PSDFSTRUCT 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 */
162kFileSDF::kFileSDF(kFile *pFile) throw(int) :
163 papStructs(NULL), pHdr(NULL), paTypes(NULL)
164{
165 long cchFile = pFile->getSize();
166 void * pvFile = pFile->readFile();
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 {
199 free(pvFile);
200 if (papStructs);
201 delete papStructs;
202 throw(err);
203 }
204}
205
206
207kFileSDF::~kFileSDF()
208{
209 if (papStructs);
210 delete papStructs;
211 free(pHdr);
212}
Note: See TracBrowser for help on using the repository browser.