parser lib for my games
Commit 9a3b392c93a7
tarball8 files changed+199-131
@@ -1,13 +1,16 @@
11
cmake_minimum_required(VERSION 3.20)22
project(jag_parser C)33
4
-add_library(jag_parser STATIC4
+add_library(jag_parse STATIC55
source/parser.c 66
)77
8
-target_include_directories(jag_parser PUBLIC8
+target_include_directories(jag_parse PUBLIC99
${CMAKE_CURRENT_SOURCE_DIR}/include1010
)1111
12
-target_compile_options(jag_parser PRIVATE12
+target_compile_options(jag_parse PRIVATE13
+ -Wextra14
+ -Wall15
+ -Wpedantic1316
)@@ -0,0 +1,27 @@
1
+{2
+ "nodes": {3
+ "nixpkgs": {4
+ "locked": {5
+ "lastModified": 1787498568,6
+ "narHash": "sha256-9i/VTdusq/+NM/tz+J1Re+ojkMB8MBf0QshnYfzHz30=",7
+ "owner": "NixOS",8
+ "repo": "nixpkgs",9
+ "rev": "56c02bc00adcf003215cc4bd996d6efaf4cff188",10
+ "type": "github"11
+ },12
+ "original": {13
+ "owner": "NixOS",14
+ "ref": "nixos-unstable",15
+ "repo": "nixpkgs",16
+ "type": "github"17
+ }18
+ },19
+ "root": {20
+ "inputs": {21
+ "nixpkgs": "nixpkgs"22
+ }23
+ }24
+ },25
+ "root": "root",26
+ "version": 727
+}@@ -0,0 +1,37 @@
1
+{2
+ description = "Jaglekon's parser library";3
+4
+ inputs = {5
+ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";6
+ };7
+8
+ outputs = {9
+ self,10
+ nixpkgs,11
+ }: let12
+ system = "x86_64-linux";13
+ pkgs = import nixpkgs {inherit system;};14
+ in {15
+ packages.${system}.default = pkgs.stdenv.mkDerivation {16
+ pname = "jag_parse";17
+ version = "1.0.1";18
+19
+ src = ./.;20
+21
+ buildPhase = ''22
+ $CC -c source/jag_parse.c \23
+ -Iinclude \24
+ -o jag_parse.o25
+ $AR rcs jag_parse.a jag_parse.o26
+ '';27
+28
+ installPhase = ''29
+ mkdir -p $out/lib30
+ mkdir -p $out/include31
+32
+ cp jag_parse.a $out/lib33
+ cp include/jag_parse.h $out/include34
+ '';35
+ };36
+ };37
+}@@ -0,0 +1,22 @@
1
+#ifndef PARSER_H2
+#define PARSER_H3
+4
+#include <string.h>5
+#include <ctype.h>6
+#include <stdint.h>7
+#include <stdbool.h>8
+9
+#define BUF_LEN 409610
+#define ARROW_LEN 3 11
+12
+typedef enum {13
+ NO_KEY, P_KEY, Q_KEY, LEFT_ARROW, RIGHT_ARROW, UP_ARROW, DOWN_ARROW, INCOMPLETE, UNKNOWN14
+} keypress;15
+16
+int add_to_buf(uint8_t *input, uint16_t input_length);17
+18
+keypress parse_input();19
+20
+void break_unfinished();21
+22
+#endif@@ -1,22 +0,0 @@
1
-#ifndef PARSER_H2
-#define PARSER_H3
-4
-#include <string.h>5
-#include <ctype.h>6
-#include <stdint.h>7
-#include <stdbool.h>8
-9
-#define BUF_LEN 409610
-#define ARROW_LEN 3 11
-12
-typedef enum {13
- NO_KEY, P_KEY, Q_KEY, LEFT_ARROW, RIGHT_ARROW, UP_ARROW, DOWN_ARROW, INCOMPLETE, UNKNOWN14
-} keypress;15
-16
-int add_to_buf(uint8_t *input, uint16_t input_length);17
-18
-keypress parse_input();19
-20
-void break_unfinished();21
-22
-#endif@@ -0,0 +1 @@
1
+/nix/store/lqi15sh8mmswdlnd09l5xsaxf7a4gc1s-jag_parse-1.0.1@@ -0,0 +1,106 @@
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
+}@@ -1,106 +0,0 @@
1
-#include <parser.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
-}