sttemp

Simple template manager
git clone git://git.konyahin.xyz/sttemp
Log | Files | Refs | README | LICENSE

commit dbc33052368e1e6c88b9be782cde3608ad2ea06f
parent 0a341a72a0f26c7e132c84174414dd705a745d53
Author: Anton Konyahin <me@konyahin.xyz>
Date:   Wed,  5 Jan 2022 21:22:54 +0300

add code for user input

Diffstat:
MMakefile | 11+++++++++--
Asrc/files.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Asrc/files.h | 18++++++++++++++++++
Msrc/main.c | 36++++++++++++++----------------------
4 files changed, 87 insertions(+), 24 deletions(-)

diff --git a/Makefile b/Makefile @@ -7,8 +7,15 @@ CFLAGS = -Wall -Werror -O all: $(BIN) -$(BIN): src/main.c src/config.h - $(CC) $(CFLAGS) src/main.c -o $(BIN) +files.o : src/files.c src/files.h + $(CC) $(CFLAGS) -c src/files.c + +main.o : src/main.c src/config.h + $(CC) $(CFLAGS) -c src/main.c + +$(BIN): main.o files.o + $(CC) $(CFLAGS) main.o files.o -o $(BIN) clean: rm $(BIN) + rm *.o diff --git a/src/files.c b/src/files.c @@ -0,0 +1,46 @@ +/* See LICENSE file for copyright and license details. */ + +#include "files.h" + + +char* freadall(FILE* input) { + char *buf = malloc(BUF_SIZE); + size_t used = 0; + size_t len = 0; + + do { + buf = realloc(buf, BUF_SIZE + used); + len = fread(buf + used, 1, BUF_SIZE, input); + used = used + len; + } while (len != 0); + + buf = realloc(buf, used + 1); + buf[used] = '\0'; + + return buf; +} + +char* freadline(FILE *input) { + char *buf = malloc(BUF_SIZE); + size_t used = 0; + + while(1) { + buf = realloc(buf, BUF_SIZE + used); + if (fgets(buf + used, BUF_SIZE, input) == NULL) { + break; + } + char *new_line = strchr(buf + used, '\n'); + if (new_line != NULL) { + used = new_line - buf; + break; + } else { + used += BUF_SIZE - 1; + } + } + + buf = realloc(buf, used + 1); + buf[used] = '\0'; + + return buf; +} + diff --git a/src/files.h b/src/files.h @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define BUF_SIZE 2 * 1024 * 1024 + +/** + * Read line from file `input` and return it content + * without new line symbol. + */ +char* freadline(FILE *input); + +/** + * Read all content of file `input` and return it. + */ +char* freadall(FILE* input); diff --git a/src/main.c b/src/main.c @@ -1,13 +1,12 @@ /* See LICENSE file for copyright and license details. */ #include "config.h" +#include "files.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> -#define BUF_SIZE 2097152 - void show_usage() { printf("sttemp - simple template manager\n"); printf("Usage:\n\tsttemp template_name\n"); @@ -22,23 +21,17 @@ char* strconcat(const char* first, const char* second) { return buf; } -char* freadall(FILE* input) { - char *buf = malloc(BUF_SIZE); - size_t used = 0; - size_t len = 0; - - do { - buf = realloc(buf, BUF_SIZE + used); - len = fread(buf + used, 1, BUF_SIZE, input); - used = used + len; - } while (len != 0); - - buf = realloc(buf, used + 1); - buf[used] = '\0'; - - return buf; +char* get_placeholder_value(const char* placeholder_name) { + printf("Enter value for {%s}: ", placeholder_name); + return freadline(stdin); } +struct token { + char* name; + char* value; +}; +typedef struct token Token; + int main(int argc, char *argv[]) { if (argc < 2) { show_usage(); @@ -54,7 +47,7 @@ int main(int argc, char *argv[]) { char *temp_path = strconcat(template_dir, template_name); FILE *template = fopen(temp_path, "rb"); if (template == NULL) { - fprintf(stderr, "Template doesn't exist: %s", temp_path); + fprintf(stderr, "Template doesn't exist: %s\n", temp_path); return 1; } free(temp_path); @@ -62,9 +55,6 @@ int main(int argc, char *argv[]) { char *buf = freadall(template); fclose(template); - printf("%s", buf); - printf("==================================\n"); - const int pat_start_len = strlen(pattern_start); const int pat_end_len = strlen(pattern_end); @@ -82,7 +72,9 @@ int main(int argc, char *argv[]) { memcpy(token_name, start, token_length); token_name[token_length] = '\0'; - printf("%s\n", token_name); + char *value = get_placeholder_value(token_name); + printf("%s:%s\n", token_name, value); + free(token_name); start = end + pat_end_len;