jag

jagParse_c

public

parser lib for my games

Star0
Watch0
ssh://git@git.jaglekon.de:30/jag/jagParse_c.git
source/jag_parse.cC136 lines2.3 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
12
void free_buf () {
13
  start = 0;
14
  end = 0;
15
  length = 0;
16
}
17
18
19
int add_to_buf(uint8_t *input, uint16_t input_length) {
20
  
21
  if (BUF_LEN - length < input_length)
22
    return 0;
23
24
  uint16_t space_end = BUF_LEN - end;
25
26
  if (input_length <= space_end) {
27
    memcpy(main_buffer + end, input, input_length);
28
  } else {
29
    memcpy(main_buffer + end, input, space_end);
30
    memcpy(main_buffer, input + space_end, input_length - space_end);
31
  }
32
33
  end = (end + input_length) % BUF_LEN;
34
  length += input_length;
35
36
  return 1;
37
}
38
39
40
uint16_t next_pos(uint16_t starting_position) {
41
  return (starting_position + 1) % BUF_LEN;
42
}
43
44
45
keypress parse_input() {
46
47
  if(length == 0)
48
    return NO_KEY;
49
50
  keypress output = UNKNOWN;
51
52
  uint8_t next_char = tolower(main_buffer[start]);
53
  uint16_t next_position = next_pos(start);
54
55
56
  if (next_char == '\033') {
57
58
    if (length < ARROW_LEN) {
59
      output = INCOMPLETE;
60
      next_position = start;
61
    } else {
62
63
      length--;
64
65
      if (main_buffer[next_position] == '[') {
66
        next_position = next_pos(next_position);
67
68
        length--;
69
70
        uint8_t escape_code = main_buffer[next_position];
71
72
        switch(escape_code) {
73
          case 'A':
74
            output = UP_ARROW;
75
            break;
76
          case 'B':
77
            output = DOWN_ARROW;
78
            break;
79
          case 'C':
80
            output = RIGHT_ARROW;
81
            break;
82
          case 'D':
83
            output = LEFT_ARROW;
84
            break;
85
          default:
86
            break;
87
        }
88
        
89
        length--;
90
91
        next_position = next_pos(next_position);
92
      } else {
93
        output = ESCAPE_KEY;
94
      }
95
    }
96
97
  } else {
98
99
    length--;
100
101
    switch (next_char) {
102
      case '\n':
103
        output = ENTER_KEY;
104
        break;
105
      case 'a':
106
        output = A_KEY;
107
        break;
108
      case 'd':
109
        output = D_KEY;
110
        break;
111
      case 'p':
112
        output = P_KEY;
113
        break;
114
      case 'q':
115
        output = Q_KEY;
116
        break;
117
      case 's':
118
        output = S_KEY;
119
        break;
120
      case 'w':
121
        output = W_KEY;
122
        break;
123
      default:
124
        break;
125
    }
126
  }
127
128
  start = next_position;
129
130
  return output;
131
}
132
133
void break_unfinished() {
134
  while (length--)
135
    start = next_pos(start);
136
}