Files
CodeChickenCore/codechicken/lib/config/ConfigFile.java
T

125 lines
3.2 KiB
Java
Raw Normal View History

2013-07-16 17:58:02 +10:00
package codechicken.lib.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ConfigFile extends ConfigTagParent
2014-04-07 14:02:47 +10:00
{
public ConfigFile(File file) {
try {
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
if (!file.exists())
2013-07-16 17:58:02 +10:00
file.createNewFile();
2014-04-07 14:02:47 +10:00
} catch (IOException e) {
throw new RuntimeException(e);
2013-07-16 17:58:02 +10:00
}
this.file = file;
newlinemode = 2;
loadConfig();
}
2014-04-07 14:02:47 +10:00
private void loadConfig() {
2013-07-16 17:58:02 +10:00
loading = true;
BufferedReader reader;
2014-04-07 14:02:47 +10:00
try {
2013-07-16 17:58:02 +10:00
reader = new BufferedReader(new FileReader(file));
2014-04-07 14:02:47 +10:00
while (true) {
2013-07-16 17:58:02 +10:00
reader.mark(2000);
String line = reader.readLine();
2014-04-07 14:02:47 +10:00
if (line != null && line.startsWith("#")) {
if (comment == null || comment.equals(""))
2013-07-16 17:58:02 +10:00
comment = line.substring(1);
else
2014-04-07 14:02:47 +10:00
comment = comment + "\n" + line.substring(1);
} else {
2013-07-16 17:58:02 +10:00
reader.reset();
break;
}
}
loadChildren(reader);
reader.close();
2014-04-07 14:02:47 +10:00
} catch (IOException e) {
2013-07-16 17:58:02 +10:00
throw new RuntimeException(e);
}
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
loading = false;
}
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
@Override
2014-04-07 14:02:47 +10:00
public ConfigFile setComment(String header) {
2013-07-16 17:58:02 +10:00
super.setComment(header);
return this;
}
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
@Override
2014-04-07 14:02:47 +10:00
public ConfigFile setSortMode(int mode) {
2013-07-16 17:58:02 +10:00
super.setSortMode(mode);
return this;
}
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
@Override
2014-04-07 14:02:47 +10:00
public String getNameQualifier() {
2013-07-16 17:58:02 +10:00
return "";
}
2014-04-07 14:02:47 +10:00
public static String readLine(BufferedReader reader) throws IOException {
2013-07-16 17:58:02 +10:00
String line = reader.readLine();
2014-04-07 14:02:47 +10:00
if (line != null)
2013-07-16 17:58:02 +10:00
return line.replace("\t", "");
return line;
}
2014-04-07 14:02:47 +10:00
public static String formatLine(String line) {
2013-07-16 17:58:02 +10:00
line = line.replace("\t", "");
2014-04-07 14:02:47 +10:00
if (line.startsWith("#")) {
2013-07-16 17:58:02 +10:00
return line;
2014-04-07 14:02:47 +10:00
} else if (line.contains("=")) {
line = line.substring(0, line.indexOf("=")).replace(" ", "") + line.substring(line.indexOf("="));
2013-07-16 17:58:02 +10:00
return line;
2014-04-07 14:02:47 +10:00
} else {
2013-07-16 17:58:02 +10:00
line = line.replace(" ", "");
return line;
}
}
2014-04-07 14:02:47 +10:00
public static void writeLine(PrintWriter writer, String line, int tabs) {
for (int i = 0; i < tabs; i++)
2013-07-16 17:58:02 +10:00
writer.print('\t');
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
writer.println(line);
}
2014-04-07 14:02:47 +10:00
public void saveConfig() {
if (loading)
2013-07-16 17:58:02 +10:00
return;
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
PrintWriter writer;
2014-04-07 14:02:47 +10:00
try {
2013-07-16 17:58:02 +10:00
writer = new PrintWriter(file);
2014-04-07 14:02:47 +10:00
} catch (FileNotFoundException e) {
2013-07-16 17:58:02 +10:00
throw new RuntimeException(e);
}
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
writeComment(writer, 0);
ConfigFile.writeLine(writer, "", 0);
saveTagTree(writer, 0, "");
writer.flush();
writer.close();
}
2014-04-07 14:02:47 +10:00
public boolean isLoading() {
2013-07-16 17:58:02 +10:00
return loading;
}
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
public File file;
private boolean loading;
2014-04-07 14:02:47 +10:00
2013-07-16 17:58:02 +10:00
public static final byte[] lineend = new byte[]{0xD, 0xA};
}