]> git.saurik.com Git - wxWidgets.git/blob - src/unix/stdpaths.cpp
prevent memory leak
[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 #include "wx/stdpaths.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/utils.h"
33 #endif //WX_PRECOMP
34
35 #include "wx/filename.h"
36 #include "wx/log.h"
37 #include "wx/textfile.h"
38
39 #if defined( __LINUX__ ) || defined( __VMS )
40 #include <unistd.h>
41 #endif
42
43 // ============================================================================
44 // common VMS/Unix part of wxStandardPaths implementation
45 // ============================================================================
46
47 void wxStandardPaths::SetInstallPrefix(const wxString& prefix)
48 {
49 m_prefix = prefix;
50 }
51
52 wxString wxStandardPaths::GetUserConfigDir() const
53 {
54 return wxFileName::GetHomeDir();
55 }
56
57
58 // ============================================================================
59 // wxStandardPaths implementation for VMS
60 // ============================================================================
61
62 #ifdef __VMS
63
64 wxString wxStandardPaths::GetInstallPrefix() const
65 {
66 if ( m_prefix.empty() )
67 {
68 wx_const_cast(wxStandardPaths *, this)->m_prefix = wxT("/sys$system");
69 }
70
71 return m_prefix;
72 }
73
74 wxString wxStandardPaths::GetConfigDir() const
75 {
76 return _T("/sys$manager");
77 }
78
79 wxString wxStandardPaths::GetDataDir() const
80 {
81 return AppendAppInfo(GetInstallPrefix() + _T("/sys$share"));
82 }
83
84 wxString wxStandardPaths::GetLocalDataDir() const
85 {
86 return AppendAppInfo(_T("/sys$manager"));
87 }
88
89 wxString wxStandardPaths::GetUserDataDir() const
90 {
91 return wxFileName::GetHomeDir();
92 }
93
94 wxString wxStandardPaths::GetPluginsDir() const
95 {
96 return wxString(); // TODO: this is wrong, it should return something
97 }
98
99 wxString
100 wxStandardPaths::GetLocalizedResourcesDir(const wxString& lang,
101 ResourceCat category) const
102 {
103 return wxStandardPathsBase::GetLocalizedResourcesDir(lang, category);
104 }
105
106 wxString wxStandardPaths::GetExecutablePath() const
107 {
108 return wxStandardPathsBase::GetExecutablePath();
109 }
110
111 #else // !__VMS
112
113 // ============================================================================
114 // wxStandardPaths implementation for Unix
115 // ============================================================================
116
117 wxString wxStandardPaths::GetExecutablePath() const
118 {
119 #ifdef __LINUX__
120 wxString exeStr;
121
122 char buf[4096];
123 int result = readlink("/proc/self/exe", buf, WXSIZEOF(buf) - sizeof(char));
124 if ( result != -1 )
125 {
126 buf[result] = '\0'; // readlink() doesn't NUL-terminate the buffer
127
128 // if the /proc/self/exe symlink has been dropped by the kernel for
129 // some reason, then readlink() could also return success but
130 // "(deleted)" as link destination...
131 if ( strcmp(buf, "(deleted)") != 0 )
132 exeStr = wxString(buf, wxConvLibc);
133 }
134
135 if ( exeStr.empty() )
136 {
137 // UPX-specific hack: when using UPX on linux, the kernel will drop the
138 // /proc/self/exe link; in this case we try to look for a special
139 // environment variable called " " which is created by UPX to save
140 // /proc/self/exe contents. See
141 // http://sf.net/tracker/?func=detail&atid=309863&aid=1565357&group_id=9863
142 // for more information about this issue.
143 wxGetEnv(wxT(" "), &exeStr);
144 }
145
146 if ( !exeStr.empty() )
147 return exeStr;
148 #endif // __LINUX__
149
150 return wxStandardPathsBase::GetExecutablePath();
151 }
152
153 void wxStandardPaths::DetectPrefix()
154 {
155 // we can try to infer the prefix from the location of the executable
156 wxString exeStr = GetExecutablePath();
157 if ( !exeStr.empty() )
158 {
159 // consider that we're in the last "bin" subdirectory of our prefix
160 size_t pos = exeStr.rfind(wxT("/bin/"));
161 if ( pos != wxString::npos )
162 m_prefix.assign(exeStr, 0, pos);
163 }
164
165 if ( m_prefix.empty() )
166 {
167 m_prefix = wxT("/usr/local");
168 }
169 }
170
171 wxString wxStandardPaths::GetInstallPrefix() const
172 {
173 if ( m_prefix.empty() )
174 {
175 wxStandardPaths *pathPtr = wx_const_cast(wxStandardPaths *, this);
176 pathPtr->DetectPrefix();
177 }
178
179 return m_prefix;
180 }
181
182 // ----------------------------------------------------------------------------
183 // public functions
184 // ----------------------------------------------------------------------------
185
186 wxString wxStandardPaths::GetConfigDir() const
187 {
188 return _T("/etc");
189 }
190
191 wxString wxStandardPaths::GetDataDir() const
192 {
193 return AppendAppInfo(GetInstallPrefix() + _T("/share"));
194 }
195
196 wxString wxStandardPaths::GetLocalDataDir() const
197 {
198 return AppendAppInfo(_T("/etc"));
199 }
200
201 wxString wxStandardPaths::GetUserDataDir() const
202 {
203 return AppendAppInfo(wxFileName::GetHomeDir() + _T("/."));
204 }
205
206 wxString wxStandardPaths::GetPluginsDir() const
207 {
208 return AppendAppInfo(GetInstallPrefix() + _T("/lib"));
209 }
210
211 wxString
212 wxStandardPaths::GetLocalizedResourcesDir(const wxString& lang,
213 ResourceCat category) const
214 {
215 if ( category != ResourceCat_Messages )
216 return wxStandardPathsBase::GetLocalizedResourcesDir(lang, category);
217
218 return GetInstallPrefix() + _T("/share/locale/") + lang + _T("/LC_MESSAGES");
219 }
220
221 wxString wxStandardPaths::GetDocumentsDir() const
222 {
223 {
224 wxLogNull logNull;
225 wxString homeDir = wxFileName::GetHomeDir();
226 wxString configPath;
227 if (wxGetenv(wxT("XDG_CONFIG_HOME")))
228 configPath = wxGetenv(wxT("XDG_CONFIG_HOME"));
229 else
230 configPath = homeDir + wxT("/.config");
231 wxString dirsFile = configPath + wxT("/user-dirs.dirs");
232 if (wxFileExists(dirsFile))
233 {
234 wxTextFile textFile;
235 if (textFile.Open(dirsFile))
236 {
237 size_t i;
238 for (i = 0; i < textFile.GetLineCount(); i++)
239 {
240 wxString line(textFile[i]);
241 int pos = line.Find(wxT("XDG_DOCUMENTS_DIR"));
242 if (pos != wxNOT_FOUND)
243 {
244 wxString value = line.AfterFirst(wxT('='));
245 value.Replace(wxT("$HOME"), homeDir);
246 value.Trim(true);
247 value.Trim(false);
248 if (!value.IsEmpty() && wxDirExists(value))
249 return value;
250 else
251 break;
252 }
253 }
254 }
255 }
256 }
257
258 return wxStandardPathsBase::GetDocumentsDir();
259 }
260
261 #endif // __VMS/!__VMS
262
263 #endif // wxUSE_STDPATHS