summaryrefslogblamecommitdiff
path: root/ASCII-Trim.cxx
blob: ea5a2c0203f274dd7d0f0f67fa56a8608f7984ab (plain) (tree)









































































































                                                                              
#include <fstream.h>
#include "CXX:Classes/FileInfoX.cxx"

char* ver	= "$VER:ASCII-Trim v2.23\n";
char* title = &ver[5];
char* usage = "USAGE: 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;
}