GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
shared-fcns.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
24 
25 #include <windows.h>
26 #include <tlhelp32.h>
27 
28 #ifdef _MSC_VER
29 #define popen _popen
30 #define pclose _pclose
31 #endif
32 
33 static std::string
34 w32_get_octave_home (void)
35 {
36  std::string retval;
37 
38  std::string bin_dir;
39 
40  HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
41 #ifdef TH32CS_SNAPMODULE32
42  | TH32CS_SNAPMODULE32
43 #endif
44  , 0);
45 
46  if (h != INVALID_HANDLE_VALUE)
47  {
48  MODULEENTRY32 mod_info;
49 
50  ZeroMemory (&mod_info, sizeof (mod_info));
51  mod_info.dwSize = sizeof (mod_info);
52 
53  if (Module32First (h, &mod_info))
54  {
55  do
56  {
57  std::string mod_name (mod_info.szModule);
58 
59  if (mod_name.find ("octave") != std::string::npos)
60  {
61  bin_dir = mod_info.szExePath;
62 
63  if (bin_dir[bin_dir.length () - 1] != '\\')
64  bin_dir.append (1, '\\');
65 
66  break;
67  }
68  }
69  while (Module32Next (h, &mod_info));
70  }
71 
72  CloseHandle (h);
73  }
74 
75  if (! bin_dir.empty ())
76  {
77  size_t pos = bin_dir.rfind ("\\bin\\");
78 
79  if (pos != std::string::npos)
80  retval = bin_dir.substr (0, pos);
81  }
82 
83  return retval;
84 }
85 
86 #endif
87 
88 // Find the directory where the octave binary is supposed to be
89 // installed.
90 
91 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
92  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
93 static const char dir_sep_char = '\\';
94 #else
95 static const char dir_sep_char = '/';
96 #endif
97 
98 static std::string
99 octave_getenv (const std::string& name)
100 {
101  char *value = ::getenv (name.c_str ());
102 
103  return value ? value : "";
104 }
105 
106 static std::string
108 {
109  std::string oh = octave_getenv ("OCTAVE_HOME");
110 
111 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
112  if (oh.empty ())
113  oh = w32_get_octave_home ();
114 #endif
115 
116  return oh.empty () ? std::string (OCTAVE_PREFIX) : oh;
117 }
118 
119 static std::string
120 subst_octave_home (const std::string& s)
121 {
122  std::string retval;
123 
124  std::string octave_home = get_octave_home ();
125 
126  std::string prefix = OCTAVE_PREFIX;
127 
128  retval = s;
129 
130  if (octave_home != prefix)
131  {
132  octave_idx_type len = prefix.length ();
133 
134  if (s.substr (0, len) == prefix)
135  retval.replace (0, len, octave_home);
136  }
137 
138  if (dir_sep_char != '/')
139  std::replace (retval.begin (), retval.end (), '/', dir_sep_char);
140 
141  return retval;
142 }
static std::string get_octave_home(void)
Definition: shared-fcns.h:107
static std::string subst_octave_home(const std::string &s)
Definition: shared-fcns.h:120
#define OCTAVE_PREFIX
Definition: main.cc:58
static void replace(QString &text, const QRegExp &re, const QString &after)
Definition: parser.cc:566
static const char dir_sep_char
Definition: shared-fcns.h:95
static std::string octave_getenv(const std::string &name)
Definition: shared-fcns.h:99