Mercurial
annotate playground/main.c @ 129:f7860f491a8c
--amend
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 08 Jan 2026 19:21:22 -0800 |
| parents | e7899c93da77 |
| children | 893d87124d16 |
| rev | line source |
|---|---|
| 126 | 1 #include <stdlib.h> |
|
18
fa2b8af609d9
[Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents:
17
diff
changeset
|
2 #include <stdio.h> |
| 126 | 3 #include <string.h> |
|
108
f07abbcd2ec5
[HgWeb] Will probably hold off on using it since it is not urgent.
June Park <parkjune1995@gmail.com>
parents:
71
diff
changeset
|
4 |
| 126 | 5 int main() { |
| 6 char *input[5] = {"Hello ", "Foo <<E", "N", "DD>", "Park <END> "}; | |
| 7 char *key = "<END>"; | |
| 8 int key_len = strlen(key); | |
|
108
f07abbcd2ec5
[HgWeb] Will probably hold off on using it since it is not urgent.
June Park <parkjune1995@gmail.com>
parents:
71
diff
changeset
|
9 |
| 126 | 10 char **buffers = malloc(sizeof(char *) * 100); |
| 11 int buffer_index = 0; | |
| 12 int key_ptr = 0; | |
| 13 for (int i = 0; i < 5; i++) { | |
| 14 char *packet = input[i]; | |
| 15 int p_len = strlen(packet); | |
|
71
75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents:
25
diff
changeset
|
16 |
| 126 | 17 for (int j = 0; j < p_len; j++) { |
| 18 if (packet[j] == key[key_ptr]) { | |
| 19 key_ptr++; | |
| 20 | |
| 21 // If the WHOLE keyword is found | |
| 22 if (key_ptr == key_len) { | |
| 23 // 1. Print all previous "safe" buffers | |
| 24 // (The ones before the one where the keyword started) | |
| 25 for (int b = 0; b < buffer_index; b++) | |
| 26 printf("%s", buffers[b]); | |
| 27 | |
| 28 // 2. Handle the "Current" packet truncation | |
| 29 // Calculate where the match started in THIS packet | |
| 30 // If key_ptr was satisfied across multiple packets, | |
| 31 // 'j' is the end of the match in the current packet. | |
| 32 int match_end_in_packet = j + 1; | |
| 33 int match_len_in_this_packet = (key_ptr <= match_end_in_packet) ? key_ptr : match_end_in_packet; | |
| 34 | |
| 35 printf("%.*s\n", (match_end_in_packet - match_len_in_this_packet), packet); | |
| 36 | |
| 37 free(buffers); | |
| 38 return 0; | |
| 39 } | |
| 40 } else { | |
| 41 // If a match fails, we must "flush" the buffers we were holding | |
| 42 if (key_ptr > 0) { | |
| 43 for (int b = 0; b < buffer_index; b++) printf("%s", buffers[b]); | |
| 44 buffer_index = 0; | |
| 45 | |
| 46 if (packet[j] == key[0]) key_ptr = 1; | |
| 47 else { | |
| 48 printf("%c", packet[j]); | |
| 49 key_ptr = 0; | |
| 50 } | |
| 51 } else { | |
| 52 printf("%c", packet[j]); | |
| 53 } | |
| 54 } | |
| 55 } | |
|
71
75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents:
25
diff
changeset
|
56 |
| 126 | 57 // If we finish a packet and we are in the middle of a match, buffer it |
| 58 if (key_ptr > 0) { | |
| 59 buffers[buffer_index++] = packet; | |
| 60 } | |
| 61 } | |
|
71
75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents:
25
diff
changeset
|
62 |
| 126 | 63 free(buffers); |
| 64 return 0; | |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
65 } |