naev 0.10.4
slots.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "naev.h"
13#include "slots.h"
14
15#include "array.h"
16#include "log.h"
17#include "nxml.h"
18#include "ndata.h"
19
20#define SP_XML_ID "slot"
25typedef struct SlotProperty_s {
26 char *name;
27 char *display;
31 int locked;
33
34static SlotProperty_t *sp_array = NULL;
36/*
37 * Prototypes.
38 */
39static int sp_check( unsigned int spid );
40
44int sp_load (void)
45{
46 char **sp_files = ndata_listRecursive( SP_DATA_PATH );
47
48 /* First pass, loads up ammunition. */
50 for (int i=0; i<array_size(sp_files); i++) {
52 xmlNodePtr node, cur;
53 xmlDocPtr doc;
54
55 /* Load and read the data. */
56 doc = xml_parsePhysFS( sp_files[i] );
57 if (doc == NULL)
58 continue;
59
60 /* Check to see if document exists. */
61 node = doc->xmlChildrenNode;
62 if (!xml_isNode(node,SP_XML_ID)) {
63 WARN(_("Malformed '%s' file: missing root element '%s'"), sp_files[i], SP_XML_ID );
64 continue;
65 }
66
67 sp = &array_grow( &sp_array );
68 memset( sp, 0, sizeof(SlotProperty_t) );
69 xmlr_attr_strd( node, "name", sp->name );
70 cur = node->xmlChildrenNode;
71 do {
72 xml_onlyNodes(cur);
73
74 /* Load data. */
75 xmlr_strd( cur, "display", sp->display );
76 xmlr_strd( cur, "description", sp->description );
77 if (xml_isNode( cur, "required" )) {
78 sp->required = 1;
79 continue;
80 }
81 if (xml_isNode( cur, "exclusive" )) {
82 sp->exclusive = 1;
83 continue;
84 }
85 if (xml_isNode( cur, "locked" )) {
86 sp->locked = 1;
87 continue;
88 }
89
90 WARN(_("Slot Property '%s' has unknown node '%s'."), node->name, cur->name);
91 } while (xml_nextNode(cur));
92
93 /* Clean up. */
94 xmlFreeDoc(doc);
95 }
96
97 for (int i=0; i<array_size(sp_files); i++)
98 free( sp_files[i] );
99 array_free( sp_files );
100 return 0;
101}
102
106void sp_cleanup (void)
107{
108 for (int i=0; i<array_size(sp_array); i++) {
109 SlotProperty_t *sp = &sp_array[i];
110 free( sp->name );
111 free( sp->display );
112 free( sp->description );
113 }
115 sp_array = NULL;
116}
117
124unsigned int sp_get( const char *name )
125{
126 if (name==NULL)
127 return 0;
128 for (int i=0; i<array_size(sp_array); i++) {
129 SlotProperty_t *sp = &sp_array[i];
130 if (strcmp( sp->name, name ) == 0)
131 return i+1;
132 }
133 WARN(_("Slot property '%s' not found in array."), name);
134 return 0;
135}
136
140static int sp_check( unsigned int spid )
141{
142 if ((spid==0) || (spid > (unsigned int)array_size(sp_array)))
143 return 1;
144 return 0;
145}
146
150const char *sp_display( unsigned int spid )
151{
152 if (sp_check(spid))
153 return NULL;
154 return sp_array[ spid-1 ].display;
155}
156
160const char *sp_description( unsigned int spid )
161{
162 if (sp_check(spid))
163 return NULL;
164 return sp_array[ spid-1 ].description;
165}
166
170int sp_required( unsigned int spid )
171{
172 if (sp_check(spid))
173 return 0;
174 return sp_array[ spid-1 ].required;
175}
176
180int sp_exclusive( unsigned int spid )
181{
182 if (sp_check(spid))
183 return 0;
184 return sp_array[ spid-1 ].exclusive;
185}
186
190int sp_locked( unsigned int spid )
191{
192 if (sp_check(spid))
193 return 0;
194 return sp_array[ spid-1 ].locked;
195}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition: array.h:158
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition: array.h:168
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
Definition: array.h:119
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition: array.h:93
Header file with generic functions and naev-specifics.
char ** ndata_listRecursive(const char *path)
Lists all the visible files in a directory, at any depth.
Definition: ndata.c:232
xmlDocPtr xml_parsePhysFS(const char *filename)
Analogous to xmlParseMemory/xmlParseFile.
Definition: nxml.c:75
int sp_locked(unsigned int spid)
Gets whether or not a slot property is locked.
Definition: slots.c:190
unsigned int sp_get(const char *name)
Gets the id of a slot property.
Definition: slots.c:124
const char * sp_description(unsigned int spid)
Gets the description of a slot property (in English).
Definition: slots.c:160
void sp_cleanup(void)
Cleans up after the slot properties.
Definition: slots.c:106
static int sp_check(unsigned int spid)
Checks to see if in bound of array.
Definition: slots.c:140
int sp_load(void)
Initializes the slot properties.
Definition: slots.c:44
#define SP_XML_ID
Definition: slots.c:20
const char * sp_display(unsigned int spid)
Gets the display name of a slot property (in English).
Definition: slots.c:150
int sp_required(unsigned int spid)
Gets whether or not a slot property is required.
Definition: slots.c:170
int sp_exclusive(unsigned int spid)
Gets whether or not a slot property is exclusive.
Definition: slots.c:180
static SlotProperty_t * sp_array
Definition: slots.c:34
Representation of a slot property.
Definition: slots.c:25
char * description
Definition: slots.c:28
int required
Definition: slots.c:29
char * display
Definition: slots.c:27
int exclusive
Definition: slots.c:30
int locked
Definition: slots.c:31
char * name
Definition: slots.c:26