table

Помоћни програм из командне линије за форматирање и приказ CSV-а
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување ba88177b656918243794827309aaac60d711cbc6
родитељ 97893356e96c3cd1f77e50ab9f62452e3c3e008f
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Mon, 21 Sep 2020 17:09:16 +0200

Switch to libunistring

Diffstat:
A.clang-format | 50++++++++++++++++++++++++++++++++++++++++++++++++++
M.gitignore | 1+
MMakefile | 1+
AREADME | 32++++++++++++++++++++++++++++++++
Mdefs.h | 19+++++++++++++++++++
At.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
At.c.html | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtable | 0
Mtable.c | 122+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
измењених датотека: 9, додавања: 425(+), брисања: 18(-)

diff --git a/.clang-format b/.clang-format @@ -0,0 +1,50 @@ +BasedOnStyle: Mozilla +AccessModifierOffset: '-4' +AlignTrailingComments: 'true' +AllowAllParametersOfDeclarationOnNextLine: 'true' +AllowShortBlocksOnASingleLine: 'true' +AllowShortFunctionsOnASingleLine: All +AllowShortLoopsOnASingleLine: 'false' +BinPackParameters: 'true' +BreakBeforeBinaryOperators: All +BreakBeforeBraces: Allman +BreakBeforeTernaryOperators: 'true' +BreakStringLiterals: 'true' +ColumnLimit: '100' +ConstructorInitializerAllOnOneLineOrOnePerLine: 'false' +ConstructorInitializerIndentWidth: '0' +ContinuationIndentWidth: '4' +Cpp11BracedListStyle: 'false' +DerivePointerAlignment: 'false' +DisableFormat: 'false' +ExperimentalAutoDetectBinPacking: 'true' +FixNamespaceComments: 'true' +IncludeBlocks: Merge +IndentCaseLabels: 'false' +IndentPPDirectives: BeforeHash +IndentWidth: '4' +IndentWrappedFunctionNames: 'false' +KeepEmptyLinesAtTheStartOfBlocks: 'false' +Language: Cpp +MaxEmptyLinesToKeep: '1' +NamespaceIndentation: Inner +ObjCSpaceAfterProperty: 'false' +ObjCSpaceBeforeProtocolList: 'false' +PointerAlignment: Right +ReflowComments: 'false' +SortIncludes: 'false' +SpaceAfterCStyleCast: 'false' +SpaceAfterLogicalNot: 'false' +SpaceAfterTemplateKeyword: 'false' +SpaceBeforeAssignmentOperators: 'true' +SpaceBeforeCpp11BracedList: 'true' +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: 'false' +SpacesBeforeTrailingComments: '2' +SpacesInAngles: 'false' +SpacesInCStyleCastParentheses: 'false' +SpacesInContainerLiterals: 'false' +SpacesInParentheses: 'false' +Standard: Cpp11 +TabWidth: '4' +UseTab: Never diff --git a/.gitignore b/.gitignore @@ -3,3 +3,4 @@ *.orig *.rej *.o +*.swp diff --git a/Makefile b/Makefile @@ -1,6 +1,7 @@ all: table table: table.o + $(CC) -lunistring -o $@ $< clean: table.o table rm table.o table 2>/dev/null diff --git a/README b/README @@ -0,0 +1,32 @@ + table + ===== + +Command line utility to format and display a CSV file. + + + Install + ------- + +$ make && sudo make install + + + License + ------- + + Command line utility to format and display a CSV file. + Copyright (C) 2020 Страхиња Радић + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + + diff --git a/defs.h b/defs.h @@ -1,3 +1,22 @@ +/* + * Command line utility to format and display a CSV file. + * Copyright (C) 2020 Страхиња Радић + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + #ifndef __DEFS_H #define __DEFS_H diff --git a/t.c b/t.c @@ -0,0 +1,102 @@ +/* + * Command line utility to format and display a CSV file. + * Copyright (C) 2020 Страхиња Радић + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + + +#include <stdarg.h> +#include <stdio.h> +#include <string.h> +#include "defs.h" + +int +version() +{ + printf("%s v%s\n", PROGRAMNAME, VERSION); + return 0; +} + +int +usage() +{ + printf("Usage: %s [-v|--version]\n", PROGRAMNAME); + return 0; +} + +int +error(int code, char *fmt, ...) +{ + char buf[BUFSIZE]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + fprintf(stderr, "%s: %s", PROGRAMNAME, buf); + return code; +} + +int +main(int argc, char **argv) +{ + char *arg; + int cmd = CMD_NONE; + while (arg = *argv++) { + if (*arg++ == '-') { + char c = *arg++; + if (c == '-') { + if (!strcmp(arg, "version")) { + cmd = CMD_VERSION; + } else { + error(1, "Invalid argument: --%s\n", arg); + return usage(); + } + } else { + switch (c) { + case 'v': + cmd = CMD_VERSION; + break; + default: + error(1, "Invalid argument: -%c\n", c); + return usage(); + } + } + } + arg++; + } + + if (cmd == CMD_VERSION) { + return version(); + } + + char line[BUFSIZE]; + + do { + fgets(line, sizeof(line), stdin); + if (!feof(stdin)) { + char *eol = strchr(line, '\n'); + if (eol) + *eol = 0; + fprintf(stdout, "%s\n", line); + } + } + while (!feof(stdin)); + + /* + * error(2, "Invalid arguments\n"); + */ + return 0; +} diff --git a/t.c.html b/t.c.html @@ -0,0 +1,116 @@ +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt><i><font color="#9A1900">/*</font></i> +<i><font color="#9A1900"> * Command line utility to format and display a CSV file.</font></i> +<i><font color="#9A1900"> * Copyright (C) 2020 Страхиња Радић</font></i> +<i><font color="#9A1900"> *</font></i> +<i><font color="#9A1900"> * This program is free software: you can redistribute it and/or modify</font></i> +<i><font color="#9A1900"> * it under the terms of the GNU General Public License as published by</font></i> +<i><font color="#9A1900"> * the Free Software Foundation, either version 3 of the License, or</font></i> +<i><font color="#9A1900"> * (at your option) any later version.</font></i> +<i><font color="#9A1900"> *</font></i> +<i><font color="#9A1900"> * This program is distributed in the hope that it will be useful,</font></i> +<i><font color="#9A1900"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</font></i> +<i><font color="#9A1900"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</font></i> +<i><font color="#9A1900"> * GNU General Public License for more details.</font></i> +<i><font color="#9A1900"> *</font></i> +<i><font color="#9A1900"> * You should have received a copy of the GNU General Public License</font></i> +<i><font color="#9A1900"> * along with this program. If not, see </font></i><u><font color="#0000FF">&lt;https://www.gnu.org/licenses/&gt;</font></u><i><font color="#9A1900">.</font></i> +<i><font color="#9A1900"> *</font></i> +<i><font color="#9A1900"> */</font></i> + + +<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdarg.h&gt;</font> +<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font> +<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;string.h&gt;</font> +<b><font color="#000080">#include</font></b> <font color="#FF0000">"defs.h"</font> + +<font color="#009900">int</font> +<b><font color="#000000">version</font></b><font color="#990000">()</font> +<font color="#FF0000">{</font> + <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"%s v%s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font> PROGRAMNAME<font color="#990000">,</font> VERSION<font color="#990000">);</font> + <b><font color="#0000FF">return</font></b> <font color="#993399">0</font><font color="#990000">;</font> +<font color="#FF0000">}</font> + +<font color="#009900">int</font> +<b><font color="#000000">usage</font></b><font color="#990000">()</font> +<font color="#FF0000">{</font> + <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Usage: %s [-v|--version]</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font> PROGRAMNAME<font color="#990000">);</font> + <b><font color="#0000FF">return</font></b> <font color="#993399">0</font><font color="#990000">;</font> +<font color="#FF0000">}</font> + +<font color="#009900">int</font> +<b><font color="#000000">error</font></b><font color="#990000">(</font><font color="#009900">int</font> code<font color="#990000">,</font> <font color="#009900">char</font> <font color="#990000">*</font>fmt<font color="#990000">,</font> <font color="#990000">...)</font> +<font color="#FF0000">{</font> + <font color="#009900">char</font> buf<font color="#990000">[</font>BUFSIZE<font color="#990000">];</font> + <font color="#008080">va_list</font> args<font color="#990000">;</font> + <b><font color="#000000">va_start</font></b><font color="#990000">(</font>args<font color="#990000">,</font> fmt<font color="#990000">);</font> + <b><font color="#000000">vsnprintf</font></b><font color="#990000">(</font>buf<font color="#990000">,</font> <b><font color="#0000FF">sizeof</font></b><font color="#990000">(</font>buf<font color="#990000">),</font> fmt<font color="#990000">,</font> args<font color="#990000">);</font> + <b><font color="#000000">va_end</font></b><font color="#990000">(</font>args<font color="#990000">);</font> + <b><font color="#000000">fprintf</font></b><font color="#990000">(</font>stderr<font color="#990000">,</font> <font color="#FF0000">"%s: %s"</font><font color="#990000">,</font> PROGRAMNAME<font color="#990000">,</font> buf<font color="#990000">);</font> + <b><font color="#0000FF">return</font></b> code<font color="#990000">;</font> +<font color="#FF0000">}</font> + +<font color="#009900">int</font> +<b><font color="#000000">main</font></b><font color="#990000">(</font><font color="#009900">int</font> argc<font color="#990000">,</font> <font color="#009900">char</font> <font color="#990000">**</font>argv<font color="#990000">)</font> +<font color="#FF0000">{</font> + <font color="#009900">char</font> <font color="#990000">*</font>arg<font color="#990000">;</font> + <font color="#009900">int</font> cmd <font color="#990000">=</font> CMD_NONE<font color="#990000">;</font> + <b><font color="#0000FF">while</font></b> <font color="#990000">(</font>arg <font color="#990000">=</font> <font color="#990000">*</font>argv<font color="#990000">++)</font> <font color="#FF0000">{</font> + <b><font color="#0000FF">if</font></b> <font color="#990000">(*</font>arg<font color="#990000">++</font> <font color="#990000">==</font> <font color="#FF0000">'-'</font><font color="#990000">)</font> + <font color="#FF0000">{</font> + <font color="#009900">char</font> c <font color="#990000">=</font> <font color="#990000">*</font>arg<font color="#990000">++;</font> + <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>c <font color="#990000">==</font> <font color="#FF0000">'-'</font><font color="#990000">)</font> + <font color="#FF0000">{</font> + <b><font color="#0000FF">if</font></b> <font color="#990000">(!</font><b><font color="#000000">strcmp</font></b><font color="#990000">(</font>arg<font color="#990000">,</font> <font color="#FF0000">"version"</font><font color="#990000">))</font> + <font color="#FF0000">{</font> + cmd <font color="#990000">=</font> CMD_VERSION<font color="#990000">;</font> + <font color="#FF0000">}</font> + <b><font color="#0000FF">else</font></b> + <font color="#FF0000">{</font> + <b><font color="#000000">error</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">,</font> <font color="#FF0000">"Invalid argument: --%s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font> arg<font color="#990000">);</font> + <b><font color="#0000FF">return</font></b> <b><font color="#000000">usage</font></b><font color="#990000">();</font> + <font color="#FF0000">}</font> + <font color="#FF0000">}</font> + <b><font color="#0000FF">else</font></b> + <font color="#FF0000">{</font> + <b><font color="#0000FF">switch</font></b> <font color="#990000">(</font>c<font color="#990000">)</font> + <font color="#FF0000">{</font> + <b><font color="#0000FF">case</font></b> <font color="#FF0000">'v'</font><font color="#990000">:</font> + cmd <font color="#990000">=</font> CMD_VERSION<font color="#990000">;</font> + <b><font color="#0000FF">break</font></b><font color="#990000">;</font> +<b><font color="#008080"> default:</font></b> + <b><font color="#000000">error</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">,</font> <font color="#FF0000">"Invalid argument: -%c</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font> c<font color="#990000">);</font> + <b><font color="#0000FF">return</font></b> <b><font color="#000000">usage</font></b><font color="#990000">();</font> + <font color="#FF0000">}</font> + <font color="#FF0000">}</font> + <font color="#FF0000">}</font> + arg<font color="#990000">++;</font> + <font color="#FF0000">}</font> + + <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>cmd <font color="#990000">==</font> CMD_VERSION<font color="#990000">)</font> + <font color="#FF0000">{</font> + <b><font color="#0000FF">return</font></b> <b><font color="#000000">version</font></b><font color="#990000">();</font> + <font color="#FF0000">}</font> + + <font color="#009900">char</font> line<font color="#990000">[</font>BUFSIZE<font color="#990000">];</font> + + <b><font color="#0000FF">do</font></b> + <font color="#FF0000">{</font> + <b><font color="#000000">fgets</font></b><font color="#990000">(</font>line<font color="#990000">,</font> <b><font color="#0000FF">sizeof</font></b><font color="#990000">(</font>line<font color="#990000">),</font> stdin<font color="#990000">);</font> + <b><font color="#0000FF">if</font></b> <font color="#990000">(!</font><b><font color="#000000">feof</font></b><font color="#990000">(</font>stdin<font color="#990000">))</font> + <font color="#FF0000">{</font> + <font color="#009900">char</font> <font color="#990000">*</font>eol <font color="#990000">=</font> <b><font color="#000000">strchr</font></b><font color="#990000">(</font>line<font color="#990000">,</font> <font color="#FF0000">'</font><font color="#CC33CC">\n</font><font color="#FF0000">'</font><font color="#990000">);</font> + <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>eol<font color="#990000">)</font> <font color="#990000">*</font>eol <font color="#990000">=</font> <font color="#993399">0</font><font color="#990000">;</font> + <b><font color="#000000">fprintf</font></b><font color="#990000">(</font>stdout<font color="#990000">,</font> <font color="#FF0000">"%s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font> line<font color="#990000">);</font> + <font color="#FF0000">}</font> + <font color="#FF0000">}</font> + <b><font color="#0000FF">while</font></b> <font color="#990000">(!</font><b><font color="#000000">feof</font></b><font color="#990000">(</font>stdin<font color="#990000">));</font> + + <i><font color="#9A1900">/*error(2, "Invalid arguments\n");*/</font></i> + <b><font color="#0000FF">return</font></b> <font color="#993399">0</font><font color="#990000">;</font> +<font color="#FF0000">}</font> + +</tt></pre> diff --git a/table b/table Бинарне датотеке се разликују. diff --git a/table.c b/table.c @@ -1,6 +1,29 @@ +/* + * Command line utility to format and display a CSV file. + * Copyright (C) 2020 Страхиња Радић + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + + #include <stdarg.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <unistr.h> +#include <unistdio.h> #include "defs.h" int @@ -18,28 +41,50 @@ usage() } int -error(int code, char *fmt, ...) +error(int code, uint8_t* fmt, ...) { - char buf[BUFSIZE]; + uint8_t buf[BUFSIZE]; va_list args; va_start(args, fmt); - vsnprintf(buf, sizeof(buf), fmt, args); + u8_vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); fprintf(stderr, "%s: %s", PROGRAMNAME, buf); return code; } -int -main(int argc, char **argv) +uint8_t* +substr(uint8_t* src, int start, int finish) { - if (argc == 1) + int len = u8_strlen(src); + if (finish > len) + finish = len; + int substr_len = finish-start; + if (substr_len < 0) + substr_len = 0; + uint8_t* result = (uint8_t*) malloc(sizeof(uint8_t) * (substr_len+1)); + + for (int i = start; i < finish && *(src+i) != '\0'; i++) { - return usage(); + *result++ = *(src+i); } + *result = '\0'; + + return result-substr_len; +} - char *arg; - int cmd = CMD_NONE; - while (arg = *argv++) { +int +main(int argc, char** argv) +{ + char* arg; + int cmd = CMD_NONE; + + /*uint8_t test[BUFSIZE];*/ + /*u8_strcpy(test, "Ово је тест. Ово је ћирилични текст.");*/ + /*fprintf(stdout, "%s\n", test);*/ + /*return 0;*/ + + while ((arg = *argv++)) + { if (*arg++ == '-') { char c = *arg++; @@ -59,12 +104,12 @@ main(int argc, char **argv) { switch (c) { - case 'v': - cmd = CMD_VERSION; - break; - default: - error(1, "Invalid argument: -%c\n", c); - return usage(); + case 'v': + cmd = CMD_VERSION; + break; + default: + error(1, "Invalid argument: -%c\n", c); + return usage(); } } } @@ -76,7 +121,48 @@ main(int argc, char **argv) return version(); } - error(2, "Invalid arguments\n"); - return usage(); + uint8_t line[BUFSIZE]; + + uint8_t table_characters[] = { '+', '-', '+', + '|', ' ', '|', + '+', '-', '+', + }; + fprintf(stdout, "%c", table_characters[0]); + int col = 0; + int cols = 80; + while (col++ < cols-2) + fprintf(stdout, "%c", table_characters[1]); + fprintf(stdout, "%c\n", table_characters[2]); + do + { + fgets(line, sizeof(line), stdin); + if (!feof(stdin)) + { + uint8_t* eol = u8_strchr(line, '\n'); + if (eol) + *eol = '\0'; + fprintf(stdout, "%c", table_characters[3]); + /*uint8_t format[BUFSIZE];*/ + /*sprintf(format, "%%%ds", cols-2);*/ + uint8_t* cropped_line = substr(line, 0, cols-2); + fprintf(stdout, "%s", cropped_line); + for (int i = 0; i < cols-2-u8_strlen(cropped_line); i++) + { + fprintf(stdout, " "); + } + fprintf(stdout, "%c\n", table_characters[5]); + } + } + while (!feof(stdin)); + fprintf(stdout, "%c", table_characters[6]); + col = 0; + cols = 80; + while (col++ < cols-2) + fprintf(stdout, "%c", table_characters[7]); + fprintf(stdout, "%c\n", table_characters[8]); + + /*error(2, "Invalid arguments\n");*/ + return 0; } +