From 289b4d8d66bff8d52d7f3c0bdcd83fed69417e32 Mon Sep 17 00:00:00 2001 From: radhitya Date: Tue, 14 Jan 2025 12:04:54 +0700 Subject: [PATCH] pure saturday --- main.c | 105 +++++++++++++++++++++++++++++++++++++++++++------------ makefile | 5 +++ 2 files changed, 87 insertions(+), 23 deletions(-) diff --git a/main.c b/main.c index 040268b..bd17db8 100644 --- a/main.c +++ b/main.c @@ -2,38 +2,73 @@ #include #include +#include + FILE *fptr; -void sf() { - fptr = fopen("data.txt", "rb+"); - if (fptr == NULL) { - printf("Sukses buat data.txt\n"); - fptr = fopen("data.txt", "wb"); - if (fptr == NULL) { - perror("Galat buat data.txt"); - exit(1); - } - } -} +void checker() { + fptr = fopen("./source/data.txt", "rb+"); + + if (fptr == NULL) { + perror("Galat checker buat ./source/data.txt"); + exit(1); + } + } int main(int argc, char *argv[]) { if (argc < 2) { - printf("Penggunaan: %s \n", argv[0]); + printf("Penggunaan: %s \natau %s -h untuk info lebih lanjut\n", argv[0], argv[0]); return 1; } - - sf(); - + for (int i = 1; i < argc; i++) { if (strcmp("-v", argv[i]) == 0) { - printf("ini -v\n"); + printf("daisy © radhitya.org\n"); + return 1; } + else if(strcmp("-h", argv[i]) == 0) { + printf("ini -h\n"); + return 1; + } + else if(strcmp("new", argv[i]) == 0) { + struct stat st; + + if(stat("./source", &st) == 0 || stat("./target", &st) == 0) { + printf("./source dan ./target telah dibuat\n"); + } + + if(stat("./source", &st) == -1) { + mkdir("./source", 0700); + } + + if(stat("./target", &st) == -1) { + mkdir("./target", 0700); + } + + fptr = fopen("./source/data.txt", "rb+"); + if (fptr == NULL) { + printf("Sukses buat ./source/data.txt\n"); + fptr = fopen("./source/data.txt", "wb"); + if (fptr == NULL) { + perror("Galat buat ./source/data.txt"); + exit(1); + } + } + return 1; + } } char txt[50], of[50]; - - strcpy(of, argv[1]); - strcat(of, ".html"); + int i, len, in_italic = 0; + const char *file_name = strrchr(argv[1], '/'); + if (file_name == NULL) { + file_name = argv[1]; + } else { + file_name++; + } + + snprintf(of, sizeof(of), "target/%s.html", file_name); + fptr = fopen(argv[1], "rt"); if (fptr == NULL) { @@ -48,12 +83,36 @@ int main(int argc, char *argv[]) { return 1; } - fprintf(fpto, "\n"); + fprintf(fpto, "\n\n"); while (fgets(txt, 50, fptr) != NULL) { - fprintf(fpto, "

%s

\n", txt); - } - fprintf(fpto, ""); + len = strlen(txt); + if (len > 0 && txt[len-1] == '\n') { + txt[--len] = '\0'; + } + fprintf(fpto, "

"); + for(i = 0; i < len; i++) { + if (txt[i] == '_' && (i == 0 || txt[i-1] != '\\')) { + if(!in_italic) { + fprintf(fpto, ""); + in_italic = 1; + } else { + fprintf(fpto, ""); + in_italic = 0; + } + } else { + fputc(txt[i], fpto); + } + } + + if(in_italic) { + fprintf(fpto, ""); + }; + fprintf(fpto, "

\n"); + in_italic = 0; + } + + fprintf(fpto, "\n"); fclose(fptr); fclose(fpto); return 0; diff --git a/makefile b/makefile index 3f9f3bf..07787d6 100644 --- a/makefile +++ b/makefile @@ -4,3 +4,8 @@ out= daisy install: $(cc) $(src) -o $(out) + +clean: + rm -rf source + rm -rf target + rm $(out)