185 |
186 |
186 flib_weaponsetlist *flib_weaponsetlist_create() { |
187 flib_weaponsetlist *flib_weaponsetlist_create() { |
187 return flib_weaponsetlist_retain(flib_calloc(1, sizeof(flib_weaponsetlist))); |
188 return flib_weaponsetlist_retain(flib_calloc(1, sizeof(flib_weaponsetlist))); |
188 } |
189 } |
189 |
190 |
190 int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *weaponset, int pos) { |
191 int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *set, int pos) { |
191 int result = -1; |
192 flib_weaponset **changedList = flib_list_insert(list->weaponsets, &list->weaponsetCount, sizeof(*list->weaponsets), &set, pos); |
192 if(!list || !weaponset || pos < 0 || pos > list->weaponsetCount) { |
193 if(changedList) { |
193 flib_log_e("Invalid parameter in flib_weaponsetlist_insert"); |
194 list->weaponsets = changedList; |
194 } else { |
195 flib_weaponset_retain(set); |
195 flib_weaponset **newSets = flib_realloc(list->weaponsets, (list->weaponsetCount+1)*sizeof(*list->weaponsets)); |
196 return 0; |
196 if(newSets) { |
197 } else { |
197 list->weaponsets = newSets; |
198 return -1; |
198 memmove(list->weaponsets+pos+1, list->weaponsets+pos, (list->weaponsetCount-pos)*sizeof(*list->weaponsets)); |
199 } |
199 list->weaponsets[pos] = flib_weaponset_retain(weaponset); |
200 } |
200 list->weaponsetCount++; |
201 |
|
202 int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) { |
|
203 int result = -1; |
|
204 if(!list || pos<0 || pos>=list->weaponsetCount) { |
|
205 flib_log_e("Invalid parameter in flib_weaponsetlist_delete"); |
|
206 } else { |
|
207 flib_weaponset *elem = list->weaponsets[pos]; |
|
208 flib_weaponset **changedList = flib_list_delete(list->weaponsets, &list->weaponsetCount, sizeof(*list->weaponsets), pos); |
|
209 if(changedList || list->weaponsetCount==0) { |
|
210 list->weaponsets = changedList; |
|
211 flib_weaponset_release(elem); |
201 result = 0; |
212 result = 0; |
202 } |
213 } |
203 } |
|
204 return result; |
|
205 } |
|
206 |
|
207 int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) { |
|
208 int result = -1; |
|
209 if(!list || pos < 0 || pos >= list->weaponsetCount) { |
|
210 flib_log_e("Invalid parameter in flib_weaponsetlist_delete"); |
|
211 } else { |
|
212 flib_weaponset_release(list->weaponsets[pos]); |
|
213 memmove(list->weaponsets+pos, list->weaponsets+pos+1, (list->weaponsetCount-(pos+1))*sizeof(*list->weaponsets)); |
|
214 list->weaponsets[list->weaponsetCount-1] = NULL; |
|
215 list->weaponsetCount--; |
|
216 |
|
217 // If the realloc fails, just keep using the old buffer... |
|
218 flib_weaponset **newSets = flib_realloc(list->weaponsets, list->weaponsetCount*sizeof(*list->weaponsets)); |
|
219 if(newSets || list->weaponsetCount==1) { |
|
220 list->weaponsets = newSets; |
|
221 } |
|
222 result = 0; |
|
223 } |
214 } |
224 return result; |
215 return result; |
225 } |
216 } |
226 |
217 |
227 flib_weaponsetlist *flib_weaponsetlist_retain(flib_weaponsetlist *list) { |
218 flib_weaponsetlist *flib_weaponsetlist_retain(flib_weaponsetlist *list) { |