jag

jagParse_c

public

parser lib for my games

Star0
Watch0
8f10175912750ea2cc3d3745b2007feebc16cdf6
1
ssh://git@git.jaglekon.de:30/jag/jagParse_c.git
source/jag_parse.cC106 lines1.9 KB
1
#include <jag_parse.h>
2
3
uint8_t main_buffer[BUF_LEN];
4
5
uint16_t start = 0;
6
uint16_t end = 0;
7
8
uint16_t length = 0;
9
10
11
int add_to_buf(uint8_t *input, uint16_t input_length) {
12
  
13
  if (BUF_LEN - length < input_length)
14
    return 0;
15
16
  uint16_t space_end = BUF_LEN - end;
17
18
  if (input_length <= space_end) {
19
    memcpy(main_buffer + end, input, input_length);
20
  } else {
21
    memcpy(main_buffer + end, input, space_end);
22
    memcpy(main_buffer, input + space_end, input_length - space_end);
23
  }
24
25
  end = (end + input_length) % BUF_LEN;
26
  length += input_length;
27
28
  return 1;
29
}
30
31
32
uint16_t next_pos(uint16_t starting_position) {
33
  return (starting_position + 1) % BUF_LEN;
34
}
35
36
37
keypress parse_input() {
38
39
  if(length == 0)
40
    return NO_KEY;
41
42
  keypress output = UNKNOWN;
43
44
  uint8_t next_char = tolower(main_buffer[start]);
45
  uint16_t next_position = next_pos(start);
46
47
48
  if (next_char == '\033') {
49
50
    if (length < ARROW_LEN) {
51
      output = INCOMPLETE;
52
      next_position = start;
53
    } else {
54
55
      length--;
56
57
      if (main_buffer[next_position] == '[') {
58
        next_position = next_pos(next_position);
59
60
        length--;
61
62
        uint8_t escape_code = main_buffer[next_position];
63
64
        switch(escape_code) {
65
          case 'A':
66
            output = UP_ARROW;
67
            break;
68
          case 'B':
69
            output = DOWN_ARROW;
70
            break;
71
          case 'C':
72
            output = RIGHT_ARROW;
73
            break;
74
          case 'D':
75
            output = LEFT_ARROW;
76
            break;
77
          default:
78
            break;
79
        }
80
        
81
        length--;
82
83
        next_position = next_pos(next_position);
84
      }
85
    }
86
87
  } else {
88
89
    length--;
90
91
    if (next_char == 'p') {
92
      output = P_KEY;
93
    } else if(next_char == 'q') {
94
      output = Q_KEY;
95
    }
96
  }
97
98
  start = next_position;
99
100
  return output;
101
}
102
103
void break_unfinished() {
104
  while (length--)
105
    start = next_pos(start);
106
}