diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-10-08 14:04:11 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-10-08 14:04:11 +0200 |
| commit | 2edc8c01fba711f4410279b59c784a43cf2dde41 (patch) | |
| tree | 67d6404c4f768ad8b1b4e4916e3b9af9f892a7aa /src/cube_array.c | |
| parent | 9ef630eefb683dcdbe56e96e51b10bf356f20505 (diff) | |
| download | nissy-core-2edc8c01fba711f4410279b59c784a43cf2dde41.tar.gz nissy-core-2edc8c01fba711f4410279b59c784a43cf2dde41.zip | |
Added write to SRC format
Diffstat (limited to '')
| -rw-r--r-- | src/cube_array.c | 118 |
1 files changed, 99 insertions, 19 deletions
diff --git a/src/cube_array.c b/src/cube_array.c index a9b9709..1d2da86 100644 --- a/src/cube_array.c +++ b/src/cube_array.c | |||
| @@ -156,6 +156,10 @@ static uint8_t readeo(char *); | |||
| 156 | static uint8_t readep(char *); | 156 | static uint8_t readep(char *); |
| 157 | static uint8_t readmove(char); | 157 | static uint8_t readmove(char); |
| 158 | static uint8_t readmodifier(char); | 158 | static uint8_t readmodifier(char); |
| 159 | static cube_t readcube_H48(char *); | ||
| 160 | static void writecube_H48(cube_t, char *); | ||
| 161 | static int writepiece_SRC(uint8_t, char *); | ||
| 162 | static void writecube_SRC(cube_t, char *); | ||
| 159 | static int permsign(uint8_t *, int); | 163 | static int permsign(uint8_t *, int); |
| 160 | 164 | ||
| 161 | static uint8_t | 165 | static uint8_t |
| @@ -219,8 +223,8 @@ readep(char *str) | |||
| 219 | return _error; | 223 | return _error; |
| 220 | } | 224 | } |
| 221 | 225 | ||
| 222 | cube_t | 226 | static cube_t |
| 223 | readcube(char *buf) | 227 | readcube_H48(char *buf) |
| 224 | { | 228 | { |
| 225 | int i; | 229 | int i; |
| 226 | uint8_t piece, orient; | 230 | uint8_t piece, orient; |
| @@ -231,10 +235,10 @@ readcube(char *buf) | |||
| 231 | while (*b == ' ' || *b == '\t' || *b == '\n') | 235 | while (*b == ' ' || *b == '\t' || *b == '\n') |
| 232 | b++; | 236 | b++; |
| 233 | if ((piece = readep(b)) == _error) | 237 | if ((piece = readep(b)) == _error) |
| 234 | goto readcube_error; | 238 | return errorcube; |
| 235 | b += 2; | 239 | b += 2; |
| 236 | if ((orient = readeo(b)) == _error) | 240 | if ((orient = readeo(b)) == _error) |
| 237 | goto readcube_error; | 241 | return errorcube; |
| 238 | b++; | 242 | b++; |
| 239 | ret.e[i] = piece | orient; | 243 | ret.e[i] = piece | orient; |
| 240 | } | 244 | } |
| @@ -242,36 +246,46 @@ readcube(char *buf) | |||
| 242 | while (*b == ' ' || *b == '\t' || *b == '\n') | 246 | while (*b == ' ' || *b == '\t' || *b == '\n') |
| 243 | b++; | 247 | b++; |
| 244 | if ((piece = readcp(b)) == _error) | 248 | if ((piece = readcp(b)) == _error) |
| 245 | goto readcube_error; | 249 | return errorcube; |
| 246 | b += 3; | 250 | b += 3; |
| 247 | if ((orient = readco(b)) == _error) | 251 | if ((orient = readco(b)) == _error) |
| 248 | goto readcube_error; | 252 | return errorcube; |
| 249 | b++; | 253 | b++; |
| 250 | ret.c[i] = piece | orient; | 254 | ret.c[i] = piece | orient; |
| 251 | } | 255 | } |
| 252 | 256 | ||
| 253 | return ret; | 257 | return ret; |
| 258 | } | ||
| 259 | |||
| 260 | cube_t | ||
| 261 | readcube(format_t format, char *buf) | ||
| 262 | { | ||
| 263 | cube_t ret; | ||
| 264 | |||
| 265 | switch (format) { | ||
| 266 | case H48: | ||
| 267 | ret = readcube_H48(buf); | ||
| 268 | break; | ||
| 269 | default: | ||
| 270 | #ifdef DEBUG | ||
| 271 | fprintf(stderr, "Cannot read cube in the given format\n"); | ||
| 272 | #endif | ||
| 273 | ret = errorcube; | ||
| 274 | } | ||
| 254 | 275 | ||
| 255 | readcube_error: | ||
| 256 | #ifdef DEBUG | 276 | #ifdef DEBUG |
| 257 | fprintf(stderr, "readcube error\n"); | 277 | if (iserror(ret)) |
| 278 | fprintf(stderr, "readcube error\n"); | ||
| 258 | #endif | 279 | #endif |
| 259 | return errorcube; | 280 | return ret; |
| 260 | } | 281 | } |
| 261 | 282 | ||
| 262 | void | 283 | static void |
| 263 | writecube(cube_t cube, char *buf) | 284 | writecube_H48(cube_t cube, char *buf) |
| 264 | { | 285 | { |
| 265 | char *errormsg; | ||
| 266 | uint8_t piece, orient; | 286 | uint8_t piece, orient; |
| 267 | size_t len; | ||
| 268 | int i; | 287 | int i; |
| 269 | 288 | ||
| 270 | if (!isconsistent(cube)) { | ||
| 271 | errormsg = "ERROR: cannot write inconsistent cube"; | ||
| 272 | goto writecube_error; | ||
| 273 | } | ||
| 274 | |||
| 275 | for (i = 0; i < 12; i++) { | 289 | for (i = 0; i < 12; i++) { |
| 276 | piece = cube.e[i] & _pbits; | 290 | piece = cube.e[i] & _pbits; |
| 277 | orient = (cube.e[i] & _eobit) >> _eoshift; | 291 | orient = (cube.e[i] & _eobit) >> _eoshift; |
| @@ -291,6 +305,73 @@ writecube(cube_t cube, char *buf) | |||
| 291 | } | 305 | } |
| 292 | 306 | ||
| 293 | buf[48+39] = '\0'; | 307 | buf[48+39] = '\0'; |
| 308 | } | ||
| 309 | |||
| 310 | static int | ||
| 311 | writepiece_SRC(uint8_t piece, char *buf) | ||
| 312 | { | ||
| 313 | char digits[3]; | ||
| 314 | int i, len = 0; | ||
| 315 | |||
| 316 | while (piece != 0) { | ||
| 317 | digits[len++] = (piece % 10) + '0'; | ||
| 318 | piece /= 10; | ||
| 319 | } | ||
| 320 | |||
| 321 | if (len == 0) | ||
| 322 | digits[len++] = '0'; | ||
| 323 | |||
| 324 | for (i = 0; i < len; i++) | ||
| 325 | buf[i] = digits[len-i-1]; | ||
| 326 | |||
| 327 | buf[len] = ','; | ||
| 328 | buf[len+1] = ' '; | ||
| 329 | |||
| 330 | return len+2; | ||
| 331 | } | ||
| 332 | |||
| 333 | static void | ||
| 334 | writecube_SRC(cube_t cube, char *buf) | ||
| 335 | { | ||
| 336 | int i, ptr; | ||
| 337 | |||
| 338 | memcpy(buf, "{\n\t.c = {", 9); | ||
| 339 | ptr = 9; | ||
| 340 | |||
| 341 | for (i = 0; i < 8; i++) | ||
| 342 | ptr += writepiece_SRC(cube.c[i], buf + ptr); | ||
| 343 | |||
| 344 | memcpy(buf+ptr-2, "},\n\t.e = {", 10); | ||
| 345 | ptr += 8; | ||
| 346 | |||
| 347 | for (i = 0; i < 12; i++) | ||
| 348 | ptr += writepiece_SRC(cube.e[i], buf + ptr); | ||
| 349 | |||
| 350 | memcpy(buf+ptr-2, "}\n}\0", 4); | ||
| 351 | } | ||
| 352 | |||
| 353 | void | ||
| 354 | writecube(format_t format, cube_t cube, char *buf) | ||
| 355 | { | ||
| 356 | char *errormsg; | ||
| 357 | size_t len; | ||
| 358 | |||
| 359 | if (!isconsistent(cube)) { | ||
| 360 | errormsg = "ERROR: cannot write inconsistent cube"; | ||
| 361 | goto writecube_error; | ||
| 362 | } | ||
| 363 | |||
| 364 | switch (format) { | ||
| 365 | case H48: | ||
| 366 | writecube_H48(cube, buf); | ||
| 367 | break; | ||
| 368 | case SRC: | ||
| 369 | writecube_SRC(cube, buf); | ||
| 370 | break; | ||
| 371 | default: | ||
| 372 | errormsg = "ERROR: cannot write cube in the given format"; | ||
| 373 | goto writecube_error; | ||
| 374 | } | ||
| 294 | 375 | ||
| 295 | return; | 376 | return; |
| 296 | 377 | ||
| @@ -304,7 +385,6 @@ writecube_error: | |||
| 304 | buf[len+1] = '\0'; | 385 | buf[len+1] = '\0'; |
| 305 | } | 386 | } |
| 306 | 387 | ||
| 307 | |||
| 308 | static uint8_t | 388 | static uint8_t |
| 309 | readmove(char c) | 389 | readmove(char c) |
| 310 | { | 390 | { |
