summaryrefslogtreecommitdiff
path: root/src/main/java/tlIItools/NameFileReader.java
blob: a96469429e80583ead941718219e370a1076c54a (plain)
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
package tlIItools;

import java.io.*;

import java.util.*;

/** Reads in a list of file names to process.
 *
 * @author Ben Culkin */
public class NameFileReader {
	/** Are we attempting to guess group names? */
	public boolean guessGroups;
	/** Regex to use for guessing group names. */
	public String groupRx;
	/** The default group to put files into. */
	public String defGroup;
	/** The map of file groups. */
	public Map<String, List<String>> fNames;

	/* The current group. */
	private String curGroup;
	/* The list of files for the current group. */
	private	List<String> curList;

	/** Counts the files read in. */
	public int fCount;

	/** Stream to write normal output to. */
	public PrintStream normOut = System.out;
	/** Stream to write error output to. */
	public PrintStream errOut  = System.err;

	/** Create a new name reader using the default settings.
	 *
	 * Guessing groups is disabled by default. */
	public NameFileReader() {
		this(false);
	}

	/** Create a new name reader using the default settings.
	 *
	 * @param guessGroups Controls whether or not to try to guess file groups from file names. */
	public NameFileReader(boolean guessGroups) {
		this(new HashMap<>(), "default", guessGroups);
	}

	/** Create a new name reader using the specified settings.
	 *
	 * @param fNames The set of groups to add files to.
	 * @param defGroup The name of the default file group.
	 * @param guessGroups Whether or not to attempt to guess group names. */
	public NameFileReader(Map<String, List<String>> fNames, String defGroup, boolean guessGroups) {
		if (!fNames.containsKey(defGroup)) fNames.put(defGroup, new ArrayList<>());

		this.fNames = fNames;

		this.defGroup    = defGroup;
		this.guessGroups = guessGroups;
		this.curGroup    = defGroup;

		this.curList     = fNames.get(curGroup);

		this.fCount = 0;
	}

	/** Read in file names from a file.
	 *
	 * @param from The name of the file to read from. */
	public void readFrom(String from) {
		try (FileReader fr = new FileReader(from)) {
			readFrom(fr);
		} catch (IOException ioex) {
			errOut.printf("Error reading names from file %s\n", from);
			ioex.printStackTrace(errOut);
			errOut.println();
		}
	}

	/** Read in file names from an input source.
	 *
	 * @param r The input source to read from. */
	public void readFrom(Reader r) {
		Scanner scn = new Scanner(r);

		while (scn.hasNextLine()) {
			String ln = scn.nextLine();

			boolean skipAdd = false;

			if (ln.startsWith("#")) {
				swapGroup(ln.substring(1));

				skipAdd = true;
			} else if (guessGroups && ln.contains("/mods/")) {
				swapGroup(ln.replaceAll(groupRx, "$1"));
			}

			if (!skipAdd) {
				fCount += 1;

				curList.add(ln);
			}

			skipAdd = false;
		}
		
		scn.close();
	}

	/** Swap to a new file group.
	 *
	 * @param groupName The name of the group to swap to. */
	public void swapGroup(String groupName) {
		curGroup = groupName;

		if (!fNames.containsKey(curGroup)) {
			curList = new ArrayList<>();

			fNames.put(curGroup, curList);
		} else {
			curList  = fNames.get(curGroup);
		}
	}
	
	/** Add a file to this file reader.
	 * 
	 * @param fName The file to add. */
	public void addFile(String fName) {
		curList.add(fName);

		fCount += 1;
	}
	
	/** Add a file to this file reader.
	 * 
	 * @param groupName The group to add the file to.
	 * @param fName The file to add. */
	public void addFile(String groupName, String fName) {
		fNames.computeIfAbsent(groupName, (key) -> new ArrayList<>()).add(fName);

		fCount += 1;
	}
}