diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-21 10:33:46 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-21 10:33:46 +0200 |
| commit | 1d9b8acfeece68c4f55d2499d8aed127f98a383c (patch) | |
| tree | d520e8726a0c37af5738779847c2da942a05cf64 | |
| parent | f1575ed335166a0897f2cfd314a0f4bade6a10fc (diff) | |
| download | nissy-core-1d9b8acfeece68c4f55d2499d8aed127f98a383c.tar.gz nissy-core-1d9b8acfeece68c4f55d2499d8aed127f98a383c.zip | |
Merged some files, renamed another
| -rw-r--r-- | src/core/core.h | 4 | ||||
| -rw-r--r-- | src/core/io_formats.h (renamed from src/core/io_cube.h) | 0 | ||||
| -rw-r--r-- | src/core/io_moves.h | 127 | ||||
| -rw-r--r-- | src/core/io_trans.h | 24 | ||||
| -rw-r--r-- | src/core/moves.h | 128 | ||||
| -rw-r--r-- | src/core/transform.h | 25 |
6 files changed, 154 insertions, 154 deletions
diff --git a/src/core/core.h b/src/core/core.h index f041178..de1c198 100644 --- a/src/core/core.h +++ b/src/core/core.h | |||
| @@ -1,7 +1,5 @@ | |||
| 1 | #include "constant_cubes.h" | 1 | #include "constant_cubes.h" |
| 2 | #include "io_moves.h" | ||
| 3 | #include "io_trans.h" | ||
| 4 | #include "cube.h" | 2 | #include "cube.h" |
| 5 | #include "io_cube.h" | 3 | #include "io_formats.h" |
| 6 | #include "moves.h" | 4 | #include "moves.h" |
| 7 | #include "transform.h" | 5 | #include "transform.h" |
diff --git a/src/core/io_cube.h b/src/core/io_formats.h index f5668d4..f5668d4 100644 --- a/src/core/io_cube.h +++ b/src/core/io_formats.h | |||
diff --git a/src/core/io_moves.h b/src/core/io_moves.h deleted file mode 100644 index 97926cc..0000000 --- a/src/core/io_moves.h +++ /dev/null | |||
| @@ -1,127 +0,0 @@ | |||
| 1 | STATIC uint8_t readmove(char); | ||
| 2 | STATIC int64_t readmoves(const char *, size_t n, uint8_t [n]); | ||
| 3 | STATIC int64_t countmoves(const char *); | ||
| 4 | STATIC uint8_t readmodifier(char); | ||
| 5 | STATIC int64_t writemoves(size_t n, const uint8_t [n], size_t m, char [m]); | ||
| 6 | |||
| 7 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ | ||
| 8 | RET_ERROR, ARG_ACTION) \ | ||
| 9 | const char *VAR_B; \ | ||
| 10 | uint8_t VAR_MOVE_NOMOD, VAR_MOD; \ | ||
| 11 | for (VAR_B = ARG_BUF, ARG_C = 0; *VAR_B != '\0'; VAR_B++, ARG_C++) { \ | ||
| 12 | while (*VAR_B == ' ' || *VAR_B == '\t' || *VAR_B == '\n') \ | ||
| 13 | VAR_B++; \ | ||
| 14 | if (*VAR_B == '\0' || ARG_C == ARG_MAX) \ | ||
| 15 | break; \ | ||
| 16 | if ((VAR_MOVE_NOMOD = readmove(*VAR_B)) == UINT8_ERROR) { \ | ||
| 17 | LOG("Unknown move: %c\n", *VAR_B); \ | ||
| 18 | return RET_ERROR; \ | ||
| 19 | } \ | ||
| 20 | if ((VAR_MOD = readmodifier(*(VAR_B+1))) != 0) \ | ||
| 21 | VAR_B++; \ | ||
| 22 | ARG_MOVE = VAR_MOVE_NOMOD + VAR_MOD; \ | ||
| 23 | ARG_ACTION \ | ||
| 24 | } | ||
| 25 | |||
| 26 | STATIC uint8_t | ||
| 27 | readmove(char c) | ||
| 28 | { | ||
| 29 | switch (c) { | ||
| 30 | case 'U': | ||
| 31 | return MOVE_U; | ||
| 32 | case 'D': | ||
| 33 | return MOVE_D; | ||
| 34 | case 'R': | ||
| 35 | return MOVE_R; | ||
| 36 | case 'L': | ||
| 37 | return MOVE_L; | ||
| 38 | case 'F': | ||
| 39 | return MOVE_F; | ||
| 40 | case 'B': | ||
| 41 | return MOVE_B; | ||
| 42 | default: | ||
| 43 | return UINT8_ERROR; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | STATIC uint8_t | ||
| 48 | readmodifier(char c) | ||
| 49 | { | ||
| 50 | switch (c) { | ||
| 51 | case '1': /* Fallthrough */ | ||
| 52 | case '2': /* Fallthrough */ | ||
| 53 | case '3': | ||
| 54 | return c - '0' - 1; | ||
| 55 | case '\'': | ||
| 56 | return 2; | ||
| 57 | default: | ||
| 58 | return 0; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | STATIC int64_t | ||
| 63 | readmoves(const char *buf, size_t n, uint8_t ret[n]) | ||
| 64 | { | ||
| 65 | uint8_t m; | ||
| 66 | uint64_t c; | ||
| 67 | |||
| 68 | FOREACH_READMOVE(buf, m, c, n, NISSY_ERROR_INVALID_MOVES, | ||
| 69 | ret[c] = m; | ||
| 70 | ) | ||
| 71 | |||
| 72 | return (int64_t)c; | ||
| 73 | } | ||
| 74 | |||
| 75 | STATIC int64_t | ||
| 76 | countmoves(const char *buf) | ||
| 77 | { | ||
| 78 | uint8_t m; | ||
| 79 | uint64_t c; | ||
| 80 | |||
| 81 | FOREACH_READMOVE(buf, m, c, INT_MAX, NISSY_ERROR_INVALID_MOVES, | ||
| 82 | {} | ||
| 83 | ) | ||
| 84 | |||
| 85 | (void)m; /* Ignore "variable set but not used" warning */ | ||
| 86 | |||
| 87 | return (int64_t)c; | ||
| 88 | } | ||
| 89 | |||
| 90 | STATIC int64_t | ||
| 91 | writemoves( | ||
| 92 | size_t nmoves, | ||
| 93 | const uint8_t m[nmoves], | ||
| 94 | size_t buf_size, | ||
| 95 | char buf[buf_size] | ||
| 96 | ) | ||
| 97 | { | ||
| 98 | size_t i, len, w; | ||
| 99 | const char *s; | ||
| 100 | |||
| 101 | if (buf_size == 0) { | ||
| 102 | LOG("Error: cannot write moves to buffer of size 0.\n"); | ||
| 103 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 104 | } | ||
| 105 | |||
| 106 | for (i = 0, w = 0; i < nmoves; i++, w++) { | ||
| 107 | s = movestr[m[i]]; | ||
| 108 | len = strlen(s); | ||
| 109 | if (len + w >= buf_size) { | ||
| 110 | LOG("Error: the given buffer is too small for " | ||
| 111 | "writing the given moves.\n"); | ||
| 112 | goto writemoves_error; | ||
| 113 | } | ||
| 114 | memcpy(buf+w, s, len); | ||
| 115 | w += len; | ||
| 116 | buf[w] = ' '; | ||
| 117 | } | ||
| 118 | |||
| 119 | if (w > 0) w--; /* Remove last space */ | ||
| 120 | buf[w] = '\0'; | ||
| 121 | |||
| 122 | return (int64_t)w; | ||
| 123 | |||
| 124 | writemoves_error: | ||
| 125 | *buf = '\0'; | ||
| 126 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 127 | } | ||
diff --git a/src/core/io_trans.h b/src/core/io_trans.h deleted file mode 100644 index 71b2287..0000000 --- a/src/core/io_trans.h +++ /dev/null | |||
| @@ -1,24 +0,0 @@ | |||
| 1 | STATIC uint8_t readtrans(const char [static NISSY_SIZE_TRANSFORMATION]); | ||
| 2 | STATIC void writetrans(uint8_t, char [static NISSY_SIZE_TRANSFORMATION]); | ||
| 3 | |||
| 4 | STATIC uint8_t | ||
| 5 | readtrans(const char buf[static NISSY_SIZE_TRANSFORMATION]) | ||
| 6 | { | ||
| 7 | uint8_t t; | ||
| 8 | |||
| 9 | for (t = 0; t < NTRANS; t++) | ||
| 10 | if (!strncmp(buf, transstr[t], 11)) | ||
| 11 | return t; | ||
| 12 | |||
| 13 | return UINT8_ERROR; | ||
| 14 | } | ||
| 15 | |||
| 16 | STATIC void | ||
| 17 | writetrans(uint8_t t, char buf[static NISSY_SIZE_TRANSFORMATION]) | ||
| 18 | { | ||
| 19 | if (t >= 48) | ||
| 20 | memcpy(buf, "error trans", 11); | ||
| 21 | else | ||
| 22 | memcpy(buf, transstr[t], 11); | ||
| 23 | buf[11] = '\0'; | ||
| 24 | } | ||
diff --git a/src/core/moves.h b/src/core/moves.h index cc91706..8f91167 100644 --- a/src/core/moves.h +++ b/src/core/moves.h | |||
| @@ -1,6 +1,12 @@ | |||
| 1 | #define MOVE(M, c) compose(c, MOVE_CUBE_ ## M) | 1 | #define MOVE(M, c) compose(c, MOVE_CUBE_ ## M) |
| 2 | #define PREMOVE(M, c) compose(MOVE_CUBE_ ## M, c) | 2 | #define PREMOVE(M, c) compose(MOVE_CUBE_ ## M, c) |
| 3 | 3 | ||
| 4 | STATIC uint8_t readmove(char); | ||
| 5 | STATIC int64_t readmoves(const char *, size_t n, uint8_t [n]); | ||
| 6 | STATIC int64_t countmoves(const char *); | ||
| 7 | STATIC uint8_t readmodifier(char); | ||
| 8 | STATIC int64_t writemoves(size_t n, const uint8_t [n], size_t m, char [m]); | ||
| 9 | |||
| 4 | STATIC_INLINE bool allowednextmove(uint8_t, uint8_t); | 10 | STATIC_INLINE bool allowednextmove(uint8_t, uint8_t); |
| 5 | STATIC bool allowedmoves(size_t n, const uint8_t [n]); | 11 | STATIC bool allowedmoves(size_t n, const uint8_t [n]); |
| 6 | 12 | ||
| @@ -17,6 +23,128 @@ STATIC bool are_lastmoves_singlecw(size_t n, const uint8_t [n]); | |||
| 17 | 23 | ||
| 18 | STATIC cube_t applymoves(cube_t, const char *); | 24 | STATIC cube_t applymoves(cube_t, const char *); |
| 19 | 25 | ||
| 26 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ | ||
| 27 | RET_ERROR, ARG_ACTION) \ | ||
| 28 | const char *VAR_B; \ | ||
| 29 | uint8_t VAR_MOVE_NOMOD, VAR_MOD; \ | ||
| 30 | for (VAR_B = ARG_BUF, ARG_C = 0; *VAR_B != '\0'; VAR_B++, ARG_C++) { \ | ||
| 31 | while (*VAR_B == ' ' || *VAR_B == '\t' || *VAR_B == '\n') \ | ||
| 32 | VAR_B++; \ | ||
| 33 | if (*VAR_B == '\0' || ARG_C == ARG_MAX) \ | ||
| 34 | break; \ | ||
| 35 | if ((VAR_MOVE_NOMOD = readmove(*VAR_B)) == UINT8_ERROR) { \ | ||
| 36 | LOG("Unknown move: %c\n", *VAR_B); \ | ||
| 37 | return RET_ERROR; \ | ||
| 38 | } \ | ||
| 39 | if ((VAR_MOD = readmodifier(*(VAR_B+1))) != 0) \ | ||
| 40 | VAR_B++; \ | ||
| 41 | ARG_MOVE = VAR_MOVE_NOMOD + VAR_MOD; \ | ||
| 42 | ARG_ACTION \ | ||
| 43 | } | ||
| 44 | |||
| 45 | STATIC uint8_t | ||
| 46 | readmove(char c) | ||
| 47 | { | ||
| 48 | switch (c) { | ||
| 49 | case 'U': | ||
| 50 | return MOVE_U; | ||
| 51 | case 'D': | ||
| 52 | return MOVE_D; | ||
| 53 | case 'R': | ||
| 54 | return MOVE_R; | ||
| 55 | case 'L': | ||
| 56 | return MOVE_L; | ||
| 57 | case 'F': | ||
| 58 | return MOVE_F; | ||
| 59 | case 'B': | ||
| 60 | return MOVE_B; | ||
| 61 | default: | ||
| 62 | return UINT8_ERROR; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | STATIC uint8_t | ||
| 67 | readmodifier(char c) | ||
| 68 | { | ||
| 69 | switch (c) { | ||
| 70 | case '1': /* Fallthrough */ | ||
| 71 | case '2': /* Fallthrough */ | ||
| 72 | case '3': | ||
| 73 | return c - '0' - 1; | ||
| 74 | case '\'': | ||
| 75 | return 2; | ||
| 76 | default: | ||
| 77 | return 0; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | STATIC int64_t | ||
| 82 | readmoves(const char *buf, size_t n, uint8_t ret[n]) | ||
| 83 | { | ||
| 84 | uint8_t m; | ||
| 85 | uint64_t c; | ||
| 86 | |||
| 87 | FOREACH_READMOVE(buf, m, c, n, NISSY_ERROR_INVALID_MOVES, | ||
| 88 | ret[c] = m; | ||
| 89 | ) | ||
| 90 | |||
| 91 | return (int64_t)c; | ||
| 92 | } | ||
| 93 | |||
| 94 | STATIC int64_t | ||
| 95 | countmoves(const char *buf) | ||
| 96 | { | ||
| 97 | uint8_t m; | ||
| 98 | uint64_t c; | ||
| 99 | |||
| 100 | FOREACH_READMOVE(buf, m, c, INT_MAX, NISSY_ERROR_INVALID_MOVES, | ||
| 101 | {} | ||
| 102 | ) | ||
| 103 | |||
| 104 | (void)m; /* Ignore "variable set but not used" warning */ | ||
| 105 | |||
| 106 | return (int64_t)c; | ||
| 107 | } | ||
| 108 | |||
| 109 | STATIC int64_t | ||
| 110 | writemoves( | ||
| 111 | size_t nmoves, | ||
| 112 | const uint8_t m[nmoves], | ||
| 113 | size_t buf_size, | ||
| 114 | char buf[buf_size] | ||
| 115 | ) | ||
| 116 | { | ||
| 117 | size_t i, len, w; | ||
| 118 | const char *s; | ||
| 119 | |||
| 120 | if (buf_size == 0) { | ||
| 121 | LOG("Error: cannot write moves to buffer of size 0.\n"); | ||
| 122 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 123 | } | ||
| 124 | |||
| 125 | for (i = 0, w = 0; i < nmoves; i++, w++) { | ||
| 126 | s = movestr[m[i]]; | ||
| 127 | len = strlen(s); | ||
| 128 | if (len + w >= buf_size) { | ||
| 129 | LOG("Error: the given buffer is too small for " | ||
| 130 | "writing the given moves.\n"); | ||
| 131 | goto writemoves_error; | ||
| 132 | } | ||
| 133 | memcpy(buf+w, s, len); | ||
| 134 | w += len; | ||
| 135 | buf[w] = ' '; | ||
| 136 | } | ||
| 137 | |||
| 138 | if (w > 0) w--; /* Remove last space */ | ||
| 139 | buf[w] = '\0'; | ||
| 140 | |||
| 141 | return (int64_t)w; | ||
| 142 | |||
| 143 | writemoves_error: | ||
| 144 | *buf = '\0'; | ||
| 145 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 146 | } | ||
| 147 | |||
| 20 | STATIC_INLINE bool | 148 | STATIC_INLINE bool |
| 21 | allowednextmove(uint8_t m1, uint8_t m2) | 149 | allowednextmove(uint8_t m1, uint8_t m2) |
| 22 | { | 150 | { |
diff --git a/src/core/transform.h b/src/core/transform.h index b480593..28b8d93 100644 --- a/src/core/transform.h +++ b/src/core/transform.h | |||
| @@ -17,6 +17,9 @@ | |||
| 17 | invertco(compose(compose(TRANS_CUBE_ ## T, c), \ | 17 | invertco(compose(compose(TRANS_CUBE_ ## T, c), \ |
| 18 | TRANS_CUBE_ ## T ## _INVERSE)) | 18 | TRANS_CUBE_ ## T ## _INVERSE)) |
| 19 | 19 | ||
| 20 | STATIC uint8_t readtrans(const char [static NISSY_SIZE_TRANSFORMATION]); | ||
| 21 | STATIC void writetrans(uint8_t, char [static NISSY_SIZE_TRANSFORMATION]); | ||
| 22 | |||
| 20 | STATIC cube_t transform_edges(cube_t, uint8_t); | 23 | STATIC cube_t transform_edges(cube_t, uint8_t); |
| 21 | STATIC cube_t transform_corners(cube_t, uint8_t); | 24 | STATIC cube_t transform_corners(cube_t, uint8_t); |
| 22 | STATIC cube_t transform(cube_t, uint8_t); | 25 | STATIC cube_t transform(cube_t, uint8_t); |
| @@ -25,6 +28,28 @@ STATIC_INLINE uint8_t inverse_trans(uint8_t); | |||
| 25 | STATIC uint8_t transform_move(uint8_t, uint8_t); | 28 | STATIC uint8_t transform_move(uint8_t, uint8_t); |
| 26 | STATIC uint64_t symmetry_mask(cube_t); | 29 | STATIC uint64_t symmetry_mask(cube_t); |
| 27 | 30 | ||
| 31 | STATIC uint8_t | ||
| 32 | readtrans(const char buf[static NISSY_SIZE_TRANSFORMATION]) | ||
| 33 | { | ||
| 34 | uint8_t t; | ||
| 35 | |||
| 36 | for (t = 0; t < NTRANS; t++) | ||
| 37 | if (!strncmp(buf, transstr[t], 11)) | ||
| 38 | return t; | ||
| 39 | |||
| 40 | return UINT8_ERROR; | ||
| 41 | } | ||
| 42 | |||
| 43 | STATIC void | ||
| 44 | writetrans(uint8_t t, char buf[static NISSY_SIZE_TRANSFORMATION]) | ||
| 45 | { | ||
| 46 | if (t >= 48) | ||
| 47 | memcpy(buf, "error trans", 11); | ||
| 48 | else | ||
| 49 | memcpy(buf, transstr[t], 11); | ||
| 50 | buf[11] = '\0'; | ||
| 51 | } | ||
| 52 | |||
| 28 | STATIC cube_t | 53 | STATIC cube_t |
| 29 | transform_edges(cube_t c, uint8_t t) | 54 | transform_edges(cube_t c, uint8_t t) |
| 30 | { | 55 | { |
