-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUNGFileAgent.cpp
More file actions
executable file
·453 lines (402 loc) · 13.5 KB
/
Copy pathUNGFileAgent.cpp
File metadata and controls
executable file
·453 lines (402 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// UNGFileAgent.cpp : implementation file
//
#include "stdafx.h"
#include "MapEditor.h"
#include "UNGFileAgent.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUNGFileAgent
CUNGFileAgent::CUNGFileAgent()
{
//Initialization
hOpenHandle = NULL;
bHandleOpened = FALSE;
}
CUNGFileAgent::~CUNGFileAgent()
{
}
/////////////////////////////////////////////////////////////////////////////
// CUNGFileAgent message handlers
BOOL CUNGFileAgent::WriteMap(LPMAP lpMap, LPCTSTR lpctstrFileName)
{
if(lpMap != NULL) // If the map exists
{
// Get file Access
hOpenHandle = CreateFile(lpctstrFileName, GENERIC_WRITE,
FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hOpenHandle == NULL)
return FALSE;
bHandleOpened = TRUE;
// File is opened so call subroutines to save everything
// Save the signature
LPSTR lpFileSignature = "UGDNR Map File ver. 1.024";
_WriteDataString(lpFileSignature);
// Save MAPHEADER structure
if(!SaveMapHeader(lpMap->pMapHeader))
return FALSE;
// Save Font
_WriteDataBinary((LPCVOID)(&lpMap->fMapFont), sizeof(LOGFONT));
// Save All Lines
LPLINEINFO lpLineInfo = lpMap->pLineInfo;
if(lpLineInfo != NULL)
{
_WriteDataBinary((LPVOID)(&lpLineInfo->nLines), sizeof(lpLineInfo->nLines));
LPLINE lpLine = lpLineInfo->pFirstEntry;
while(lpLine != NULL)
{
SaveLine(lpLine);
lpLine = lpLine->pNextObject;
}
}
// Save Incidence
LPINCIDENCE lpIncidence = lpMap->pIncidence;
while(lpIncidence != NULL)
{
SaveIncidence(lpMap, lpIncidence);
lpIncidence = lpIncidence->pNextEntry;
}
// Close file
CloseHandle(hOpenHandle);
bHandleOpened = FALSE;
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// Service / IO operations
/////////////////////////////////////////////////////////////////////////////
LPSTR CUNGFileAgent::CreateString(int iStrLen)
{
LPSTR csString;
if((csString = new CHAR[iStrLen]) == NULL)
return NULL;
return csString;
}
LPINCIDENCE CUNGFileAgent::CreateIncidenceRecord()
{
LPINCIDENCE lpIncidence;
if((lpIncidence = new (INCIDENCE)) == NULL)
return NULL;
// New worked
lpIncidence->lpAlpha = lpIncidence->lpBeta = NULL;
lpIncidence->pNextEntry = lpIncidence->pPrevEntry = NULL;
lpIncidence->wRouteTime = 0;
return lpIncidence;
}
LPSTATION CUNGFileAgent::GetStationByIDs(LPLINEINFO lpLineInfo, UINT nStID, UINT nParID)
{
LPLINE lpCntLine = lpLineInfo->pFirstEntry;
LPSTATION lpCntStation = NULL;
BOOL bParentFound = FALSE;
// Loop is all we have
while(lpCntLine != NULL)
{
if(lpCntLine->nID == nParID)
{
bParentFound = TRUE;
break;
}
lpCntLine = lpCntLine->pNextObject;
}
// If parent is found
if(bParentFound)
{
// Lets find a station
lpCntStation = lpCntLine->pStationInfo->pFirstEntry;
while(lpCntStation != NULL)
{
if(lpCntStation->nID == nStID)
return lpCntStation;
lpCntStation = lpCntStation->pNextObject;
}
}
return NULL;
}
UINT CUNGFileAgent::GetParentLineID(LPMAP lpMap, LPSTATION lpStation)
{
LPLINEINFO lpLineInfoBlock = lpMap->pLineInfo;
if(lpLineInfoBlock != NULL)
{
LPLINE lpCurrentLine = lpLineInfoBlock->pFirstEntry;
LPSTATION lpCurrentStation;
while(lpCurrentLine != NULL)
{
lpCurrentStation = lpCurrentLine->pStationInfo->pFirstEntry;
while(lpCurrentStation != NULL)
{
if(lpStation == lpCurrentStation)
return lpCurrentLine->nID;
lpCurrentStation = lpCurrentStation->pNextObject;
}
lpCurrentLine = lpCurrentLine->pNextObject;
}
}
return 0;
}
void CUNGFileAgent::_WriteDataString(LPSTR lpBuffer)
{
int iStringSize;
DWORD dwBytesWritten;
// To write the terminating Character we will increase the length
iStringSize = lstrlen((LPCTSTR)lpBuffer) + 1;
// Check if the file is opened
if(bHandleOpened)
{
// First write the length of the string
_WriteDataBinary((LPCVOID)(&iStringSize), sizeof(iStringSize));
// Then the string itself
WriteFile(hOpenHandle, (LPCVOID)lpBuffer, iStringSize, &dwBytesWritten, NULL);
}
}
void CUNGFileAgent::_WriteDataBinary(LPCVOID lpData, DWORD dwDataSize)
{
DWORD dwBytesWritten;
// Check if the file is opened
if(bHandleOpened)
{
WriteFile(hOpenHandle, lpData, dwDataSize, &dwBytesWritten, NULL);
}
}
BOOL CUNGFileAgent::_ReadDataString(LPSTR *lpBuffer)
{
DWORD dwBytesRead;
BOOL bResult;
int iDataLength;
if(bHandleOpened)
{
// Read the length of the string
bResult = ReadFile(hOpenHandle, (LPVOID)&iDataLength, sizeof(int), &dwBytesRead, NULL);
if(bResult && dwBytesRead == 0)
return FALSE; // End of the file
*lpBuffer = CreateString(iDataLength);// Create a string
// Read the string itself
bResult = ReadFile(hOpenHandle, (LPVOID)(*lpBuffer), iDataLength, &dwBytesRead, NULL);
if(bResult && dwBytesRead == 0)
return FALSE; // End of the file
}
return TRUE;
}
BOOL CUNGFileAgent::_ReadDataBinary(LPVOID lpData, DWORD dwDataSize)
{
DWORD dwBytesRead;
BOOL bResult;
if(bHandleOpened)
{
// Read Data
bResult = ReadFile(hOpenHandle, lpData, dwDataSize, &dwBytesRead, NULL);
if(bResult && dwBytesRead == 0)
return FALSE; // We are at the end of the File
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// Saving Procedures
/////////////////////////////////////////////////////////////////////////////
BOOL CUNGFileAgent::SaveMapHeader(LPMAPHEADER lpMapHeader)
{
_WriteDataString(lpMapHeader->csCountry);
_WriteDataString(lpMapHeader->csRegion);
_WriteDataString(lpMapHeader->csCity);
_WriteDataString(lpMapHeader->csUndergroundName);
_WriteDataString(lpMapHeader->csAuthor);
_WriteDataString(lpMapHeader->csCopyright);
_WriteDataString(lpMapHeader->csYear);
return TRUE;
}
BOOL CUNGFileAgent::SaveLine(LPLINE lpLine)
{
// Save nID
_WriteDataBinary((LPCVOID)(&lpLine->nID), sizeof(lpLine->nID));
// Save Name
_WriteDataString(lpLine->csName);
// Save Style
_WriteDataBinary((LPCVOID)(&lpLine->lsStyle), sizeof(lpLine->lsStyle));
// Save LineStatus
_WriteDataBinary((LPCVOID)(&lpLine->mosLineStatus), sizeof(lpLine->mosLineStatus));
// Save Color
_WriteDataBinary((LPCVOID)(&lpLine->crColor), sizeof(lpLine->crColor));
// Save Polypoint Info
if(lpLine->pPointInfo != NULL)
SavePolyPoints(lpLine->pPointInfo);
// Save Stations
LPSTATIONINFO lpStationInfo = lpLine->pStationInfo;
if(lpStationInfo != NULL)
{
// Save station number
_WriteDataBinary((LPCVOID)(&lpStationInfo->nStations), sizeof(lpStationInfo->nStations));
LPSTATION lpStation = lpStationInfo->pFirstEntry;
while(lpStation != NULL)
{
SaveStation(lpStation);
lpStation = lpStation->pNextObject;
}
}
return TRUE;
}
BOOL CUNGFileAgent::SaveStation(LPSTATION lpStation)
{
// Save nID
_WriteDataBinary((LPVOID)(&lpStation->nID), sizeof(lpStation->nID));
// Save Name
_WriteDataString(lpStation->csName);
// Save Style
_WriteDataBinary((LPVOID)(&lpStation->ssStyle), sizeof(lpStation->ssStyle));
// Save Station Status
_WriteDataBinary((LPVOID)(&lpStation->mosStationStatus), sizeof(lpStation->mosStationStatus));
// Save IsChange
_WriteDataBinary((LPVOID)(&lpStation->bIsChange), sizeof(lpStation->bIsChange));
// Save Coordinates
_WriteDataBinary((LPVOID)(&lpStation->stCoords), sizeof(lpStation->stCoords));
// Save Font Coordinates
_WriteDataBinary((LPVOID)(&lpStation->fntCoords), sizeof(lpStation->fntCoords));
return TRUE;
}
BOOL CUNGFileAgent::SavePolyPoints(LPPOLYPOINTINFO lpPointInfo)
{
// Save Points Number
_WriteDataBinary((LPCVOID)(&lpPointInfo->nPoints), sizeof(lpPointInfo->nPoints));
LPPOLYPOINT lpPoint = lpPointInfo->pFirstEntry;
while(lpPoint != NULL)
{
_WriteDataBinary((LPCVOID)(&lpPoint->x), sizeof(lpPoint->x));
_WriteDataBinary((LPCVOID)(&lpPoint->y), sizeof(lpPoint->y));
lpPoint = lpPoint->next;
}
return TRUE;
}
BOOL CUNGFileAgent::SaveIncidence(LPMAP lpMap, LPINCIDENCE lpIncidence)
{
UINT nParentAlphaId;
UINT nParentBetaId;
LPSTATION lpStAlpha = lpIncidence->lpAlpha;
LPSTATION lpStBeta = lpIncidence->lpBeta;
nParentAlphaId = GetParentLineID(lpMap, lpStAlpha);
nParentBetaId = GetParentLineID(lpMap, lpStBeta);
// Write Stations then the time between them
_WriteDataBinary((LPCVOID)(&lpStAlpha->nID), sizeof(lpStAlpha->nID));
_WriteDataBinary((LPCVOID)(&lpStBeta->nID), sizeof(lpStBeta->nID));
// Write two parent IDs
_WriteDataBinary((LPCVOID)(&nParentAlphaId), sizeof(nParentAlphaId));
_WriteDataBinary((LPCVOID)(&nParentBetaId), sizeof(nParentBetaId));
// Write time
_WriteDataBinary((LPCVOID)(&lpIncidence->wRouteTime), sizeof(lpIncidence->wRouteTime));
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// Loading Procedures
/////////////////////////////////////////////////////////////////////////////
BOOL CUNGFileAgent::PrepareMapReading(LPCTSTR lpctstrFileName)
{
hOpenHandle = CreateFile(lpctstrFileName, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hOpenHandle == NULL)
return FALSE;
bHandleOpened = TRUE;
return TRUE;
}
BOOL CUNGFileAgent::ReadingDone()
{
//Close File
bHandleOpened = FALSE;
return CloseHandle(hOpenHandle);
}
/////////////////////////////////////////////////////////////////////////////
// Structures Reading
/////////////////////////////////////////////////////////////////////////////
BOOL CUNGFileAgent::ReadMapHeader(LPMAPHEADER lpMapHeader)
{
if(!(_ReadDataString(&(lpMapHeader->csCountry)) &&
_ReadDataString(&(lpMapHeader->csRegion)) &&
_ReadDataString(&(lpMapHeader->csCity)) &&
_ReadDataString(&(lpMapHeader->csUndergroundName)) &&
_ReadDataString(&(lpMapHeader->csAuthor)) &&
_ReadDataString(&(lpMapHeader->csCopyright)) &&
_ReadDataString(&(lpMapHeader->csYear))))
return FALSE;
return TRUE;
}
BOOL CUNGFileAgent::ReadLine(LPLINE lpLine)
{
if(!(_ReadDataBinary((LPVOID)(&lpLine->nID), sizeof(UINT)) && // Read nId
_ReadDataString(&(lpLine->csName)) && // Read Name
_ReadDataBinary((LPVOID)(&lpLine->lsStyle), sizeof(lpLine->lsStyle)) && //Read LineStyle
_ReadDataBinary((LPVOID)(&lpLine->mosLineStatus), sizeof(lpLine->mosLineStatus)) && //Read LineStatus
_ReadDataBinary((LPVOID)(&lpLine->crColor), sizeof(COLORREF)))) //Read Color
return FALSE;
return TRUE;
}
BOOL CUNGFileAgent::ReadPoint(POINT *lpPoint)
{
if(!(_ReadDataBinary((LPVOID)(&lpPoint->x), sizeof(long)) && // Read x Coordinate
_ReadDataBinary((LPVOID)(&lpPoint->y), sizeof(long)))) // Read y Coordinate
return FALSE;
return TRUE;
}
BOOL CUNGFileAgent::ReadStation(LPSTATION lpStation)
{
if(!(_ReadDataBinary((LPVOID)(&lpStation->nID), sizeof(UINT)) && // Read nId
_ReadDataString(&(lpStation->csName)) && // Read Name
_ReadDataBinary((LPVOID)(&lpStation->ssStyle), sizeof(lpStation->ssStyle)) && // Read StationStyle
_ReadDataBinary((LPVOID)(&lpStation->mosStationStatus), sizeof(lpStation->mosStationStatus)) && // Read Station Status
_ReadDataBinary((LPVOID)(&lpStation->bIsChange), sizeof(BOOL)) && // Read isChange Property
ReadPoint(&lpStation->stCoords) && // Read Coordinates
ReadPoint(&lpStation->fntCoords))) // Read font coordinates
return FALSE;
return TRUE;
}
BOOL CUNGFileAgent::ReadIncidenceBlock(LPMAP lpMap, LPINCIDENCE lpIncidence)
{
UINT nStAlphaID, nStBetaID;
UINT nStAlphaParentID, nStBetaParentID;
WORD wRouteTime;
// Lets read
if(!(_ReadDataBinary((LPVOID)(&nStAlphaID), sizeof(UINT)) && // Read Alpha station
_ReadDataBinary((LPVOID)(&nStBetaID), sizeof(UINT)) && // Read Beta station
_ReadDataBinary((LPVOID)(&nStAlphaParentID), sizeof(UINT)) && // Read Alpha station's Parent
_ReadDataBinary((LPVOID)(&nStBetaParentID), sizeof(UINT)) && // Read Beta station's Parent
_ReadDataBinary((LPVOID)(&wRouteTime), sizeof(WORD)))) // Read Route Time
return FALSE; // We can't read anymore
// Or we do?
lpIncidence->wRouteTime = wRouteTime;
if((lpIncidence->lpAlpha = GetStationByIDs(lpMap->pLineInfo,
nStAlphaID, nStAlphaParentID)) != NULL &&
(lpIncidence->lpBeta = GetStationByIDs(lpMap->pLineInfo,
nStBetaID, nStBetaParentID)) != NULL)
return TRUE;
return FALSE;
}
BOOL CUNGFileAgent::ReadIncidence(LPMAP lpMap)
{
LPINCIDENCE lpIncLastEntry = NULL;
LPINCIDENCE lpCntIncidence = NULL;
// Lets Create our incidence Record;
if((lpCntIncidence = CreateIncidenceRecord()) == NULL)
return FALSE;
// We have an Incidence Record;
if(!ReadIncidenceBlock(lpMap, lpCntIncidence)) // We couldnt read
{
delete(lpCntIncidence);
return FALSE; // There are no records
}
else // Oh, yeah (we did it)
{
lpMap->pIncidence = lpCntIncidence; // the first record
lpIncLastEntry = lpCntIncidence;
// Read the file until the end;
if((lpCntIncidence = CreateIncidenceRecord()) == NULL)
return FALSE;
while(ReadIncidenceBlock(lpMap, lpCntIncidence))
{
lpCntIncidence->pPrevEntry = lpIncLastEntry;
lpIncLastEntry->pNextEntry = lpCntIncidence;
lpIncLastEntry = lpCntIncidence;
if((lpCntIncidence = CreateIncidenceRecord()) == NULL)
return TRUE;
}
delete(lpCntIncidence);
}
return TRUE;
}