Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/plugin/PluginUtil.cpp

Issue 10825021: Move settings page into the application directory (Closed)
Patch Set: Fixed nit and removed copying of html directory Created June 4, 2013, 9:15 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/plugin/PluginUtil.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/plugin/PluginUtil.cpp
===================================================================
--- a/src/plugin/PluginUtil.cpp
+++ b/src/plugin/PluginUtil.cpp
@@ -1,41 +1,65 @@
-#include "PluginStdAfx.h"
-#include "PluginUtil.h"
-#include "PluginSettings.h"
-
-static CString sDllDir()
-{
- TCHAR filePath[MAX_PATH + 1];
- filePath[0] = L'\0';
-
- if (GetModuleFileName(_AtlBaseModule.GetModuleInstance(), filePath, countof(filePath) - 1))
- {
- TCHAR* pLastBackslash = wcsrchr(filePath, L'\\');
- if (pLastBackslash)
- {
- *(pLastBackslash + 1) = L'\0';
- }
- }
-
- return filePath;
-}
-
-const CString& DllDir()
-{
- static CString s_dllDir = sDllDir();
- return s_dllDir;
-}
-
-const CString& UserSettingsFileUrl()
-{
- static CString s_url = FileUrl(CPluginSettings::GetDataPath("html\\templates\\index.html"));
- return s_url;
-}
-
-CString FileUrl(const CString& url)
-{
- CString tmpUrl = url;
- tmpUrl.Replace(L'\\', L'/');
-
- return CString("file:///") + tmpUrl;
-}
-
+#include "PluginStdAfx.h"
+#include <algorithm>
+#include <stdexcept>
+#include <vector>
+
+#include "PluginUtil.h"
+#include "PluginSettings.h"
+
+BString::BString(const std::wstring& value)
+ : value(::SysAllocString(value.c_str()))
+{
+}
+
+BString::~BString()
+{
+ ::SysFreeString(value);
+}
+
+BString::operator BSTR()
+{
+ return value;
+}
+
+std::wstring DllDir()
+{
+ std::vector<WCHAR> path(MAX_PATH);
+ DWORD length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], path.size());
+
+ while (length == path.size())
+ {
+ // Buffer too small, double buffer size
+ path.resize(path.size() * 2);
+ length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], path.size());
+ }
+
+ try
+ {
+ if (length == 0)
+ throw std::runtime_error("Failed determining module path");
+
+ std::vector<WCHAR>::reverse_iterator it = std::find(path.rbegin(), path.rend(), L'\\');
+ if (it == path.rend())
+ throw std::runtime_error("Unexpected plugin path, no backslash found");
+
+ return std::wstring(path.begin(), it.base());
+ }
+ catch (const std::exception& e)
+ {
+ DEBUG_GENERAL(e.what());
+ return std::wstring();
+ }
+}
+
+std::wstring UserSettingsFileUrl()
+{
+ return FileUrl(DllDir() + L"html\\templates\\index.html");
+}
+
+std::wstring FileUrl(const std::wstring& path)
+{
+ std::wstring url = path;
+ std::replace(url.begin(), url.end(), L'\\', L'/');
+ return L"file:///" + url;
+}
+
« no previous file with comments | « src/plugin/PluginUtil.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld