New project

This commit is contained in:
Krzosa Karol
2024-07-17 07:30:46 +02:00
parent 63bd416e7c
commit f7ac92cd01
10 changed files with 510 additions and 1721 deletions

View File

@@ -1,173 +1,17 @@
#include "src/build_tool/library.cpp"
struct Library {
Array<S8_String> sources;
Array<S8_String> objects;
Array<S8_String> include_paths;
Array<S8_String> defines;
enum {
PROFILE_DEBUG,
PROFILE_RELEASE,
};
int Profile = PROFILE_DEBUG;
Library PreparePdfio(S8_String zlib_include_path) {
Library l = {};
l.include_paths.add("../src/external/pdfio");
l.sources.add("../src/external/pdfio/pdfio-aes.c");
l.sources.add("../src/external/pdfio/pdfio-array.c");
l.sources.add("../src/external/pdfio/pdfio-common.c");
l.sources.add("../src/external/pdfio/pdfio-content.c");
l.sources.add("../src/external/pdfio/pdfio-crypto.c");
l.sources.add("../src/external/pdfio/pdfio-dict.c");
l.sources.add("../src/external/pdfio/pdfio-file.c");
l.sources.add("../src/external/pdfio/pdfio-md5.c");
l.sources.add("../src/external/pdfio/pdfio-object.c");
l.sources.add("../src/external/pdfio/pdfio-page.c");
l.sources.add("../src/external/pdfio/pdfio-rc4.c");
l.sources.add("../src/external/pdfio/pdfio-sha256.c");
l.sources.add("../src/external/pdfio/pdfio-stream.c");
l.sources.add("../src/external/pdfio/pdfio-string.c");
l.sources.add("../src/external/pdfio/pdfio-token.c");
l.sources.add("../src/external/pdfio/pdfio-value.c");
l.sources.add("../src/external/pdfio/ttf.c");
l.objects.add("pdfio-aes.obj");
l.objects.add("pdfio-array.obj");
l.objects.add("pdfio-common.obj");
l.objects.add("pdfio-content.obj");
l.objects.add("pdfio-crypto.obj");
l.objects.add("pdfio-dict.obj");
l.objects.add("pdfio-file.obj");
l.objects.add("pdfio-md5.obj");
l.objects.add("pdfio-object.obj");
l.objects.add("pdfio-page.obj");
l.objects.add("pdfio-rc4.obj");
l.objects.add("pdfio-sha256.obj");
l.objects.add("pdfio-stream.obj");
l.objects.add("pdfio-string.obj");
l.objects.add("pdfio-token.obj");
l.objects.add("pdfio-value.obj");
l.objects.add("ttf.obj");
if (!OS_FileExists(l.objects[0])) {
Array<S8_String> cmd = {};
cmd.add("cl.exe -c -nologo -Zi -MP -FC ");
For(l.include_paths) cmd.add(S8_Format(Perm, "-I %.*s ", S8_Expand(it)));
cmd.add(S8_Format(Perm, "-I %.*s ", S8_Expand(zlib_include_path)));
cmd += l.sources;
Run(cmd);
}
return l;
}
Library PrepareZLib() {
Library l = {};
l.sources.add("../src/external/zlib-1.3.1/adler32.c");
l.sources.add("../src/external/zlib-1.3.1/compress.c");
l.sources.add("../src/external/zlib-1.3.1/crc32.c");
l.sources.add("../src/external/zlib-1.3.1/deflate.c");
l.sources.add("../src/external/zlib-1.3.1/gzclose.c");
l.sources.add("../src/external/zlib-1.3.1/gzlib.c");
l.sources.add("../src/external/zlib-1.3.1/gzread.c");
l.sources.add("../src/external/zlib-1.3.1/gzwrite.c");
l.sources.add("../src/external/zlib-1.3.1/inflate.c");
l.sources.add("../src/external/zlib-1.3.1/infback.c");
l.sources.add("../src/external/zlib-1.3.1/inftrees.c");
l.sources.add("../src/external/zlib-1.3.1/inffast.c");
l.sources.add("../src/external/zlib-1.3.1/trees.c");
l.sources.add("../src/external/zlib-1.3.1/uncompr.c");
l.sources.add("../src/external/zlib-1.3.1/zutil.c");
l.include_paths.add("../src/external/zlib-1.3.1/");
l.objects.add("adler32.obj");
l.objects.add("compress.obj");
l.objects.add("crc32.obj");
l.objects.add("deflate.obj");
l.objects.add("gzclose.obj");
l.objects.add("gzlib.obj");
l.objects.add("gzread.obj");
l.objects.add("gzwrite.obj");
l.objects.add("inflate.obj");
l.objects.add("infback.obj");
l.objects.add("inftrees.obj");
l.objects.add("inffast.obj");
l.objects.add("trees.obj");
l.objects.add("uncompr.obj");
l.objects.add("zutil.obj");
l.objects.add("Advapi32.lib");
if (!OS_FileExists(l.objects[0])) {
Array<S8_String> cmd = {};
cmd.add("cl.exe -c -nologo -Zi -MP -FC -O2 ");
For(l.include_paths) cmd.add(S8_Format(Perm, "-I %.*s ", S8_Expand(it)));
cmd += l.sources;
Run(cmd);
}
return l;
}
Library PrepareIMGUI(S8_String sdl_inc_path) {
Library l = {};
l.include_paths.add("../src/external/imgui/");
l.include_paths.add("../src/external/imgui/backends");
l.sources.add("../src/external/imgui/backends/imgui_impl_sdl3.cpp");
l.sources.add("../src/external/imgui/backends/imgui_impl_opengl3.cpp");
l.sources.add("../src/external/imgui/imgui.cpp");
l.sources.add("../src/external/imgui/imgui_demo.cpp");
l.sources.add("../src/external/imgui/imgui_draw.cpp");
l.sources.add("../src/external/imgui/imgui_tables.cpp");
l.sources.add("../src/external/imgui/imgui_widgets.cpp");
l.objects.add("imgui_impl_sdl3.obj");
l.objects.add("imgui_impl_opengl3.obj");
l.objects.add("imgui.obj");
l.objects.add("imgui_demo.obj");
l.objects.add("imgui_draw.obj");
l.objects.add("imgui_tables.obj");
l.objects.add("imgui_widgets.obj");
if (!OS_FileExists(l.objects[0])) {
Array<S8_String> cmd = {};
cmd.add("cl.exe -c -nologo -Zi -MP -FC -O2 ");
cmd.add(Fmt("-I %.*s", S8_Expand(sdl_inc_path)));
For(l.include_paths) cmd.add(S8_Format(Perm, "-I %.*s ", S8_Expand(it)));
cmd += l.sources;
Run(cmd);
}
return l;
}
Library PrepareSDL() {
Library l = {};
l.include_paths.add("../src/external/SDL/include");
l.objects.add("../src/external/SDL/VisualC/x64/Release/SDL3.lib");
return l;
}
Library PrepareGlad() {
Library l = {};
l.sources.add("../src/external/glad/glad.c");
l.include_paths.add("../src/external/glad");
l.objects.add("glad.obj");
if (!OS_FileExists(l.objects[0])) {
Array<S8_String> cmd = {};
cmd.add("cl.exe -c -nologo -Zi -MP -FC -O2 ");
For(l.include_paths) cmd.add(S8_Format(Perm, "-I %.*s ", S8_Expand(it)));
cmd += l.sources;
Run(cmd);
}
return l;
}
void AddCommonFlags(Array<S8_String> *cmd, bool debug = true) {
void AddCommonFlags(Array<S8_String> *cmd) {
cmd->add("/MP /Zi /FC /nologo");
cmd->add("/WX /W3 /wd4200 /diagnostics:column");
cmd->add("/Oi");
cmd->add("-D_CRT_SECURE_NO_WARNINGS");
if (debug) {
if (Profile == PROFILE_DEBUG) {
cmd->add("-DDEBUG_BUILD=1");
cmd->add("-DRELEASE_BUILD=0");
// cmd->add("-D_DEBUG /MDd");
@@ -178,45 +22,86 @@ void AddCommonFlags(Array<S8_String> *cmd, bool debug = true) {
}
}
int main() {
MA_InitScratch();
SRC_InitCache(Perm, "pdf_browser.cache");
struct Library {
Array<S8_String> sources;
Array<S8_String> objects;
Array<S8_String> include_paths;
Array<S8_String> defines;
};
Library PrepareSDL() {
Library l = {};
l.include_paths.add("../src/external/SDL/include");
l.objects.add("../src/external/SDL/VisualC/x64/Release/SDL3.lib");
return l;
}
Library PrepareRaylib() {
Library l = {};
l.include_paths.add("../src/external/raylib/include");
if (0) {
l.objects.add("../src/external/raylib/lib/raylib.lib");
} else {
l.objects.add("../src/external/raylib/lib/raylibdll.lib");
OS_CopyFile("../src/external/raylib/lib/raylib.dll", "./raylib.dll", false);
}
return l;
}
Library PrepareGlad() {
Library l = {};
l.sources.add("../src/external/glad/glad.c");
l.include_paths.add("../src/external/glad");
l.objects.add("glad.obj");
if (!OS_FileExists(l.objects[0])) {
Array<S8_String> cmd = {};
cmd.add("cl.exe -c");
AddCommonFlags(&cmd);
For(l.include_paths) cmd.add(S8_Format(Perm, "-I %.*s ", S8_Expand(it)));
cmd += l.sources;
Run(cmd);
}
return l;
}
int CompileTextEditor() {
int result = 0;
Array<Library> libs = {};
libs.add(PrepareSDL());
libs.add(PrepareZLib());
libs.add(PreparePdfio(libs[1].include_paths[0]));
libs.add(PrepareGlad());
libs.add(PrepareIMGUI(libs[0].include_paths[0]));
libs.add(PrepareRaylib());
Array<S8_String> cmd = {};
cmd.add("cl.exe");
cmd.add("-Fe:transcript_browser.exe");
cmd.add("-Fd:transcript_browser.pdb");
cmd.add("-std:c++20"); // semaphore
cmd.add("-Fe:te.exe");
cmd.add("-Fd:te.pdb");
AddCommonFlags(&cmd);
For2(lib, libs) For(lib.defines) cmd.add(it);
cmd.add("../src/transcript_browser/main.cpp");
cmd.add("../src/text_editor/text_editor.cpp");
cmd.add("../src/basic/win32.cpp");
For2(lib, libs) For(lib.include_paths) cmd.add(Fmt("-I %.*s", S8_Expand(it)));
cmd.add("/link");
cmd.add("/incremental:no");
cmd.add("/SUBSYSTEM:WINDOWS");
cmd.add("opengl32.lib gdi32.lib winmm.lib Setupapi.lib ole32.lib oleaut32.lib version.lib");
// cmd.add("/SUBSYSTEM:WINDOWS");
cmd.add("opengl32.lib gdi32.lib winmm.lib shell32.lib user32.lib msvcrt.lib /NODEFAULTLIB:LIBCMT");
For(libs) For2(o, it.objects) cmd.add(o);
OS_DeleteFile("transcript_browser.pdb");
OS_DeleteFile("te.pdb");
// For(cmd) IO_Printf("%.*s\n", S8_Expand(it));
result += Run(cmd);
return result;
}
int main() {
MA_InitScratch();
SRC_InitCache(Perm, "te.cache");
int result = CompileTextEditor();
if (result != 0) {
OS_DeleteFile("pdf_browser.cache");
OS_DeleteFile("te.cache");
return result;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,220 +0,0 @@
struct Var {
String key;
String value;
};
struct ConfigParseResult {
Array<Var> vars;
Array<String> errors;
};
struct ReplaceVar {
String replace;
String with;
};
// @todo: move the config errors to config menu!!!
char SRTCommand[512] = "\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" --start-time {time_in_seconds} {video}";
char PDFCommand[512] = "C:/Users/Karol/AppData/Local/SumatraPDF/SumatraPDF.exe -page {page} {file}";
char TXTCommand[512] = "notepad.exe {file}";
char ReadOnStart[512] = "-";
bool ReadFoldersRecursively = false;
void SkipWhitespace(String *s) {
while (s->len && IsWhitespace(s->data[0])) {
*s = s->skip(1);
}
}
String ParseWord(String *s) {
String result = {};
SkipWhitespace(s);
if (s->len && IsAlphabetic(s->data[0])) {
result = {s->data, 0};
while (s->len && IsAlphanumeric(s->data[0])) {
result.len += 1;
*s = s->skip(1);
}
}
return result;
}
bool MatchString(String *s, String expect) {
SkipWhitespace(s);
if (StartsWith(*s, expect)) {
*s = s->skip(expect.len);
return true;
}
return false;
}
ConfigParseResult ParseConfig(Allocator allocator, String string) {
Scratch scratch;
Array<String> lines = Split(scratch, string, "\n");
ConfigParseResult result = {};
result.errors = {allocator};
result.vars = {allocator};
For(lines) {
String s = Trim(it);
if (s.len == 0) continue;
String key = ParseWord(&s);
if (key.len == 0) {
result.errors.add(Format(allocator, "failed to parse config at line: %d, the key is invalid", (int)lines.get_index(it)));
continue;
}
bool e = MatchString(&s, "=");
if (!e) {
result.errors.add(Format(allocator, "failed to parse config at line: %d, expected '=' assignment sign", (int)lines.get_index(it)));
continue;
}
String value = Trim(s);
if (value.len == 0) {
result.errors.add(Format(allocator, "failed to parse config at line: %d, the value is invalid", (int)lines.get_index(it)));
continue;
}
result.vars.add({key, value});
}
return result;
}
String GetValue(Array<Var> vars, String key) {
For(vars) {
if (it.key == key) return it.value;
}
return {};
}
String SerializeConfig(Allocator allocator) {
String content = Format(allocator, "SRTCommand = %s\nPDFCommand = %s\nTXTCommand = %s\nReadOnStart = %s", SRTCommand, PDFCommand, TXTCommand, ReadOnStart);
return content;
}
String CreateDefaultConfig(String path) {
String content = SerializeConfig(Perm);
WriteFile(path, content);
return content;
}
void LoadConfig() {
Scratch scratch;
String exe_dir = GetExeDir(scratch);
String config_path = Format(scratch, "%.*s/transcript_browser.config", FmtString(exe_dir));
String config = ReadFile(Perm, config_path);
if (config.len == 0) {
config = CreateDefaultConfig(config_path);
}
ConfigParseResult result = ParseConfig(Perm, config);
For(result.errors) {
XFileLoadResults.add({"", it});
}
String pdf_command = GetValue(result.vars, "PDFCommand");
if (pdf_command.len) {
int len = ClampTop((int)pdf_command.len, (int)sizeof(PDFCommand) - 1);
memcpy(PDFCommand, pdf_command.data, len);
PDFCommand[len] = 0;
}
String txt_command = GetValue(result.vars, "TXTCommand");
if (txt_command.len) {
int len = ClampTop((int)txt_command.len, (int)sizeof(TXTCommand) - 1);
memcpy(TXTCommand, txt_command.data, len);
TXTCommand[len] = 0;
}
String srt_command = GetValue(result.vars, "SRTCommand");
if (srt_command.len) {
int len = ClampTop((int)srt_command.len, (int)sizeof(SRTCommand) - 1);
memcpy(SRTCommand, srt_command.data, len);
SRTCommand[len] = 0;
}
String read_on_start = GetValue(result.vars, "ReadOnStart");
if (read_on_start.len) {
int len = ClampTop((int)read_on_start.len, (int)sizeof(ReadOnStart) - 1);
memcpy(ReadOnStart, read_on_start.data, len);
ReadOnStart[len] = 0;
}
}
void SaveConfig() {
Scratch scratch;
String exe_dir = GetExeDir(scratch);
String config_path = Format(scratch, "%.*s/transcript_browser.config", FmtString(exe_dir));
String content = SerializeConfig(scratch);
WriteFile(config_path, content);
}
String ReplaceVars(Allocator allocator, Array<ReplaceVar> vars_to_replace, String string) {
Array<char> sb = {allocator};
for (int64_t i = 0; i < string.len; i += 1) {
if (string[i] == '{') {
// extract the variable
i += 1;
String var = {string.data + i, 0};
for (; i < string.len && string[i] != '}'; i += 1)
var.len += 1;
// find the replacement
String found = "<not found>";
For(vars_to_replace) {
if (it.replace == var) {
found = it.with;
break;
}
}
For(found) sb.add(it);
} else sb.add(string[i]);
}
sb.add('\0');
String result = {sb.data, sb.len - 1};
return result;
}
void TestReplaceVars() {
Scratch scratch;
Array<ReplaceVar> vars_to_replace = {scratch};
String exe_folder = GetExePath(scratch);
vars_to_replace.add({"exe_folder", exe_folder});
String data_folder = Format(scratch, "%.*s/data", FmtString(exe_folder));
vars_to_replace.add({"data_folder", data_folder});
{
String r = ReplaceVars(scratch, vars_to_replace, "{exe_folder}");
Assert(r == exe_folder);
}
{
String r = ReplaceVars(scratch, vars_to_replace, "{exe_folder}{data_folder}");
Assert(r == Format(scratch, "%.*s%.*s", FmtString(exe_folder), FmtString(data_folder)));
}
{
String r = ReplaceVars(scratch, vars_to_replace, "..{exe_folder}..{data_folder}..{exe_folder}asd");
Assert(r == Format(scratch, "..%.*s..%.*s..%.*sasd", FmtString(exe_folder), FmtString(data_folder), FmtString(exe_folder)));
}
}
void TestConfig() {
Scratch scratch;
{
ConfigParseResult result = ParseConfig(scratch, "\nPDFCommand = CoolCommand\nTXTCommand = ABC\nSRTCommand = DAS\n");
Assert(result.errors.len == 0);
Assert(result.vars.len == 3);
Assert(result.vars[0].key == "PDFCommand");
Assert(result.vars[1].value == "ABC");
Assert(result.vars[2].value == "DAS");
Assert(result.vars[2].key == "SRTCommand");
}
{
ConfigParseResult result = ParseConfig(scratch, "\n= CoolCommand\nTXTCommand = ABC\nSRTCommand =\n");
Assert(result.errors.len == 2);
Assert(result.vars.len == 1);
}
LoadConfig();
}

View File

@@ -1,207 +0,0 @@
struct ParseThreadIO {
Array<String> input_files;
};
enum SourceKind {
SourceKind_Invalid,
SourceKind_SRT,
SourceKind_PDF,
SourceKind_TXT,
};
struct XToSource {
SourceKind kind;
String string; // String inside x arena
String filepath;
union {
struct {
uint16_t hour;
uint16_t minute;
uint16_t second;
} srt;
struct {
int page;
} pdf;
struct {
int64_t row;
} txt;
};
};
Arena XArena;
std::mutex XArenaAddMutex;
Array<XToSource> XToSourceArray;
int64_t XLoadThreadComplete;
void XInitLoading() {
InitArena(&XArena);
XArena.align = 0;
XToSourceArray.reserve(1000000);
XFileLoadResults.reserve(10000);
}
WORK_FUNCTION(ParseFilesWork) {
ParseThreadIO *io = (ParseThreadIO *)data;
ForItem(it_time_file, io->input_files) {
Scratch scratch;
if (EndsWith(it_time_file, ".srt", true)) {
Array<TimeString> time_strings = ParseSrtFile(scratch, it_time_file);
XArenaAddMutex.lock();
For(time_strings) {
String s = Copy(XArena, it.string);
s.data[s.len] = ' ';
XToSource t = {SourceKind_SRT, s, it_time_file};
t.srt = {it.hour, it.minute, it.second};
XToSourceArray.add(t);
}
XArenaAddMutex.unlock();
XFileLoadResults.bounded_add({it_time_file});
} else if (EndsWith(it_time_file, ".txt", true) || EndsWith(it_time_file, ".html", true)) {
String string = ReadFile(scratch, it_time_file);
if (string.data) {
Array<String> lines = Split(scratch, string, "\n");
XArenaAddMutex.lock();
For(lines) {
String s = Copy(XArena, it);
s.data[s.len] = ' ';
XToSource t = {SourceKind_TXT, s, it_time_file};
t.txt = {lines.get_index(it)};
XToSourceArray.add(t);
}
XArenaAddMutex.unlock();
XFileLoadResults.bounded_add({it_time_file});
} else {
XFileLoadResults.bounded_add({it_time_file, "failed to read the file"});
}
} else if (EndsWith(it_time_file, ".pdf", true)) {
PDF pdf = pdfioReadPDF(scratch, it_time_file);
XArenaAddMutex.lock();
For(pdf.pages) {
String s = Copy(XArena, it.string);
s.data[s.len] = ' ';
XToSource t = {SourceKind_PDF, s, it_time_file};
t.pdf = {it.number};
XToSourceArray.add(t);
}
XArenaAddMutex.unlock();
} else {
XFileLoadResults.bounded_add({it_time_file, "internal error: extension is not supported but got propagated to parse stage"});
}
}
AtomicIncrement(&XLoadThreadComplete);
}
String XGetArenaString() {
XArenaAddMutex.lock();
String buffer = {(char *)XArena.data, (int64_t)XArena.len};
XArenaAddMutex.unlock();
return buffer;
}
Array<FileLoadResult> XLockFileLoadResults() {
return XFileLoadResults;
}
void XUnlockFileResults() {
}
void GetFilesRecursive(Array<String> *arr, String folder) {
for (FileIter it = IterateFiles(Perm, folder); IsValid(it); Advance(&it)) {
if (it.is_directory) {
GetFilesRecursive(arr, it.absolute_path);
continue;
}
arr->add(Copy(arr->allocator, it.absolute_path));
}
}
Array<String> GetFilesRecursive(Allocator allocator, String path) {
Array<String> files = {allocator};
GetFilesRecursive(&files, path);
return files;
}
void XAddFolder(String folder, Array<String> *filenames) {
Scratch scratch;
Array<String> files_here = {Perm};
if (ReadFoldersRecursively) {
files_here = GetFilesRecursive(Perm, folder);
} else {
for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) {
if (iter.is_directory) continue;
String file = Copy(Perm, iter.absolute_path);
files_here.add(file);
}
}
Array<String> files_to_parse = {scratch};
For(files_here) {
if (EndsWith(it, ".srt", true)) {
String srt_command = SRTCommand;
if (srt_command == "-") continue;
files_to_parse.add(it);
} else if (EndsWith(it, ".txt", true) || EndsWith(it, ".html", true)) {
String txt_command = TXTCommand;
if (txt_command == "-") continue;
files_to_parse.add(it);
} else if (EndsWith(it, ".pdf", true)) {
String pdf_command = PDFCommand;
if (pdf_command == "-") continue;
files_to_parse.add(it);
}
filenames->add(it);
}
if (files_to_parse.len == 0) {
XFileLoadResults.add({Copy(Perm, folder), "no files found"});
return;
}
int64_t thread_count = MainWorkQueue.thread_count;
int64_t files_per_thread = files_to_parse.len / thread_count;
int64_t remainder = files_to_parse.len % thread_count;
int64_t fi = 0;
Array<ParseThreadIO> io = {Perm};
io.reserve(thread_count);
for (int ti = 0; ti < thread_count; ti += 1) {
Array<String> files = {Perm};
for (int i = 0; fi < files_to_parse.len && i < files_per_thread + remainder; fi += 1, i += 1) {
files.add(files_to_parse[fi]);
}
if (remainder) remainder = 0;
ParseThreadIO *i = io.alloc();
i->input_files = files;
PushWork(&MainWorkQueue, (void *)i, ParseFilesWork);
}
}
XToSource *XFindSource(String string) {
XToSource *result = NULL;
XArenaAddMutex.lock();
For(XToSourceArray) {
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) {
result = &it;
break;
}
}
XArenaAddMutex.unlock();
return result;
}
bool XLoadingComplete() {
bool result = MainWorkQueue.thread_count == XLoadThreadComplete;
return result;
}

View File

@@ -1,398 +0,0 @@
/*
@todo: popup when error
@todo: pdf files
@todo: help menu
@todo: check for number of cores before creating threads
*/
#define BASIC_IMPL
#include "../basic/basic.h"
#include "../basic/filesystem.h"
#include "../basic/thread_queue.h"
#include <mutex>
#include <stdio.h>
#include "imgui.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_opengl3.h"
#include "SDL3/SDL.h"
#include "glad.h"
struct FileLoadResult {
String filename;
String error;
};
Arena Perm;
Array<FileLoadResult> XFileLoadResults;
#include "config.cpp"
#include <pdfio.h>
#include "read_pdf.cpp"
#include "read_srt.cpp"
WorkQueue MainWorkQueue;
#include "loading_thread.cpp"
#include "searching_thread.cpp"
void UISearchResults(Array<String> filenames) {
Scratch scratch;
Array<String> matches = LockSearchResults();
defer { UnlockSearchResults(); };
float font_size = ImGui::GetFontSize();
ImFont *font = ImGui::GetFont();
const ImGuiViewport *main_viewport = ImGui::GetMainViewport();
int64_t chars_per_line = (int64_t)(main_viewport->WorkSize.x / 2) / (int64_t)font->GetCharAdvance('_') - strlen(Prompt) * 2;
ImGuiListClipper clipper;
clipper.Begin((int)matches.len);
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
ImGui::PushID(i);
defer { ImGui::PopID(); };
auto &it = matches[i];
uintptr_t begin_region = (uintptr_t)XArena.data;
uintptr_t end_region = (uintptr_t)XArena.data + XArena.len;
uint64_t begin = (uintptr_t)(it.data - chars_per_line / 2);
uint64_t end = (uintptr_t)(it.data + it.len + chars_per_line / 2);
uint64_t a = Clamp(begin, begin_region, end_region);
uint64_t b = Clamp((uintptr_t)it.data, begin_region, end_region);
String left = {(char *)a, (int64_t)(b - a)};
uint64_t c = Clamp((uintptr_t)(it.data + it.len), begin_region, end_region);
uint64_t d = Clamp(end, begin_region, end_region);
String right = {(char *)c, (int64_t)(d - c)};
String middle = it;
String string = Format(scratch, "%.*s**%.*s**%.*s",
FmtString(left), FmtString(middle), FmtString(right));
XToSource *item = XFindSource(middle);
if (ImGui::Button(string.data)) {
String exe_folder = GetExeDir(scratch);
exe_folder = Format(scratch, "\"%.*s\"", FmtString(exe_folder));
Array<ReplaceVar> replace_vars = {scratch};
replace_vars.add({"exe_folder", GetExeDir(scratch)});
if (item->kind == SourceKind_SRT) {
String it = FindVideoForSRT(filenames, item->filepath);
if (it.len) {
int seconds = item->srt.hour * 60 * 60 + item->srt.minute * 60 + item->srt.second;
String copy = Copy(scratch, it);
for (int i = 0; i < copy.len; i += 1)
if (copy.data[i] == '/') copy.data[i] = '\\';
replace_vars.add({"video", Format(scratch, "\"%.*s\"", FmtString(copy))});
replace_vars.add({"time_in_seconds", Format(scratch, "%d", seconds)});
String args = ReplaceVars(scratch, replace_vars, SRTCommand);
Process process = RunEx(args);
if (!process.is_valid) XFileLoadResults.bounded_add({"", process.error_message});
}
} else if (item->kind == SourceKind_TXT) {
replace_vars.add({"file", Format(scratch, "\"%.*s\"", FmtString(item->filepath))});
replace_vars.add({"line", Format(scratch, "%d", item->txt.row + 1)});
String args = ReplaceVars(scratch, replace_vars, TXTCommand);
Process process = RunEx(args);
if (!process.is_valid) XFileLoadResults.bounded_add({"", process.error_message});
} else if (item->kind == SourceKind_PDF) {
replace_vars.add({"file", Format(scratch, "\"%.*s\"", FmtString(item->filepath))});
replace_vars.add({"page", Format(scratch, "%d", item->pdf.page)});
String args = ReplaceVars(scratch, replace_vars, PDFCommand);
Process process = RunEx(args);
if (!process.is_valid) XFileLoadResults.bounded_add({"", process.error_message});
} else {
Assert(!"Invalid codepath");
}
}
ImGui::SameLine();
ImGui::Text(SkipToLastSlash(item->filepath).data);
}
}
}
void UILoadedFiles() {
Scratch scratch;
Array<FileLoadResult> file_load_results = XLockFileLoadResults();
defer { XUnlockFileResults(); };
ImGuiListClipper clipper;
clipper.Begin((int)file_load_results.len);
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
FileLoadResult file = file_load_results[file_load_results.len - 1 - i];
if (file.error.len) {
ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)ImColor::HSV(7.0f, 0.6f, 0.6f));
String string = Format(scratch, "%.*s error: %.*s", FmtString(file.filename), FmtString(file.error));
ImGui::Text(string.data);
ImGui::PopStyleColor();
} else {
String string = Format(scratch, "%.*s loaded", FmtString(file.filename));
ImGui::Text(string.data);
}
}
}
}
int EntryPoint() {
InitOS();
InitScratch();
InitArena(&Perm);
XInitLoading();
InitSearch();
LoadConfig();
ThreadStartupInfo infos[16] = {};
InitWorkQueue(&MainWorkQueue, Lengthof(infos), infos);
Array<String> filenames = {};
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
printf("Error: %s\n", SDL_GetError());
return -1;
}
const char *glsl_version = "#version 150";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY);
SDL_Window *window = SDL_CreateWindow("Transcript browser", 960, 600, window_flags);
if (window == nullptr) {
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return -1;
}
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context);
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) {
return -1;
}
SDL_GL_SetSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
// Setup Dear ImGui style
// ImGui::StyleColorsDark();
ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
bool deffered_read_on_start = false;
{
String read_on_start = ReadOnStart;
deffered_read_on_start = (read_on_start != "-");
}
enum MenuOpen {
MenuOpen_Query,
MenuOpen_LoadedFiles,
MenuOpen_Config,
MenuOpen_Count,
};
MenuOpen menu_open = MenuOpen_Query;
bool set_focus_to_input = true;
int64_t frame = -1;
// Main loop
bool done = false;
while (!done) {
Scratch frame_arena;
frame += 1;
if (frame > 4) set_focus_to_input = false;
bool set_focus_to_list = false;
bool tab_press = false;
bool enter_press = false;
bool f1_press = false;
bool f2_press = false;
SDL_Event event;
SDL_Event input_event_saved;
#if 1
while (SDL_PollEvent(&event)) {
#else
if (SDL_WaitEvent(&event)) {
#endif
ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_KEY_DOWN) {
switch (event.key.key) {
case SDLK_UP:
case SDLK_DOWN:
case SDLK_PAGEUP:
case SDLK_PAGEDOWN:
set_focus_to_list = true;
break;
case SDLK_BACKSPACE:
case SDLK_DELETE:
set_focus_to_input = true;
input_event_saved = event;
break;
case SDLK_RETURN:
enter_press = true;
break;
case SDLK_TAB:
tab_press = true;
break;
case SDLK_F1:
f1_press = true;
break;
case SDLK_F2:
f2_press = true;
break;
}
} else if (event.type == SDL_EVENT_TEXT_INPUT) {
set_focus_to_input = true;
input_event_saved = event;
} else if (event.type == SDL_EVENT_QUIT) {
done = true;
} else if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) {
done = true;
}
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
#if 0
ImGui::ShowDemoWindow();
#else
// Query input window
{
ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse;
const ImGuiViewport *main_viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(main_viewport->Size.x, 40), ImGuiCond_Always);
ImGui::Begin("input", NULL, window_flags);
if (menu_open == MenuOpen_LoadedFiles) {
if (ImGui::Button("Hide files (F1)") || f1_press) {
menu_open = MenuOpen_Query;
}
} else {
if (ImGui::Button("Show files (F1)") || f1_press) {
menu_open = MenuOpen_LoadedFiles;
}
}
ImGui::SameLine();
if (menu_open == MenuOpen_Config) {
if (ImGui::Button("Hide config (F2)") || f2_press) {
menu_open = MenuOpen_Query;
}
} else {
if (ImGui::Button("Show config (F2)") || f2_press) {
menu_open = MenuOpen_Config;
}
}
ImGui::SameLine();
if (menu_open == MenuOpen_Query && set_focus_to_input) {
ImGui::SetKeyboardFocusHere(0);
}
if (tab_press && ImGui::IsWindowFocused()) set_focus_to_list = true;
if (ImGui::InputText("Input your query", Prompt, sizeof(Prompt))) {
StartSearchingForMatches();
}
ImGui::End();
if (enter_press) {
String prompt = Prompt;
if (StartsWith(prompt, "read=")) {
XAddFolder(prompt.skip(5), &filenames);
memset(Prompt, 0, sizeof(Prompt));
}
}
if (frame > 2 && deffered_read_on_start) {
deffered_read_on_start = false;
Scratch scratch;
String read_on_start = ReadOnStart;
Array<String> strings = Split(scratch, read_on_start, ";");
For(strings) XAddFolder(it, &filenames);
}
}
//
{
ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse;
const ImGuiViewport *main_viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(0, 35), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(main_viewport->Size.x, main_viewport->Size.y - 35), ImGuiCond_Always);
ImGui::Begin("result", NULL, window_flags);
if (!ImGui::IsWindowFocused() && set_focus_to_list) ImGui::SetKeyboardFocusHere(0);
if (menu_open == MenuOpen_Config) {
ImGui::InputText(".srt", SRTCommand, sizeof(SRTCommand));
ImGui::InputText(".pdf", PDFCommand, sizeof(PDFCommand));
ImGui::InputText(".txt", TXTCommand, sizeof(TXTCommand));
ImGui::InputText("Folders to read during startup", ReadOnStart, sizeof(ReadOnStart));
if (ImGui::Button("Save config")) {
SaveConfig();
LoadConfig();
}
} else if (menu_open == MenuOpen_LoadedFiles) {
UILoadedFiles();
} else {
UISearchResults(filenames);
}
ImGui::End();
}
#endif
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
SDL_Delay(16);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
SDL_GL_DestroyContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
#if _WIN32
int WinMain(void *hInstance, void *hPrevInstance, const char *cmd, int show_cmd) {
return EntryPoint();
}
#else
int main() {
return EntryPoint();
}
#endif

