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;
};
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}";
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])) {
@@ -31,7 +39,7 @@ String ParseWord(String *s) {
return result;
}
bool ExpectString(String *s, String expect) {
bool MatchString(String *s, String expect) {
SkipWhitespace(s);
if (StartsWith(*s, expect)) {
*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)));
continue;
}
bool e = ExpectString(&s, "=");
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;
@@ -81,7 +89,7 @@ String GetValue(Array<Var> vars, String key) {
}
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;
}
@@ -123,6 +131,12 @@ void LoadConfig() {
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() {
@@ -133,6 +147,59 @@ void SaveConfig() {
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;
{