From 49a1f0456f00d10b19f0ff97dc42e1c0e11d772a Mon Sep 17 00:00:00 2001 From: rexy712 Date: Mon, 22 Jul 2019 13:29:53 -0700 Subject: [PATCH 1/7] Output error on directory --- src/roflcat.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/roflcat.cpp b/src/roflcat.cpp index 4ae6476..176e923 100644 --- a/src/roflcat.cpp +++ b/src/roflcat.cpp @@ -34,6 +34,8 @@ #include //std::chrono::* #include //srand #include //char_traits +#include //struct stat +#include //struct stat constexpr const char version_string[] = "roflcat version 1.0.4"; @@ -135,11 +137,23 @@ void nonprinting_notation(int in, char* dest){ } } +bool is_directory(const char* file){ + struct stat st; + if(stat(file, &st) != 0){ + return true; + } + return S_ISDIR(st.st_mode); +} FILE* open_file(const char* file, const char* mode){ + if(is_directory(file)){ + fflush(stdout); + fprintf(stderr, "Unable to open file \"%s\": Is a directory \n", file); + return nullptr; + } FILE* fp = fopen(file, mode); if(!fp){ fflush(stdout); - fprintf(stderr, "Unable to open file %s\n", file); + fprintf(stderr, "Unable to open file for reading: \"%s\"\n", file); } return fp; } From c0abcee00bdcf45dbb583bc968bee3566b848873 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Mon, 22 Jul 2019 13:32:11 -0700 Subject: [PATCH 2/7] Version bump --- src/roflcat.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/roflcat.cpp b/src/roflcat.cpp index 176e923..b1dbc9e 100644 --- a/src/roflcat.cpp +++ b/src/roflcat.cpp @@ -37,7 +37,7 @@ #include //struct stat #include //struct stat -constexpr const char version_string[] = "roflcat version 1.0.4"; +constexpr const char version_string[] = "roflcat version 1.1"; static constexpr size_t constexpr_strlen(const char* str){ size_t i = 0; From 72891834c20b10bff268ff5fdb145c3134af84bb Mon Sep 17 00:00:00 2001 From: rexy712 Date: Mon, 22 Jul 2019 13:50:46 -0700 Subject: [PATCH 3/7] Updated Readme and added public repo to help output --- README.md | 29 ++++++++++++++++++++--------- src/roflcat.cpp | 1 + 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 444090c..c0a54c7 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,42 @@ # README ## About -roflcat is a C++ rewrite of [lolcat][1] which runs much faster and supports all the options of GNU cat. I am not going to support the animation options of lolcat (--animate, --duration, --speed) because there is no real purpose to them. +roflcat is a C++ rewrite of [lolcat][1] which runs much faster and supports all the options of GNU cat. I am not going to support the animation options of lolcat (--animate, --duration, --speed) because there is no real purpose to them. Currently I can only confirm it runs on a linux system and I have no desire to expand to supporting anything else at this time. To keep compatability with both GNU cat and lolcat, I wanted all command line options from both to either be implemented or ignored. However there are a few conflicts between the two, namely -s -t and -v. For these I will be implementing the GNU cat functionality. -Current version is 0.1a as of writing this, so if the version is much newer, remind me to update this readme :) -![alt text](https://i.postimg.cc/1tYmJzxd/roflcat-usage.png "Screenshot of --help output") +Current version is 1.1 as of writing this, so if the version is much newer, remind me to update this readme :) +![alt text](https://i.postimg.cc/0Qk6k34j/roflcat-usage.png "Screenshot of --help output") + +## Dependencies +ncurses +cmake ## Building -There is no release right now. There is only the debug build which will produce an executable called 'tester'. -To build just run `make`. +``` +mkdir build +cd build +cmake .. +make +``` ## Installing -I would not recommend installing at this time. There is no install target in the makefile. +From within the build directory: +``` +make install +``` ## More Screenshots Some test outputs on a file filled with laughing cat emojis -256 color: +256 color: ![alt text](https://i.postimg.cc/1tZC2k6r/roflcat-roflcats.png "roflcats in 256 color") -Truecolor (24 bit): +Truecolor (24 bit): ![alt text](https://i.postimg.cc/tJF2527m/roflcat-roflcats-truecolor.png "roflcats in truecolor") -16 color (type that will work in a tty): +16 color (type that will work in a tty): ![alt text](https://i.postimg.cc/LX99RbFj/roflcat-roflcats-16color.png "roflcats in 16 color") [1]: https://github.com/busyloop/lolcat diff --git a/src/roflcat.cpp b/src/roflcat.cpp index b1dbc9e..7933d01 100644 --- a/src/roflcat.cpp +++ b/src/roflcat.cpp @@ -94,6 +94,7 @@ constexpr void print_usage(Printer&& p){ p.print(L'\n'); } p.print(L"\nroflcat Copyright (C) 2019 rexy712\n"); + p.print(L"Source code can be found at https://gitlab.com/rexy712/roflcat\n"); p.print(L"This program comes with ABSOLUTELY NO WARRANTY\n"); p.print(L"This is free software, and you are welcome to redistribute it\n"); p.print(L"under certain conditions; see the GNU GPLv3 for details.\n"); From e3586a9e93f8a02444ecfc436937c270e5625951 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Mon, 22 Jul 2019 14:13:16 -0700 Subject: [PATCH 4/7] Reverted 2a733a0. Fixes binary printing --- src/roflcat.cpp | 67 ++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/src/roflcat.cpp b/src/roflcat.cpp index 7933d01..1c212c0 100644 --- a/src/roflcat.cpp +++ b/src/roflcat.cpp @@ -173,47 +173,52 @@ template int real_print_files(const cmd_args& args){ int ret = 0; Printer p(args); - if(args.treatbinary){ //binary files - for(size_t i = 0;i < args.filenames.size();++i){ - if(!strcmp(args.filenames[i], "-")){ //stdin - do_stdin(p, fgetc); - }else{ //everything besides stdin - FILE* fp = open_file(args.filenames[i], "rb"); - if(!fp){ - ret = 2; - continue; - } + for(size_t i = 0;i < args.filenames.size();++i){ + if(!strcmp(args.filenames[i], "-")){ //stdin + if(args.treatbinary){ + //Don't check if buf is empty because that's not how GNU cat handles Ctrl+D in middle of line + for(int in;(in = fgetc(stdin)) != WEOF;){ + p.print(static_cast(in)); + if(in == L'\n') //Only reset on newline to save print overhead + p.reset(); + } + }else{ + for(wint_t in;(in = fgetwc(stdin)) != WEOF;){ + p.print(static_cast(in)); + if(in == L'\n') + p.reset(); + } + } + clearerr(stdin); + + }else{ //everything besides stdin + + FILE* fp = open_file(args.filenames[i], "r"); + if(!fp){ + ret = 2; + continue; + } + if(args.treatbinary){ if(args.nonprinting){ - for(char in;(in = fgetc(fp)) != EOF;){ + for(int in;(in = fgetc(fp)) != WEOF;){ char tmp[5]; nonprinting_notation(in, tmp); p.print(tmp); } }else{ - for(char in;(in = fgetc(fp)) != EOF;) - p.print(in); + for(int in;(in = fgetc(fp)) != WEOF;) + p.print(static_cast(in)); } - p.reset(); - fclose(fp); - } - } - }else{ //non binary files - for(size_t i = 0;i < args.filenames.size();++i){ - if(!strcmp(args.filenames[i], "-")){ - do_stdin(p, fgetwc); }else{ - FILE* fp = open_file(args.filenames[i], "r"); - if(!fp){ - ret = 2; - continue; - } - for(wchar_t in;(in = fgetwc(fp)) != WEOF;){ - p.print(in); - } - p.reset(); - fclose(fp); + //set to wide character mode before anything + fwide(fp, 1); + for(wint_t in;(in = fgetwc(fp)) != WEOF;) + p.print(static_cast(in)); } + p.reset(); + fclose(fp); + } } p.reset(); From ca79f26d4e9a1100bd7c0b024d5fdedad493388a Mon Sep 17 00:00:00 2001 From: rexy712 Date: Mon, 16 Sep 2019 16:16:14 -0700 Subject: [PATCH 5/7] Fixed error output on nonexistent file --- src/roflcat.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/roflcat.cpp b/src/roflcat.cpp index 1c212c0..5e27f52 100644 --- a/src/roflcat.cpp +++ b/src/roflcat.cpp @@ -141,16 +141,25 @@ void nonprinting_notation(int in, char* dest){ bool is_directory(const char* file){ struct stat st; if(stat(file, &st) != 0){ - return true; + return false; } return S_ISDIR(st.st_mode); } +bool file_exists(const char* file){ + struct stat st; + return stat(file, &st) == 0; +} FILE* open_file(const char* file, const char* mode){ if(is_directory(file)){ fflush(stdout); fprintf(stderr, "Unable to open file \"%s\": Is a directory \n", file); return nullptr; } + if(!file_exists(file)){ + fflush(stdout); + fprintf(stderr, "Unable to open file \"%s\": No such file\n", file); + return nullptr; + } FILE* fp = fopen(file, mode); if(!fp){ fflush(stdout); From ee4c9a6ea1051b18426a23b56ff8af459025f152 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Thu, 24 Oct 2019 12:19:42 -0700 Subject: [PATCH 6/7] add to todo --- TODO | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO b/TODO index c86b517..7a06794 100644 --- a/TODO +++ b/TODO @@ -1 +1,2 @@ move GNU cat logic out of printer class to make it more modular +fix ^` not printing From 3928766c6bbe38fb2a7cc1e6821e434b275aea5d Mon Sep 17 00:00:00 2001 From: rexy712 Date: Fri, 27 Dec 2019 10:21:59 -0800 Subject: [PATCH 7/7] Fix uninitialized variable in cmdargs and incorrect WEOF usage --- src/cmd.cpp | 2 +- src/roflcat.cpp | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/cmd.cpp b/src/cmd.cpp index de49aa0..b98e8ec 100644 --- a/src/cmd.cpp +++ b/src/cmd.cpp @@ -25,7 +25,7 @@ #include //atol cmd_args::cmd_args(void)noexcept: - truecol(0), color(0), number(0), ends(0), squeeze(0), tabs(0), nonprinting(0), error(0), usage(0), version(0){} + truecol(0), color(0), number(0), ends(0), squeeze(0), tabs(0), nonprinting(0), treatbinary(0), error(0), usage(0), version(0){} void cmd_args::clear_gnu_options(void){ number = PRINT_LINE_NEVER; ends = squeeze = tabs = nonprinting = 0; diff --git a/src/roflcat.cpp b/src/roflcat.cpp index 5e27f52..d7dee88 100644 --- a/src/roflcat.cpp +++ b/src/roflcat.cpp @@ -187,7 +187,7 @@ int real_print_files(const cmd_args& args){ if(args.treatbinary){ //Don't check if buf is empty because that's not how GNU cat handles Ctrl+D in middle of line - for(int in;(in = fgetc(stdin)) != WEOF;){ + for(int in;(in = fgetc(stdin)) != EOF;){ p.print(static_cast(in)); if(in == L'\n') //Only reset on newline to save print overhead p.reset(); @@ -210,13 +210,13 @@ int real_print_files(const cmd_args& args){ } if(args.treatbinary){ if(args.nonprinting){ - for(int in;(in = fgetc(fp)) != WEOF;){ + for(int in;(in = fgetc(fp)) != EOF;){ char tmp[5]; nonprinting_notation(in, tmp); p.print(tmp); } }else{ - for(int in;(in = fgetc(fp)) != WEOF;) + for(int in;(in = fgetc(fp)) != EOF;) p.print(static_cast(in)); } }else{ @@ -276,6 +276,5 @@ int main(int argc, char** argv){ }else if(avail_colors == 256){ return print_files>(args); } - return 0; }