View File

@@ -1,76 +0,0 @@
struct PDFPage {
String string;
int number;
};
struct PDF {
String filename;
Array<PDFPage> pages;
};
// @todo: pull request the object close
// @todo: something needs to be done with unicode codepoints
PDF pdfioReadPDF(Allocator allocator, String filename) {
Assert(filename.data[filename.len] == 0);
PDF result = {};
result.pages = {allocator};
result.filename = filename;
pdfio_file_t *file = pdfioFileOpen(filename.data, NULL, NULL, NULL, NULL);
Assert(file);
defer { pdfioFileClose(file); };
char buffer[1024];
size_t page_count = pdfioFileGetNumPages(file);
result.pages.reserve(page_count);
for (size_t page_i = 0; page_i < page_count; page_i += 1) {
pdfio_obj_t *obj = pdfioFileGetPage(file, page_i);
if (obj == NULL) continue;
defer { pdfioObjClose(obj); };
PDFPage *page = result.pages.alloc();
page->number = (int)page_i;
Scratch scratch((Arena *)allocator.object);
Array<String> strings = {scratch};
size_t num_streams = pdfioPageGetNumStreams(obj);
for (size_t stream_i = 0; stream_i < num_streams; stream_i += 1) {
pdfio_stream_t *st = pdfioPageOpenStream(obj, stream_i, true);
if (st == NULL) continue;
defer { pdfioStreamClose(st); };
bool first = true;
while (pdfioStreamGetToken(st, buffer, sizeof(buffer))) {
if (buffer[0] == '(') {
if (first) {
first = false;
}
strings.add(Copy(scratch, buffer + 1));
} else if (!strcmp(buffer, "Td") || !strcmp(buffer, "TD") || !strcmp(buffer, "T*") || !strcmp(buffer, "\'") || !strcmp(buffer, "\"")) {
first = true;
}
}
}
page->string = Merge(allocator, strings, "");
}
return result;
}
#if 1
extern "C" void OutputDebugStringA(const char *);
void Printf(const char *string, ...) {
Scratch scratch;
STRING_FORMAT(scratch, string, result);
OutputDebugStringA(result.data);
}
#else
#define Printf(...) (0)
#endif
void PrintPDF(PDF pdf) {
For(pdf.pages) {
Printf("%d, %.*s\n", it.number, FmtString(it.string));
}
}

