cleanup of some duplicate code
This commit is contained in:
parent
75aa0c2475
commit
2a733a0224
@ -33,6 +33,7 @@
|
|||||||
#include <cstddef> //size_t
|
#include <cstddef> //size_t
|
||||||
#include <chrono> //std::chrono::*
|
#include <chrono> //std::chrono::*
|
||||||
#include <cstdlib> //srand
|
#include <cstdlib> //srand
|
||||||
|
#include <string> //char_traits
|
||||||
|
|
||||||
constexpr const char version_string[] = "roflcat version 1.0.4";
|
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
|
//Use the given type of printer to output the contents of files and/or stdin
|
||||||
template<class Printer>
|
template<class Printer>
|
||||||
int real_print_files(const cmd_args& args){
|
int real_print_files(const cmd_args& args){
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
Printer p(args);
|
Printer p(args);
|
||||||
for(size_t i = 0;i < args.filenames.size();++i){
|
if(args.treatbinary){ //binary files
|
||||||
if(!strcmp(args.filenames[i], "-")){ //stdin
|
for(size_t i = 0;i < args.filenames.size();++i){
|
||||||
|
if(!strcmp(args.filenames[i], "-")){ //stdin
|
||||||
if(args.treatbinary){
|
do_stdin<char>(p, fgetc);
|
||||||
//Don't check if buf is empty because that's not how GNU cat handles Ctrl+D in middle of line
|
}else{ //everything besides stdin
|
||||||
for(int in;(in = fgetc(stdin)) != WEOF;){
|
FILE* fp = open_file(args.filenames[i], "rb");
|
||||||
p.print(static_cast<char>(in));
|
if(!fp){
|
||||||
if(in == L'\n') //Only reset on newline to save print overhead
|
ret = 2;
|
||||||
p.reset();
|
continue;
|
||||||
}
|
}
|
||||||
}else{
|
|
||||||
for(wint_t in;(in = fgetwc(stdin)) != WEOF;){
|
|
||||||
p.print(static_cast<wchar_t>(in));
|
|
||||||
if(in == L'\n')
|
|
||||||
p.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
clearerr(stdin);
|
|
||||||
|
|
||||||
}else{ //everything besides stdin
|
|
||||||
|
|
||||||
FILE* fp = fopen(args.filenames[i], "r");
|
|
||||||
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){
|
if(args.nonprinting){
|
||||||
for(int in;(in = fgetc(fp)) != WEOF;){
|
for(char in;(in = fgetc(fp)) != EOF;){
|
||||||
char tmp[5];
|
char tmp[5];
|
||||||
nonprinting_notation(in, tmp);
|
nonprinting_notation(in, tmp);
|
||||||
p.print(tmp);
|
p.print(tmp);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
for(int in;(in = fgetc(fp)) != WEOF;)
|
for(char in;(in = fgetc(fp)) != EOF;)
|
||||||
p.print(static_cast<char>(in));
|
p.print(in);
|
||||||
}
|
}
|
||||||
}else{
|
p.reset();
|
||||||
//set to wide character mode before anything
|
fclose(fp);
|
||||||
fwide(fp, 1);
|
}
|
||||||
for(wint_t in;(in = fgetwc(fp)) != WEOF;)
|
}
|
||||||
p.print(static_cast<wchar_t>(in));
|
}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();
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.reset();
|
p.reset();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user