don't crash in GetInstallPrefix() if wxTheApp == NULL (especially as we don't need...
[wxWidgets.git] / src / unix / stdpaths.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: unix/stdpaths.cpp
3 // Purpose: wxStandardPaths implementation for Unix & OpenVMS systems
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 2004-10-19
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_STDPATHS
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #endif //WX_PRECOMP
32
33 #include "wx/filename.h"
34
35 #include "wx/stdpaths.h"
36
37 #if defined( __LINUX__ ) || defined( __VMS )
38 #include <unistd.h>
39 #endif
40
41 #if defined(__WXMAC__)
42 #include "wx/mac/private.h"
43 #endif
44
45 // ============================================================================
46 // wxStandardPaths implementation
47 // ============================================================================
48
49 // ----------------------------------------------------------------------------
50 // prefix management
51 // ----------------------------------------------------------------------------
52
53 void wxStandardPaths::SetInstallPrefix(const wxString& prefix)
54 {
55 m_prefix = prefix;
56 }
57
58 wxString wxStandardPaths::GetInstallPrefix() const
59 {
60 if ( m_prefix.empty() )
61 {
62 wxStandardPaths *pathPtr = wx_const_cast(wxStandardPaths *, this);
63
64 #ifdef __LINUX__
65 // under Linux, we can try to infer the prefix from the location of the
66 // executable
67 char buf[4096];
68 int result = readlink("/proc/self/exe", buf, WXSIZEOF(buf) - sizeof(char));
69 if ( result != -1 )
70 {
71 buf[result] = '\0'; // readlink() doesn't NUL-terminate the buffer
72
73 const wxString exeStr(buf, wxConvLibc);
74
75 // consider that we're in the last "bin" subdirectory of our prefix
76 size_t pos = exeStr.rfind(wxT("/bin/"));
77 if ( pos != wxString::npos )
78 pathPtr->m_prefix.assign(exeStr, 0, pos);
79 }
80 #endif // __LINUX__
81
82 if ( m_prefix.empty() )
83 {
84 #ifdef __VMS
85 pathPtr->m_prefix = wxT("/sys$system");
86 #else
87 pathPtr->m_prefix = wxT("/usr/local");
88 #endif
89 }
90 }
91
92 return m_prefix;
93 }
94
95 // ----------------------------------------------------------------------------
96 // public functions
97 // ----------------------------------------------------------------------------
98
99 wxString wxStandardPaths::GetConfigDir() const
100 {
101 #ifdef __VMS
102 return _T("/sys$manager");
103 #else
104 return _T("/etc");
105 #endif
106 }
107
108 wxString wxStandardPaths::GetUserConfigDir() const
109 {
110 return wxFileName::GetHomeDir();
111 }
112
113 wxString wxStandardPaths::GetDataDir() const
114 {
115 #ifdef __VMS
116 return AppendAppName(GetInstallPrefix() + _T("/sys$share"));
117 #else
118 return AppendAppName(GetInstallPrefix() + _T("/share"));
119 #endif
120 }
121
122 wxString wxStandardPaths::GetLocalDataDir() const
123 {
124 #ifdef __VMS
125 return AppendAppName(_T("/sys$manager"));
126 #else
127 return AppendAppName(_T("/etc"));
128 #endif
129 }
130
131 wxString wxStandardPaths::GetUserDataDir() const
132 {
133 #ifdef __VMS
134 return wxFileName::GetHomeDir();
135 #elif defined(__WXMAC__)
136 return AppendAppName(wxMacFindFolder((short) kUserDomain, kApplicationSupportFolderType, kDontCreateFolder));
137 #else
138 return AppendAppName(wxFileName::GetHomeDir() + _T("/."));
139 #endif
140 }
141
142 wxString wxStandardPaths::GetPluginsDir() const
143 {
144 return wxString();
145 }
146
147 #endif // wxUSE_STDPATHS