File: sarfio.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (512 lines) | stat: -rw-r--r-- 16,220 bytes parent folder | download | duplicates (6)
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "../include/string.h"

#include "obj.h"
#include "sarfio.h"
#include "config.h"


void *SARParmNew(int type);
void *SARParmNewAppend(int type, void ***ptr, int *total);
void SARParmDelete(void *p);
void SARParmDeleteAll(void ***ptr, int *total);


#ifndef SAR_COMMENT_CHAR
# define SAR_COMMENT_CHAR	'#'
#endif

#define ATOI(s)		(((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)		(((s) != NULL) ? atol(s) : 0)
#define ATOF(s)		(((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s)	(((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
   
#define ISCOMMENT(c)    ((c) == SAR_COMMENT_CHAR)
#define ISCR(c)         (((c) == '\n') || ((c) == '\r'))

#define DEGTORAD(d)     ((d) * PI / 180)
#define RADTODEG(r)     ((r) * 180 / PI)


/*
 *	Allocates a new parameter structure of the given type or NULL
 *	on error.
 */
void *SARParmNew(int type)
{
	void *p = NULL;
	int len = 0;


	switch(type)
	{
	  case SAR_PARM_VERSION:
	    len = sizeof(sar_parm_version_struct);
	    break;
	  case SAR_PARM_NAME:
	    len = sizeof(sar_parm_name_struct);
	    break;
          case SAR_PARM_DESCRIPTION:
            len = sizeof(sar_parm_description_struct);
            break;

          case SAR_PARM_PLAYER_MODEL_FILE:
            len = sizeof(sar_parm_player_model_file_struct);
            break;
	  case SAR_PARM_WEATHER:
	    len = sizeof(sar_parm_weather_struct);
	    break;
	  case SAR_PARM_TIME_OF_DAY:
	    len = sizeof(sar_parm_time_of_day_struct);
            break;
          case SAR_PARM_REGISTER_LOCATION:
            len = sizeof(sar_parm_register_location_struct);
            break;

          case SAR_PARM_SCENE_GPS:
            len = sizeof(sar_parm_scene_gps_struct);
            break;
          case SAR_PARM_SCENE_MAP:
            len = sizeof(sar_parm_scene_map_struct);
            break;
          case SAR_PARM_SCENE_ELEVATION:
            len = sizeof(sar_parm_scene_elevation_struct);
            break;
          case SAR_PARM_SCENE_CANT:
            len = sizeof(sar_parm_scene_cant_struct);
            break;
          case SAR_PARM_SCENE_GROUND_FLAGS:
            len = sizeof(sar_parm_scene_ground_flags_struct);
            break;
          case SAR_PARM_SCENE_GROUND_TILE:
            len = sizeof(sar_parm_scene_ground_tile_struct);
            break;

          case SAR_PARM_TEXTURE_BASE_DIRECTORY:
            len = sizeof(sar_parm_texture_base_directory_struct);
            break;
          case SAR_PARM_TEXTURE_LOAD:
            len = sizeof(sar_parm_texture_load_struct);
            break;

          case SAR_PARM_MISSION_SCENE_FILE:
            len = sizeof(sar_parm_mission_scene_file_struct);
            break;
          case SAR_PARM_MISSION_NEW_OBJECTIVE:
            len = sizeof(sar_parm_mission_new_objective_struct);
            break;
          case SAR_PARM_MISSION_TIME_LEFT:
            len = sizeof(sar_parm_mission_time_left_struct);
            break;
          case SAR_PARM_MISSION_BEGIN_AT:
            len = sizeof(sar_parm_mission_begin_at_struct);
            break;
	  case SAR_PARM_MISSION_BEGIN_AT_POS:
            len = sizeof(sar_parm_mission_begin_at_pos_struct);
            break;
          case SAR_PARM_MISSION_ARRIVE_AT:
            len = sizeof(sar_parm_mission_arrive_at_struct);
            break;
          case SAR_PARM_MISSION_MESSAGE_SUCCESS:
            len = sizeof(sar_parm_mission_message_success_struct);
            break;
          case SAR_PARM_MISSION_MESSAGE_FAIL:
            len = sizeof(sar_parm_mission_message_fail_struct);
            break;
          case SAR_PARM_MISSION_HUMANS_TALLY:
            len = sizeof(sar_parm_mission_humans_tally_struct);
            break;
          case SAR_PARM_MISSION_ADD_INTERCEPT:
            len = sizeof(sar_parm_mission_add_intercept_struct);
            break;

	  case SAR_PARM_MISSION_LOG_HEADER:
            len = sizeof(sar_parm_mission_log_header_struct);
            break;
	  case SAR_PARM_MISSION_LOG_EVENT:
            len = sizeof(sar_parm_mission_log_event_struct);
            break;

          case SAR_PARM_NEW_OBJECT:
            len = sizeof(sar_parm_new_object_struct);
            break;
          case SAR_PARM_NEW_HELIPAD:
            len = sizeof(sar_parm_new_helipad_struct);
            break;
          case SAR_PARM_NEW_RUNWAY:
            len = sizeof(sar_parm_new_runway_struct);
            break;
          case SAR_PARM_NEW_HUMAN:
            len = sizeof(sar_parm_new_human_struct);
            break;
	  case SAR_PARM_NEW_FIRE:
	    len = sizeof(sar_parm_new_fire_struct);
	    break;
	  case SAR_PARM_NEW_SMOKE:
	    len = sizeof(sar_parm_new_smoke_struct);
            break;
          case SAR_PARM_NEW_PREMODELED:
            len = sizeof(sar_parm_new_premodeled_struct);
            break;

	  case SAR_PARM_SELECT_OBJECT_BY_NAME:
	    len = sizeof(sar_parm_select_object_by_name_struct);
	    break;

          case SAR_PARM_MODEL_FILE:
            len = sizeof(sar_parm_model_file_struct);
            break;
          case SAR_PARM_RANGE:
            len = sizeof(sar_parm_range_struct);
            break;
	  case SAR_PARM_RANGE_FAR:
	    len = sizeof(sar_parm_range_far_struct);
            break;
          case SAR_PARM_TRANSLATE:
            len = sizeof(sar_parm_translate_struct);
            break;
          case SAR_PARM_TRANSLATE_RANDOM:
            len = sizeof(sar_parm_translate_random_struct);
            break;
          case SAR_PARM_ROTATE:
            len = sizeof(sar_parm_rotate_struct);
            break;

          case SAR_PARM_NO_DEPTH_TEST:
            len = sizeof(sar_parm_no_depth_test_struct);
            break;
          case SAR_PARM_POLYGON_OFFSET:
            len = sizeof(sar_parm_polygon_offset_struct);
            break;

          case SAR_PARM_CONTACT_BOUNDS_SPHERICAL:
            len = sizeof(sar_parm_contact_bounds_spherical_struct);
            break;
          case SAR_PARM_CONTACT_BOUNDS_CYLENDRICAL:
            len = sizeof(sar_parm_contact_bounds_cylendrical_struct);
            break;
          case SAR_PARM_CONTACT_BOUNDS_RECTANGULAR:
            len = sizeof(sar_parm_contact_bounds_rectangular_struct);
            break;

          case SAR_PARM_GROUND_ELEVATION:
            len = sizeof(sar_parm_ground_elevation_struct);
            break;

          case SAR_PARM_OBJECT_NAME:
            len = sizeof(sar_parm_object_name_struct);
            break;
          case SAR_PARM_OBJECT_MAP_DESCRIPTION:
            len = sizeof(sar_parm_object_map_description_struct);
            break;

          case SAR_PARM_FUEL:
            len = sizeof(sar_parm_fuel_struct);
            break;
          case SAR_PARM_HITPOINTS:
            len = sizeof(sar_parm_hitpoints_struct);
            break;
          case SAR_PARM_ENGINE_STATE:
            len = sizeof(sar_parm_engine_state_struct);
            break;
          case SAR_PARM_PASSENGERS:
            len = sizeof(sar_parm_passengers_struct);
            break;

          case SAR_PARM_RUNWAY_APPROACH_LIGHTING_NORTH:
            len = sizeof(sar_parm_runway_approach_lighting_north_struct);
            break;
          case SAR_PARM_RUNWAY_APPROACH_LIGHTING_SOUTH:
            len = sizeof(sar_parm_runway_approach_lighting_south_struct);
            break;

          case SAR_PARM_HUMAN_MESSAGE_ENTER:
            len = sizeof(sar_parm_human_message_enter_struct);
            break;
          case SAR_PARM_HUMAN_REFERENCE:
            len = sizeof(sar_parm_human_reference_struct);
            break;

/* Add support for other parm structure types here */

	  default:
	    fprintf(
		stderr,
"SARParmNew(): Unsupported object type `%i'.\n",
		type
	    );
	    break;
	}

	/* If length is positive it means a valid structure was
	 * matched above
	 */
	if(len > 0)
	{
	    p = calloc(1, len);
	    if(p != NULL)
		*(int *)p = type;
	}

	return(p);
}

