Skip to content

Commit e13e3e3

Browse files
committed
Initial commit
0 parents  commit e13e3e3

File tree

7 files changed

+387
-0
lines changed

7 files changed

+387
-0
lines changed

LICENSE.TXT

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright 2017 Artisan du Numérique. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7+
8+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
TOURNAMENT
2+
==========
3+
4+
Project just for fun

main.c

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*-
2+
* Copyright (c) 2017 Artisan du Numérique <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
#include <string.h>
29+
#include "tournament.h"
30+
31+
int main(int argc, char * argv[]) {
32+
int i = 0;
33+
const char * names[] = { "A", "B", "C", "D", "E", NULL };
34+
Opponent * opp;
35+
Pool * pool;
36+
37+
pool = pool_init(0);
38+
if(! is_null(pool)) {
39+
for(i = 0; !is_null(names[i]); i++) {
40+
opp = opponent_init(i, (char *)names[i]);
41+
if(!is_null(opp)) {
42+
printf("Add %s\n", opp->name);
43+
pool_add(pool, opp);
44+
}
45+
}
46+
any_dump(NULL, pool);
47+
any_free(pool);
48+
}
49+
50+
51+
return 0;
52+
}

opponent.c

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*-
2+
* Copyright (c) 2017 Artisan du Numérique <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
#include <assert.h>
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <string.h>
30+
#include "tournament.h"
31+
32+
Opponent * opponent_init(unsigned int id, char * name) {
33+
Opponent * opp = NULL;
34+
size_t name_len = 0;
35+
36+
assert(!is_null(name));
37+
38+
opp = calloc(1, sizeof(*opp));
39+
if(!is_null(opp)) {
40+
opp->_type = TOURNAMENT_TYPE_OPPONENT;
41+
opp->id = id;
42+
name_len = strlen(name) + 1;
43+
if(MAX_OPPONENT_NAME > 0 && name_len > MAX_OPPONENT_NAME) {
44+
name_len = MAX_OPPONENT_NAME;
45+
}
46+
opp->name = calloc(name_len, sizeof(*(opp->name)));
47+
if(is_null(opp->name)) {
48+
null(opp);
49+
} else {
50+
strncpy(opp->name, name, name_len);
51+
opp->name[name_len - 1] = '\0';
52+
}
53+
}
54+
55+
return opp;
56+
}
57+
58+
void opponent_dump(FILE * stream, Opponent * opp) {
59+
assert(!is_null(opp));
60+
61+
if(is_null(stream)) {
62+
stream = stdout;
63+
}
64+
65+
fprintf(stream, "Opponent %d : %s\n", opp->id, opp->name);
66+
67+
return;
68+
}
69+
70+
/* Return always NULL */
71+
void * opponent_free(Opponent * opp) {
72+
assert(!is_null(opp));
73+
74+
if(!is_null(opp->name)) {
75+
null(opp->name);
76+
}
77+
null(opp);
78+
79+
return NULL;
80+
}

pool.c

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*-
2+
* Copyright (c) 2017 Artisan du Numérique <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
#include <assert.h>
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include "tournament.h"
30+
31+
32+
Pool * pool_init(unsigned int id) {
33+
Pool * pool = NULL;
34+
35+
pool = calloc(1, sizeof(*pool));
36+
if(!is_null(pool)) {
37+
pool->_type = TOURNAMENT_TYPE_POOL;
38+
pool->id = id;
39+
pool->meetings = NULL;
40+
pool->opponents = NULL;
41+
pool->opp_count = 0;
42+
}
43+
44+
return pool;
45+
}
46+
47+
int pool_add(Pool * pool, Opponent * op) {
48+
void * tmp = NULL;
49+
50+
assert(!is_null(pool));
51+
assert(!is_null(op));
52+
53+
tmp = realloc(pool->opponents, sizeof(*(pool->opponents)) * (pool->opp_count + 1));
54+
if(!is_null(tmp)) {
55+
pool->opponents = (Opponent **)tmp;
56+
pool->opponents[pool->opp_count] = op;
57+
pool->opp_count++;
58+
return 1;
59+
}
60+
61+
return 0;
62+
}
63+
64+
void pool_dump(FILE * stream, Pool * pool) {
65+
int i = 0;
66+
assert(!is_null(pool));
67+
68+
if(is_null(stream)) {
69+
stream = stdout;
70+
}
71+
72+
fprintf(stream, "<POOL %d>\n", pool->id);
73+
fprintf(stream, "Opponents %d\n", pool->opp_count);
74+
for(i = 0; i < pool->opp_count; i++) {
75+
any_dump(stream, pool->opponents[i]);
76+
}
77+
fprintf(stream, "</POOL>\n");
78+
79+
return;
80+
}
81+
82+
/* Return always NULL */
83+
void * pool_free(Pool * pool) {
84+
int i = 0;
85+
86+
assert(!is_null(pool));
87+
88+
if(!is_null(pool->opponents)) {
89+
for(i = 0; i < pool->opp_count; i++) {
90+
if(pool->opponents[i] != NULL) {
91+
pool->opponents[i] = any_free(pool->opponents[i]);
92+
}
93+
}
94+
pool->opp_count = 0;
95+
null(pool->opponents);
96+
}
97+
98+
null(pool);
99+
return NULL;
100+
}
101+

