Skip to content

Commit 1df063b

Browse files
committed
CatCommand with support for common flags
- Concatenates and displays file contents, mimicking Unix's cat utility. - Handles flags -n (number all lines), -b (number non-blank lines), -e (display end-of-line characters), and -r (replace multiple empty lines). - Supports multiple file arguments and flag combinations. Included cross-platform path normalization.
1 parent f885ca3 commit 1df063b

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed
Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,110 @@
11
package com.unixtools.command.filecontent;
22

3-
public class CatCommand {
4-
3+
import com.unixtools.core.Command;
4+
5+
import java.io.*;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class CatCommand implements Command {
12+
private static final String VALID_FLAGS = "nber";
13+
private boolean numberLines = false;
14+
private boolean numberNonBlankLines = false;
15+
private boolean showEndOfLine = false;
16+
private boolean replaceMultipleEmptyLines = false;
17+
18+
@Override
19+
public void execute(String[] args) {
20+
List<String> flags = new ArrayList<>();
21+
List<String> filePaths = new ArrayList<>();
22+
parseArguments(args, flags, filePaths);
23+
24+
if (!areFlagsValid(flags)) {
25+
System.out.println("Invalid flags. Valid flags are: " + VALID_FLAGS);
26+
return;
27+
}
28+
29+
for (String filePath : filePaths) {
30+
readFileContents(normalizePath(filePath));
31+
}
32+
}
33+
34+
private void parseArguments(String[] args, List<String> flags, List<String> paths) {
35+
for (String arg : args) {
36+
if (arg.startsWith("-")) {
37+
for (char flag : arg.substring(1).toCharArray()) {
38+
switch (flag) {
39+
case 'n':
40+
numberLines = true;
41+
break;
42+
case 'b':
43+
numberNonBlankLines = true;
44+
break;
45+
case 'e':
46+
showEndOfLine = true;
47+
break;
48+
case 'r':
49+
replaceMultipleEmptyLines = true;
50+
break;
51+
default:
52+
flags.add(String.valueOf(flag));
53+
break;
54+
}
55+
}
56+
} else {
57+
paths.add(arg);
58+
}
59+
}
60+
}
61+
62+
private boolean areFlagsValid(List<String> flags) {
63+
for (String flag : flags) {
64+
if (!VALID_FLAGS.contains(flag)) {
65+
return false;
66+
}
67+
}
68+
return true;
69+
}
70+
71+
private void readFileContents(String filePath) {
72+
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
73+
String line;
74+
int lineNumber = 0;
75+
boolean lastLineEmpty = false;
76+
77+
while ((line = reader.readLine()) != null) {
78+
boolean isLineEmpty = line.trim().isEmpty();
79+
80+
if (replaceMultipleEmptyLines && isLineEmpty) {
81+
if (lastLineEmpty)
82+
continue;
83+
lastLineEmpty = true;
84+
} else {
85+
lastLineEmpty = false;
86+
}
87+
88+
if (numberLines) {
89+
System.out.printf("%6d ", ++lineNumber);
90+
} else if (numberNonBlankLines && !isLineEmpty) {
91+
System.out.printf("%6d ", ++lineNumber);
92+
}
93+
94+
System.out.print(line);
95+
if (showEndOfLine) {
96+
System.out.print("$");
97+
}
98+
System.out.println();
99+
}
100+
} catch (IOException e) {
101+
System.out.println("Error reading file: " + e.getMessage());
102+
}
103+
}
104+
105+
private String normalizePath(String path) {
106+
String normalizedPath = path.replace("\\", "/");
107+
Path pathObj = Paths.get(normalizedPath).normalize();
108+
return pathObj.toString();
109+
}
5110
}

src/com/unixtools/core/CommandFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public static Command getCommand(String commandName) {
5050
*/
5151
case "echo":
5252
return new EchoCommand();
53+
case "cat":
54+
return new CatCommand();
5355
default:
5456
return null;
5557
}

0 commit comments

Comments
 (0)