-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.cpp
More file actions
166 lines (152 loc) · 3.31 KB
/
count.cpp
File metadata and controls
166 lines (152 loc) · 3.31 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
static char rcsid[] = "$Id: count.cpp,v 1.8 2013-09-24 17:40:52-07 sglaser Exp $";
struct fdata_t {
static unsigned int max_fname_len;
static unsigned int nup;
static unsigned int nrows;
bool processed;
unsigned int max_col;
unsigned int lines;
unsigned int pages;
const char* fname;
fdata_t(const char* fname_p = 0) :
fname(fname_p),
max_col(0),
lines(0),
pages(0),
processed(false)
{
int len = (fname_p != 0) ? strlen(fname_p) : 0;
if (len > max_fname_len)
max_fname_len = len;
}
fdata_t& operator + (const fdata_t& f) {
if (max_col < f.max_col)
max_col = f.max_col;
lines += f.lines;
pages += f.pages;
if (fdata_t::nup == 2) {
if ((f.pages % 2) == 1)
pages++;
}
return *this;
}
};
unsigned int fdata_t::max_fname_len = 0;
unsigned int fdata_t::nup = 2;
unsigned int fdata_t::nrows = 50;
const char* program_name = "??";
ostream& operator<< (ostream& s, const fdata_t& f) {
s << setw(f.max_fname_len+2) << f.fname;
s << setw(5) << f.pages << " page" << ((f.pages > 1) ? "s " : " ");
s << setw(6) << f.lines << " line" << ((f.lines > 1) ? "s " : " ");
s << setw(6) << f.max_col << " max col" << endl;
return s;
}
void
process(fdata_t& f)
{
ifstream fs;
istream* s = 0;
if (f.fname != 0) {
fs.open(f.fname);
if (fs) s = &fs;
} else {
s = &cin;
}
if (s != 0) {
int c;
f.lines = 0;
f.pages = 0;
f.max_col = 0;
int col = 0;
int row = 0;
c = (*s).get();
while (*s && (c != EOF)) {
if (c == '\f') {
f.lines++;
f.pages++;
col = 0;
row = 0;
} else if (c == '\n') {
f.lines++;
if (row++ > f.nrows) {
f.pages++;
row = 0;
}
col = 0;
} else if (c == '\t') {
while ((++col) % 8 != 0) {
/* nothing */
}
} else {
col++;
}
if (col > f.max_col) {
f.max_col = col;
}
c = (*s).get();
}
if ((row > 0) || (col > 0)) {
f.lines++;
f.pages++;
row = 0;
col = 0;
}
f.processed = true;
if (f.fname) fs.close();
} else {
cerr << program_name << ": Can't open " << f.fname << " ignroring file" << endl;
}
}
int
main(int argc, char** argv)
{
vector<fdata_t> files;
program_name = argv[0];
if (argc == 1) {
cerr << "Usage: count [-1] [-2] [-50] [-66] [files or -]" << endl;
exit(1);
}
for (int i = 1; i < argc; i++) {
//cout << i << " " << argv[i] << endl;
if (strcmp(argv[i], "-") == 0) {
fdata_t fdata_stdin(0);
files.push_back(fdata_stdin);
} else if (strcmp(argv[i], "-1") == 0) {
fdata_t::nup = 1;
} else if (strcmp(argv[i], "-2") == 0) {
fdata_t::nup = 2;
} else if (strcmp(argv[i], "-50") == 0) {
fdata_t::nrows = 50;
} else if (strcmp(argv[i], "-66") == 0) {
fdata_t::nrows = 66;
} else {
fdata_t fdata_argv(argv[i]);
files.push_back(fdata_argv);
}
}
vector<fdata_t>::iterator fi;
for (fi = files.begin(); fi != files.end(); ++fi) {
process(*fi);
}
fdata_t total("TOTAL-->");
int i = 0;
for (fi = files.begin(); fi != files.end(); ++fi) {
if (fi->processed) {
cout << *fi;
total = total + *fi;
i++;
}
}
if (i > 1) {
cout << total;
}
return 0;
}