summaryrefslogtreecommitdiff
path: root/ASCII-Trim.cxx
blob: ea5a2c0203f274dd7d0f0f67fa56a8608f7984ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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;
}