View File

@@ -1,63 +0,0 @@
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;
}
String FindVideoForSRT(Array<String> &filenames, String srt_path) {
String base = ChopLastPeriod(srt_path); // .srt
base = ChopLastPeriod(base); // .en
For(filenames) {
if (StartsWith(it, base)) {
if (EndsWith(it, ".mkv") || EndsWith(it, ".webm") || EndsWith(it, ".mp4")) {
return it;
}
}
}
return {};
}

View File

@@ -1,43 +0,0 @@
Array<String> Matches = {};
char Prompt[256];
std::mutex SearchThreadArrayMutex;
int64_t SearchThreadStopSearching;
WORK_FUNCTION(SearchForMatchesWork) {
if (Prompt[0] == 0) return;
int64_t search_thread_stop_searching = SearchThreadStopSearching;
SearchThreadArrayMutex.lock();
Matches.clear();
SearchThreadArrayMutex.unlock();
String buffer = XGetArenaString();
String find = Prompt;
int64_t index = 0;
while (Seek(buffer, find, &index, SeekFlag_IgnoreCase)) {
String found = {buffer.data + index, find.len};
if (search_thread_stop_searching != SearchThreadStopSearching) break;
Matches.bounded_add(found);
buffer = buffer.skip(index + find.len);
}
}
void StartSearchingForMatches() {
SearchThreadStopSearching += 1;
PushWork(&MainWorkQueue, NULL, SearchForMatchesWork);
}
Array<String> LockSearchResults() {
SearchThreadArrayMutex.lock();
Array<String> copy = Matches;
return copy;
}
void UnlockSearchResults() {
SearchThreadArrayMutex.unlock();
}
void InitSearch() {
Matches.reserve(100000);
}

