diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2021-01-18 12:11:03 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2021-01-18 12:11:03 -0500 |
commit | 44f374480888812456892f59a9f25f475e564819 (patch) | |
tree | 444cc5b5547fca9a0bad720ca8a4c49c14a63092 | |
parent | 6db2ef3ce69c17f6291b30fcb9278e7d4df53c71 (diff) |
got windows build to partly work
-rw-r--r-- | base.h | 4 | ||||
-rw-r--r-- | config.c | 2 | ||||
-rw-r--r-- | filesystem-win.c | 2 | ||||
-rw-r--r-- | main.c | 47 | ||||
-rw-r--r-- | make.bat | 2 | ||||
-rw-r--r-- | math.c | 2 | ||||
-rw-r--r-- | ted-base.c | 31 |
7 files changed, 63 insertions, 27 deletions
@@ -7,6 +7,10 @@ #if _WIN32 #include <windows.h> +#include <shlobj.h> +#define PATH_SEPARATOR "\\" +#else +#define PATH_SEPARATOR "/" #endif #include <stdbool.h> @@ -160,6 +160,8 @@ void config_read(Ted *ted, char const *filename) { char *newline = strchr(line, '\n'); if (newline || feof(fp)) { if (newline) *newline = '\0'; + char *carriage_return = strchr(line, '\r'); + if (carriage_return) *carriage_return = '\0'; // ok, we've now read a line. switch (line[0]) { diff --git a/filesystem-win.c b/filesystem-win.c index 52dc64d..25b8d95 100644 --- a/filesystem-win.c +++ b/filesystem-win.c @@ -6,7 +6,7 @@ static bool fs_file_exists(char const *path) { struct _stat statbuf = {0}; if (_stat(path, &statbuf) != 0) return false; - return statbuf.st_mode == _S_IFREG; + return (statbuf.st_mode & _S_IFREG) != 0; } static char **fs_list_directory(char const *dirname) { @@ -68,30 +68,57 @@ INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, die("Out of memory."); } for (int i = 0; i < argc; i++) { - LPWSTR wide_arg = wide_args[i]; - int len = wcslen(wide_arg); + LPWSTR wide_arg = wide_argv[i]; + int len = (int)wcslen(wide_arg); argv[i] = malloc(len + 1); if (!argv[i]) die("Out of memory."); for (int j = 0; j <= len; j++) argv[i][j] = (char)wide_arg[j]; } - LocalFree(wide_args); + LocalFree(wide_argv); #else int main(int argc, char **argv) { #endif setlocale(LC_ALL, ""); // allow unicode + { // get local data directory +#if _WIN32 + wchar_t *appdata = NULL; + KNOWNFOLDERID id = FOLDERID_LocalAppData; + if (SHGetKnownFolderPath(&id, 0, NULL, &appdata) == S_OK) { + strbuf_printf(ted_local_data_dir, "%ls" PATH_SEPARATOR "ted", appdata); + CoTaskMemFree(appdata); + } +#else + char *home = getenv("HOME"); + strbuf_printf(ted_local_data_dir, "%s/.local/share/ted", home); +#endif + } + { // check if this is the installed version of ted (as opposed to just running it from the directory with the source) - char executable_path[TED_PATH_MAX] = {0}; + char executable_path[TED_PATH_MAX] = {0}, cwd[TED_PATH_MAX] = {0}; #if _WIN32 - // @TODO(windows): GetModuleFileNameW + if (GetModuleFileNameA(NULL, executable_path, sizeof executable_path) > 0) { + char *last_backslash = strrchr(executable_path, '\\'); + if (last_backslash && GetCurrentDirectory(sizeof cwd, cwd) > 0) { + *last_backslash = '\0'; + ted_search_cwd = streq(cwd, executable_path); + } + } #else ssize_t len = readlink("/proc/self/exe", executable_path, sizeof executable_path - 1); if (len == -1) { // some posix systems don't have /proc/self/exe. oh well. } else { executable_path[len] = '\0'; - ted_search_cwd = !str_is_prefix(executable_path, "/usr"); + char *last_slash = strrchr(executable_path, '/'); + if (last_slash) { + *last_slash = '\0'; + if (getcwd(cwd, sizeof cwd)) { + // @TODO: make sure this is working + ted_search_cwd = streq(cwd, executable_path); + } + } } #endif } @@ -106,7 +133,7 @@ int main(int argc, char **argv) { { // read global configuration file first to establish defaults char global_config_filename[TED_PATH_MAX]; - strbuf_printf(global_config_filename, "%s/ted.cfg", ted_global_data_dir); + strbuf_printf(global_config_filename, "%s" PATH_SEPARATOR "ted.cfg", ted_global_data_dir); if (fs_file_exists(global_config_filename)) config_read(ted, global_config_filename); } @@ -192,6 +219,12 @@ int main(int argc, char **argv) { bool ctrl_down = false; bool shift_down = false; bool alt_down = false; + { + char appdata[MAX_PATH] = {0}; + if (SHGetSpecialFolderPathA(NULL, appdata, CSIDL_LOCAL_APPDATA, false)) { + debug_println("%s", appdata); + } + } while (!quit) { #if DEBUG //printf("\033[H\033[2J"); @@ -4,7 +4,7 @@ if _%VCVARS% == _ ( call vcvarsall x64 ) -SET CFLAGS=/nologo /W3 /D_CRT_SECURE_NO_WARNINGS /I SDL2/include SDL2/lib/x64/SDL2main.lib SDL2/lib/x64/SDL2.lib opengl32.lib +SET CFLAGS=/nologo /W3 /D_CRT_SECURE_NO_WARNINGS /I SDL2/include SDL2/lib/x64/SDL2main.lib SDL2/lib/x64/SDL2.lib opengl32.lib shell32.lib ole32.lib rc /nologo ted.rc SET SOURCES=main.c text.c ted.res if _%1 == _ ( @@ -701,7 +701,7 @@ static void gl_color2f(float v, float a) { glColor4f(v,v,v,a); } -static void rgba_u32_to_floats(u32 rgba, float floats[static 4]) { +static void rgba_u32_to_floats(u32 rgba, float floats[4]) { floats[0] = (float)(rgba >> 24) / 255.f; floats[1] = (float)(rgba >> 16) / 255.f; floats[2] = (float)(rgba >> 8) / 255.f; @@ -1,4 +1,3 @@ - // this is a macro so we get -Wformat warnings #define ted_seterr(buffer, ...) \ snprintf(ted->error, sizeof ted->error - 1, __VA_ARGS__) @@ -35,34 +34,32 @@ static void *ted_realloc(Ted *ted, void *p, size_t new_size) { // should the working directory be searched for files? set to true if the executable isn't "installed" static bool ted_search_cwd = false; +static char const ted_global_data_dir[] = #if _WIN32 -// @TODO + "C:\\Program Files\\ted"; #else -static char const *const ted_global_data_dir = "/usr/share/ted"; + "/usr/share/ted"; #endif +static char ted_local_data_dir[TED_PATH_MAX]; // filled out in main() // Check the various places a file could be, and return the full path. static Status ted_get_file(char const *name, char *out, size_t outsz) { -#if _WIN32 - #error "@TODO(windows)" -#else if (ted_search_cwd && fs_file_exists(name)) { // check in current working directory str_cpy(out, outsz, name); return true; } - - char *home = getenv("HOME"); - if (home) { - str_printf(out, outsz, "%s/.local/share/ted/%s", home, name); - if (!fs_file_exists(out)) { - str_printf(out, outsz, "%s/%s", ted_global_data_dir, name); - if (!fs_file_exists(out)) - return false; - } + if (*ted_local_data_dir) { + str_printf(out, outsz, "%s" PATH_SEPARATOR "%s", ted_local_data_dir, name); + if (fs_file_exists(out)) + return true; } - return true; -#endif + if (*ted_global_data_dir) { + str_printf(out, outsz, "%s" PATH_SEPARATOR "%s", ted_global_data_dir, name); + if (fs_file_exists(out)) + return true; + } + return false; } static void ted_load_font(Ted *ted) { |