Combine binary and normal printing function to save binary space and remove duplicate code
This commit is contained in:
parent
cb55e21846
commit
75aa0c2475
104
src/roflcat.cpp
104
src/roflcat.cpp
@ -135,77 +135,58 @@ void nonprinting_notation(int in, char* dest){
|
||||
}
|
||||
|
||||
//Use the given type of printer to output the contents of files and/or stdin
|
||||
//treat the file as text in the current locale
|
||||
template<class Printer>
|
||||
int print_normal_files(const cmd_args& args){
|
||||
int real_print_files(const cmd_args& args){
|
||||
int ret = 0;
|
||||
Printer p(args);
|
||||
for(size_t i = 0;i < args.filenames.size();++i){
|
||||
if(!strcmp(args.filenames[i], "-")){ //stdin
|
||||
|
||||
//Don't check if buf is empty because that's not how GNU cat handles Ctrl+D in middle of line
|
||||
for(wint_t in;(in = fgetwc(stdin)) != WEOF;){
|
||||
p.print(static_cast<wchar_t>(in));
|
||||
if(in == L'\n') //Only reset on newline to save print overhead
|
||||
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;
|
||||
}
|
||||
//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));
|
||||
p.reset();
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
p.reset();
|
||||
return ret;
|
||||
}
|
||||
//Use the given type of printer to output the contents of files and/or stdin
|
||||
//Treat the file as a binary file (ascii)
|
||||
template<class Printer>
|
||||
int print_binary_files(const cmd_args& args){
|
||||
int ret = 0;
|
||||
Printer p(args);
|
||||
for(size_t i = 0;i < args.filenames.size();++i){
|
||||
if(!strcmp(args.filenames[i], "-")){ //stdin
|
||||
|
||||
//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();
|
||||
}
|
||||
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.nonprinting){
|
||||
for(int in;(in = fgetc(fp)) != WEOF;){
|
||||
char tmp[5];
|
||||
nonprinting_notation(in, tmp);
|
||||
p.print(tmp);
|
||||
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(int in;(in = fgetc(fp)) != WEOF;){
|
||||
p.print(static_cast<char>(in));
|
||||
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){
|
||||
for(int in;(in = fgetc(fp)) != WEOF;){
|
||||
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));
|
||||
}
|
||||
p.reset();
|
||||
fclose(fp);
|
||||
|
||||
}
|
||||
}
|
||||
p.reset();
|
||||
@ -223,10 +204,7 @@ int print_files(cmd_args& args){
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(args.treatbinary)
|
||||
return print_binary_files<Printer>(args);
|
||||
else
|
||||
return print_normal_files<Printer>(args);
|
||||
return real_print_files<Printer>(args);
|
||||
}
|
||||
|
||||
auto get_time(void){
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user