GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
shared-fcns.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // These functions are also defined in liboctave or libinterp. They
27 // are repeated here to avoid having to link the main Octave program
28 // with the Octave libraries.
29 
30 #if ! defined (octave_shared_fcns_h)
31 #define octave_shared_fcns_h 1
32 
33 #include <cctype>
34 
35 #if defined (OCTAVE_USE_WINDOWS_API)
36 
37 # include <windows.h>
38 # include <tlhelp32.h>
39 # include <locale>
40 # include <codecvt>
41 
42 # if defined (_MSC_VER)
43 # define popen _popen
44 # define pclose _pclose
45 # endif
46 
47 static std::string
48 w32_get_octave_home (void)
49 {
50  std::string retval;
51 
52  std::string bin_dir;
53 
54  wchar_t namebuf[MAX_PATH+1];
55  DWORD n_size
56  = GetModuleFileNameW (GetModuleHandle (nullptr), namebuf, MAX_PATH);
57  if (n_size < MAX_PATH)
58  {
59  // convert wide character string to multibyte UTF-8 string
60  std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> wchar_conv;
61  std::string exe_name
62  = wchar_conv.to_bytes (std::wstring (namebuf, n_size));
63 
64  std::size_t pos = exe_name.rfind ('\\');
65 
66  if (pos != std::string::npos)
67  bin_dir = exe_name.substr (0, pos + 1);
68  }
69 
70  if (! bin_dir.empty ())
71  {
72  std::size_t pos = bin_dir.rfind (R"(\bin\)");
73 
74  if (pos != std::string::npos)
75  retval = bin_dir.substr (0, pos);
76  }
77 
78  return retval;
79 }
80 
81 #endif
82 
83 // Find the directory where the octave binary is supposed to be
84 // installed.
85 
86 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
87  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
88 static const char dir_sep_char = '\\';
89 #else
90 static const char dir_sep_char = '/';
91 #endif
92 
93 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
94 static std::string dir_sep_chars = R"(/\)";
95 #else
96 static std::string dir_sep_chars = "/";
97 #endif
98 
99 static std::string
100 octave_getenv (const std::string& name)
101 {
102  char *value = ::getenv (name.c_str ());
103 
104  return value ? value : "";
105 }
106 
107 static std::string Voctave_home;
108 static std::string Voctave_exec_home;
109 
110 static void
112 {
113  std::string op = OCTAVE_PREFIX;
114  std::string oep = OCTAVE_EXEC_PREFIX;
115 
116  std::string oh = octave_getenv ("OCTAVE_HOME");
117  std::string oeh = octave_getenv ("OCTAVE_EXEC_HOME");
118 
119 #if defined (OCTAVE_USE_WINDOWS_API)
120  if (oh.empty ())
121  oh = w32_get_octave_home ();
122 #endif
123 
124  // If OCTAVE_HOME is set in the environment, use that. Otherwise,
125  // default to ${prefix} from configure.
126 
127  Voctave_home = (oh.empty () ? op : oh);
128 
129  // If OCTAVE_EXEC_HOME is set in the environment, use that.
130  // Otherwise, if ${prefix} and ${exec_prefix} from configure are set
131  // to the same value, use OCTAVE_HOME from the environment if it is set.
132  // Otherwise, default to ${exec_prefix} from configure.
133 
134  if (! oeh.empty ())
135  Voctave_exec_home = oeh;
136  else if (op == oep && ! oh.empty ())
137  Voctave_exec_home = oh;
138  else
139  Voctave_exec_home = oep;
140 }
141 
142 static bool is_dir_sep (char c)
143 {
144  return dir_sep_chars.find (c) != std::string::npos;
145 }
146 
147 static bool
148 absolute_pathname (const std::string& s)
149 {
150  std::size_t len = s.length ();
151 
152  if (len == 0)
153  return false;
154 
155  if (is_dir_sep (s[0]))
156  return true;
157 
158 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
159  if ((len == 2 && isalpha (s[0]) && s[1] == ':')
160  || (len > 2 && isalpha (s[0]) && s[1] == ':'
161  && is_dir_sep (s[2])))
162  return true;
163 #endif
164 
165  return false;
166 }
167 
168 static std::string
169 prepend_home_dir (const std::string& hd, const std::string& s)
170 {
171  std::string retval = s;
172 
173  if (! absolute_pathname (retval))
174  retval = hd + dir_sep_char + s;
175 
176  if (dir_sep_char != '/')
177  std::replace (retval.begin (), retval.end (), '/', dir_sep_char);
178 
179  return retval;
180 }
181 
182 // prepend_octave_home is used in mkoctfile.in.cc and
183 // octave-config.in.cc but not in main.in.cc. Tagging it as unused
184 // avoids warnings from GCC about an unused function but should not
185 // cause trouble in the event that it actually is used.
186 
188 static std::string
189 prepend_octave_home (const std::string& s)
190 {
191  return prepend_home_dir (Voctave_home, s);
192 }
193 
194 static std::string
195 prepend_octave_exec_home (const std::string& s)
196 {
198 }
199 
200 #endif
std::string bin_dir(void)
Definition: defaults.cc:171
#define OCTAVE_PREFIX
Definition: main.in.cc:75
#define OCTAVE_EXEC_PREFIX
Definition: main.in.cc:79
#define OCTAVE_UNUSED
static std::string Voctave_home
Definition: shared-fcns.h:107
static void set_octave_home(void)
Definition: shared-fcns.h:111
static bool absolute_pathname(const std::string &s)
Definition: shared-fcns.h:148
static std::string prepend_octave_exec_home(const std::string &s)
Definition: shared-fcns.h:195
static const char dir_sep_char
Definition: shared-fcns.h:90
static std::string Voctave_exec_home
Definition: shared-fcns.h:108
static bool is_dir_sep(char c)
Definition: shared-fcns.h:142
static std::string dir_sep_chars
Definition: shared-fcns.h:96
static std::string prepend_home_dir(const std::string &hd, const std::string &s)
Definition: shared-fcns.h:169
static std::string octave_getenv(const std::string &name)
Definition: shared-fcns.h:100
static OCTAVE_UNUSED std::string prepend_octave_home(const std::string &s)
Definition: shared-fcns.h:189
F77_RET_T len
Definition: xerbla.cc:61