comparison playground/main.c @ 126:e7899c93da77

Remove playground.
author June Park <parkjune1995@gmail.com>
date Thu, 08 Jan 2026 18:03:34 -0800
parents 1c446ab6f945
children 893d87124d16
comparison
equal deleted inserted replaced
125:f236c895604e 126:e7899c93da77
1 #include <stdlib.h>
1 #include <stdio.h> 2 #include <stdio.h>
2 #include <stdlib.h> 3 #include <string.h>
3 4
4 #define REPO_ROOT "/Users/mrjunejune/zenbu" 5 int main() {
6 char *input[5] = {"Hello ", "Foo <<E", "N", "DD>", "Park <END> "};
7 char *key = "<END>";
8 int key_len = strlen(key);
5 9
6 int main() 10 char **buffers = malloc(sizeof(char *) * 100);
7 { 11 int buffer_index = 0;
8 char command[512]; 12 int key_ptr = 0;
9 snprintf(command, sizeof(command), "hg -R %s serve --stdio", REPO_ROOT); 13 for (int i = 0; i < 5; i++) {
10 printf("command: %s\n", command); 14 char *packet = input[i];
11 15 int p_len = strlen(packet);
12 FILE *hg_pipe = popen(command, "r+");
13 if (!hg_pipe)
14 {
15 printf("Failed to open pipe\n");
16 return -1;
17 }
18 16
19 fprintf(hg_pipe, "capabilities\n"); 17 for (int j = 0; j < p_len; j++) {
20 fflush(hg_pipe); 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 }
21 56
22 char *output = malloc(sizeof(char) * 2048); 57 // If we finish a packet and we are in the middle of a match, buffer it
23 char *curr = output; 58 if (key_ptr > 0) {
24 int c; 59 buffers[buffer_index++] = packet;
25 int number_of_breakline = 0; 60 }
26 while ((c = fgetc(hg_pipe)) != NULL) 61 }
27 {
28 *curr++ = c;
29 printf("output: %s\n", output);
30 if (c == '\n')
31 number_of_breakline++;
32 if (number_of_breakline == 2)
33 break;
34 printf("char: %c\n", c);
35 }
36 pclose(hg_pipe);
37 62
38 63 free(buffers);
39 return 0; 64 return 0;
40 } 65 }
41
42