]> git repos ~mattia - cmc-find.git/commitdiff
Init
authorMattia Cabrini <dev@mattiacabrini.com>
Tue, 9 Sep 2025 13:12:02 +0000 (15:12 +0200)
committerMattia Cabrini <dev@mattiacabrini.com>
Tue, 9 Sep 2025 13:12:02 +0000 (15:12 +0200)
LICENSE [new file with mode: 0644]
Makefile [new file with mode: 0644]
README.MD [new file with mode: 0644]
cmc-find.c [new file with mode: 0644]

diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..df2e18d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2025 Mattia Cabrini
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..932d77d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,24 @@
+CFLAGS=-Wall -Werror -Wno-unused-function
+CFLAGS_REL=$(CFLSGS) -O2 
+CFLAGS_DEB=$(CFLAGS) -g -O0 
+
+SRC=cmc-find.c
+BIN=cmc-find
+
+INDEND_FLAGS=-nbad -bap -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 \
+            -cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -npsl \
+            -ncs -nsc -sob -ts8 
+
+all: fmt release
+
+release:
+       gcc -o $(BIN) $(SRC) $(CFLAGS_REL)
+
+debug: 
+       gcc -o $(BIN) $(SRC) $(CFLAGS_DEB) 
+
+fmt:
+       indent $(INDEND_FLAGS) $(SRC)
+
+run: # on Unix-like systems...
+       cat /dev/urandom | ./find 1606
diff --git a/README.MD b/README.MD
new file mode 100644 (file)
index 0000000..481cbe2
--- /dev/null
+++ b/README.MD
@@ -0,0 +1,7 @@
+# CMC-Find
+
+Find the position(s) of a string in a stream.
+
+## License
+This program is licensed under the MIT license.
+See the file LICENSE.
diff --git a/cmc-find.c b/cmc-find.c
new file mode 100644 (file)
index 0000000..78a8372
--- /dev/null
@@ -0,0 +1,131 @@
+/* Copyright (c) Mattia Cabrini <dev@mattiacabrini.com> */
+/* SPDX-License-Identifier: MIT                         */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#ifndef MAX_LEN
+#define MAX_LEN 64
+#endif
+
+typedef struct cs_t {
+       char buf[MAX_LEN];
+
+       size_t cap /* max string size; <= MAX_LEN */ ;
+       size_t len /* number of chars actually set */ ;
+       size_t start;           /* index of firs char */
+} *cs;
+
+static void cs_init(cs S, size_t cap);
+static void cs_add(cs S, char ch);
+static int cs_cmp(cs S, char *ch);
+static void cs_print(cs S);
+
+int main(int argc, char **argv)
+{
+       int ch = 0;
+       uint64_t n;
+
+       char str[MAX_LEN];
+       struct cs_t matcher;
+
+       if (argc < 2) {
+               printf("Usage: %s <string to find>\n", argv[0]);
+               exit(1);
+       }
+       strncpy(str, argv[1], sizeof(str));
+       printf("Check against %s\n", str);
+
+       cs_init(&matcher, strlen(str));
+
+       for (n = 1; (ch = fgetc(stdin)) != EOF; ++n) {
+               cs_add(&matcher, ch);
+
+               if (matcher.len == matcher.cap) {
+                       if (cs_cmp(&matcher, str)) {
+                               printf("Found at %llu\n", n - matcher.cap);
+                               /* cs_print(&matcher); printf("\n"); */
+                       }
+               }
+       }
+
+       return 0;
+}
+
+static void cs_init(cs S, size_t cap)
+{
+       S->cap = cap;
+       S->len = 0;
+       S->start = 0;
+}
+
+static void cs_add(cs S, char ch)
+{
+       if (S->len < S->cap) {
+               S->buf[S->len] = ch;
+               ++S->len;
+       } else {
+               S->buf[S->start] = ch;
+               ++S->start;
+               if (S->start == S->cap)
+                       S->start = 0;
+       }
+}
+
+static int cs_cmp(cs S, char *ch)
+{
+       size_t i;
+       size_t count;
+
+       i = S->start;
+       count = 0;
+
+       while (count < S->len && *ch) {
+               if (*ch != S->buf[i])
+                       return 0;
+
+               ++i;
+               if (i == S->cap)
+                       i = 0;
+               ++count;
+               ++ch;
+       }
+
+/*
+        count == len | *ch == 0 | Ret
+        1            | 1        | 1
+        1            | 0        | 0
+        0            | 1        | 0
+        0            | 0        | IMP.
+
+
+       if (count == S->len && *ch)
+               return 0;
+       if (count < S->len && *ch == '\0')
+               return 0;
+
+       return 1;
+*/
+       return count == S->len && *ch == '\0';
+}
+
+static void cs_print(cs S)
+{
+       size_t i;
+       size_t count;
+
+       i = S->start;
+       count = 0;
+
+       while (count < S->len) {
+               putc(S->buf[i], stdout);
+
+               ++i;
+               if (i == S->cap)
+                       i = 0;
+
+               ++count;
+       }
+}