diff options
author | Steffen Jensen | 2021-10-13 19:59:08 +0200 |
---|---|---|
committer | Steffen Jensen | 2021-10-13 19:59:08 +0200 |
commit | 2e870a0305607e71f3a1b02823e9f01eee05127f (patch) | |
tree | f51fc6fa3364d4db4ac0e510b7868e84fc1a47f0 |
-rw-r--r-- | ASCII-Trim.cxx | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/ASCII-Trim.cxx b/ASCII-Trim.cxx new file mode 100644 index 0000000..ea5a2c0 --- /dev/null +++ b/ASCII-Trim.cxx @@ -0,0 +1,106 @@ +#include <fstream.h> +#include "CXX:Classes/FileInfoX.cxx" + +char* ver = "$VER:[1;0;1mASCII-Trim v2.23[;;m\n"; +char* title = &ver[5]; +char* usage = "[1;0;1mUSAGE:[;;m ASCII-Trim INFILE/A,OUTFILE/A"; +char* err_file="FILE ERROR\n\n"; + +int main(int argc, char** argv) +{ + if (argc != 3) {cout<<title<<usage<<endl;return 0;} + + cout<<"Triming "<<argv[1]<<" to "<<argv[2]<<"..."<<endl; + + ifstream in(argv[1]); + if (in.fail()){ + cout<<err_file; + return 5;} + + char respons; + FileInfoX fix(argv[2]); + switch (fix.Status()){ + case FileInfoX::failed: + break; + + case FileInfoX::dir: + cout<<"Destination was a directory!"<<endl; + return 5; + + case FileInfoX::file: + cout<<argv[2]<<" already exists! Replace? (y/N)"<<flush; + cin.get(respons); + switch (respons){ + case 'y': + case 'Y': + break; + + default: + cout<<"Aborted!"<<endl; + return 5;} + } + ofstream out(argv[2]); + if (out.fail()){ + cout<<err_file; + return 5;} + + int spaces=0; + int garbage=0; + int tabs=0; + unsigned char c; + char comment[80]; + long protection; + FileInfoX in_data(argv[1]); + + + while (in.peek() != EOF) + { + in.get(c); + if (c<32 && c!='\n' && c!='\t' && c!=27) {garbage++;continue;} + + switch (c) + { + case '\t': + while (spaces--) out.put(' '); + spaces=0; + tabs++; + break; + + case ' ': + while (tabs--) out.put('\t'); + tabs=0; + spaces++; + break; + + case '\n': + garbage+=spaces; + garbage+=tabs; + spaces=0; + tabs=0; + out<<endl; + break; + + default: + while (spaces--) out.put(' '); + while (tabs--) out.put('\t'); + spaces=tabs=0; + out.put(c); + } + } + + // Close streams for good orders sake + in.close(); + out.close(); + + // Get comment from infile and set to outfile + in_data.Comment(comment); + ::SetComment(argv[2],comment); + + // Get protection bits from infile and set to outfile + protection =in_data.CompareProtBits(FileInfoX::protection(~0)); + ::SetProtection(argv[2],protection); + + if (garbage) cout<<garbage<<" bytes saved"<<endl<<endl; + else cout<<"Unchanged"<<endl<<endl; + return 0; +} |