Trim ascii files
This commit is contained in:
commit
2e870a0305
1 changed files with 106 additions and 0 deletions
106
ASCII-Trim.cxx
Normal file
106
ASCII-Trim.cxx
Normal file
|
@ -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;
|
||||
}
|
Loading…
Add table
Reference in a new issue