1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        utils.h
3 // Purpose:     Miscellaneous utilities
4 // Author:      Alex Thuering
5 // Created:	23.10.2003
6 // RCS-ID:      $Id: utils.cpp,v 1.6 2010/02/16 21:10:19 ntalex Exp $
7 // Copyright:   (c) Alex Thuering
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 #include "utils.h"
12 #include <wx/dir.h>
13 #include <wx/log.h>
14 #include <wx/filename.h>
15 
16 #ifdef __UNIX_LIKE__
17 #include <stdlib.h>
18 #include <sys/param.h>
19 #endif
20 
21 wxString appPath;
22 
wxGetAppPath()23 wxString wxGetAppPath()
24 {
25   if (appPath.length() == 0)
26 #if defined(__WXMAC__) && !defined(__DARWIN__)
27     // On Mac, the current directory is the relevant one when
28     // the application starts.
29     appPath = wxGetWorkingDirectory();
30 #else
31   {
32     if (wxIsAbsolutePath(wxTheApp->argv[0]))
33       appPath = wxTheApp->argv[0];
34     else
35     {
36 	  // Is it a relative path?
37 	  wxString currentDir(wxGetCwd());
38 	  if (currentDir.Last() != wxFILE_SEP_PATH)
39 		currentDir += wxFILE_SEP_PATH;
40 	  wxString str = currentDir + wxTheApp->argv[0];
41 	  if (wxFileExists(str))
42 		appPath = str;
43 	  else
44 	  {
45 		// OK, it's neither an absolute path nor a relative path.
46 		// Search PATH.
47 		wxPathList pathList;
48 		pathList.AddEnvList(wxT("PATH"));
49 		str = pathList.FindAbsoluteValidPath(wxTheApp->argv[0]);
50 		if (!str.IsEmpty())
51 		  appPath = str;
52 	  }
53     }
54 #ifdef __UNIX_LIKE__
55 	// realfullname
56 	char realnameBuf[MAXPATHLEN];
57 	char* realname = realpath(appPath.mb_str(), (char*)realnameBuf);
58 	appPath = wxString(realname, *wxConvCurrent);
59 #endif
60 	appPath = wxPathOnly(appPath);
61   }
62 #endif
63   if (appPath.Last() != wxFILE_SEP_PATH)
64     appPath += wxFILE_SEP_PATH;
65   return appPath;
66 }
67 
wxSetAppPath(wxString value)68 void wxSetAppPath(wxString value)
69 {
70   appPath = value;
71 }
72 
wxFindDataDirectory(wxString dir)73 wxString wxFindDataDirectory(wxString dir)
74 {
75   wxString d = wxGetAppPath() + dir;
76   if (wxDir::Exists(d))
77 	return d;
78   wxFileName dname(wxGetAppPath() + wxT("..") +
79      wxFILE_SEP_PATH + dir + wxFILE_SEP_PATH);
80   dname.Normalize();
81   if (wxDir::Exists(dname.GetFullPath()))
82 	return dname.GetFullPath();
83 #ifdef DATADIR
84   return wxString(DATADIR,wxConvLocal) + wxFILE_SEP_PATH + dir + wxFILE_SEP_PATH;
85 #else
86   return wxGetAppPath() + dir + wxFILE_SEP_PATH;
87 #endif
88 }
89 
wxFindDataFile(wxString filename)90 wxString wxFindDataFile(wxString filename)
91 {
92   wxString d = wxGetAppPath() + filename;
93   if (wxFileExists(d))
94 	return d;
95   wxFileName fname(wxGetAppPath() + wxT("..") + wxFILE_SEP_PATH + filename);
96   fname.Normalize();
97   if (wxFileExists(fname.GetFullPath()))
98 	return fname.GetFullPath();
99 #ifdef DATADIR
100   return wxString(DATADIR,wxConvLocal) + wxFILE_SEP_PATH + filename;
101 #else
102   return wxGetAppPath() + filename;
103 #endif
104 }
105