Read .tl files while respecting sections
This commit is contained in:
parent
6acd40719f
commit
d554b69c82
|
@ -6,58 +6,77 @@ import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class TLReader {
|
public class TLReader {
|
||||||
|
|
||||||
|
public static final Map<String, List<TLObject>> readTlObjects(final File file) {
|
||||||
public static List<TLObject> readTlObjects(final File file) {
|
final Map<String, List<TLObject>> result = new HashMap<>();
|
||||||
final List<String> lines = readFileWithDelimiter(new File("telegram_api.tl"), ';');
|
for (final Map.Entry<String, List<String>> entry : readTl(file).entrySet()) {
|
||||||
sanitizeTlLines(lines);
|
final List<TLObject> objects = new ArrayList<>();
|
||||||
final List<TLObject> objects = new ArrayList<>(lines.size());
|
for (final String string : entry.getValue()) {
|
||||||
for (final String line : lines) {
|
objects.add(TLObject.fromString(string));
|
||||||
objects.add(TLObject.fromString(line));
|
|
||||||
}
|
}
|
||||||
return objects;
|
result.put(entry.getKey(), objects);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> readFileWithDelimiter(final File file, final char delimiter) {
|
private static Map<String, List<String>> readTl(final File file) {
|
||||||
final List<String> result = new ArrayList<>();
|
final Map<String, List<String>> result = new HashMap<>();
|
||||||
|
|
||||||
|
String section = "types";
|
||||||
try (final CharacterIterator iter = new CharacterIterator(new FileReader(file))) {
|
try (final CharacterIterator iter = new CharacterIterator(new FileReader(file))) {
|
||||||
|
String tmp;
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
for (char c : iter) {
|
for (char c : iter) {
|
||||||
if (c == delimiter) {
|
switch (c) {
|
||||||
result.add(builder.toString());
|
// 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);
|
builder.setLength(0);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
c = iter.next();
|
||||||
|
if (c != '-') {
|
||||||
builder.append(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) {
|
} catch (IOException ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue