Read on start and omit file type

This commit is contained in:
Krzosa Karol
2024-07-12 12:47:29 +02:00
parent 1ec217905d
commit c660118f8b
3 changed files with 127 additions and 76 deletions

View File

@@ -8,9 +8,17 @@ struct ConfigParseResult {
Array<String> errors; Array<String> errors;
}; };
char SRTCommand[512] = "\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" --start-time {time_in_seconds} {video}"; struct ReplaceVar {
char PDFCommand[512] = "C:/Users/Karol/AppData/Local/SumatraPDF/SumatraPDF.exe -page {page} {file}"; String replace;
char TXTCommand[512] = "notepad.exe {file}"; 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) { void SkipWhitespace(String *s) {
while (s->len && IsWhitespace(s->data[0])) { while (s->len && IsWhitespace(s->data[0])) {
@@ -31,7 +39,7 @@ String ParseWord(String *s) {
return result; return result;
} }
bool ExpectString(String *s, String expect) { bool MatchString(String *s, String expect) {
SkipWhitespace(s); SkipWhitespace(s);
if (StartsWith(*s, expect)) { if (StartsWith(*s, expect)) {
*s = s->skip(expect.len); *s = s->skip(expect.len);
@@ -57,7 +65,7 @@ ConfigParseResult ParseConfig(Allocator allocator, String string) {
result.errors.add(Format(allocator, "failed to parse config at line: %d, the key is invalid", (int)lines.get_index(it))); result.errors.add(Format(allocator, "failed to parse config at line: %d, the key is invalid", (int)lines.get_index(it)));
continue; continue;
} }
bool e = ExpectString(&s, "="); bool e = MatchString(&s, "=");
if (!e) { if (!e) {
result.errors.add(Format(allocator, "failed to parse config at line: %d, expected '=' assignment sign", (int)lines.get_index(it))); result.errors.add(Format(allocator, "failed to parse config at line: %d, expected '=' assignment sign", (int)lines.get_index(it)));
continue; continue;
@@ -81,7 +89,7 @@ String GetValue(Array<Var> vars, String key) {
} }
String SerializeConfig(Allocator allocator) { String SerializeConfig(Allocator allocator) {
String content = Format(allocator, "SRTCommand = %s\nPDFCommand = %s\nTXTCommand = %s\n", SRTCommand, PDFCommand, TXTCommand); String content = Format(allocator, "SRTCommand = %s\nPDFCommand = %s\nTXTCommand = %s\nReadOnStart = %s", SRTCommand, PDFCommand, TXTCommand, ReadOnStart);
return content; return content;
} }
@@ -123,6 +131,12 @@ void LoadConfig() {
memcpy(SRTCommand, srt_command.data, len); memcpy(SRTCommand, srt_command.data, len);
SRTCommand[len] = 0; 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() { void SaveConfig() {
@@ -133,6 +147,59 @@ void SaveConfig() {
WriteFile(config_path, content); 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() { void TestConfig() {
Scratch scratch; Scratch scratch;
{ {

View File

@@ -111,21 +111,53 @@ Array<FileLoadResult> XLockFileLoadResults() {
void XUnlockFileResults() { 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) { void XAddFolder(String folder, Array<String> *filenames) {
Scratch scratch; Scratch scratch;
Array<String> files_to_parse = {scratch}; Array<String> files_here = {Perm};
for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) { if (ReadFoldersRecursively) {
String file = Copy(Perm, iter.absolute_path); files_here = GetFilesRecursive(Perm, folder);
filenames->add(file); } else {
if (EndsWith(iter.filename, ".srt", true)) { for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) {
files_to_parse.add(file); if (iter.is_directory) continue;
} else if (EndsWith(iter.filename, ".txt", true) || EndsWith(iter.filename, ".html", true)) { String file = Copy(Perm, iter.absolute_path);
files_to_parse.add(file); files_here.add(file);
} else if (EndsWith(iter.filename, ".pdf", true)) {
files_to_parse.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) { if (files_to_parse.len == 0) {
XFileLoadResults.add({Copy(Perm, folder), "no files found"}); XFileLoadResults.add({Copy(Perm, folder), "no files found"});
return; return;

View File

@@ -43,64 +43,6 @@ WorkQueue MainWorkQueue;
#include "loading_thread.cpp" #include "loading_thread.cpp"
#include "searching_thread.cpp" #include "searching_thread.cpp"
struct ReplaceVar {
String replace;
String with;
};
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 UISearchResults(Array<String> filenames) { void UISearchResults(Array<String> filenames) {
Scratch scratch; Scratch scratch;
Array<String> matches = LockSearchResults(); Array<String> matches = LockSearchResults();
@@ -226,11 +168,10 @@ int main(int, char **) {
XInitLoading(); XInitLoading();
InitSearch(); InitSearch();
LoadConfig();
ThreadStartupInfo infos[16] = {}; ThreadStartupInfo infos[16] = {};
InitWorkQueue(&MainWorkQueue, Lengthof(infos), infos); InitWorkQueue(&MainWorkQueue, Lengthof(infos), infos);
memcpy(Prompt, "read=D:/zizek", sizeof("read=D:/zizek"));
Array<String> filenames = {}; Array<String> filenames = {};
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
@@ -281,6 +222,15 @@ int main(int, char **) {
ImGui_ImplOpenGL3_Init(glsl_version); ImGui_ImplOpenGL3_Init(glsl_version);
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f); io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
{
String read_on_start = ReadOnStart;
if (read_on_start != "-") {
Scratch scratch;
Array<String> strings = Split(scratch, read_on_start, ";");
For(strings) XAddFolder(it, &filenames);
}
}
bool command_menu_open = false; bool command_menu_open = false;
bool show_loaded_files = false; bool show_loaded_files = false;
bool set_focus_to_input = true; bool set_focus_to_input = true;
@@ -407,8 +357,10 @@ int main(int, char **) {
ImGui::InputText(".srt", SRTCommand, sizeof(SRTCommand)); ImGui::InputText(".srt", SRTCommand, sizeof(SRTCommand));
ImGui::InputText(".pdf", PDFCommand, sizeof(PDFCommand)); ImGui::InputText(".pdf", PDFCommand, sizeof(PDFCommand));
ImGui::InputText(".txt", TXTCommand, sizeof(TXTCommand)); ImGui::InputText(".txt", TXTCommand, sizeof(TXTCommand));
ImGui::InputText("Folders to read during startup", ReadOnStart, sizeof(ReadOnStart));
if (ImGui::Button("Save config")) { if (ImGui::Button("Save config")) {
SaveConfig(); SaveConfig();
LoadConfig();
} }
} else { } else {
if (show_loaded_files) { if (show_loaded_files) {