author | Medo <smaxein@googlemail.com> |
Wed, 27 Jun 2012 18:02:45 +0200 | |
changeset 7275 | 15f722e0b96f |
parent 7269 | 5b0aeef8ba2a |
child 7314 | 6171f0bad318 |
permissions | -rw-r--r-- |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
1 |
/** |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
2 |
* Simple dynamic array manipulation functions. |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
*/ |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
#ifndef LIST_H_ |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
#define LIST_H_ |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
#include <stddef.h> |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
9 |
#include <string.h> |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
10 |
#include <stdlib.h> |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
11 |
#include "util.h" |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
12 |
#include "logging.h" |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
/** |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
15 |
* 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
|
16 |
* 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
|
17 |
* 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
|
18 |
* 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
|
19 |
* 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
|
20 |
*/ |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
21 |
#define GENERATE_STATIC_LIST_INSERT(fname, type) \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
22 |
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
|
23 |
int result = -1; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
24 |
if(!listptr || !listSizePtr || pos < 0 || pos > *listSizePtr) { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
25 |
flib_log_e("Invalid parameter in "#fname); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
26 |
} else { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
27 |
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
|
28 |
if(newList) { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
29 |
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
|
30 |
newList[pos] = element; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
31 |
(*listSizePtr)++; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
32 |
*listptr = newList; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
33 |
result = 0; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
34 |
} \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
35 |
} \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
36 |
return result; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
37 |
} |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
|
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 |
* 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
|
41 |
* 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
|
42 |
* 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
|
43 |
* 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
|
44 |
* 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
|
45 |
*/ |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
46 |
#define GENERATE_STATIC_LIST_DELETE(fname, type) \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
47 |
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
|
48 |
int result = -1; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
49 |
if(!listPtr || !listSizePtr || pos < 0 || pos >= *listSizePtr) { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
50 |
flib_log_e("Invalid parameter in "#fname); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
51 |
} else { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
52 |
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
|
53 |
(*listSizePtr)--; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
54 |
\ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
55 |
size_t newCharSize = (*listSizePtr)*sizeof(type); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
56 |
type *newList = flib_realloc((*listPtr), newCharSize); \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
57 |
if(newList || newCharSize==0) { \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
58 |
(*listPtr) = newList; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
59 |
} /* 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
|
60 |
result = 0; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
61 |
} \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
62 |
return result; \ |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
63 |
} |
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
64 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
65 |
#endif /* LIST_H_ */ |