/*
 *	Creates a new prarameter of the given type and appends it to the
 *	given list of parameters.
 */
void *SARParmNewAppend(int type, void ***ptr, int *total)
{
	int i;
	void *p;

	if((ptr == NULL) || (total == NULL))
	    return(NULL);

	i = MAX(*total, 0);
	*total = i + 1;
	*ptr = (void **)realloc(*ptr, (*total) * sizeof(void *));
	if(*ptr == NULL)
	{
	    *total = 0;
	    return(NULL);
	}

	(*ptr)[i] = p = SARParmNew(type);

	return(p);
}

/*
 *	Deletes the parameter.
 */
void SARParmDelete(void *p)
{
	int type;
	sar_parm_version_struct *p_version;
	sar_parm_name_struct *p_name;
	sar_parm_description_struct *p_description;
	sar_parm_player_model_file_struct *p_player_model_file;
	sar_parm_weather_struct *p_weather;
	sar_parm_register_location_struct *p_register_location;
	sar_parm_scene_map_struct *p_scene_map;
	sar_parm_scene_ground_tile_struct *p_scene_ground_tile;
	sar_parm_texture_base_directory_struct *p_texture_base_directory;
	sar_parm_texture_load_struct *p_texture_load;
	sar_parm_mission_scene_file_struct *p_mission_scene_file;
	sar_parm_mission_new_objective_struct *p_mission_new_objective;
	sar_parm_mission_begin_at_struct *p_mission_begin_at;
	sar_parm_mission_begin_at_pos_struct *p_mission_begin_at_pos;
	sar_parm_mission_arrive_at_struct *p_mission_arrive_at;
	sar_parm_mission_message_success_struct *p_mission_message_success;
	sar_parm_mission_message_fail_struct *p_mission_message_fail;
	sar_parm_mission_add_intercept_struct *p_mission_add_intercept;
	sar_parm_mission_log_header_struct *p_mission_log_header;
	sar_parm_mission_log_event_struct *p_mission_log_event;
	sar_parm_new_helipad_struct *p_new_helipad;
	sar_parm_new_runway_struct *p_new_runway;
	sar_parm_new_human_struct *p_new_human;
	sar_parm_new_fire_struct *p_new_fire;
	sar_parm_new_smoke_struct *p_new_smoke;
	sar_parm_new_premodeled_struct *p_new_premodeled;
	sar_parm_select_object_by_name_struct *p_select_object_by_name;
	sar_parm_model_file_struct *p_model_file;
	sar_parm_object_name_struct *p_object_name;
	sar_parm_object_map_description_struct *p_object_map_description;
	sar_parm_runway_approach_lighting_north_struct *p_runway_approach_lighting_north;
	sar_parm_runway_approach_lighting_south_struct *p_runway_approach_lighting_south;
	sar_parm_human_message_enter_struct *p_human_message_enter;
	sar_parm_human_reference_struct *p_human_reference;


	if(p == NULL)
	    return;

	type = *(int *)p;
	switch(type)
	{
	  case SAR_PARM_VERSION:
	    p_version = (sar_parm_version_struct *)p;
	    free(p_version->copyright);
	    break;
	  case SAR_PARM_NAME:
	    p_name = (sar_parm_name_struct *)p;
	    free(p_name->name);
	    break;
	  case SAR_PARM_DESCRIPTION:
	    p_description = (sar_parm_description_struct *)p;
	    free(p_description->description);
	    break;

	  case SAR_PARM_PLAYER_MODEL_FILE:
	    p_player_model_file = (sar_parm_player_model_file_struct *)p;
	    free(p_player_model_file->file);
	    break;
	  case SAR_PARM_WEATHER:
	    p_weather = (sar_parm_weather_struct *)p;
	    free(p_weather->weather_preset_name);
	    break;
	  case SAR_PARM_REGISTER_LOCATION:
	    p_register_location = (sar_parm_register_location_struct *)p;
	    free(p_register_location->name);
	    break;

	  case SAR_PARM_SCENE_MAP:
	    p_scene_map = (sar_parm_scene_map_struct *)p;
	    free(p_scene_map->file);
	    break;
	  case SAR_PARM_SCENE_GROUND_TILE:
	    p_scene_ground_tile = (sar_parm_scene_ground_tile_struct *)p;
	    free(p_scene_ground_tile->texture_name);
	    break;

	  case SAR_PARM_TEXTURE_BASE_DIRECTORY:
	    p_texture_base_directory = (sar_parm_texture_base_directory_struct *)p;
	    free(p_texture_base_directory->directory);
	    break;
	  case SAR_PARM_TEXTURE_LOAD:
	    p_texture_load = (sar_parm_texture_load_struct *)p;
	    free(p_texture_load->name);
	    free(p_texture_load->file);
	    break;

	  case SAR_PARM_MISSION_SCENE_FILE:
	    p_mission_scene_file = (sar_parm_mission_scene_file_struct *)p;
	    free(p_mission_scene_file->file);
	    break;
	  case SAR_PARM_MISSION_NEW_OBJECTIVE:
	    p_mission_new_objective = (sar_parm_mission_new_objective_struct *)p;
	    break;
	  case SAR_PARM_MISSION_BEGIN_AT:
	    p_mission_begin_at = (sar_parm_mission_begin_at_struct *)p;
	    free(p_mission_begin_at->name);
	    break;
          case SAR_PARM_MISSION_BEGIN_AT_POS:
            p_mission_begin_at_pos = (sar_parm_mission_begin_at_pos_struct *)p;
            break;
          case SAR_PARM_MISSION_ARRIVE_AT:
            p_mission_arrive_at = (sar_parm_mission_arrive_at_struct *)p;
            free(p_mission_arrive_at->name);
            break;
	  case SAR_PARM_MISSION_MESSAGE_SUCCESS:
	    p_mission_message_success = (sar_parm_mission_message_success_struct *)p;
	    free(p_mission_message_success->message);
	    break;
	  case SAR_PARM_MISSION_MESSAGE_FAIL:
	    p_mission_message_fail = (sar_parm_mission_message_fail_struct *)p;
	    free(p_mission_message_fail->message);
	    break;
	  case SAR_PARM_MISSION_ADD_INTERCEPT:
	    p_mission_add_intercept = (sar_parm_mission_add_intercept_struct *)p;
	    free(p_mission_add_intercept->name);
	    break;
	  case SAR_PARM_MISSION_LOG_HEADER:
	    p_mission_log_header = (sar_parm_mission_log_header_struct *)p;
	    free(p_mission_log_header->title);
	    free(p_mission_log_header->scene_file);
	    free(p_mission_log_header->player_stats_file);
	    break;
	  case SAR_PARM_MISSION_LOG_EVENT:
	    p_mission_log_event = (sar_parm_mission_log_event_struct *)p;
	    free(p_mission_log_event->message);
	    break;

	  case SAR_PARM_NEW_HELIPAD:
	    p_new_helipad = (sar_parm_new_helipad_struct *)p;
	    free(p_new_helipad->style);
	    free(p_new_helipad->label);
	    free(p_new_helipad->ref_obj_name);
	    break;
	  case SAR_PARM_NEW_RUNWAY:
	    p_new_runway = (sar_parm_new_runway_struct *)p;
	    free(p_new_runway->north_label);
	    free(p_new_runway->south_label);
	    break;
	  case SAR_PARM_NEW_HUMAN:
	    p_new_human = (sar_parm_new_human_struct *)p;
	    free(p_new_human->type_name);
	    break;
	  case SAR_PARM_NEW_FIRE:
	    p_new_fire = (sar_parm_new_fire_struct *)p;
	    break;
	  case SAR_PARM_NEW_SMOKE:
	    p_new_smoke = (sar_parm_new_smoke_struct *)p;
	    break;
	  case SAR_PARM_NEW_PREMODELED:
	    p_new_premodeled = (sar_parm_new_premodeled_struct *)p;
	    free(p_new_premodeled->model_type);
	    strlistfree(p_new_premodeled->argv, p_new_premodeled->argc);
	    break;

	  case SAR_PARM_SELECT_OBJECT_BY_NAME:
	    p_select_object_by_name = (sar_parm_select_object_by_name_struct *)p;
	    free(p_select_object_by_name->name);
	    break;

	  case SAR_PARM_MODEL_FILE:
	    p_model_file = (sar_parm_model_file_struct *)p;
	    free(p_model_file->file);
	    break;
	  case SAR_PARM_OBJECT_NAME:
	    p_object_name = (sar_parm_object_name_struct *)p;
	    free(p_object_name->name);
	    break;
	  case SAR_PARM_OBJECT_MAP_DESCRIPTION:
	    p_object_map_description = (sar_parm_object_map_description_struct *)p;
	    free(p_object_map_description->description);
	    break;

	  case SAR_PARM_RUNWAY_APPROACH_LIGHTING_NORTH:
	    p_runway_approach_lighting_north =
		(sar_parm_runway_approach_lighting_north_struct *)p;
	    break;
          case SAR_PARM_RUNWAY_APPROACH_LIGHTING_SOUTH:
            p_runway_approach_lighting_south =
                (sar_parm_runway_approach_lighting_south_struct *)p;
            break;

	  case SAR_PARM_HUMAN_MESSAGE_ENTER:
	    p_human_message_enter = (sar_parm_human_message_enter_struct *)p;
	    free(p_human_message_enter->message);
	    break;
	  case SAR_PARM_HUMAN_REFERENCE:
	    p_human_reference = (sar_parm_human_reference_struct *)p;
	    free(p_human_reference->reference_name);
	    break;

/* Add support for parm structures that have allocated members here.
 * All parm structures without allocated members should be ignored.
 */
	}

	free(p);
}

/*
 *	Deletes the list of parameters.
 */
void SARParmDeleteAll(void ***ptr, int *total)
{
	int i;

	if((ptr == NULL) || (total == NULL))
	    return;

	for(i = 0; i < *total; i++)
	    SARParmDelete((*ptr)[i]);

	if(*ptr != NULL)
	{
	    free(*ptr);
	    *ptr = NULL;
	}
	*total = 0;
}