Gui improvements and optimizations

This commit is contained in:
Krzosa Karol
2024-07-06 06:56:18 +02:00
parent 0dd6289509
commit 30fa22aed5
3 changed files with 234 additions and 284 deletions

View File

@@ -0,0 +1,141 @@
struct TimeString {
uint16_t hour;
uint16_t minute;
uint16_t second;
String string;
};
Array<TimeString> ParseSrtFile(Arena *arena, String filename) {
String content = ReadFile(*arena, filename);
Array<String> lines = Split(*arena, content, "\n");
IterRemove(lines) {
IterRemovePrepare(lines);
it = Trim(it);
if (it.len == 0) remove_item = true;
}
long section_number = 1;
Array<TimeString> time_strings = {*arena};
for (int i = 0; i < lines.len;) {
String it0 = lines[i++];
long num = strtol(it0.data, NULL, 10);
Assert(section_number == num);
section_number += 1;
TimeString item = {};
String it1 = lines[i++];
item.hour = (uint16_t)strtol(it1.data, NULL, 10);
item.minute = (uint16_t)strtol(it1.data + 3, NULL, 10);
item.second = (uint16_t)strtol(it1.data + 6, NULL, 10);
String next_section_number = Format(*arena, "%d", section_number);
while (i < lines.len && lines[i] != next_section_number) {
String it = lines[i];
item.string = lines[i];
time_strings.add(item);
i += 1;
}
}
IterRemove(time_strings) {
IterRemovePrepare(time_strings);
if (i > 0 && AreEqual(time_strings[i - 1].string, time_strings[i].string, true)) {
remove_item = true;
}
}
return time_strings;
}
struct TimeFile {
Array<TimeString> time_strings;
String file;
};
struct ParseThreadIO {
Array<String> input_files;
// output
Arena *arena;
Array<TimeFile> time_files;
};
void ParseThreadEntry(ParseThreadIO *io) {
io->arena = AllocArena();
io->time_files.allocator = *io->arena;
For(io->input_files) {
Array<TimeString> time_strings = ParseSrtFile(io->arena, it);
io->time_files.add({time_strings, it});
}
}
struct XToTimeString {
String string; // String inside transcript arena
uint16_t hour;
uint16_t minute;
uint16_t second;
String filepath;
};
Arena XArena;
void AddFolder(String folder, Array<String> *filenames, Array<XToTimeString> *x_to_time_string) {
Scratch scratch;
Array<String> srt_files = {scratch};
for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) {
String file = Copy(Perm, iter.absolute_path);
filenames->add(file);
if (EndsWith(iter.filename, ".srt")) {
srt_files.add(file);
}
}
int64_t thread_count = 16;
Array<std::thread *> threads = {scratch};
int64_t files_per_thread = srt_files.len / thread_count;
int64_t remainder = srt_files.len % thread_count;
int64_t fi = 0;
Array<ParseThreadIO> io = {scratch};
io.reserve(thread_count);
for (int ti = 0; ti < thread_count; ti += 1) {
Array<String> files = {scratch};
for (int i = 0; fi < srt_files.len && i < files_per_thread + remainder; fi += 1, i += 1) {
files.add(srt_files[fi]);
}
if (remainder) remainder = 0;
ParseThreadIO *i = io.alloc();
i->input_files = files;
threads.add(new std::thread(ParseThreadEntry, i));
}
For(threads) {
it->join();
delete it;
}
ForItem(it_io, io) {
ForItem(it_time_file, it_io.time_files) {
For(it_time_file.time_strings) {
String s = Copy(XArena, it.string);
s.data[s.len] = ' ';
x_to_time_string->add({s, it.hour, it.minute, it.second, it_time_file.file});
}
}
Release(it_io.arena);
}
}
XToTimeString *FindItem(Array<XToTimeString> &x_to_time_string, String string) {
For(x_to_time_string) {
uintptr_t begin = (uintptr_t)(it.string.data);
uintptr_t end = (uintptr_t)(it.string.data + it.string.len);
uintptr_t needle = (uintptr_t)string.data;
if (needle >= begin && needle < end) {
return &it;
}
}
return NULL;
}