author | unc0rr |
Fri, 16 Aug 2013 14:15:00 +0400 | |
changeset 9393 | 094a176dd623 |
parent 7316 | f7b49b2c5d84 |
child 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7314
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
1 |
/* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
4 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
9 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
13 |
* GNU General Public License for more details. |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
14 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
18 |
*/ |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7275
diff
changeset
|
19 |
|
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
/** |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
* Simple dynamic array manipulation functions. |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
*/ |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
#ifndef LIST_H_ |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
#define LIST_H_ |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
#include <stddef.h> |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
28 |
#include <string.h> |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
29 |
#include <stdlib.h> |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
30 |
#include "util.h" |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
31 |
#include "logging.h" |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
/** |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
34 |
* Generate a static function that inserts a new value into a heap array of the given type, |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
35 |
* using realloc and memmove to increase the capacity and shift existing values. |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
36 |
* The function takes a pointer to the array variable and a pointer to the size variable |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
37 |
* because both can be changed by this operation (realloc / increment). |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
38 |
* The function returns 0 on success and leaves the array unchanged on error. |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
*/ |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
40 |
#define GENERATE_STATIC_LIST_INSERT(fname, type) \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
41 |
static int fname(type **listptr, int *listSizePtr, type element, int pos) { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
42 |
int result = -1; \ |
7316
f7b49b2c5d84
frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents:
7314
diff
changeset
|
43 |
if(!log_badargs_if4(listptr==NULL, listSizePtr==NULL, pos < 0, pos > *listSizePtr)) { \ |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
44 |
type *newList = flib_realloc(*listptr, ((*listSizePtr)+1)*sizeof(type)); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
45 |
if(newList) { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
46 |
memmove(newList + (pos+1), newList + pos, ((*listSizePtr)-pos)*sizeof(type)); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
47 |
newList[pos] = element; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
48 |
(*listSizePtr)++; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
49 |
*listptr = newList; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
50 |
result = 0; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
51 |
} \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
52 |
} \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
53 |
return result; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
54 |
} |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
55 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
56 |
/** |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
57 |
* Generate a static function that deletes a value from a heap array of the given type, |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
58 |
* using realloc and memmove to decrease the capacity and shift existing values. |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
59 |
* The function takes a pointer to the array variable and a pointer to the size variable |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
60 |
* because both can be changed by this operation (realloc / decrement). |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
61 |
* The function returns 0 on success and leaves the array unchanged on error. |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
62 |
*/ |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
63 |
#define GENERATE_STATIC_LIST_DELETE(fname, type) \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
64 |
static int fname(type **listPtr, int *listSizePtr, int pos) { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
65 |
int result = -1; \ |
7316
f7b49b2c5d84
frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents:
7314
diff
changeset
|
66 |
if(!log_badargs_if4(listPtr==NULL, listSizePtr==NULL, pos < 0, pos >= *listSizePtr)) { \ |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
67 |
memmove((*listPtr) + pos, (*listPtr) + (pos+1), ((*listSizePtr)-(pos+1))*sizeof(type)); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
68 |
(*listSizePtr)--; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
69 |
\ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
70 |
size_t newCharSize = (*listSizePtr)*sizeof(type); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
71 |
type *newList = flib_realloc((*listPtr), newCharSize); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
72 |
if(newList || newCharSize==0) { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
73 |
(*listPtr) = newList; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
74 |
} /* If the realloc fails, just keep using the old buffer...*/ \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
75 |
result = 0; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
76 |
} \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
77 |
return result; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
78 |
} |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
79 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
80 |
#endif /* LIST_H_ */ |