-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccwc.java
More file actions
127 lines (107 loc) · 3.68 KB
/
Copy pathccwc.java
File metadata and controls
127 lines (107 loc) · 3.68 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
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class ccwc {
public static String arg = "-a";
public static String file = "";
public static int BUFFER_SIZE = 64 * 1024;
public static int l = 0; // line count
public static int c = 0; // byte count
public static int m = 0; // character count
public static int w = 0; // word count
public static void main(String[] args) {
if (args.length > 0) {
if (args[0].startsWith("-")) {
arg = args[0];
if (args.length > 1) {
file = args[1];
}
} else {
file = args[0];
}
}
try (BufferedInputStream reader = (file.equals(""))
? new BufferedInputStream(System.in, BUFFER_SIZE)
: new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = 0;
boolean inWord = false;
int utf8BytesLeft = 0;
while ((bytesRead = reader.read(buffer)) != -1) {
c += bytesRead; // count bytes
for (int i = 0; i < bytesRead; i++) {
int b = buffer[i] & 0xFF;
if (b == '\n') { // count lines
l++;
}
if (utf8BytesLeft == 0) { // count characters
m++;
if ((b & 0x80) != 0) {
if ((b & 0xE0) == 0xC0) {
utf8BytesLeft = 1;
}
else if ((b & 0xF0) == 0xE0) {
utf8BytesLeft = 2;
}
else if ((b & 0xF8) == 0xF0) {
utf8BytesLeft = 3;
}
}
else {
utf8BytesLeft = 0;
}
}
else if ((b & 0xC0) == 0x80) {
utf8BytesLeft--;
}
if (b == ' ' || b == '\t' || b == '\n' || b == '\r') { // count words
inWord = false;
} else if (!inWord) {
inWord = true;
w++;
}
}
}
reader.close();
} catch (IOException e) {
System.out.println("File not found!");
showHelp();
return;
}
switch (arg) {
case "-c":
fPrint(file, c);
break;
case "-l":
fPrint(file, l);
break;
case "-w":
fPrint(file, w);
break;
case "-m":
fPrint(file, m);
break;
case "-a":
fPrint(file, l, w, c);
break;
default:
showHelp();
return;
}
}
public static void showHelp() {
System.out.println("usage: ccwc [options] [file]");
System.out.println("options:");
System.out.println("\t-l: Line count");
System.out.println("\t-c: Byte count");
System.out.println("\t-w: Word count");
System.out.println("\t-m: Character count");
}
public static void fPrint(String file, int... n) {
System.out.print(" ");
for (int i : n) {
System.out.print(i + " ");
}
System.out.println(file);
}
}