io_moves.h (1313B)
1 STATIC uint8_t readmove(char); 2 STATIC uint8_t readmodifier(char); 3 STATIC int64_t writemoves(uint8_t *, int, uint64_t, char *); 4 5 STATIC uint8_t 6 readmove(char c) 7 { 8 switch (c) { 9 case 'U': 10 return MOVE_U; 11 case 'D': 12 return MOVE_D; 13 case 'R': 14 return MOVE_R; 15 case 'L': 16 return MOVE_L; 17 case 'F': 18 return MOVE_F; 19 case 'B': 20 return MOVE_B; 21 default: 22 return UINT8_ERROR; 23 } 24 } 25 26 STATIC uint8_t 27 readmodifier(char c) 28 { 29 switch (c) { 30 case '1': /* Fallthrough */ 31 case '2': /* Fallthrough */ 32 case '3': 33 return c - '0' - 1; 34 case '\'': 35 return 2; 36 default: 37 return 0; 38 } 39 } 40 41 STATIC int64_t 42 writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) 43 { 44 int i; 45 uint64_t len; 46 int64_t written; 47 const char *s; 48 char *b; 49 50 if (buf_size == 0) { 51 LOG("Error: cannot write moves to buffer of size 0.\n"); 52 return NISSY_ERROR_BUFFER_SIZE; 53 } 54 55 for (i = 0, b = buf, written = 0; i < n; i++, b++, written++) { 56 s = movestr[m[i]]; 57 len = strlen(s); 58 if (len + written >= buf_size) { 59 LOG("Error: the given buffer is too small for " 60 "writing the given moves.\n"); 61 goto writemoves_error; 62 } 63 memcpy(b, s, len); 64 written += len; 65 b += len; 66 *b = ' '; 67 } 68 69 if (b != buf) 70 b--; /* Remove last space */ 71 *b = '\0'; 72 73 return written; 74 75 writemoves_error: 76 *buf = '\0'; 77 return NISSY_ERROR_BUFFER_SIZE; 78 }