чување 0c51010dca54dc9191dbff187be84a3db0129eaa
родитељ 1d7e61494df96f477d8196e20f1c346a46f6dbab
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Tue, 22 Sep 2020 18:58:22 +0200
strlen -> strwidth; changed symbols array to use enum
Diffstat:
| M | defs.h | | | 18 | ++++++++++++------ |
| D | t.c | | | 102 | ------------------------------------------------------------------------------- |
| D | t.c.html | | | 116 | ------------------------------------------------------------------------------- |
| M | table | | | 0 | |
| M | table.c | | | 25 | ++++++++++++------------- |
измењених датотека: 5, додавања: 24(+), брисања: 237(-)
diff --git a/defs.h b/defs.h
@@ -1,5 +1,5 @@
/*
- * Command line utility to format and display a CSV file.
+ * Command line utility to format and display CSV.
* Copyright (C) 2020 Страхиња Радић
*
* This program is free software: you can redistribute it and/or modify it
@@ -25,12 +25,18 @@
#define BUFSIZE 4096
-#define CMD_NONE 0
-#define CMD_VERSION 1
+typedef enum
+{
+ CMD_NONE,
+ CMD_VERSION
+} Command;
-#define TABLE_SYMBOLS_ASCII 0
-#define TABLE_SYMBOLS_SINGLE 1
-#define TABLE_SYMBOLS_DOUBLE 2
+enum
+{
+ TABLE_SYMBOLS_ASCII,
+ TABLE_SYMBOLS_SINGLE,
+ TABLE_SYMBOLS_DOUBLE
+};
#endif
diff --git a/t.c b/t.c
@@ -1,102 +0,0 @@
-/*
- * 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
@@ -1,116 +0,0 @@
-<!-- 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"><https://www.gnu.org/licenses/></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"><stdarg.h></font>
-<b><font color="#000080">#include</font></b> <font color="#FF0000"><stdio.h></font>
-<b><font color="#000080">#include</font></b> <font color="#FF0000"><string.h></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,5 +1,5 @@
/*
- * Command line utility to format and display a CSV file.
+ * Command line utility to format and display CSV.
* Copyright (C) 2020 Страхиња Радић
*
* This program is free software: you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
#include <string.h>
#include <unistr.h>
#include <unistdio.h>
+#include <uniwidth.h>
#include "defs.h"
int
@@ -35,7 +36,10 @@ version()
int
usage()
{
- printf("Usage: %s [-v|--version]\n", PROGRAMNAME);
+ printf("Usage: %s [options]\n", PROGRAMNAME);
+ printf(" Where [options] is one or more of the following:\n");
+ printf(" -h or --help Prints this usage information screen\n");
+ printf(" -v or --version Prints program version and exits\n");
return 0;
}
@@ -75,12 +79,7 @@ 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;*/
+ Command cmd = CMD_NONE;
while ((arg = *argv++))
{
@@ -122,21 +121,21 @@ main(int argc, char** argv)
uint8_t line[BUFSIZE];
- uint8_t* table_symbols[][9] =
+ static const uint8_t* table_symbols[][9] =
{
- {
+ [TABLE_SYMBOLS_ASCII] = {
// ascii
(uint8_t*)"+", (uint8_t*)"-", (uint8_t*)"+",
(uint8_t*)"|", (uint8_t*)" ", (uint8_t*)"|",
(uint8_t*)"+", (uint8_t*)"-", (uint8_t*)"+",
},
- {
+ [TABLE_SYMBOLS_SINGLE] = {
// single
(uint8_t*)"\u250c", (uint8_t*)"\u2500", (uint8_t*)"\u2510",
(uint8_t*)"\u2502", (uint8_t*)" ", (uint8_t*)"\u2502",
(uint8_t*)"\u2514", (uint8_t*)"\u2500", (uint8_t*)"\u2518",
},
- {
+ [TABLE_SYMBOLS_DOUBLE] = {
// double
(uint8_t*)"\u2554", (uint8_t*)"\u2550", (uint8_t*)"\u2557",
(uint8_t*)"\u2551", (uint8_t*)" ", (uint8_t*)"\u2551",
@@ -166,7 +165,7 @@ main(int argc, char** argv)
uint8_t* cropped_line = substr(line, 0, cols-2);
fprintf(stdout, "%s", cropped_line);
/*for (int i = 0; i < cols-2-u8_mblen(cropped_line, BUFSIZE); i++)*/
- for (int i = 0; i < cols-2-u8_strlen(cropped_line); i++)
+ for (int i = 0; i < cols-2-u8_strwidth(cropped_line, "utf-8"); i++)
/*for (int i = 0; i < cols-2-strlen(cropped_line); i++)*/
{
fprintf(stdout, " ");