View File

@@ -1,80 +0,0 @@
void TestArena() {
Arena arena = {};
{
void *value = PushSize(&arena, sizeof(int));
Assert(value == 0);
}
InitArena(&arena, MiB(256) + PAGE_SIZE);
Assert(arena.data && arena.reserve);
{
uint8_t *data = (uint8_t *)PushSize(&arena, sizeof(uint8_t));
*data = 10;
data[PAGE_SIZE - 1] = 0;
// data[PAGE_SIZE] = 0;
Assert(*data == 10);
Assert(arena.len == 1);
Assert(arena.commit == PAGE_SIZE);
uint8_t *d2 = (uint8_t *)PushSize(&arena, sizeof(uint8_t));
*d2 = 20;
Assert(arena.len == 9);
}
{
int *alot_of_data = (int *)PushSize(&arena, MiB(256));
for (int i = 0; i < 1024 * 1024 * 1; i += 1) {
alot_of_data[i] = i;
}
for (int i = 0; i < 1024 * 1024 * 1; i += 1) {
Assert(alot_of_data[i] == i);
}
Assert(arena.len == 16 + MiB(256));
Assert(arena.commit == AlignUp(16 + MiB(256), PAGE_SIZE));
}
{
void *result = PushSize(&arena, PAGE_SIZE - 16);
Assert(result);
Assert(arena.commit == arena.reserve);
Assert(arena.commit == arena.len);
void *v = PushSize(&arena, 2423);
Assert(v == NULL);
}
Release(&arena);
{
Arena *arena = AllocArena();
int *a = (int *)PushSize(arena, 1024);
for (int i = 0; i < 64; i += 1) a[i] = 0;
Clear(arena);
a = (int *)PushSize(arena, 1024);
for (int i = 0; i < 64; i += 1) a[i] = 0;
Release(arena);
}
}
void TestArray() {
Array<int> arr = {};
defer { arr.dealloc(); };
for (int i = 0; i < 32; i += 1) arr.add(i);
int i = 31;
For(arr.reverse_iter()) {
defer { arr.unordered_remove(it); };
Assert(i-- == it);
}
}
void RunTests() {
TestArena();
TestArray();
}

View File

@@ -1,4 +0,0 @@
When you are refering to a particular program through an absolute path, for example:
> C:/Program files/SumatraPDF/SumatraPDF.exe
If the path has spaces, make sure to sandvich the path with quotes '"', like this:
> "C:/Program files/SumatraPDF/SumatraPDF.exe"