From ac7ad70d4c6d7db2ab9e01a4662611f2a706b6fe Mon Sep 17 00:00:00 2001
From: Robert Roebling <robert@roebling.de>
Date: Sun, 12 Nov 2006 12:58:10 +0000
Subject: [PATCH]   [ 1590221 ] wxStandardPaths::GetExecutablePath     Required
 for optimised stackwalker patch.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43340 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/stdpaths.tex               | 17 ++++++++++++++++-
 include/wx/mac/corefoundation/stdpaths.h |  1 +
 include/wx/msw/stdpaths.h                |  1 +
 include/wx/palmos/stdpaths.h             |  1 +
 include/wx/stdpaths.h                    |  7 +++++++
 include/wx/unix/stdpaths.h               |  1 +
 samples/console/console.cpp              | 23 ++++++++++++++++++++++-
 src/common/stdpbase.cpp                  | 21 +++++++++++++++++++++
 src/mac/corefoundation/stdpaths_cf.cpp   | 16 ++++++++++++++++
 src/msw/stdpaths.cpp                     |  5 +++++
 src/unix/stdpaths.cpp                    | 16 ++++++++++++----
 11 files changed, 103 insertions(+), 6 deletions(-)

diff --git a/docs/latex/wx/stdpaths.tex b/docs/latex/wx/stdpaths.tex
index 4ef4821098..62a2ab82ea 100644
--- a/docs/latex/wx/stdpaths.tex
+++ b/docs/latex/wx/stdpaths.tex
@@ -111,6 +111,21 @@ Example return values:
 \newsince{2.7.0}
 
 
+\membersection{wxStandardPaths::GetExecutablePath}\label{wxstandardpathsgetexecutablepath}
+
+\constfunc{wxString}{GetExecutablePath}{\void}
+
+Return the directory and the filename for the current executable.
+
+Example return values:
+\begin{itemize}
+    \item Unix: \texttt{/usr/local/bin/exename}
+    \item Windows: \texttt{C:$\backslash$Programs$\backslash$AppFolder$\backslash$exename.exe}
+    \item Mac: \texttt{/Programs/exename}
+\end{itemize}
+
+
+
 \membersection{wxStandardPaths::GetInstallPrefix}\label{wxstandardpathsgetinstallprefix}
 
 \constfunc{wxString}{GetInstallPrefix}{\void}
@@ -253,7 +268,7 @@ all platforms except Windows where it returns
 \textbf{Note:} This function is only available under Unix.
 
 Lets wxStandardPaths know about the real program installation prefix on a Unix
-system. By default, the value returned by 
+system. By default, the value returned by
 \helpref{GetInstallPrefix}{wxstandardpathsgetinstallprefix} is used.
 
 Although under Linux systems the program prefix may usually be determined
diff --git a/include/wx/mac/corefoundation/stdpaths.h b/include/wx/mac/corefoundation/stdpaths.h
index 9950bdbbd7..b4b2a88dc8 100644
--- a/include/wx/mac/corefoundation/stdpaths.h
+++ b/include/wx/mac/corefoundation/stdpaths.h
@@ -33,6 +33,7 @@ public:
     void SetBundle(wxCFBundleRef bundle);
 
     // implement base class pure virtuals
+    virtual wxString GetExecutablePath() const;
     virtual wxString GetConfigDir() const;
     virtual wxString GetUserConfigDir() const;
     virtual wxString GetDataDir() const;
