aboutsummaryrefslogtreecommitdiff
path: root/tools/main.c
diff options
context:
space:
mode:
authorDevine Lu Linvega <aliceffekt@gmail.com>2020-09-14 09:39:15 -0700
committerDevine Lu Linvega <aliceffekt@gmail.com>2020-09-14 09:39:15 -0700
commitadfb65521dfe51d70f0c3033afef76ec1c66880e (patch)
tree561b8bbe47aeb239a169c16c6f644c2f7e5558d8 /tools/main.c
parent2f808f508985038cd47be774667e5b24b2e91f25 (diff)
Added c tool
Diffstat (limited to 'tools/main.c')
-rw-r--r--tools/main.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/tools/main.c b/tools/main.c
new file mode 100644
index 0000000..20c92ce
--- /dev/null
+++ b/tools/main.c
@@ -0,0 +1,106 @@
+#include <stdio.h>
+
+#define BUFLEN 256
+
+int
+slen(char* s)
+{
+ int n = 0;
+ while(s[n] != '\0' && s[++n])
+ ;
+ return n;
+}
+
+int
+cpos(char* s, char c)
+{
+ int i;
+ for(i = 0; i < slen(s); i++)
+ if(s[i] == c)
+ return i;
+ return -1;
+}
+
+void
+sstr(char* src, char* dest, int from, int to)
+{
+ int i;
+ char *a = (char*)src + from, *b = (char*)dest;
+ for(i = 0; i < to; i++)
+ b[i] = a[i];
+ dest[to] = '\0';
+}
+
+unsigned char
+chex(char c)
+{
+ if(c >= 'a' && c <= 'f')
+ return 10 + c - 'a';
+ if(c >= 'A' && c <= 'F')
+ return 10 + c - 'A';
+ return (c - '0') & 0xF;
+}
+
+unsigned long
+shex(char* s)
+{
+ int i, n = 0, l = slen(s);
+ for(i = 0; i < l; ++i)
+ n |= (chex(s[i]) << ((l - i - 1) * 4));
+ return n;
+}
+
+int
+error(char* name)
+{
+ printf("Error: %s\n", name);
+ return 0;
+}
+
+int
+scmp(char* a, char* b)
+{
+ int i, l = slen(a);
+ if(l != slen(b))
+ return 0;
+ for(i = 0; i < l; ++i)
+ if(a[i] != b[i])
+ return 0;
+ return 1;
+}
+
+int
+parse(FILE* f)
+{
+ int i, id = 0;
+ long theme[9];
+ char line[BUFLEN], hexs[BUFLEN];
+ while(fgets(line, BUFLEN, f)) {
+ int split = cpos(line, '#');
+ if(split < 0 || id > 9)
+ continue;
+ sstr(line, hexs, split, 7);
+ printf("%s ", hexs);
+ theme[id] = shex(hexs + 1);
+ id++;
+ }
+ if(id != 9)
+ return error("Invalid theme");
+ for(i = 0; i < 9; ++i) {
+ printf("%ld ", theme[i]);
+ }
+ return 1;
+}
+
+int
+main(int argc, char* argv[])
+{
+ FILE* input;
+ if(argc == 2) {
+ input = fopen(argv[1], "rb");
+ if(input == NULL)
+ return error("Invalid input.\n");
+ } else
+ input = stdin;
+ return parse(input);
+} \ No newline at end of file