From 7ae6e6da88749f27d26dd06c8944281587472b61 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Tue, 8 Feb 2011 00:21:42 +0000 Subject: [PATCH] Completely untested implementation of agi::Path for Windows. Originally committed to SVN as r5319. --- aegisub/libaegisub/windows/path.cpp | 78 ++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 7 deletions(-) diff --git a/aegisub/libaegisub/windows/path.cpp b/aegisub/libaegisub/windows/path.cpp index 09bcd1d20..de7844b73 100644 --- a/aegisub/libaegisub/windows/path.cpp +++ b/aegisub/libaegisub/windows/path.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011, <@aegisub.org> +// Copyright (c) 2011, Niels Martin Hansen // // Permission to use, copy, modify, and distribute this software for any // purpose with or without fee is hereby granted, provided that the above @@ -26,31 +26,95 @@ #endif #include +#include + + +namespace { +#include +#include + +const std::string WinGetFolderPath(int folder) { + wchar_t path[MAX_PATH+1] = {0}; + HRESULT res = SHGetFolderPathW( + 0, // hwndOwner + folder, // nFolder + 0, // hToken + 0, // dwFlags + path // pszPath + ); + if (FAILED(res)) + throw new agi::PathErrorInternal; //< @fixme error message? + else + return agi::charset::ConvertW(std::wstring(path)); +} + +std::string get_install_path() { + static std::string install_path; + static bool install_path_valid = false; + + if (install_path_valid == false) { + // Excerpt from : + // lpCmdLine [in] + // If this parameter is an empty string the function returns + // the path to the current executable file. + int argc; + LPWSTR *argv = CommandLineToArgvW(L"", &argc); + + wchar_t path[MAX_PATH+1] = {0}; + wchar_t *fn; + DWORD res = GetFullPathNameW(argv, MAX_PATH, path, &fn); + LocalFree(argv); + + if (res > 0 && GetLastError() == 0) { + *fn = "\0"; // fn points to filename part of path, set an end marker there + install_path = agi::charset::ConvertW(std::wstring(path)); + install_path_valid = true; + } else { + throw new agi::PathErrorInternal; + } + } + + return install_path; +} + +}; + namespace agi { const std::string Path::Data() { - return ""; + return get_install_path; } const std::string Path::Doc() { - return ""; + std::string path = Data(); + path.append("docs\\"); + return path; } const std::string Path::User() { - return ""; + return WinGetFolderPath(CSIDL_PERSONAL); } const std::string Path::Locale() { - return ""; + std::string path = Data(); + path.append("locale\\"); + return path; } const std::string Path::Config() { - return ""; + std::string path = WinGetFolderPath(CSIDL_APPDATA); + path.append("Aegisub-"); + path.append(AEGISUB_VERSION_DATA); + return path; } const std::string Path::Temp() { - return ""; + wchar_t path[MAX_PATH+1] = {0}; + if (GetTempPath(MAX_PATH, path) == 0) + throw new PathErrorInternal; + else + return charset::ConvertW(std::wstring(path)); } } // namespace agi