Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCCommandLineOptionTests/SimpleGrep.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2004, 2004 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
import java.io.BufferedReader;
23
import java.io.FileInputStream;
24
import java.io.FileNotFoundException;
25
import java.io.FileReader;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.InputStreamReader;
29
//import java.util.regex.Pattern;
30
31
/**
32
* Check for presence of text in specified file or stdin
33
*/
34
public class SimpleGrep
35
{
36
static String sSCCSid="%Z%%M% %I% %W% %G% %U%";
37
38
public static void main(String[] args) throws IOException
39
{
40
boolean result = false;
41
switch (args.length)
42
{
43
case 2 :
44
result = searchForStringInFile(args[0], args[1]);
45
break;
46
case 3 :
47
result = searchForStringInFile(args[0], args[1], args[2]);
48
break;
49
case 0 :
50
default :
51
System.err.println("usage: SimpleGrep <string> <filename> [<count>]");
52
System.err.println(" search for <string> in <filename>");
53
System.err.println(" if <min> is specified, search for at <count> matching lines");
54
break;
55
}
56
printResult(result);
57
}
58
public static boolean searchForStringInFile(String searchText, String filename, String count) throws IOException, FileNotFoundException
59
{
60
boolean result;
61
//System.out.println("Searching for " + count + " occurences of \"" + searchText + "\" in file \"" + filename + "\"");
62
int actualCount = countLinesContainingString(searchText, new FileInputStream(filename));
63
int expectedCount = Integer.parseInt(count);
64
//System.out.println("Found " + actualCount + " matching line" + (actualCount == 1 ? "." : "s."));
65
result = expectedCount == actualCount;
66
if (!result)
67
displayFile(filename);
68
return result;
69
}
70
public static boolean searchForStringInFile(String searchText, String filename) throws IOException, FileNotFoundException
71
{
72
boolean result;
73
//System.out.println("Searching for \"" + searchText + "\" in file \"" + filename + "\"");
74
result = searchForString(searchText, new FileInputStream(filename));
75
//System.out.println("Found" + (result ? " " : " no ") + "match.");
76
if (!result)
77
displayFile(filename);
78
return result;
79
}
80
public static boolean searchForString(String searchString, InputStream in) throws IOException
81
{
82
// create a buffered reader with a 1Mb buffer
83
BufferedReader br = new BufferedReader(new InputStreamReader(in));
84
85
for (String line = br.readLine(); line != null; line = br.readLine())
86
{
87
if (line.indexOf(searchString) != -1)
88
{
89
return true;
90
}
91
}
92
br.close();
93
return false;
94
}
95
96
public static int countLinesContainingString(String searchString, InputStream in) throws IOException
97
{
98
int count = 0;
99
// create a buffered reader with a 1Mb buffer
100
BufferedReader br = new BufferedReader(new InputStreamReader(in));
101
102
for (String line = br.readLine(); line != null; line = br.readLine())
103
{
104
if (line.indexOf(searchString) != -1)
105
{
106
count++;
107
}
108
}
109
br.close();
110
return count;
111
}
112
113
public static void displayFile(String filename) throws IOException, FileNotFoundException
114
{
115
BufferedReader br = new BufferedReader(new FileReader(filename));
116
117
for (String line = br.readLine(); line != null; line = br.readLine())
118
{
119
System.out.println(filename + ": " + line);
120
}
121
}
122
public static void printResult(boolean result)
123
{
124
System.out.println("TEST " + (result ? "PASSED" : "FAILED"));
125
}
126
}
127
128