annotate src/shared-fcns.h @ 23083:e9a0469dedd9 stable

maint: strip extra trailing newlines from files.
author John W. Eaton <jwe@octave.org>
date Fri, 20 Jan 2017 12:19:08 -0500
parents e8d64dce0afd
children ef4d915df748 3ac9f9ecfae5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 /*
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2
22323
bac0d6f07a3e maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents: 22197
diff changeset
3 Copyright (C) 2008-2016 Michael Goffioul
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 This file is part of Octave.
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 option) any later version.
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 for more details.
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 along with Octave; see the file COPYING. If not, see
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 */
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22
21213
f7d1050b9b53 maint: Clean up various usages of #ifdef.
Rik <rik@octave.org>
parents: 21202
diff changeset
23 #if ! defined (octave_shared_fcns_h)
f7d1050b9b53 maint: Clean up various usages of #ifdef.
Rik <rik@octave.org>
parents: 21202
diff changeset
24 #define octave_shared_fcns_h 1
f7d1050b9b53 maint: Clean up various usages of #ifdef.
Rik <rik@octave.org>
parents: 21202
diff changeset
25
21979
d04da18a407a use OCTAVE_USE_WINDOWS_API more consistently
John W. Eaton <jwe@octave.org>
parents: 21960
diff changeset
26 #if defined (OCTAVE_USE_WINDOWS_API)
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 #include <windows.h>
18400
e0b96a751afc Include tlhelp32.h in shared-fcns.h
John Donoghue <john.donoghue@ieee.org>
parents: 18360
diff changeset
29 #include <tlhelp32.h>
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21213
diff changeset
31 #if defined (_MSC_VER)
21202
f7121e111991 maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents: 19697
diff changeset
32 # define popen _popen
f7121e111991 maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents: 19697
diff changeset
33 # define pclose _pclose
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 #endif
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 static std::string
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 w32_get_octave_home (void)
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 {
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 std::string retval;
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 std::string bin_dir;
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42
23003
d591b38e3410 Use GetModuleFileName for getting octave path in windows (bug #48671)
John D
parents: 22412
diff changeset
43 char namebuf[MAX_PATH+1];
d591b38e3410 Use GetModuleFileName for getting octave path in windows (bug #48671)
John D
parents: 22412
diff changeset
44 if (GetModuleFileName (GetModuleHandle (NULL), namebuf, MAX_PATH))
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 {
23003
d591b38e3410 Use GetModuleFileName for getting octave path in windows (bug #48671)
John D
parents: 22412
diff changeset
46 namebuf[MAX_PATH] = '\0';
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47
23082
e8d64dce0afd maint: strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 23003
diff changeset
48 std::string exe_name = namebuf;
23003
d591b38e3410 Use GetModuleFileName for getting octave path in windows (bug #48671)
John D
parents: 22412
diff changeset
49 size_t pos = exe_name.rfind ("\\");
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50
23003
d591b38e3410 Use GetModuleFileName for getting octave path in windows (bug #48671)
John D
parents: 22412
diff changeset
51 if (pos != std::string::npos)
d591b38e3410 Use GetModuleFileName for getting octave path in windows (bug #48671)
John D
parents: 22412
diff changeset
52 bin_dir = exe_name.substr (0, pos + 1);
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 }
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 if (! bin_dir.empty ())
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 {
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 size_t pos = bin_dir.rfind ("\\bin\\");
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 if (pos != std::string::npos)
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 retval = bin_dir.substr (0, pos);
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 }
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 return retval;
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 }
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 #endif
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 // Find the directory where the octave binary is supposed to be
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 // installed.
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 21979
diff changeset
71 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 static const char dir_sep_char = '\\';
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 #else
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 static const char dir_sep_char = '/';
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 #endif
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 static std::string
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 octave_getenv (const std::string& name)
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 {
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 char *value = ::getenv (name.c_str ());
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 return value ? value : "";
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 }
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 static std::string
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 get_octave_home (void)
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 {
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 std::string oh = octave_getenv ("OCTAVE_HOME");
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90
21979
d04da18a407a use OCTAVE_USE_WINDOWS_API more consistently
John W. Eaton <jwe@octave.org>
parents: 21960
diff changeset
91 #if defined (OCTAVE_USE_WINDOWS_API)
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 if (oh.empty ())
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 oh = w32_get_octave_home ();
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 #endif
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 return oh.empty () ? std::string (OCTAVE_PREFIX) : oh;
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 }
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 static std::string
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 subst_octave_home (const std::string& s)
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 {
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102 std::string retval;
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 std::string octave_home = get_octave_home ();
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 std::string prefix = OCTAVE_PREFIX;
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 retval = s;
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110 if (octave_home != prefix)
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 {
21960
63f8e75e3043 shared-fcns.h (susbt_octave_home): String length is size_t.
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
112 size_t len = prefix.length ();
18360
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
114 if (s.substr (0, len) == prefix)
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115 retval.replace (0, len, octave_home);
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116 }
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
117
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118 if (dir_sep_char != '/')
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119 std::replace (retval.begin (), retval.end (), '/', dir_sep_char);
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121 return retval;
f1ee47866e6b use common code to get OCTAVE_HOME in main, octave-config and mkoctfile
John W. Eaton <jwe@octave.org>
parents:
diff changeset
122 }
21213
f7d1050b9b53 maint: Clean up various usages of #ifdef.
Rik <rik@octave.org>
parents: 21202
diff changeset
123
f7d1050b9b53 maint: Clean up various usages of #ifdef.
Rik <rik@octave.org>
parents: 21202
diff changeset
124 #endif