diff options
| author | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-09-21 21:25:50 -0500 |
|---|---|---|
| committer | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-09-21 21:25:50 -0500 |
| commit | 535fc291b68534d4da53c894d9f84cb185de72d5 (patch) | |
| tree | 8187b6a40cb704402184b626d5a7fd7a37baa30a | |
| parent | f4cb6a5879a7be0bbc15a2a04eb4977248316ad1 (diff) | |
| download | life-535fc291b68534d4da53c894d9f84cb185de72d5.tar.gz life-535fc291b68534d4da53c894d9f84cb185de72d5.tar.bz2 life-535fc291b68534d4da53c894d9f84cb185de72d5.zip | |
Abuse the preprocessor instead of using typeof
Add -Wextra to CFLAGS
Created foreach.h with the "magic" macros
Update calls in life.c
Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
| -rw-r--r-- | Makefile.am | 2 | ||||
| -rw-r--r-- | Makefile.in | 2 | ||||
| -rw-r--r-- | src/foreach.h | 24 | ||||
| -rw-r--r-- | src/life-macros.h | 7 | ||||
| -rw-r--r-- | src/life.c | 20 |
5 files changed, 37 insertions, 18 deletions
diff --git a/Makefile.am b/Makefile.am index 73f1665..a4f03dd 100644 --- a/Makefile.am +++ b/Makefile.am | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | AM_OPTIONS = gnu | 1 | AM_OPTIONS = gnu |
| 2 | AM_CFLAGS = -Wall -pedantic | 2 | AM_CFLAGS = -Wall -Wextra -pedantic |
| 3 | AM_CPPFLAGS = -I$(srcdir) -I. | 3 | AM_CPPFLAGS = -I$(srcdir) -I. |
| 4 | man_MANS = docs/life.6 | 4 | man_MANS = docs/life.6 |
| 5 | EXTRA_DIST = $(man_MANS) | 5 | EXTRA_DIST = $(man_MANS) |
diff --git a/Makefile.in b/Makefile.in index bda2bbf..44c7af4 100644 --- a/Makefile.in +++ b/Makefile.in | |||
| @@ -320,7 +320,7 @@ top_build_prefix = @top_build_prefix@ | |||
| 320 | top_builddir = @top_builddir@ | 320 | top_builddir = @top_builddir@ |
| 321 | top_srcdir = @top_srcdir@ | 321 | top_srcdir = @top_srcdir@ |
| 322 | AM_OPTIONS = gnu | 322 | AM_OPTIONS = gnu |
| 323 | AM_CFLAGS = -Wall -pedantic | 323 | AM_CFLAGS = -Wall -Wextra -pedantic |
| 324 | AM_CPPFLAGS = -I$(srcdir) -I. | 324 | AM_CPPFLAGS = -I$(srcdir) -I. |
| 325 | man_MANS = docs/life.6 | 325 | man_MANS = docs/life.6 |
| 326 | EXTRA_DIST = $(man_MANS) | 326 | EXTRA_DIST = $(man_MANS) |
diff --git a/src/foreach.h b/src/foreach.h new file mode 100644 index 0000000..e684b93 --- /dev/null +++ b/src/foreach.h | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | #ifndef NARG | ||
| 2 | #define NARG(...) NARG_(__VA_ARGS__, RSEQ_N) | ||
| 3 | #define NARG_(...) NARG_N(__VA_ARGS__) | ||
| 4 | #define RSEQ_N 8, 7, 6, 5, 4, 3, 2, 1, 0 | ||
| 5 | #define NARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N | ||
| 6 | #endif | ||
| 7 | |||
| 8 | #ifndef foreach | ||
| 9 | #define FOREACH_0(func) } while (0) | ||
| 10 | #define FOREACH_1(func, first) func(first); FOREACH_0(func) | ||
| 11 | #define FOREACH_2(func, first,...) func(first); FOREACH_1(func, __VA_ARGS__) | ||
| 12 | #define FOREACH_3(func, first,...) func(first); FOREACH_2(func, __VA_ARGS__) | ||
| 13 | #define FOREACH_4(func, first,...) func(first); FOREACH_3(func, __VA_ARGS__) | ||
| 14 | #define FOREACH_5(func, first,...) func(first); FOREACH_4(func, __VA_ARGS__) | ||
| 15 | #define FOREACH_6(func, first,...) func(first); FOREACH_5(func, __VA_ARGS__) | ||
| 16 | #define FOREACH_7(func, first,...) func(first); FOREACH_6(func, __VA_ARGS__) | ||
| 17 | #define FOREACH_8(func, first,...) func(first); FOREACH_7(func, __VA_ARGS__) | ||
| 18 | |||
| 19 | #define CONCAT1(x, y) x##y | ||
| 20 | #define CONCATENATE(x, y) CONCAT1(x, y) | ||
| 21 | |||
| 22 | #define foreach(func, ...) do { CONCATENATE(FOREACH_,NARG(__VA_ARGS__))(func, __VA_ARGS__) | ||
| 23 | #endif | ||
| 24 | |||
diff --git a/src/life-macros.h b/src/life-macros.h index 32eb3f0..f2a6a8b 100644 --- a/src/life-macros.h +++ b/src/life-macros.h | |||
| @@ -2,9 +2,4 @@ | |||
| 2 | #define has(var, bit) (((var) & (bit)) == (bit)) | 2 | #define has(var, bit) (((var) & (bit)) == (bit)) |
| 3 | #endif | 3 | #endif |
| 4 | 4 | ||
| 5 | #ifndef foreach | 5 | #include "foreach.h" |
| 6 | #define foreach(func, count, first,...) \ | ||
| 7 | {typeof(first) things[] = {__VA_ARGS__}; \ | ||
| 8 | func(first); \ | ||
| 9 | for (int _i = 0; _i < count - 1; _i++) func(things[_i]);} | ||
| 10 | #endif | ||
| @@ -127,7 +127,7 @@ int main (int argc, char * argv[]) { | |||
| 127 | x = width >> 2; | 127 | x = width >> 2; |
| 128 | y = height >> 2; | 128 | y = height >> 2; |
| 129 | while (ch != 'q') { | 129 | while (ch != 'q') { |
| 130 | foreach(werase, 4, stdscr, board, stat_bar, entry); | 130 | foreach(werase, stdscr, board, stat_bar, entry); |
| 131 | if (playing == FALSE) { | 131 | if (playing == FALSE) { |
| 132 | // Numpad and arrow directions | 132 | // Numpad and arrow directions |
| 133 | switch(ch) { | 133 | switch(ch) { |
| @@ -189,26 +189,26 @@ int main (int argc, char * argv[]) { | |||
| 189 | break; | 189 | break; |
| 190 | case 'h': | 190 | case 'h': |
| 191 | wtimeout(board, -1); | 191 | wtimeout(board, -1); |
| 192 | foreach(werase, 3, stdscr, board, stat_bar); | 192 | foreach(werase, stdscr, board, stat_bar); |
| 193 | stat_bar_print(stat_bar, "Press any key to return"); | 193 | stat_bar_print(stat_bar, "Press any key to return"); |
| 194 | print_help(board); | 194 | print_help(board); |
| 195 | foreach(wnoutrefresh, 3, stdscr, board, stat_bar); | 195 | foreach(wnoutrefresh, stdscr, board, stat_bar); |
| 196 | doupdate(); | 196 | doupdate(); |
| 197 | getch(); | 197 | getch(); |
| 198 | wtimeout(board, timeout_val); | 198 | wtimeout(board, timeout_val); |
| 199 | break; | 199 | break; |
| 200 | case 'w': | 200 | case 'w': |
| 201 | wtimeout(board, -1); | 201 | wtimeout(board, -1); |
| 202 | foreach(werase, 4, stdscr, board, stat_bar, entry); | 202 | foreach(werase, stdscr, board, stat_bar, entry); |
| 203 | stat_bar_print(stat_bar, "Press any key to return"); | 203 | stat_bar_print(stat_bar, "Press any key to return"); |
| 204 | print_copying_warranty(board); | 204 | print_copying_warranty(board); |
| 205 | foreach(wnoutrefresh, 3, stdscr, entry, stat_bar); | 205 | foreach(wnoutrefresh, stdscr, entry, stat_bar); |
| 206 | doupdate(); | 206 | doupdate(); |
| 207 | getch(); | 207 | getch(); |
| 208 | wtimeout(board, timeout_val); | 208 | wtimeout(board, timeout_val); |
| 209 | break; | 209 | break; |
| 210 | case CTRL('r'): | 210 | case CTRL('r'): |
| 211 | foreach(werase, 4, stdscr, board, stat_bar, entry); | 211 | foreach(werase, stdscr, board, stat_bar, entry); |
| 212 | stat_bar_print(stat_bar, "Click Cancel to exit or Done to exit and save rule"); | 212 | stat_bar_print(stat_bar, "Click Cancel to exit or Done to exit and save rule"); |
| 213 | wrefresh(stat_bar); | 213 | wrefresh(stat_bar); |
| 214 | wrefresh(rule_entry_box); | 214 | wrefresh(rule_entry_box); |
| @@ -234,14 +234,14 @@ int main (int argc, char * argv[]) { | |||
| 234 | } else if (ch == '.') { | 234 | } else if (ch == '.') { |
| 235 | delaymax += (delaymax < 20 ? 1 : 0); | 235 | delaymax += (delaymax < 20 ? 1 : 0); |
| 236 | } else if (ch== CTRL('l')) { | 236 | } else if (ch== CTRL('l')) { |
| 237 | foreach(wclear, 4, stdscr, entry, stat_bar, board); | 237 | foreach(wclear, stdscr, entry, stat_bar, board); |
| 238 | } | 238 | } |
| 239 | for (int i = 0; i < height; ++i) { | 239 | for (int i = 0; i < height; ++i) { |
| 240 | mvwaddnstr(board, i, 0, map + (i * width), width); | 240 | mvwaddnstr(board, i, 0, map + (i * width), width); |
| 241 | } | 241 | } |
| 242 | mvwchgat(board, y, x, 1, A_STANDOUT, COLOR_PAIR(0), NULL); | 242 | mvwchgat(board, y, x, 1, A_STANDOUT, COLOR_PAIR(0), NULL); |
| 243 | stat_bar_print(stat_bar, "(%d, %d)\t\tGeneration: %d\t\tDelay Time: %d", x, y, generation, delaymax); | 243 | stat_bar_print(stat_bar, "(%d, %d)\t\tGeneration: %d\t\tDelay Time: %d", x, y, generation, delaymax); |
| 244 | foreach(wnoutrefresh, 4, stdscr, board, entry, stat_bar); | 244 | foreach(wnoutrefresh, stdscr, board, entry, stat_bar); |
| 245 | doupdate(); | 245 | doupdate(); |
| 246 | if (playing == TRUE) { | 246 | if (playing == TRUE) { |
| 247 | delay++; | 247 | delay++; |
| @@ -254,8 +254,8 @@ int main (int argc, char * argv[]) { | |||
| 254 | ch = wgetch(board); | 254 | ch = wgetch(board); |
| 255 | ch = tolower(ch); | 255 | ch = tolower(ch); |
| 256 | } | 256 | } |
| 257 | foreach(werase, 6, stdscr, board, stat_bar, entry, rule_entry, rule_entry_box); | 257 | foreach(werase, stdscr, board, stat_bar, entry, rule_entry, rule_entry_box); |
| 258 | foreach(delwin, 5, entry, stat_bar, board, rule_entry, rule_entry_box); | 258 | foreach(delwin, entry, stat_bar, board, rule_entry, rule_entry_box); |
| 259 | board = stat_bar = entry = rule_entry = rule_entry_box = NULL; | 259 | board = stat_bar = entry = rule_entry = rule_entry_box = NULL; |
| 260 | endwin(); | 260 | endwin(); |
| 261 | delwin(stdscr); | 261 | delwin(stdscr); |
