Refactor to be more input/output, remove globals from parsing
This commit is contained in:
@@ -8,14 +8,6 @@
|
|||||||
#include <semaphore>
|
#include <semaphore>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
struct XToTimeString {
|
|
||||||
String string; // String inside transcript arena
|
|
||||||
uint16_t hour;
|
|
||||||
uint16_t minute;
|
|
||||||
uint16_t second;
|
|
||||||
String filepath;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TimeString {
|
struct TimeString {
|
||||||
uint16_t hour;
|
uint16_t hour;
|
||||||
uint16_t minute;
|
uint16_t minute;
|
||||||
@@ -23,15 +15,6 @@ struct TimeString {
|
|||||||
String string;
|
String string;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TimeFile {
|
|
||||||
Array<TimeString> time_strings;
|
|
||||||
String file;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool AppInitializedWithFolder;
|
|
||||||
Arena XArena;
|
|
||||||
Arena Perm;
|
|
||||||
|
|
||||||
Array<TimeString> ParseSrtFile(Arena *arena, String filename) {
|
Array<TimeString> ParseSrtFile(Arena *arena, String filename) {
|
||||||
String content = ReadFile(*arena, filename);
|
String content = ReadFile(*arena, filename);
|
||||||
Array<String> lines = Split(*arena, content, "\n");
|
Array<String> lines = Split(*arena, content, "\n");
|
||||||
@@ -75,31 +58,46 @@ Array<TimeString> ParseSrtFile(Arena *arena, String filename) {
|
|||||||
return time_strings;
|
return time_strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
Array<Arena *> ArenasToFree;
|
struct TimeFile {
|
||||||
Array<TimeFile> TimeFiles;
|
Array<TimeString> time_strings;
|
||||||
std::mutex ParseThreadMutex;
|
String file;
|
||||||
void ParseThreadEntry(Array<String> files) {
|
};
|
||||||
Arena *arena = AllocArena();
|
|
||||||
ParseThreadMutex.lock();
|
struct ParseThreadIO {
|
||||||
ArenasToFree.add(arena);
|
Array<String> input_files;
|
||||||
ParseThreadMutex.unlock();
|
|
||||||
For(files) {
|
// output
|
||||||
Array<TimeString> time_strings = ParseSrtFile(arena, it);
|
Arena *arena;
|
||||||
ParseThreadMutex.lock();
|
Array<TimeFile> time_files;
|
||||||
TimeFiles.add({time_strings, it});
|
};
|
||||||
ParseThreadMutex.unlock();
|
|
||||||
|
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});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Array<XToTimeString> XToTimeStringArray = {Perm};
|
struct XToTimeString {
|
||||||
Array<String> InitForFolder(String folder) {
|
String string; // String inside transcript arena
|
||||||
Scratch scratch;
|
uint16_t hour;
|
||||||
ArenasToFree.allocator = scratch;
|
uint16_t minute;
|
||||||
TimeFiles.allocator = scratch;
|
uint16_t second;
|
||||||
|
String filepath;
|
||||||
|
};
|
||||||
|
|
||||||
int thread_count = 16;
|
Arena Perm;
|
||||||
Array<String> filenames = {Perm};
|
Array<XToTimeString> XToTimeStringArray = {Perm};
|
||||||
Array<String> srt_files = {scratch};
|
bool AppInitializedWithFolder;
|
||||||
|
Arena XArena;
|
||||||
|
|
||||||
|
Array<String> InitForFolder(String folder) {
|
||||||
|
Scratch scratch;
|
||||||
|
|
||||||
|
Array<String> filenames = {Perm};
|
||||||
|
Array<String> srt_files = {scratch};
|
||||||
for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) {
|
for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) {
|
||||||
filenames.add(Copy(Perm, iter.absolute_path));
|
filenames.add(Copy(Perm, iter.absolute_path));
|
||||||
if (EndsWith(iter.filename, ".srt")) {
|
if (EndsWith(iter.filename, ".srt")) {
|
||||||
@@ -107,17 +105,24 @@ Array<String> InitForFolder(String folder) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int thread_count = 16;
|
||||||
Array<std::thread *> threads = {scratch};
|
Array<std::thread *> threads = {scratch};
|
||||||
int files_per_thread = srt_files.len / thread_count;
|
int files_per_thread = srt_files.len / thread_count;
|
||||||
int remainder = srt_files.len % thread_count;
|
int remainder = srt_files.len % thread_count;
|
||||||
int fi = 0;
|
int fi = 0;
|
||||||
|
|
||||||
|
Array<ParseThreadIO> io = {scratch};
|
||||||
|
io.reserve(thread_count);
|
||||||
for (int ti = 0; ti < thread_count; ti += 1) {
|
for (int ti = 0; ti < thread_count; ti += 1) {
|
||||||
Array<String> files = {scratch};
|
Array<String> files = {scratch};
|
||||||
for (int i = 0; fi < srt_files.len && i < files_per_thread + remainder; fi += 1, i += 1) {
|
for (int i = 0; fi < srt_files.len && i < files_per_thread + remainder; fi += 1, i += 1) {
|
||||||
files.add(srt_files[fi]);
|
files.add(srt_files[fi]);
|
||||||
}
|
}
|
||||||
if (remainder) remainder = 0;
|
if (remainder) remainder = 0;
|
||||||
threads.add(new std::thread(ParseThreadEntry, files));
|
|
||||||
|
ParseThreadIO *i = io.alloc();
|
||||||
|
i->input_files = files;
|
||||||
|
threads.add(new std::thread(ParseThreadEntry, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
For(threads) {
|
For(threads) {
|
||||||
@@ -125,14 +130,16 @@ Array<String> InitForFolder(String folder) {
|
|||||||
delete it;
|
delete it;
|
||||||
}
|
}
|
||||||
|
|
||||||
ForItem(file, TimeFiles) {
|
ForItem(it_io, io) {
|
||||||
For(file.time_strings) {
|
ForItem(it_time_file, it_io.time_files) {
|
||||||
String s = Copy(XArena, it.string);
|
For(it_time_file.time_strings) {
|
||||||
s.data[s.len] = ' ';
|
String s = Copy(XArena, it.string);
|
||||||
XToTimeStringArray.add({s, it.hour, it.minute, it.second, file.file});
|
s.data[s.len] = ' ';
|
||||||
|
XToTimeStringArray.add({s, it.hour, it.minute, it.second, it_time_file.file});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Release(it_io.arena);
|
||||||
}
|
}
|
||||||
For(ArenasToFree) Release(it);
|
|
||||||
|
|
||||||
AppInitializedWithFolder = true;
|
AppInitializedWithFolder = true;
|
||||||
return filenames;
|
return filenames;
|
||||||
|
|||||||
Reference in New Issue
Block a user