cleanup of some duplicate code
This commit is contained in:
parent
75aa0c2475
commit
2a733a0224
@ -33,6 +33,7 @@
|
||||
#include <cstddef> //size_t
|
||||
#include <chrono> //std::chrono::*
|
||||
#include <cstdlib> //srand
|
||||
#include <string> //char_traits
|
||||
|
||||
constexpr const char version_string[] = "roflcat version 1.0.4";
|
||||
|
||||
@ -134,59 +135,70 @@ void nonprinting_notation(int in, char* dest){
|
||||
}
|
||||
}
|
||||
|
||||
FILE* open_file(const char* file, const char* mode){
|
||||
FILE* fp = fopen(file, mode);
|
||||
if(!fp){
|
||||
fflush(stdout);
|
||||
fprintf(stderr, "Unable to open file %s\n", file);
|
||||
}
|
||||
return fp;
|
||||
}
|
||||
|
||||
template<class T, class Printer, class Func>
|
||||
inline void do_stdin(Printer&& p, Func&& f){
|
||||
for(T in;(in = f(stdin)) != std::char_traits<T>::eof();){
|
||||
p.print(in);
|
||||
if(in == '\n')
|
||||
p.reset();
|
||||
}
|
||||
clearerr(stdin);
|
||||
}
|
||||
//Use the given type of printer to output the contents of files and/or stdin
|
||||
template<class Printer>
|
||||
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
|
||||
|
||||
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<char>(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<wchar_t>(in));
|
||||
if(in == L'\n')
|
||||
p.reset();
|
||||
}
|
||||
}
|
||||
clearerr(stdin);
|
||||
|
||||
do_stdin<char>(p, fgetc);
|
||||
}else{ //everything besides stdin
|
||||
|
||||
FILE* fp = fopen(args.filenames[i], "r");
|
||||
FILE* fp = open_file(args.filenames[i], "rb");
|
||||
if(!fp){
|
||||
fflush(stdout); //force color update before error
|
||||
fprintf(stderr, "Unable to open file %s\n", args.filenames[i]);
|
||||
ret = 2;
|
||||
continue;
|
||||
}
|
||||
if(args.treatbinary){
|
||||
|
||||
if(args.nonprinting){
|
||||
for(int in;(in = fgetc(fp)) != WEOF;){
|
||||
for(char in;(in = fgetc(fp)) != EOF;){
|
||||
char tmp[5];
|
||||
nonprinting_notation(in, tmp);
|
||||
p.print(tmp);
|
||||
}
|
||||
}else{
|
||||
for(int in;(in = fgetc(fp)) != WEOF;)
|
||||
p.print(static_cast<char>(in));
|
||||
}
|
||||
}else{
|
||||
//set to wide character mode before anything
|
||||
fwide(fp, 1);
|
||||
for(wint_t in;(in = fgetwc(fp)) != WEOF;)
|
||||
p.print(static_cast<wchar_t>(in));
|
||||
for(char in;(in = fgetc(fp)) != EOF;)
|
||||
p.print(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<wchar_t>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
p.reset();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user