commit 0a341a72a0f26c7e132c84174414dd705a745d53
parent 4e02faa8f4354338d5fdbdaeb9ccd2c38ba9b8a3
Author: Anton Konyahin <me@konyahin.xyz>
Date: Sun, 2 Jan 2022 20:25:15 +0300
finding of substitution
Diffstat:
4 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -1,6 +1,6 @@
MIT/X Consortium License
-(C)opyright 2021 Anton Konyahin <me@konyahin.xyz>
+(C)opyright 2021, 2022 Anton Konyahin <me@konyahin.xyz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
diff --git a/Makefile b/Makefile
@@ -5,7 +5,9 @@ BIN = sttemp
CC = cc
CFLAGS = -Wall -Werror -O
-all: src/main.c src/config.h
+all: $(BIN)
+
+$(BIN): src/main.c src/config.h
$(CC) $(CFLAGS) src/main.c -o $(BIN)
clean:
diff --git a/src/config.h b/src/config.h
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-char *template_dir = "/Users/antonkonjahin/projects/templates/";
-char *pattern_start = "{";
-char *pattern_end = "}";
+const char *template_dir = "/Users/antonkonjahin/projects/templates/";
+const char *pattern_start = "{";
+const char *pattern_end = "}";
diff --git a/src/main.c b/src/main.c
@@ -1,3 +1,5 @@
+/* See LICENSE file for copyright and license details. */
+
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
@@ -11,12 +13,12 @@ void show_usage() {
printf("Usage:\n\tsttemp template_name\n");
}
-char* strconcat(char* first, char* second) {
+char* strconcat(const char* first, const char* second) {
size_t first_len = strlen(first);
size_t second_len = strlen(second);
char *buf = malloc(first_len + second_len + 1);
- strncpy(buf, first, first_len);
- strncpy(buf + first_len, second, second_len + 1);
+ memcpy(buf, first, first_len);
+ memcpy(buf + first_len, second, second_len + 1);
return buf;
}
@@ -61,6 +63,31 @@ int main(int argc, char *argv[]) {
fclose(template);
printf("%s", buf);
+ printf("==================================\n");
+
+ const int pat_start_len = strlen(pattern_start);
+ const int pat_end_len = strlen(pattern_end);
+
+ char *start = buf;
+ while ((start = strstr(start, pattern_start)) != NULL) {
+ start = start + pat_start_len;
+ char* end = strstr(start, pattern_end);
+ if (end == NULL) {
+ fprintf(stderr, "Unfinished pattern: %10s", start);
+ return 1;
+ }
+
+ size_t token_length = end - start;
+ char* token_name = malloc(token_length + 1);
+ memcpy(token_name, start, token_length);
+ token_name[token_length] = '\0';
+
+ printf("%s\n", token_name);
+ free(token_name);
+
+ start = end + pat_end_len;
+ }
+
free(buf);
return 0;