diff --git a/include/wx/msw/stdpaths.h b/include/wx/msw/stdpaths.h
index 56bae840f5..d8f15d43c9 100644
--- a/include/wx/msw/stdpaths.h
+++ b/include/wx/msw/stdpaths.h
@@ -20,6 +20,7 @@ class WXDLLIMPEXP_BASE wxStandardPaths : public wxStandardPathsBase
 {
 public:
     // implement base class pure virtuals
+    virtual wxString GetExecutablePath() const;
     virtual wxString GetConfigDir() const;
     virtual wxString GetUserConfigDir() const;
     virtual wxString GetDataDir() const;
diff --git a/include/wx/palmos/stdpaths.h b/include/wx/palmos/stdpaths.h
index b650979ed9..8fdf87618b 100644
--- a/include/wx/palmos/stdpaths.h
+++ b/include/wx/palmos/stdpaths.h
@@ -21,6 +21,7 @@ class WXDLLIMPEXP_BASE wxStandardPaths : public wxStandardPathsBase
 public:
     void SetInstallPrefix(const wxString& prefix) { m_prefix = prefix; }
     wxString GetInstallPrefix() const { return m_prefix; }
+    virtual wxString GetExecutablePath() const { return m_prefix; }
     virtual wxString GetConfigDir() const { return m_prefix; }
     virtual wxString GetUserConfigDir() const { return m_prefix; }
     virtual wxString GetDataDir() const { return m_prefix; }
diff --git a/include/wx/stdpaths.h b/include/wx/stdpaths.h
index 80d3a8e89d..7828b69e62 100644
--- a/include/wx/stdpaths.h
+++ b/include/wx/stdpaths.h
@@ -43,6 +43,11 @@ public:
     // return the global standard paths object
     static wxStandardPathsBase& Get();
 
+    // return the path (directory+filename) of the running executable or
+    // wxEmptyString if it couldn't be determined.
+    // The path is returned as an absolute path whenever possible.
+    // Default implementation only try to use wxApp->argv[0].
+    virtual wxString GetExecutablePath() const;
 
     // return the directory with system config files:
     // /etc under Unix, c:\Documents and Settings\All Users\Application Data
@@ -152,6 +157,8 @@ class WXDLLIMPEXP_BASE wxStandardPaths : public wxStandardPathsBase
 public:
     void SetInstallPrefix(const wxString& prefix) { m_prefix = prefix; }
     wxString GetInstallPrefix() const { return m_prefix; }
+
+    virtual wxString GetExecutablePath() const { return m_prefix; }
     virtual wxString GetConfigDir() const { return m_prefix; }
     virtual wxString GetUserConfigDir() const { return m_prefix; }
     virtual wxString GetDataDir() const { return m_prefix; }
diff --git a/include/wx/unix/stdpaths.h b/include/wx/unix/stdpaths.h
index dee1224b06..ed37b43ae4 100644
--- a/include/wx/unix/stdpaths.h
+++ b/include/wx/unix/stdpaths.h
@@ -38,6 +38,7 @@ public:
 
 
     // implement base class pure virtuals
+    virtual wxString GetExecutablePath() const;
     virtual wxString GetConfigDir() const;
     virtual wxString GetUserConfigDir() const;
     virtual wxString GetDataDir() const;
diff --git a/samples/console/console.cpp b/samples/console/console.cpp
index 90548b2445..8c405aab04 100644
--- a/samples/console/console.cpp
+++ b/samples/console/console.cpp
@@ -88,7 +88,27 @@
     #define TEST_WCHAR
     #define TEST_ZIP
 #else // #if TEST_ALL
+<<<<<<< console.cpp
+<<<<<<< console.cpp
+<<<<<<< console.cpp
+<<<<<<< console.cpp
+<<<<<<< console.cpp
+    #define TEST_MIME
+=======
+    #define TEST_STDPATHS
+>>>>>>> 1.197
+=======
+    #define TEST_MODULE
+>>>>>>> 1.199
+=======
+    #define TEST_INFO_FUNCTIONS
+>>>>>>> 1.201
+=======
+    #define TEST_STDPATHS
+>>>>>>> 1.203
+=======
     #define TEST_FILE
+>>>>>>> 1.204
 #endif
 
 // some tests are interactive, define this to run them
@@ -2799,6 +2819,7 @@ static void TestStandardPaths()
     wxPrintf(_T("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str());
     wxPrintf(_T("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str());
     wxPrintf(_T("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str());
+    wxPrintf(_T("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str());
     wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
     wxPrintf(_T("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str());
     wxPrintf(_T("Localized res. dir:\t%s\n"),
@@ -4367,9 +4388,9 @@ int main(int argc, char **argv)
     wxLog::AddTraceMask(_T("mime"));
     #if TEST_ALL
         TestMimeEnum();
+    #endif
         TestMimeOverride();
         TestMimeAssociate();
-    #endif
     TestMimeFilename();
 #endif // TEST_MIME
 
diff --git a/src/common/stdpbase.cpp b/src/common/stdpbase.cpp
index dd9fb2801f..3aacd46b8a 100644
--- a/src/common/stdpbase.cpp
+++ b/src/common/stdpbase.cpp
@@ -53,6 +53,27 @@ wxStandardPathsBase& wxStandardPathsBase::Get()
     return traits->GetStandardPaths();
 }
 
+wxString wxStandardPathsBase::GetExecutablePath() const
+{
+    if ( !wxTheApp || !wxTheApp->argv )
+        return wxEmptyString;
+
+    wxString argv0 = wxTheApp->argv[0];
+    if (wxIsAbsolutePath(argv0))
+        return argv0;
+
+    // Search PATH.environment variable...
+    wxPathList pathlist;
+    pathlist.AddEnvList(wxT("PATH"));
+    wxString path = pathlist.FindAbsoluteValidPath(argv0);
+    if ( path.empty() )
+        return argv0;       // better than nothing
+
+    wxFileName filename(path);
+    filename.Normalize();
+    return filename.GetFullPath();
+}
+
 wxStandardPathsBase& wxAppTraitsBase::GetStandardPaths()
 {
     return gs_stdPaths;
diff --git a/src/mac/corefoundation/stdpaths_cf.cpp b/src/mac/corefoundation/stdpaths_cf.cpp
index 451acd14df..12c4e3db04 100644
--- a/src/mac/corefoundation/stdpaths_cf.cpp
+++ b/src/mac/corefoundation/stdpaths_cf.cpp
@@ -151,6 +151,22 @@ wxString wxStandardPathsCF::GetDataDir() const
     return GetFromFunc(CFBundleCopySharedSupportURL);
 }
 
+wxString wxStandardPathsCF::GetExecutablePath() const
+{
+    ProcessInfoRec processinfo;
+    ProcessSerialNumber procno ;
+    FSSpec fsSpec;
+
+    procno.highLongOfPSN = NULL ;
+    procno.lowLongOfPSN = kCurrentProcess ;
+    processinfo.processInfoLength = sizeof(ProcessInfoRec);
+    processinfo.processName = NULL;
+    processinfo.processAppSpec = &fsSpec;
+
+    GetProcessInformation( &procno , &processinfo ) ;
+    return wxMacFSSpec2MacFilename(&fsSpec);
+}
+
 wxString wxStandardPathsCF::GetLocalDataDir() const
 {
 #ifdef __WXMAC__
diff --git a/src/msw/stdpaths.cpp b/src/msw/stdpaths.cpp
index 611d49a198..96fe3e23e7 100644
--- a/src/msw/stdpaths.cpp
+++ b/src/msw/stdpaths.cpp
@@ -274,6 +274,11 @@ wxString wxStandardPaths::GetDocumentsDir() const
 // public functions
 // ----------------------------------------------------------------------------
 
+wxString wxStandardPaths::GetExecutablePath() const
+{
+    return wxGetFullModuleName();
+}
+
 wxString wxStandardPaths::GetConfigDir() const
 {
     return AppendAppName(DoGetDirectory(CSIDL_COMMON_APPDATA));
diff --git a/src/unix/stdpaths.cpp b/src/unix/stdpaths.cpp
index 06a2e30781..edbaef8fed 100644
--- a/src/unix/stdpaths.cpp
+++ b/src/unix/stdpaths.cpp
@@ -106,11 +106,9 @@ wxStandardPaths::GetLocalizedResourcesDir(const wxChar *lang,
 // wxStandardPaths implementation for Unix
 // ============================================================================
 
-void wxStandardPaths::DetectPrefix()
+wxString wxStandardPaths::GetExecutablePath() const
 {
 #ifdef __LINUX__
-    // under Linux, we can try to infer the prefix from the location of the
-    // executable
     wxString exeStr;
 
     char buf[4096];
@@ -137,6 +135,17 @@ void wxStandardPaths::DetectPrefix()
         wxGetEnv(wxT("   "), &exeStr);
     }
 
+    if ( !exeStr.empty() )
+        return exeStr;
+#endif // __LINUX__
+
+    return wxStandardPathsBase::GetExecutablePath();
+}
+
+void wxStandardPaths::DetectPrefix()
+{
+    // we can try to infer the prefix from the location of the executable
+    wxString exeStr = GetExecutablePath();
     if ( !exeStr.empty() )
     {
         // consider that we're in the last "bin" subdirectory of our prefix
@@ -144,7 +153,6 @@ void wxStandardPaths::DetectPrefix()
         if ( pos != wxString::npos )
             m_prefix.assign(exeStr, 0, pos);
     }
-#endif // __LINUX__
 
     if ( m_prefix.empty() )
     {
-- 
2.50.0

