GNU Octave  4.4.1
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-2018 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 // These functions are also defined in liboctave or libinterp. They
24 // are repeated here to avoid having to link the main Octave program
25 // with the Octave libraries.
26 
27 #if ! defined (octave_shared_fcns_h)
28 #define octave_shared_fcns_h 1
29 
30 #include <cctype>
31 
32 #if defined (OCTAVE_USE_WINDOWS_API)
33 
34 #include <windows.h>
35 #include <tlhelp32.h>
36 
37 #if defined (_MSC_VER)
38 # define popen _popen
39 # define pclose _pclose
40 #endif
41 
42 static std::string
43 w32_get_octave_home (void)
44 {
46 
48 
49  char namebuf[MAX_PATH+1];
50  if (GetModuleFileName (GetModuleHandle (NULL), namebuf, MAX_PATH))
51  {
52  namebuf[MAX_PATH] = '\0';
53 
54  std::string exe_name = namebuf;
55  size_t pos = exe_name.rfind ('\\');
56 
57  if (pos != std::string::npos)
58  bin_dir = exe_name.substr (0, pos + 1);
59  }
60 
61  if (! bin_dir.empty ())
62  {
63  size_t pos = bin_dir.rfind (R"(\bin\)");
64 
65  if (pos != std::string::npos)
66  retval = bin_dir.substr (0, pos);
67  }
68 
69  return retval;
70 }
71 
72 #endif
73 
74 // Find the directory where the octave binary is supposed to be
75 // installed.
76 
77 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
78  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
79 static const char dir_sep_char = '\\';
80 #else
81 static const char dir_sep_char = '/';
82 #endif
83 
84 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
85 static std::string dir_sep_chars = R"(/\)";
86 #else
88 #endif
89 
90 static std::string
92 {
93  char *value = ::getenv (name.c_str ());
94 
95  return value ? value : "";
96 }
97 
100 
101 static void
103 {
106 
107  std::string oh = octave_getenv ("OCTAVE_HOME");
108  std::string oeh = octave_getenv ("OCTAVE_EXEC_HOME");
109 
110 #if defined (OCTAVE_USE_WINDOWS_API)
111  if (oh.empty ())
112  oh = w32_get_octave_home ();
113 #endif
114 
115  // If OCTAVE_HOME is set in the enviornment, use that. Otherwise,
116  // default to ${prefix} from configure.
117 
118  Voctave_home = (oh.empty () ? op : oh);
119 
120  // If OCTAVE_EXEC_HOME is set in the environment, use that.
121  // Otherwise, if ${prefix} and ${exec_prefix} from configure are set
122  // to the same value, use OCTAVE_HOME from the environment if it is set.
123  // Othewise, default to ${exec_prefix} from configure.
124 
125  if (! oeh.empty ())
126  Voctave_exec_home = oeh;
127  else if (op == oep && ! oh.empty ())
128  Voctave_exec_home = oh;
129  else
130  Voctave_exec_home = oep;
131 }
132 
133 static bool is_dir_sep (char c)
134 {
135  return dir_sep_chars.find (c) != std::string::npos;
136 }
137 
138 static bool
140 {
141  size_t len = s.length ();
142 
143  if (len == 0)
144  return false;
145 
146  if (is_dir_sep (s[0]))
147  return true;
148 
149 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
150  if ((len == 2 && isalpha (s[0]) && s[1] == ':')
151  || (len > 2 && isalpha (s[0]) && s[1] == ':'
152  && is_dir_sep (s[2])))
153  return true;
154 #endif
155 
156  return false;
157 }
158 
159 static std::string
161 {
162  std::string retval = s;
163 
164  if (! absolute_pathname (retval))
165  retval = hd + dir_sep_char + s;
166 
167  if (dir_sep_char != '/')
168  std::replace (retval.begin (), retval.end (), '/', dir_sep_char);
169 
170  return retval;
171 }
172 
173 // prepend_octave_home is used in mkoctfile.in.cc and
174 // octave-config.in.cc but not in main.in.cc. Tagging it as unused
175 // avoids warnings from GCC about an unused function but should not
176 // cause trouble in the event that it actually is used.
177 
179 static std::string
181 {
182  return prepend_home_dir (Voctave_home, s);
183 }
184 
185 static std::string
187 {
189 }
190 
191 #endif
static std::string dir_sep_chars
Definition: shared-fcns.h:87
static void set_octave_home(void)
Definition: shared-fcns.h:102
static std::string prepend_octave_exec_home(const std::string &s)
Definition: shared-fcns.h:186
static std::string prepend_home_dir(const std::string &hd, const std::string &s)
Definition: shared-fcns.h:160
static std::string Voctave_exec_home
Definition: shared-fcns.h:99
#define OCTAVE_UNUSED
#define OCTAVE_EXEC_PREFIX
Definition: main.in.cc:64
static bool is_dir_sep(char c)
Definition: shared-fcns.h:133
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
s
Definition: file-io.cc:2729
std::string bin_dir(void)
Definition: defaults.cc:273
nd deftypefn *std::string name
Definition: sysdep.cc:647
static bool absolute_pathname(const std::string &s)
Definition: shared-fcns.h:139
octave_value retval
Definition: data.cc:6246
static std::string Voctave_home
Definition: shared-fcns.h:98
static const char dir_sep_char
Definition: shared-fcns.h:81
static OCTAVE_UNUSED std::string prepend_octave_home(const std::string &s)
Definition: shared-fcns.h:180
static std::string octave_getenv(const std::string &name)
Definition: shared-fcns.h:91
#define OCTAVE_PREFIX
Definition: main.in.cc:60
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
nd group nd example For each display the value
Definition: sysdep.cc:866