diff options
Diffstat (limited to 'src/core/io_cube.h')
| -rw-r--r-- | src/core/io_cube.h | 62 |
1 files changed, 49 insertions, 13 deletions
diff --git a/src/core/io_cube.h b/src/core/io_cube.h index 9ae82d7..1ac8ec7 100644 --- a/src/core/io_cube.h +++ b/src/core/io_cube.h | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | STATIC cube_t readcube(const char *, const char *); | ||
| 2 | STATIC int writecube(const char *, cube_t, char *); | ||
| 3 | STATIC void log_available_formats(void); | ||
| 1 | STATIC uint8_t readco(const char *); | 4 | STATIC uint8_t readco(const char *); |
| 2 | STATIC uint8_t readcp(const char *); | 5 | STATIC uint8_t readcp(const char *); |
| 3 | STATIC uint8_t readeo(const char *); | 6 | STATIC uint8_t readeo(const char *); |
| @@ -29,7 +32,7 @@ STATIC struct { | |||
| 29 | { .name = "NONE", .read = NULL, .write = NULL }, | 32 | { .name = "NONE", .read = NULL, .write = NULL }, |
| 30 | }; | 33 | }; |
| 31 | 34 | ||
| 32 | cube_t | 35 | STATIC cube_t |
| 33 | readcube(const char *format, const char *buf) | 36 | readcube(const char *format, const char *buf) |
| 34 | { | 37 | { |
| 35 | int i; | 38 | int i; |
| @@ -38,11 +41,12 @@ readcube(const char *format, const char *buf) | |||
| 38 | if (!strcmp(format, ioformat[i].name)) | 41 | if (!strcmp(format, ioformat[i].name)) |
| 39 | return ioformat[i].read(buf); | 42 | return ioformat[i].read(buf); |
| 40 | 43 | ||
| 41 | LOG("Cannot read cube in the given format\n"); | 44 | LOG("Cannot read cube: unknown format '%s'\n", format); |
| 45 | log_available_formats(); | ||
| 42 | return ZERO_CUBE; | 46 | return ZERO_CUBE; |
| 43 | } | 47 | } |
| 44 | 48 | ||
| 45 | void | 49 | STATIC int |
| 46 | writecube(const char *format, cube_t cube, char *buf) | 50 | writecube(const char *format, cube_t cube, char *buf) |
| 47 | { | 51 | { |
| 48 | char *errormsg; | 52 | char *errormsg; |
| @@ -58,18 +62,30 @@ writecube(const char *format, cube_t cube, char *buf) | |||
| 58 | for (i = 0; ioformat[i].write != NULL; i++) { | 62 | for (i = 0; ioformat[i].write != NULL; i++) { |
| 59 | if (!strcmp(format, ioformat[i].name)) { | 63 | if (!strcmp(format, ioformat[i].name)) { |
| 60 | ioformat[i].write(cube, buf); | 64 | ioformat[i].write(cube, buf); |
| 61 | return; | 65 | return 0; |
| 62 | } | 66 | } |
| 63 | } | 67 | } |
| 64 | 68 | ||
| 69 | LOG("Cannot write cube: unknown format '%s'\n", format); | ||
| 70 | log_available_formats(); | ||
| 65 | errormsg = "ERROR: format"; | 71 | errormsg = "ERROR: format"; |
| 66 | 72 | ||
| 67 | writecube_error: | 73 | writecube_error: |
| 68 | LOG("writecube error, see stdout for details\n"); | ||
| 69 | len = strlen(errormsg); | 74 | len = strlen(errormsg); |
| 70 | memcpy(buf, errormsg, len); | 75 | memcpy(buf, errormsg, len); |
| 71 | buf[len] = '\n'; | 76 | buf[len] = '\0'; |
| 72 | buf[len+1] = '\0'; | 77 | return 1; |
| 78 | } | ||
| 79 | |||
| 80 | STATIC void | ||
| 81 | log_available_formats(void) | ||
| 82 | { | ||
| 83 | int i; | ||
| 84 | |||
| 85 | LOG("Available formats: "); | ||
| 86 | for (i = 0; ioformat[i].read != NULL; i++) | ||
| 87 | LOG("'%s' ", ioformat[i].name); | ||
| 88 | LOG("\n"); | ||
| 73 | } | 89 | } |
| 74 | 90 | ||
| 75 | STATIC uint8_t | 91 | STATIC uint8_t |
| @@ -133,14 +149,34 @@ readcube_B32(const char *buf) | |||
| 133 | 149 | ||
| 134 | for (i = 0; i < 8; i++) { | 150 | for (i = 0; i < 8; i++) { |
| 135 | c[i] = b32tocorner(buf[i]); | 151 | c[i] = b32tocorner(buf[i]); |
| 136 | DBG_ASSERT(c[i] < 255, ZERO_CUBE, | 152 | if (c[i] == UINT8_ERROR) { |
| 137 | "Error reading B32 corner %d (char %d)\n", i, i); | 153 | LOG("Error reading B32 corner %d ", i); |
| 154 | if (buf[i] == 0) { | ||
| 155 | LOG("(string terminated early)\n"); | ||
| 156 | } else { | ||
| 157 | LOG("(char '%c')\n", buf[i]); | ||
| 158 | } | ||
| 159 | return ZERO_CUBE; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | if (buf[8] != '=') { | ||
| 164 | LOG("Error reading B32 separator: a single '=' " | ||
| 165 | "must be used to separate edges and corners\n"); | ||
| 166 | return ZERO_CUBE; | ||
| 138 | } | 167 | } |
| 139 | 168 | ||
| 140 | for (i = 0; i < 12; i++) { | 169 | for (i = 0; i < 12; i++) { |
| 141 | e[i] = b32toedge(buf[i+9]); | 170 | e[i] = b32toedge(buf[i+9]); |
| 142 | DBG_ASSERT(e[i] < 255, ZERO_CUBE, | 171 | if (e[i] == UINT8_ERROR) { |
| 143 | "Error reading B32 edge %d (char %d)\n", i, i+9); | 172 | LOG("Error reading B32 edge %d ", i); |
| 173 | if (buf[i+9] == 0) { | ||
| 174 | LOG("(string terminated early)\n"); | ||
| 175 | } else { | ||
| 176 | LOG("(char '%c')\n", buf[i+9]); | ||
| 177 | } | ||
| 178 | return ZERO_CUBE; | ||
| 179 | } | ||
| 144 | } | 180 | } |
| 145 | 181 | ||
| 146 | return cubefromarray(c, e); | 182 | return cubefromarray(c, e); |
| @@ -314,7 +350,7 @@ STATIC uint8_t | |||
| 314 | b32toedge(char c) | 350 | b32toedge(char c) |
| 315 | { | 351 | { |
| 316 | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) | 352 | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) |
| 317 | return 255; | 353 | return UINT8_ERROR; |
| 318 | 354 | ||
| 319 | return c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; | 355 | return c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; |
| 320 | } | 356 | } |
| @@ -324,7 +360,7 @@ b32tocorner(char c) { | |||
| 324 | uint8_t val; | 360 | uint8_t val; |
| 325 | 361 | ||
| 326 | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) | 362 | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) |
| 327 | return 255; | 363 | return UINT8_ERROR; |
| 328 | 364 | ||
| 329 | val = c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; | 365 | val = c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; |
| 330 | 366 | ||