tournament.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*-
2+
* Copyright (c) 2017 Artisan du Numérique <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
#include <assert.h>
27+
#include <stdlib.h>
28+
#include "tournament.h"
29+
30+
/* Return always NULL */
31+
void * any_free(void * any) {
32+
assert(!is_null(any));
33+
34+
switch(*(unsigned short *)any) {
35+
case TOURNAMENT_TYPE_OPPONENT: opponent_free(any); break;
36+
case TOURNAMENT_TYPE_POOL: pool_free(any); break;
37+
default: null(any); break;
38+
}
39+
40+
return NULL;
41+
}
42+
43+
void any_dump(FILE * stream, void * any) {
44+
assert(!is_null(any));
45+
46+
if(is_null(stream)) {
47+
stream = stdout;
48+
}
49+
50+
switch(*(unsigned short *)any) {
51+
case TOURNAMENT_TYPE_OPPONENT: opponent_dump(stream, any); break;
52+
case TOURNAMENT_TYPE_POOL: pool_dump(stream, any); break;
53+
default: printf("%p\n", any); break;
54+
}
55+
}

tournament.h

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*-
2+
* Copyright (c) 2017 Artisan du Numérique <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
#ifndef TOURNAMENT_H__
27+
#define TOURNAMENT_H__
28+
29+
#define MAX_OPPONENT_NAME 50
30+
31+
#define TOURNAMENT_TYPE_POOL 0xA001
32+
#define TOURNAMENT_TYPE_OPPONENT 0xA002
33+
34+
#ifndef is_null
35+
#define is_null(A) ((A) == NULL)
36+
#endif /* is_null */
37+
38+
#ifndef null
39+
#define null(A) free(A); (A) = NULL;
40+
#endif /* null */
41+
42+
43+
#include <stdio.h>
44+
45+
typedef struct {
46+
unsigned short _type; /* must be first in struct */
47+
unsigned int id;
48+
unsigned int count;
49+
char * name;
50+
void * any;
51+
} Opponent;
52+
53+
typedef struct {
54+
unsigned short _type; /* must be first in struct */
55+
unsigned int id;
56+
Opponent * white;
57+
Opponent * blue;
58+
void * any;
59+
60+
void * next;
61+
} Meeting;
62+
63+
typedef struct {
64+
unsigned short _type; /* must be first in struct */
65+
unsigned int id;
66+
Meeting * meetings;
67+
Opponent ** opponents;
68+
size_t opp_count;
69+
} Pool;
70+
71+
/* Free anything based on first unsigned int of the struct */
72+
void * any_free(void *);
73+
void any_dump(FILE * stream, void * any);
74+
75+
76+
/* *** Pool function *** */
77+
Pool * pool_init(unsigned int);
78+
int pool_add(Pool *, Opponent *);
79+
void * pool_free(Pool *);
80+
void pool_dump(FILE *, Pool *);
81+
82+
/* *** Opponent function *** */
83+
Opponent * opponent_init(unsigned int, char *);
84+
void * opponent_free(Opponent *);
85+
void opponent_dump(FILE *, Opponent *);
86+
87+
#endif /* TOURNAMENT_H__ */

0 commit comments

Comments
 (0)