summaryrefslogtreecommitdiff
path: root/files.c
diff options
context:
space:
mode:
authorkaa <kaa@laptosh.my.domain>2023-06-23 11:19:51 -0700
committerkaa <kaa@laptosh.my.domain>2023-06-23 11:19:51 -0700
commit8828e75a9ee7a5408ad00ad17e9878a2740cc866 (patch)
treebd60b53fb9896316ebc9928a30e0b0806b1c64ae /files.c
"Initial."
Diffstat (limited to 'files.c')
-rw-r--r--files.c205
1 files changed, 205 insertions, 0 deletions
diff --git a/files.c b/files.c
new file mode 100644
index 0000000..cc9a2bc
--- /dev/null
+++ b/files.c
@@ -0,0 +1,205 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAX 200
+
+int
+checkdir(char *line)
+{
+ int i;
+
+ for (i = strlen(line) - 1; i >= 0; --i)
+ if (line[i] == '.')
+ return 1;
+ else if (line[i] == '/')
+ return 0;
+ return 0;
+}
+
+int
+filter(char *pattern, char *buffer, char *line)
+{
+ if (strcmp(pattern, buffer) != 0)
+ return 1;
+ else if (checkdir(line) == 0)
+ return 1;
+
+ return 0;
+}
+
+void
+rip(char *in, char *out, int startlen, int endlen)
+{
+ int len = strlen(in);
+ int i;
+
+ for (i = startlen; i < len - endlen - 1; ++i)
+ out[i - startlen] = in[i];
+
+ out[i - startlen] = '\0';
+}
+
+void
+stripendfn(char *name)
+{
+ int i;
+
+ for (i = strlen(name) - 1; i >= 0; --i)
+ if (name[i] == '/') {
+ name[i] = '\0';
+ return;
+ }
+}
+
+void
+striprest(char *name)
+{
+ int i;
+
+ for (i = 1; i < strlen(name); ++i)
+ if (name[i] == '/') {
+ name[i] = '\0';
+ return;
+ }
+}
+
+int
+cntdl(char *name)
+{
+ int i;
+ int dl = 0;
+
+ for (i = 0; i < strlen(name); ++i)
+ if (name[i] == '/')
+ dl += 1;
+
+ return dl;
+}
+
+void
+splitdir(char *preout, int predl, char *site, char *out)
+{
+ char *p = preout + 1;
+ char *buffer = calloc(MAX, sizeof(char));
+ strcpy(buffer, preout);
+ char *delim = "/";
+ int i;
+
+ printf("%s", "&#x2560;&#x2550;&#x2561;");
+ strcpy(buffer, p);
+ striprest(buffer);
+ if (checkdir(buffer) == 0) {
+ printf("%s", buffer);
+ printf("%s", "&#x2502;");
+ } else {
+ printf("%s%s%s%s%s%s",
+ "<a href=\"",
+ site,
+ preout,
+ "\">",
+ buffer,
+ "</a>");
+ }
+
+ for (i = 0; i < predl; ++i) {
+ p = strstr(p, delim) + 1;
+ strcpy(buffer, p);
+ striprest(buffer);
+ if (checkdir(buffer) == 0) {
+ printf("%s", buffer);
+ printf("%s", "&#x2502;");
+ } else {
+ printf("%s%s%s%s%s%s",
+ "<a href=\"",
+ site,
+ preout + 1,
+ "\">",
+ buffer,
+ "</a>");
+ }
+ }
+ putchar('\n');
+}
+
+int
+pad(char *site, char *out, char *preout, int predl)
+{
+ int dl = cntdl(out);
+ char buffer[MAX], preoutbup[MAX];
+ char *p;
+
+ strcpy(preoutbup, preout);
+ strcpy(buffer, preout);
+ strcpy(preout, "/");
+ strcat(preout, buffer);
+
+ if (predl == -1) {
+ printf("\n%s\n", "&#x2565");
+ return dl;
+ }
+ else if (predl == 0) {
+ printf("%s", "&#x2560;&#x2550;&#x2561;");
+ p = preout + 1;
+ printf("%s%s%s%s%s%s\n",
+ "<a href=\"",
+ site,
+ preoutbup,
+ "\">",
+ p,
+ "</a>");
+ }
+ else
+ splitdir(preout, predl, site, out);
+
+ return dl;
+}
+
+int
+main(int argc, char **argv)
+{
+ FILE* in = fopen("files.json", "r");
+ FILE* header = fopen("header/files.txt", "r");
+ FILE* footer = fopen("footer/files.txt", "r");
+ char line[MAX], buffer[MAX];
+ char out[MAX], preout[MAX];
+ char pattern[32] = " \"path\": \"";
+ char site[64] = "https://kaa.neocities.org/";
+ char end[32] = "\",";
+ int i, patlen, endlen, predl;
+
+ if ((in == NULL) || (header == NULL) || (footer == NULL)) {
+ puts("That's pungent!");
+ return 1;
+ }
+
+ patlen = strlen(pattern);
+ endlen = strlen(end);
+ predl = -1;
+ preout[0] = '\0';
+
+ while (fgets(line, MAX, header) != NULL)
+ printf("%s", line);
+ fclose(header);
+
+ while (fgets(line, MAX, in) != NULL) {
+ for (i = 0; i < patlen; ++i)
+ buffer[i] = line[i];
+
+ if (filter(pattern, buffer, line) == 0) {
+ rip(line, out, patlen, endlen);
+ predl = pad(site, out, preout, predl);
+ strcpy(preout, out);
+ }
+ }
+
+ fclose(in);
+
+ printf("%s", "&#x2568;");
+
+ while (fgets(line, MAX, footer) != NULL)
+ printf("%s", line);
+ fclose(footer);
+
+ return 0;
+}