New project
This commit is contained in:
241
build_file.cpp
241
build_file.cpp
@@ -1,173 +1,17 @@
|
|||||||
#include "src/build_tool/library.cpp"
|
#include "src/build_tool/library.cpp"
|
||||||
|
|
||||||
struct Library {
|
enum {
|
||||||
Array<S8_String> sources;
|
PROFILE_DEBUG,
|
||||||
Array<S8_String> objects;
|
PROFILE_RELEASE,
|
||||||
Array<S8_String> include_paths;
|
|
||||||
Array<S8_String> defines;
|
|
||||||
};
|
};
|
||||||
|
int Profile = PROFILE_DEBUG;
|
||||||
|
|
||||||
Library PreparePdfio(S8_String zlib_include_path) {
|
void AddCommonFlags(Array<S8_String> *cmd) {
|
||||||
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) {
|
|
||||||
cmd->add("/MP /Zi /FC /nologo");
|
cmd->add("/MP /Zi /FC /nologo");
|
||||||
cmd->add("/WX /W3 /wd4200 /diagnostics:column");
|
cmd->add("/WX /W3 /wd4200 /diagnostics:column");
|
||||||
cmd->add("/Oi");
|
cmd->add("/Oi");
|
||||||
cmd->add("-D_CRT_SECURE_NO_WARNINGS");
|
cmd->add("-D_CRT_SECURE_NO_WARNINGS");
|
||||||
if (debug) {
|
if (Profile == PROFILE_DEBUG) {
|
||||||
cmd->add("-DDEBUG_BUILD=1");
|
cmd->add("-DDEBUG_BUILD=1");
|
||||||
cmd->add("-DRELEASE_BUILD=0");
|
cmd->add("-DRELEASE_BUILD=0");
|
||||||
// cmd->add("-D_DEBUG /MDd");
|
// cmd->add("-D_DEBUG /MDd");
|
||||||
@@ -178,45 +22,86 @@ void AddCommonFlags(Array<S8_String> *cmd, bool debug = true) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
struct Library {
|
||||||
MA_InitScratch();
|
Array<S8_String> sources;
|
||||||
SRC_InitCache(Perm, "pdf_browser.cache");
|
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;
|
int result = 0;
|
||||||
|
|
||||||
Array<Library> libs = {};
|
Array<Library> libs = {};
|
||||||
libs.add(PrepareSDL());
|
libs.add(PrepareRaylib());
|
||||||
libs.add(PrepareZLib());
|
|
||||||
libs.add(PreparePdfio(libs[1].include_paths[0]));
|
|
||||||
libs.add(PrepareGlad());
|
|
||||||
libs.add(PrepareIMGUI(libs[0].include_paths[0]));
|
|
||||||
|
|
||||||
Array<S8_String> cmd = {};
|
Array<S8_String> cmd = {};
|
||||||
cmd.add("cl.exe");
|
cmd.add("cl.exe");
|
||||||
cmd.add("-Fe:transcript_browser.exe");
|
cmd.add("-Fe:te.exe");
|
||||||
cmd.add("-Fd:transcript_browser.pdb");
|
cmd.add("-Fd:te.pdb");
|
||||||
cmd.add("-std:c++20"); // semaphore
|
|
||||||
AddCommonFlags(&cmd);
|
AddCommonFlags(&cmd);
|
||||||
For2(lib, libs) For(lib.defines) cmd.add(it);
|
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");
|
cmd.add("../src/basic/win32.cpp");
|
||||||
|
|
||||||
For2(lib, libs) For(lib.include_paths) cmd.add(Fmt("-I %.*s", S8_Expand(it)));
|
For2(lib, libs) For(lib.include_paths) cmd.add(Fmt("-I %.*s", S8_Expand(it)));
|
||||||
|
|
||||||
cmd.add("/link");
|
cmd.add("/link");
|
||||||
cmd.add("/incremental:no");
|
cmd.add("/incremental:no");
|
||||||
cmd.add("/SUBSYSTEM:WINDOWS");
|
// cmd.add("/SUBSYSTEM:WINDOWS");
|
||||||
cmd.add("opengl32.lib gdi32.lib winmm.lib Setupapi.lib ole32.lib oleaut32.lib version.lib");
|
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);
|
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));
|
// For(cmd) IO_Printf("%.*s\n", S8_Expand(it));
|
||||||
|
|
||||||
result += Run(cmd);
|
result += Run(cmd);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
MA_InitScratch();
|
||||||
|
SRC_InitCache(Perm, "te.cache");
|
||||||
|
int result = CompileTextEditor();
|
||||||
|
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
OS_DeleteFile("pdf_browser.cache");
|
OS_DeleteFile("te.cache");
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,17 @@
|
|||||||
#define TiB(x) (GiB(x) * 1024ull)
|
#define TiB(x) (GiB(x) * 1024ull)
|
||||||
#define Lengthof(x) ((int64_t)((sizeof(x) / sizeof((x)[0]))))
|
#define Lengthof(x) ((int64_t)((sizeof(x) / sizeof((x)[0]))))
|
||||||
|
|
||||||
|
using U8 = uint8_t;
|
||||||
|
using U16 = uint16_t;
|
||||||
|
using U32 = uint32_t;
|
||||||
|
using U64 = uint64_t;
|
||||||
|
using S8 = int8_t;
|
||||||
|
using S16 = int16_t;
|
||||||
|
using S32 = int32_t;
|
||||||
|
using S64 = int64_t;
|
||||||
|
using Int = S64;
|
||||||
|
using UInt = U64;
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
T Min(T a, T b) {
|
T Min(T a, T b) {
|
||||||
if (a > b) return b;
|
if (a > b) return b;
|
||||||
@@ -168,10 +179,11 @@ void Dealloc(Allocator alo, T **p) {
|
|||||||
|
|
||||||
Allocator GetSystemAllocator();
|
Allocator GetSystemAllocator();
|
||||||
#define MemoryZero(x, size) memset(x, 0, size)
|
#define MemoryZero(x, size) memset(x, 0, size)
|
||||||
|
#define MemoryCopy(dst, src, size) memcpy(dst, src, size)
|
||||||
|
#define MemoryMove(dst, src, size) memmove(dst, src, size)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Iterating and removing elements
|
// Iterating and removing elements
|
||||||
|
|
||||||
for (int i = 0; i < array.len; i += 1) {
|
for (int i = 0; i < array.len; i += 1) {
|
||||||
auto &it = array[i];
|
auto &it = array[i];
|
||||||
bool remove_item = false;
|
bool remove_item = false;
|
||||||
@@ -184,23 +196,16 @@ for (int i = 0; i < array.len; i += 1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Simple delete
|
// Simple delete
|
||||||
|
|
||||||
IterRemove(arr) {
|
IterRemove(arr) {
|
||||||
IterRemovePrepare(arr);
|
IterRemovePrepare(arr);
|
||||||
|
|
||||||
remove_item = true;
|
remove_item = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// Deleting backwards
|
// Deleting backwards
|
||||||
//
|
|
||||||
|
|
||||||
For(arr.reverse_iter()) {
|
For(arr.reverse_iter()) {
|
||||||
defer{ arr.unordered_remove(it); };
|
defer{ arr.unordered_remove(it); };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
#define IterRemove(a) for (int i = 0; i < (a).len; i += 1)
|
#define IterRemove(a) for (int i = 0; i < (a).len; i += 1)
|
||||||
#define IterRemovePrepare(a) \
|
#define IterRemovePrepare(a) \
|
||||||
@@ -216,6 +221,7 @@ For(arr.reverse_iter()) {
|
|||||||
#define For(array) ForItem(it, array)
|
#define For(array) ForItem(it, array)
|
||||||
|
|
||||||
constexpr int64_t SLICE_LAST = INT64_MIN;
|
constexpr int64_t SLICE_LAST = INT64_MIN;
|
||||||
|
int64_t WideLength(wchar_t *string);
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
struct Slice {
|
struct Slice {
|
||||||
@@ -224,163 +230,15 @@ struct Slice {
|
|||||||
|
|
||||||
Slice() = default;
|
Slice() = default;
|
||||||
Slice(T *s, int64_t l) : data(s), len(l) {}
|
Slice(T *s, int64_t l) : data(s), len(l) {}
|
||||||
|
|
||||||
Slice(char *s) : data(s), len(strlen(s)) {}
|
Slice(char *s) : data(s), len(strlen(s)) {}
|
||||||
Slice(const char *s) : data((char *)s), len(strlen((char *)s)) {}
|
Slice(const char *s) : data((char *)s), len(strlen((char *)s)) {}
|
||||||
Slice(const char *s, int64_t l) : data((char *)s), len(l) {}
|
Slice(const char *s, int64_t l) : data((char *)s), len(l) {}
|
||||||
|
|
||||||
Slice<T> copy(Allocator allocator) {
|
Slice(wchar_t *s) : data(s), len(WideLength(s)) {}
|
||||||
Slice<T> result = {AllocArray(allocator, T, len), len};
|
Slice(const wchar_t *s) : data((wchar_t *)s), len(WideLength((wchar_t *)s)) {}
|
||||||
return result;
|
Slice(const wchar_t *s, int64_t l) : data((wchar_t *)s), len(l) {}
|
||||||
}
|
|
||||||
|
|
||||||
// @copy_paste array slice
|
|
||||||
|
|
||||||
bool contains(T item) {
|
|
||||||
for (int64_t i = 0; i < len; i += 1) {
|
|
||||||
if (data[i] == item) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
T get_or_default(int64_t i, T default_value = {}) {
|
|
||||||
T result = default_value;
|
|
||||||
if (i >= 0 && i < len) {
|
|
||||||
result = data[i];
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
T pop() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data[--len];
|
|
||||||
}
|
|
||||||
|
|
||||||
void unordered_remove(T &item) { // DONT USE IN LOOPS !!!!
|
|
||||||
Assert(&item >= begin() && &item < end());
|
|
||||||
item = data[--len];
|
|
||||||
}
|
|
||||||
|
|
||||||
void unordered_remove_index(int64_t index) {
|
|
||||||
Assert(index >= 0 && index < len);
|
|
||||||
data[index] = data[--len];
|
|
||||||
}
|
|
||||||
|
|
||||||
void ordered_remove(T &item) { // DONT USE IN LOOPS !!!
|
|
||||||
Assert(&item >= begin() && &item < end());
|
|
||||||
int64_t index = get_index(item);
|
|
||||||
ordered_remove_index(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ordered_remove_index(int64_t index) {
|
|
||||||
Assert(index >= 0 && index < len);
|
|
||||||
int64_t right_len = len - index - 1;
|
|
||||||
memmove(data + index, data + index + 1, right_len * sizeof(T));
|
|
||||||
len -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// @copy_paste array slice string
|
|
||||||
|
|
||||||
Slice<T> chop(int64_t ilen) {
|
|
||||||
ilen = ClampTop(ilen, len);
|
|
||||||
Slice<T> result = {data, len - ilen};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Slice<T> skip(int64_t ilen) {
|
|
||||||
ilen = ClampTop(ilen, len);
|
|
||||||
Slice<T> result = {data + ilen, len - ilen};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Slice<T> get_postfix(int64_t ilen) {
|
|
||||||
ilen = ClampTop(ilen, len);
|
|
||||||
int64_t remain_len = len - ilen;
|
|
||||||
Slice<T> result = {data + remain_len, ilen};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Slice<T> get_prefix(int64_t ilen) {
|
|
||||||
ilen = ClampTop(ilen, len);
|
|
||||||
Slice<T> result = {data, ilen};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Slice<T> slice(int64_t first_index, int64_t one_past_last_index) {
|
|
||||||
// Negative indexes work in python style, they return you the index counting from end of list
|
|
||||||
if (one_past_last_index == SLICE_LAST) one_past_last_index = len;
|
|
||||||
if (one_past_last_index < 0) one_past_last_index = len + one_past_last_index;
|
|
||||||
|
|
||||||
if (first_index == SLICE_LAST) first_index = len;
|
|
||||||
if (first_index < 0) first_index = len + first_index;
|
|
||||||
|
|
||||||
Slice<T> result = *this;
|
|
||||||
if (len > 0) {
|
|
||||||
if (one_past_last_index > first_index) {
|
|
||||||
first_index = ClampTop(first_index, len - 1);
|
|
||||||
one_past_last_index = ClampTop(one_past_last_index, len);
|
|
||||||
result.data += first_index;
|
|
||||||
result.len = one_past_last_index - first_index;
|
|
||||||
} else {
|
|
||||||
result.len = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Reverse_Iter {
|
|
||||||
T *data;
|
|
||||||
Slice<T> *arr;
|
|
||||||
|
|
||||||
Reverse_Iter operator++(int) {
|
|
||||||
Reverse_Iter ret = *this;
|
|
||||||
data -= 1;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
Reverse_Iter &operator++() {
|
|
||||||
data -= 1;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
T &operator*() { return data[0]; }
|
|
||||||
T *operator->() { return data; }
|
|
||||||
|
|
||||||
friend bool operator==(const Reverse_Iter &a, const Reverse_Iter &b) { return a.data == b.data; };
|
|
||||||
friend bool operator!=(const Reverse_Iter &a, const Reverse_Iter &b) { return a.data != b.data; };
|
|
||||||
|
|
||||||
Reverse_Iter begin() { return Reverse_Iter{arr->end() - 1, arr}; }
|
|
||||||
Reverse_Iter end() { return Reverse_Iter{arr->begin() - 1, arr}; }
|
|
||||||
};
|
|
||||||
Reverse_Iter reverse_iter() { return {end() - 1, this}; }
|
|
||||||
|
|
||||||
bool contains(T &item) {
|
|
||||||
bool result = &item >= data && &item < data + len;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t get_index(const T &item) {
|
|
||||||
ptrdiff_t index = (ptrdiff_t)(&item - data);
|
|
||||||
Assert(index >= 0 && index < len);
|
|
||||||
return (int64_t)index;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_first(T &item) { return &item == first(); }
|
|
||||||
bool is_last(T &item) { return &item == last(); }
|
|
||||||
T *first() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
T *last() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data + len - 1;
|
|
||||||
}
|
|
||||||
T *front() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
T *back() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data + len - 1;
|
|
||||||
}
|
|
||||||
T &operator[](int64_t index) {
|
T &operator[](int64_t index) {
|
||||||
Assert(index < len);
|
Assert(index < len);
|
||||||
return data[index];
|
return data[index];
|
||||||
@@ -390,249 +248,107 @@ struct Slice {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
struct Array {
|
Slice<T> Copy(Allocator alo, Slice<T> array) {
|
||||||
Allocator allocator;
|
Slice<T> result = {alo};
|
||||||
T *data;
|
result.data = AllocArray(alo, T, array.len);
|
||||||
int64_t cap, len;
|
memcpy(result.data, array.data, sizeof(T) * array.len);
|
||||||
|
result.len = array.len;
|
||||||
void add(const T &item) {
|
|
||||||
try_growing();
|
|
||||||
data[len++] = item;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bounded_add(T item) {
|
|
||||||
if (len + 1 <= cap) {
|
|
||||||
data[len++] = item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
T *alloc(const T &item) {
|
|
||||||
try_growing();
|
|
||||||
T *ref = data + len++;
|
|
||||||
*ref = item;
|
|
||||||
return ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
T *alloc() {
|
|
||||||
try_growing();
|
|
||||||
T *ref = data + len++;
|
|
||||||
*ref = {};
|
|
||||||
return ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
T *alloc_multiple(int64_t size) {
|
|
||||||
try_growing_to_fit_item_count(size);
|
|
||||||
T *result = data + len;
|
|
||||||
len += size;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_array(T *items, int64_t item_count) {
|
template <class T>
|
||||||
for (int64_t i = 0; i < item_count; i += 1) {
|
T Pop(Slice<T> *arr) {
|
||||||
add(items[i]);
|
Assert(arr->len > 0);
|
||||||
}
|
return arr->data[--arr->len];
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_array(Array<T> items) {
|
template <class T>
|
||||||
add_array(items.data, items.len);
|
bool Contains(Slice<T> &arr, T item) {
|
||||||
}
|
For(arr) if (it == item) return true;
|
||||||
|
|
||||||
void reserve(int64_t size) {
|
|
||||||
if (size > cap) {
|
|
||||||
if (!allocator.proc) allocator = GetSystemAllocator();
|
|
||||||
|
|
||||||
void *new_data = AllocSize(allocator, size * sizeof(T));
|
|
||||||
Assert(new_data);
|
|
||||||
memcpy(new_data, data, len * sizeof(T));
|
|
||||||
Dealloc(allocator, &data);
|
|
||||||
|
|
||||||
data = (T *)new_data;
|
|
||||||
cap = size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void init(Allocator allocator, int64_t size) {
|
|
||||||
len = 0;
|
|
||||||
cap = 0;
|
|
||||||
data = 0;
|
|
||||||
this->allocator = allocator;
|
|
||||||
reserve(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
len = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void insert(T item, int64_t index) {
|
|
||||||
if (index == len) {
|
|
||||||
add(item);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert(index < len);
|
|
||||||
Assert(index >= 0);
|
|
||||||
|
|
||||||
try_growing();
|
|
||||||
int64_t right_len = len - index;
|
|
||||||
memmove(data + index + 1, data + index, sizeof(T) * right_len);
|
|
||||||
data[index] = item;
|
|
||||||
len += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dealloc() {
|
|
||||||
if (data) Dealloc(allocator, &data);
|
|
||||||
len = cap = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Array<T> copy(Allocator allocator) {
|
|
||||||
Array result = {};
|
|
||||||
result.allocator = allocator;
|
|
||||||
result.reserve(cap);
|
|
||||||
|
|
||||||
memmove(result.data, data, sizeof(T) * len);
|
|
||||||
result.len = len;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Array<T> tight_copy(Allocator allocator) {
|
|
||||||
Array result = {};
|
|
||||||
result.allocator = allocator;
|
|
||||||
result.reserve(len);
|
|
||||||
|
|
||||||
memmove(result.data, data, sizeof(T) * len);
|
|
||||||
result.len = len;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void try_growing() {
|
|
||||||
if (len + 1 > cap) {
|
|
||||||
int64_t new_size = cap * 2;
|
|
||||||
if (new_size < 16) new_size = 16;
|
|
||||||
|
|
||||||
reserve(new_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void try_growing_to_fit_item_count(int64_t item_count) {
|
|
||||||
if (len + item_count > cap) {
|
|
||||||
int64_t new_size = (cap + item_count) * 2;
|
|
||||||
if (new_size < 16) new_size = 16;
|
|
||||||
reserve(new_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// @copy_paste array slice
|
|
||||||
|
|
||||||
bool contains(T item) {
|
|
||||||
for (int64_t i = 0; i < len; i += 1) {
|
|
||||||
if (data[i] == item) return true;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
T get_or_default(int64_t i, T default_value = {}) {
|
template <class T>
|
||||||
|
int64_t GetIndex(Slice<T> &arr, const T &item) {
|
||||||
|
ptrdiff_t index = (ptrdiff_t)(&item - arr.data);
|
||||||
|
Assert(index >= 0 && index < arr.len);
|
||||||
|
return (int64_t)index;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T Get(Slice<T> &arr, int64_t i, T default_value = {}) {
|
||||||
T result = default_value;
|
T result = default_value;
|
||||||
if (i >= 0 && i < len) {
|
if (i >= 0 && i < arr.len) result = arr[i];
|
||||||
result = data[i];
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
T pop() {
|
template <class T>
|
||||||
Assert(len > 0);
|
bool IsLast(Slice<T> &arr, T &item) {
|
||||||
return data[--len];
|
bool result = arr.last() == &item;
|
||||||
}
|
|
||||||
|
|
||||||
void unordered_remove(T &item) { // DONT USE IN LOOPS !!!!
|
|
||||||
Assert(len > 0);
|
|
||||||
Assert(&item >= begin() && &item < end());
|
|
||||||
item = data[--len];
|
|
||||||
}
|
|
||||||
|
|
||||||
void unordered_remove_index(int64_t index) {
|
|
||||||
Assert(index >= 0 && index < len);
|
|
||||||
data[index] = data[--len];
|
|
||||||
}
|
|
||||||
|
|
||||||
void ordered_remove(T &item) {
|
|
||||||
Assert(len > 0);
|
|
||||||
Assert(&item >= begin() && &item < end());
|
|
||||||
int64_t index = get_index(item);
|
|
||||||
ordered_remove_index(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ordered_remove_index(int64_t index) {
|
|
||||||
Assert(index >= 0 && index < len);
|
|
||||||
int64_t right_len = len - index - 1;
|
|
||||||
memmove(data + index, data + index + 1, right_len * sizeof(T));
|
|
||||||
len -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// @copy_paste array string slice
|
|
||||||
struct Reverse_Iter {
|
|
||||||
T *data;
|
|
||||||
Array<T> *arr;
|
|
||||||
|
|
||||||
Reverse_Iter operator++(int) {
|
|
||||||
Reverse_Iter ret = *this;
|
|
||||||
data -= 1;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
Reverse_Iter &operator++() {
|
|
||||||
data -= 1;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
T &operator*() { return data[0]; }
|
|
||||||
T *operator->() { return data; }
|
|
||||||
|
|
||||||
friend bool operator==(const Reverse_Iter &a, const Reverse_Iter &b) { return a.data == b.data; };
|
|
||||||
friend bool operator!=(const Reverse_Iter &a, const Reverse_Iter &b) { return a.data != b.data; };
|
|
||||||
|
|
||||||
Reverse_Iter begin() { return Reverse_Iter{arr->end() - 1, arr}; }
|
|
||||||
Reverse_Iter end() { return Reverse_Iter{arr->begin() - 1, arr}; }
|
|
||||||
};
|
|
||||||
|
|
||||||
Reverse_Iter reverse_iter() { return {end() - 1, this}; }
|
|
||||||
|
|
||||||
Slice<T> chop(int64_t ilen) {
|
|
||||||
ilen = ClampTop(ilen, len);
|
|
||||||
Slice<T> result = {data, len - ilen};
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Slice<T> skip(int64_t ilen) {
|
template <class T>
|
||||||
ilen = ClampTop(ilen, len);
|
bool IsFirst(Slice<T> &arr, T &item) {
|
||||||
Slice<T> result = {data + ilen, len - ilen};
|
bool result = arr.first() == &item;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Slice<T> get_postfix(int64_t ilen) {
|
template <class T>
|
||||||
ilen = ClampTop(ilen, len);
|
T *GetFirst(Slice<T> &arr) {
|
||||||
int64_t remain_len = len - ilen;
|
Assert(arr.len > 0);
|
||||||
Slice<T> result = {data + remain_len, ilen};
|
return arr.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T *GetLast(Slice<T> &arr) {
|
||||||
|
Assert(arr.len > 0);
|
||||||
|
return arr.data + arr.len - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Slice<T> Chop(Slice<T> &arr, int64_t len) {
|
||||||
|
len = ClampTop(len, arr.len);
|
||||||
|
Slice<T> result = {arr.data, arr.len - len};
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Slice<T> get_prefix(int64_t ilen) {
|
template <class T>
|
||||||
ilen = ClampTop(ilen, len);
|
Slice<T> Skip(Slice<T> &arr, int64_t len) {
|
||||||
Slice<T> result = {data, ilen};
|
len = ClampTop(len, arr.len);
|
||||||
|
Slice<T> result = {arr.data + len, arr.len - len};
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Slice<T> slice(int64_t first_index = 0, int64_t one_past_last_index = SLICE_LAST) {
|
template <class T>
|
||||||
|
Slice<T> GetPostfix(Slice<T> &arr, int64_t len) {
|
||||||
|
len = ClampTop(len, arr.len);
|
||||||
|
int64_t remain_len = arr.len - len;
|
||||||
|
Slice<T> result = {arr.data + remain_len, len};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Slice<T> GetPrefix(Slice<T> &arr, int64_t len) {
|
||||||
|
len = ClampTop(len, arr.len);
|
||||||
|
Slice<T> result = {arr.data, len};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Slice<T> GetSlice(Slice<T> &arr, int64_t first_index = 0, int64_t one_past_last_index = SLICE_LAST) {
|
||||||
// Negative indexes work in python style, they return you the index counting from end of list
|
// Negative indexes work in python style, they return you the index counting from end of list
|
||||||
if (one_past_last_index == SLICE_LAST) one_past_last_index = len;
|
if (one_past_last_index == SLICE_LAST) one_past_last_index = arr.len;
|
||||||
if (one_past_last_index < 0) one_past_last_index = len + one_past_last_index;
|
if (one_past_last_index < 0) one_past_last_index = arr.len + one_past_last_index;
|
||||||
|
|
||||||
if (first_index == SLICE_LAST) first_index = len;
|
if (first_index == SLICE_LAST) first_index = arr.len;
|
||||||
if (first_index < 0) first_index = len + first_index;
|
if (first_index < 0) first_index = arr.len + first_index;
|
||||||
|
|
||||||
Slice<T> result = {data, len};
|
Slice<T> result = {arr.data, arr.len};
|
||||||
if (len > 0) {
|
if (arr.len > 0) {
|
||||||
if (one_past_last_index > first_index) {
|
if (one_past_last_index > first_index) {
|
||||||
first_index = ClampTop(first_index, len - 1);
|
first_index = ClampTop(first_index, arr.len - 1);
|
||||||
one_past_last_index = ClampTop(one_past_last_index, len);
|
one_past_last_index = ClampTop(one_past_last_index, arr.len);
|
||||||
result.data += first_index;
|
result.data += first_index;
|
||||||
result.len = one_past_last_index - first_index;
|
result.len = one_past_last_index - first_index;
|
||||||
} else {
|
} else {
|
||||||
@@ -642,36 +358,18 @@ struct Array {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t get_index(const T &item) {
|
template <class T>
|
||||||
ptrdiff_t index = (ptrdiff_t)(&item - data);
|
struct Array {
|
||||||
Assert(index >= 0 && index < len);
|
Allocator allocator;
|
||||||
// Assert(index > INT_MIN && index < INT_MAX);
|
int64_t cap;
|
||||||
return (int64_t)index;
|
union {
|
||||||
}
|
Slice<T> slice;
|
||||||
|
struct {
|
||||||
|
T *data;
|
||||||
|
int64_t len;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
bool contains(T &item) {
|
|
||||||
bool result = &item >= data && &item < data + len;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_first(T &item) { return &item == first(); }
|
|
||||||
bool is_last(T &item) { return &item == last(); }
|
|
||||||
T *first() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
T *last() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data + len - 1;
|
|
||||||
}
|
|
||||||
T *front() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
T *back() {
|
|
||||||
Assert(len > 0);
|
|
||||||
return data + len - 1;
|
|
||||||
}
|
|
||||||
T &operator[](int64_t index) {
|
T &operator[](int64_t index) {
|
||||||
Assert(index >= 0 && index < len);
|
Assert(index >= 0 && index < len);
|
||||||
return data[index];
|
return data[index];
|
||||||
@@ -680,6 +378,304 @@ struct Array {
|
|||||||
T *end() { return data + len; }
|
T *end() { return data + len; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T *GetFirst(Array<T> &arr) {
|
||||||
|
Assert(arr.len > 0);
|
||||||
|
return arr.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T *GetLast(Array<T> &arr) {
|
||||||
|
Assert(arr.len > 0);
|
||||||
|
return arr.data + arr.len - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Reserve(Array<T> *arr, int64_t size) {
|
||||||
|
if (size > arr->cap) {
|
||||||
|
if (!arr->allocator.proc) arr->allocator = GetSystemAllocator();
|
||||||
|
|
||||||
|
T *new_data = AllocArray(arr->allocator, T, size);
|
||||||
|
Assert(new_data);
|
||||||
|
memcpy(new_data, arr->data, arr->len * sizeof(T));
|
||||||
|
Dealloc(arr->allocator, &arr->data);
|
||||||
|
|
||||||
|
arr->data = new_data;
|
||||||
|
arr->cap = size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void TryGrowing(Array<T> *arr) {
|
||||||
|
if (arr->len + 1 > arr->cap) {
|
||||||
|
int64_t new_size = ClampBottom((int64_t)16, arr->cap * 2);
|
||||||
|
Reserve(arr, new_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void TryGrowing(Array<T> *arr, int64_t item_count) {
|
||||||
|
if (arr->len + item_count > arr->cap) {
|
||||||
|
int64_t new_size = ClampBottom((int64_t)16, (arr->cap + item_count) * 2);
|
||||||
|
Reserve(arr, new_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Add(Array<T> *arr, T item) {
|
||||||
|
TryGrowing(arr);
|
||||||
|
arr->data[arr->len++] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Add(Array<T> *arr, Array<T> &another) {
|
||||||
|
For(another) Add(arr, it);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Add(Array<T> *arr, T *items, int64_t item_count) {
|
||||||
|
for (int64_t i = 0; i < item_count; i += 1) Add(arr, items[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void BoundedAdd(Array<T> *arr, T item) {
|
||||||
|
if (arr->len + 1 <= arr->cap) arr->data[arr->len++] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T *Alloc(Array<T> *arr, T item) {
|
||||||
|
TryGrowing();
|
||||||
|
T *ref = arr->data + arr->len++;
|
||||||
|
ref[0] = item;
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T *Alloc(Array<T> *arr) {
|
||||||
|
return Alloc(arr, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T *AllocMultiple(Array<T> *arr, int64_t count, bool zero_memory = true) {
|
||||||
|
TryGrowing(arr, count);
|
||||||
|
T *result = arr->data + arr->len;
|
||||||
|
arr->len += count;
|
||||||
|
if (zero_memory)
|
||||||
|
for (int64_t i = 0; i < count; i += 1) result[i] = {};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Insert(Array<T> *arr, T item, int64_t index) {
|
||||||
|
if (index == arr->len) {
|
||||||
|
Add(arr, item);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert(index < arr->len);
|
||||||
|
Assert(index >= 0);
|
||||||
|
TryGrowing(arr);
|
||||||
|
int64_t right_len = arr->len - index;
|
||||||
|
memmove(arr->data + index + 1, arr->data + index, sizeof(T) * right_len);
|
||||||
|
arr->data[index] = item;
|
||||||
|
arr->len += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Array<T> Copy(Allocator alo, Array<T> array) {
|
||||||
|
Array<T> result = {alo};
|
||||||
|
result.reserve(array.cap);
|
||||||
|
memcpy(result.data, array.data, sizeof(T) * array.len);
|
||||||
|
result.len = array.len;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Array<T> TightCopy(Allocator alo, Array<T> array) {
|
||||||
|
Array<T> result = {alo};
|
||||||
|
result.reserve(array.len);
|
||||||
|
memcpy(result.data, array.data, sizeof(T) * array.len);
|
||||||
|
result.len = array.len;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T Pop(Array<T> *arr) {
|
||||||
|
Assert(arr->len > 0);
|
||||||
|
return arr->data[--arr->len];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
bool Contains(Array<T> &arr, T item) {
|
||||||
|
For(arr) if (it == item) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
int64_t GetIndex(Array<T> &arr, const T &item) {
|
||||||
|
ptrdiff_t index = (ptrdiff_t)(&item - arr.data);
|
||||||
|
Assert(index >= 0 && index < arr.len);
|
||||||
|
return (int64_t)index;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void RemoveByIndex(Array<T> *arr, int64_t index) {
|
||||||
|
Assert(index >= 0 && index < arr->len);
|
||||||
|
int64_t right_len = arr->len - index - 1;
|
||||||
|
memmove(arr->data + index, arr->data + index + 1, right_len * sizeof(T));
|
||||||
|
arr->len -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Remove(Array<T> *arr, T &item) {
|
||||||
|
Assert(arr->len > 0);
|
||||||
|
Assert(&item >= arr->begin() && &item < arr->end());
|
||||||
|
int64_t index = GetIndex(*arr, item);
|
||||||
|
RemoveByIndex(arr, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void UnorderedRemove(Array<T> *arr, T &item) {
|
||||||
|
Assert(arr->len > 0);
|
||||||
|
Assert(&item >= arr->begin && &item < arr->end);
|
||||||
|
item = arr->data[--arr->len];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void UnorderedRemoveByIndex(Array<T> *arr, int64_t index) {
|
||||||
|
Assert(arr->len > 0);
|
||||||
|
Assert(index >= 0 && index < arr->len);
|
||||||
|
arr->data[index] = arr->data[--arr->len];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void InsertArray(Array<T> *arr, T *items, int64_t count, int64_t index) {
|
||||||
|
if (index == arr->len) {
|
||||||
|
Add(arr, items, count);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Assert(index < arr->len);
|
||||||
|
|
||||||
|
TryGrowing(arr, count);
|
||||||
|
T *gap_begin = arr->data + index;
|
||||||
|
T *gap_end = gap_begin + count;
|
||||||
|
int64_t item_count = arr->len - index;
|
||||||
|
memmove(gap_end, gap_begin, item_count * sizeof(T));
|
||||||
|
for (int64_t i = 0; i < count; i += 1) arr->data[index + i] = items[i];
|
||||||
|
arr->len += count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T Get(Array<T> &arr, int64_t i, T default_value = {}) {
|
||||||
|
T result = default_value;
|
||||||
|
if (i >= 0 && i < arr.len) result = arr[i];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Dealloc(Array<T> *arr) {
|
||||||
|
if (arr->data) Dealloc(arr->allocator, arr->data);
|
||||||
|
arr->len = arr->cap = arr->data = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
bool IsLast(Array<T> &arr, T &item) {
|
||||||
|
bool result = GetLast(arr) == &item;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
bool IsFirst(Array<T> &arr, T &item) {
|
||||||
|
bool result = GetFirst(arr) == &item;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Slice<T> Chop(Array<T> &arr, int64_t len) {
|
||||||
|
len = ClampTop(len, arr.len);
|
||||||
|
Slice<T> result = {arr.data, arr.len - len};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Slice<T> Skip(Array<T> &arr, int64_t len) {
|
||||||
|
len = ClampTop(len, arr.len);
|
||||||
|
Slice<T> result = {arr.data + len, arr.len - len};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Slice<T> GetPostfix(Array<T> &arr, int64_t len) {
|
||||||
|
len = ClampTop(len, arr.len);
|
||||||
|
int64_t remain_len = arr.len - len;
|
||||||
|
Slice<T> result = {arr.data + remain_len, len};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Slice<T> GetPrefix(Array<T> &arr, int64_t len) {
|
||||||
|
len = ClampTop(len, arr.len);
|
||||||
|
Slice<T> result = {arr.data, len};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Slice<T> GetSlice(Array<T> &arr, int64_t first_index = 0, int64_t one_past_last_index = SLICE_LAST) {
|
||||||
|
// Negative indexes work in python style, they return you the index counting from end of list
|
||||||
|
if (one_past_last_index == SLICE_LAST) one_past_last_index = arr.len;
|
||||||
|
if (one_past_last_index < 0) one_past_last_index = arr.len + one_past_last_index;
|
||||||
|
|
||||||
|
if (first_index == SLICE_LAST) first_index = arr.len;
|
||||||
|
if (first_index < 0) first_index = arr.len + first_index;
|
||||||
|
|
||||||
|
Slice<T> result = {arr.data, arr.len};
|
||||||
|
if (arr.len > 0) {
|
||||||
|
if (one_past_last_index > first_index) {
|
||||||
|
first_index = ClampTop(first_index, arr.len - 1);
|
||||||
|
one_past_last_index = ClampTop(one_past_last_index, arr.len);
|
||||||
|
result.data += first_index;
|
||||||
|
result.len = one_past_last_index - first_index;
|
||||||
|
} else {
|
||||||
|
result.len = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct ReverseIter {
|
||||||
|
T *data;
|
||||||
|
Slice<T> *arr;
|
||||||
|
|
||||||
|
ReverseIter operator++(int) {
|
||||||
|
ReverseIter ret = *this;
|
||||||
|
data -= 1;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
ReverseIter &operator++() {
|
||||||
|
data -= 1;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
T &operator*() { return data[0]; }
|
||||||
|
T *operator->() { return data; }
|
||||||
|
|
||||||
|
friend bool operator==(const ReverseIter &a, const ReverseIter &b) { return a.data == b.data; };
|
||||||
|
friend bool operator!=(const ReverseIter &a, const ReverseIter &b) { return a.data != b.data; };
|
||||||
|
|
||||||
|
ReverseIter begin() { return ReverseIter{arr->end() - 1, arr}; }
|
||||||
|
ReverseIter end() { return ReverseIter{arr->begin() - 1, arr}; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
ReverseIter<T> IterateInReverse(Array<T> *arr) {
|
||||||
|
return {arr->end() - 1, &arr->slice};
|
||||||
|
}
|
||||||
|
template <class T>
|
||||||
|
ReverseIter<T> IterateInReverse(Slice<T> *slice) {
|
||||||
|
return {slice->end() - 1, slice};
|
||||||
|
}
|
||||||
|
|
||||||
struct UTF32Result {
|
struct UTF32Result {
|
||||||
uint32_t out_str;
|
uint32_t out_str;
|
||||||
int64_t advance;
|
int64_t advance;
|
||||||
@@ -756,7 +752,6 @@ String ToString(Allocator allocator, wchar_t *string, int64_t len);
|
|||||||
String ToString(Allocator allocator, wchar_t *wstring);
|
String ToString(Allocator allocator, wchar_t *wstring);
|
||||||
String16 ToString16(Allocator allocator, String string);
|
String16 ToString16(Allocator allocator, String string);
|
||||||
wchar_t *ToWidechar(Allocator allocator, String string);
|
wchar_t *ToWidechar(Allocator allocator, String string);
|
||||||
int64_t WideLength(wchar_t *string);
|
|
||||||
void NormalizePathInPlace(String s);
|
void NormalizePathInPlace(String s);
|
||||||
String ChopLastSlash(String s);
|
String ChopLastSlash(String s);
|
||||||
String ChopLastPeriod(String s);
|
String ChopLastPeriod(String s);
|
||||||
@@ -1284,13 +1279,13 @@ bool AreEqual(String a, String b, unsigned ignore_case) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool EndsWith(String a, String end, unsigned ignore_case = false) {
|
bool EndsWith(String a, String end, unsigned ignore_case = false) {
|
||||||
String a_end = a.get_postfix(end.len);
|
String a_end = GetPostfix(a, end.len);
|
||||||
bool result = AreEqual(end, a_end, ignore_case);
|
bool result = AreEqual(end, a_end, ignore_case);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StartsWith(String a, String start, unsigned ignore_case = false) {
|
bool StartsWith(String a, String start, unsigned ignore_case = false) {
|
||||||
String a_start = a.get_prefix(start.len);
|
String a_start = GetPrefix(a, start.len);
|
||||||
bool result = AreEqual(start, a_start, ignore_case);
|
bool result = AreEqual(start, a_start, ignore_case);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -1316,7 +1311,7 @@ String Trim(String string) {
|
|||||||
if (whitespace_begin == whitespace_end) {
|
if (whitespace_begin == whitespace_end) {
|
||||||
string.len = 0;
|
string.len = 0;
|
||||||
} else {
|
} else {
|
||||||
string = string.slice(whitespace_begin, whitespace_end);
|
string = GetSlice(string, whitespace_begin, whitespace_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
return string;
|
return string;
|
||||||
@@ -1330,7 +1325,7 @@ String TrimEnd(String string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String result = string.get_prefix(whitespace_end);
|
String result = GetPrefix(string, whitespace_end);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1377,7 +1372,7 @@ bool Seek(String string, String find, int64_t *index_out = NULL, SeekFlag flags
|
|||||||
if (flags & SeekFlag_MatchFindLast) {
|
if (flags & SeekFlag_MatchFindLast) {
|
||||||
for (int64_t i = string.len; i != 0; i--) {
|
for (int64_t i = string.len; i != 0; i--) {
|
||||||
int64_t index = i - 1;
|
int64_t index = i - 1;
|
||||||
String substring = string.slice(index, index + find.len);
|
String substring = GetSlice(string, index, index + find.len);
|
||||||
if (AreEqual(substring, find, ignore_case)) {
|
if (AreEqual(substring, find, ignore_case)) {
|
||||||
if (index_out)
|
if (index_out)
|
||||||
*index_out = index;
|
*index_out = index;
|
||||||
@@ -1387,7 +1382,7 @@ bool Seek(String string, String find, int64_t *index_out = NULL, SeekFlag flags
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int64_t i = 0; i < string.len; i++) {
|
for (int64_t i = 0; i < string.len; i++) {
|
||||||
String substring = string.slice(i, i + find.len);
|
String substring = GetSlice(string, i, i + find.len);
|
||||||
if (AreEqual(substring, find, ignore_case)) {
|
if (AreEqual(substring, find, ignore_case)) {
|
||||||
if (index_out)
|
if (index_out)
|
||||||
*index_out = i;
|
*index_out = i;
|
||||||
@@ -1405,10 +1400,10 @@ Array<String> Split(Allocator allocator, String string, String delimiter) {
|
|||||||
int64_t index = 0;
|
int64_t index = 0;
|
||||||
while (Seek(string, delimiter, &index)) {
|
while (Seek(string, delimiter, &index)) {
|
||||||
String before_match = {string.data, index};
|
String before_match = {string.data, index};
|
||||||
result.add(before_match);
|
Add(&result, before_match);
|
||||||
string = string.skip(index + delimiter.len);
|
string = Skip(string, index + delimiter.len);
|
||||||
}
|
}
|
||||||
result.add(string);
|
Add(&result, string);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1428,7 +1423,7 @@ String SkipToLastSlash(String s) {
|
|||||||
int64_t pos;
|
int64_t pos;
|
||||||
String result = s;
|
String result = s;
|
||||||
if (Seek(s, "/", &pos, SeekFlag_MatchFindLast)) {
|
if (Seek(s, "/", &pos, SeekFlag_MatchFindLast)) {
|
||||||
result = result.skip(pos + 1);
|
result = Skip(result, pos + 1);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -1437,7 +1432,7 @@ String SkipToLastPeriod(String s) {
|
|||||||
int64_t pos;
|
int64_t pos;
|
||||||
String result = s;
|
String result = s;
|
||||||
if (Seek(s, ".", &pos, SeekFlag_MatchFindLast)) {
|
if (Seek(s, ".", &pos, SeekFlag_MatchFindLast)) {
|
||||||
result = result.skip(pos + 1);
|
result = Skip(result, pos + 1);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -1457,7 +1452,7 @@ String Merge(Allocator allocator, Array<String> list, String separator = " ") {
|
|||||||
Assert(string.len + it.len <= size);
|
Assert(string.len + it.len <= size);
|
||||||
memcpy(string.data + string.len, it.data, it.len);
|
memcpy(string.data + string.len, it.data, it.len);
|
||||||
string.len += it.len;
|
string.len += it.len;
|
||||||
if (!list.is_last(it)) {
|
if (!IsLast(list, it)) {
|
||||||
memcpy(string.data + string.len, separator.data, separator.len);
|
memcpy(string.data + string.len, separator.data, separator.len);
|
||||||
string.len += separator.len;
|
string.len += separator.len;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
|
||||||
@@ -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 = ⁢
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
XArenaAddMutex.unlock();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool XLoadingComplete() {
|
|
||||||
bool result = MainWorkQueue.thread_count == XLoadThreadComplete;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
@@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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 {};
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
@@ -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"
|
|
||||||
Reference in New Issue
Block a user