diff options
| author | Devine Lu Linvega <aliceffekt@gmail.com> | 2020-09-14 09:39:15 -0700 |
|---|---|---|
| committer | Devine Lu Linvega <aliceffekt@gmail.com> | 2020-09-14 09:39:15 -0700 |
| commit | adfb65521dfe51d70f0c3033afef76ec1c66880e (patch) | |
| tree | 561b8bbe47aeb239a169c16c6f644c2f7e5558d8 /tools | |
| parent | 2f808f508985038cd47be774667e5b24b2e91f25 (diff) | |
Added c tool
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/.clang-format | 65 | ||||
| -rwxr-xr-x | tools/build.sh | 18 | ||||
| -rw-r--r-- | tools/main.c | 106 |
3 files changed, 189 insertions, 0 deletions
diff --git a/tools/.clang-format b/tools/.clang-format new file mode 100644 index 0000000..5ca25b8 --- /dev/null +++ b/tools/.clang-format @@ -0,0 +1,65 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: true +AlignEscapedNewlinesLeft: false +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AlwaysBreakAfterDefinitionReturnType: true +AlwaysBreakTemplateDeclarations: false +AlwaysBreakBeforeMultilineStrings: false +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BinPackParameters: true +BinPackArguments: true +ColumnLimit: 0 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +DerivePointerAlignment: false +ExperimentalAutoDetectBinPacking: false +IndentCaseLabels: false +IndentWrappedFunctionNames: false +IndentFunctionDeclarationAfterType: false +MaxEmptyLinesToKeep: 1 +KeepEmptyLinesAtTheStartOfBlocks: true +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakString: 1000 +PenaltyBreakFirstLessLess: 120 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +SpacesBeforeTrailingComments: 1 +Cpp11BracedListStyle: true +Standard: Cpp11 +IndentWidth: 8 +TabWidth: 8 +UseTab: ForIndentation +BreakBeforeBraces: Linux +SortIncludes: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpacesInAngles: false +SpaceInEmptyParentheses: false +SpacesInCStyleCastParentheses: false +SpaceAfterCStyleCast: false +SpacesInContainerLiterals: true +SpaceBeforeAssignmentOperators: true +ContinuationIndentWidth: 4 +CommentPragmas: '^ IWYU pragma:' +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +SpaceBeforeParens: Never +DisableFormat: false +... diff --git a/tools/build.sh b/tools/build.sh new file mode 100755 index 0000000..256b3c9 --- /dev/null +++ b/tools/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +clang-format -i main.c + +# Linux +cc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined -o main main.c + +# RPi +# tcc -Wall main.c -o main + +# Plan9 +# pcc main.c -o main + +# ./main ../themes/apollo.svg + +cat ../themes/apollo.svg | ./main + +rm ./main 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 |
