Read .tl files while respecting sections

This commit is contained in:
Lonami Exo 2018-07-23 15:35:19 +02:00
parent 6acd40719f
commit d554b69c82
1 changed files with 56 additions and 37 deletions

View File

@ -6,31 +6,72 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TLReader {
public static List<TLObject> readTlObjects(final File file) {
final List<String> lines = readFileWithDelimiter(new File("telegram_api.tl"), ';');
sanitizeTlLines(lines);
final List<TLObject> objects = new ArrayList<>(lines.size());
for (final String line : lines) {
objects.add(TLObject.fromString(line));
public static final Map<String, List<TLObject>> readTlObjects(final File file) {
final Map<String, List<TLObject>> result = new HashMap<>();
for (final Map.Entry<String, List<String>> entry : readTl(file).entrySet()) {
final List<TLObject> objects = new ArrayList<>();
for (final String string : entry.getValue()) {
objects.add(TLObject.fromString(string));
}
result.put(entry.getKey(), objects);
}
return objects;
return result;
}
private static List<String> readFileWithDelimiter(final File file, final char delimiter) {
final List<String> result = new ArrayList<>();
private static Map<String, List<String>> readTl(final File file) {
final Map<String, List<String>> result = new HashMap<>();
String section = "types";
try (final CharacterIterator iter = new CharacterIterator(new FileReader(file))) {
String tmp;
final StringBuilder builder = new StringBuilder();
for (char c : iter) {
if (c == delimiter) {
result.add(builder.toString());
builder.setLength(0);
} else {
builder.append(c);
switch (c) {
// Assume '/' only occurs in "// comments...\n"
case '/':
do {
c = iter.next();
} while (iter.hasNext() && c != '\n');
break;
// Assume '-' only occurs in "---sections---\n"
case '-':
if (builder.length() != 0) {
tmp = builder.toString().trim();
if (!tmp.isEmpty()) {
result.computeIfAbsent(section, k -> new ArrayList<>()).add(tmp);
}
builder.setLength(0);
}
do {
c = iter.next();
if (c != '-') {
builder.append(c);
}
} while (iter.hasNext() && c != '\n');
section = builder.toString().trim();
builder.setLength(0);
break;
// Definitions are separated by ';'
case ';':
tmp = builder.toString().trim();
if (!tmp.isEmpty()) {
result.computeIfAbsent(section, k -> new ArrayList<>()).add(tmp);
}
builder.setLength(0);
break;
default:
builder.append(c);
break;
}
}
} catch (IOException ignored) {
@ -38,26 +79,4 @@ public class TLReader {
return result;
}
private static void sanitizeTlLines(final List<String> lines) {
String line;
int comment;
int newline;
for (int i = lines.size(); i-- != 0; ) {
line = lines.get(i);
while ((comment = line.indexOf("//")) != -1) {
newline = line.indexOf("\n", comment + 2);
if (newline == -1) {
newline = line.length();
}
line = line.substring(0, comment) + line.substring(newline);
}
line = line.trim();
if (line.isEmpty()) {
lines.remove(i);
} else {
lines.set(i, line);
}
}
}
}