Mercurial > hg > octave-lyh
annotate scripts/path/pathdef.m @ 10491:077fef5da460
optimize null assignment with bool masks
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 06 Apr 2010 15:38:56 +0200 |
parents | 9f25290a35e8 |
children | 95c3e38098bf |
rev | line source |
---|---|
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
1 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 Bill Denney |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
2 ## Copyright (C) 2007, 2008, 2009 Ben Abbott |
7388 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
17 ## along with Octave; see the file COPYING. If not, see | |
18 ## <http://www.gnu.org/licenses/>. | |
19 | |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {@var{val} =} pathdef () | |
22 ## Return the default path for Octave. | |
23 ## The path information is extracted from one of three sources. | |
24 ## In order of preference, those are; | |
25 ## | |
26 ## @enumerate | |
27 ## @item @file{~/.octaverc} | |
9038
fca0dc2fb042
Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents:
7393
diff
changeset
|
28 ## @item @file{<octave-home>/@dots{}/<version>/m/startup/octaverc} |
7392 | 29 ## @item Octave's path prior to changes by any octaverc. |
7388 | 30 ## @end enumerate |
31 ## @seealso{path, addpath, rmpath, genpath, savepath, pathsep} | |
32 ## @end deftypefn | |
33 | |
34 function val = pathdef () | |
35 | |
7392 | 36 ## Locate the site octaverc file. |
7391 | 37 pathdir = octave_config_info ("localstartupfiledir"); |
7392 | 38 site_octaverc = fullfile (pathdir, "octaverc"); |
7388 | 39 |
7393 | 40 ## Locate the user ~\.octaverc file. |
7392 | 41 user_octaverc = fullfile ("~", ".octaverc"); |
7388 | 42 |
7391 | 43 ## Extract the specified paths from the site and user octaverc"s. |
7393 | 44 site_path = __extractpath__ (site_octaverc); |
7392 | 45 if (exist (user_octaverc, "file")) |
7393 | 46 user_path = __extractpath__ (user_octaverc); |
7388 | 47 else |
7393 | 48 user_path = ""; |
7388 | 49 endif |
50 | |
7392 | 51 ## A path definition in the user octaverc has precedence over the |
52 ## site. | |
53 | |
7393 | 54 if (! isempty (user_path)) |
55 val = user_path; | |
56 elseif (! isempty (site_path)) | |
57 val = site_path; | |
58 else | |
59 val = __pathorig__ (); | |
7388 | 60 endif |
61 | |
62 endfunction | |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
63 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
64 ## Extact the path information from the script/function @var{file}, |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
65 ## created by @file{savepath.m}. If @var{file} is omitted, |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
66 ## @file{~/.octaverc} is used. If successful, @code{__extractpath__} |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
67 ## returns the path specified in @var{file}. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
68 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
69 ## Author: Ben Abbott <bpabbott@mac.com> |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
70 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
71 function specifiedpath = __extractpath__ (savefile) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
72 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
73 ## The majority of this code was borrowed from savepath.m. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
74 ## FIXME -- is there some way to share the common parts instead of |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
75 ## duplicating? |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
76 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
77 beginstring = "## Begin savepath auto-created section, do not edit"; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
78 endstring = "## End savepath auto-created section"; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
79 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
80 if (nargin == 0) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
81 savefile = tilde_expand ("~/.octaverc"); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
82 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
83 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
84 ## Parse the file if it exists to see if we should replace a section |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
85 ## or create a section. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
86 startline = 0; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
87 endline = 0; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
88 filelines = {}; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
89 if (exist (savefile) == 2) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
90 ## read in all lines of the file |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
91 [fid, msg] = fopen (savefile, "rt"); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
92 if (fid < 0) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
93 error ("__extractpath__: could not open savefile, %s: %s", savefile, msg); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
94 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
95 unwind_protect |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
96 linenum = 0; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
97 while (linenum >= 0) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
98 result = fgetl (fid); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
99 if (isnumeric (result)) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
100 ## End at the end of file. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
101 linenum = -1; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
102 else |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
103 linenum++; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
104 filelines{linenum} = result; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
105 ## Find the first and last lines if they exist in the file. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
106 if (strcmp (result, beginstring)) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
107 startline = linenum + 1; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
108 elseif (strcmp (result, endstring)) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
109 endline = linenum - 1; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
110 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
111 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
112 endwhile |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
113 unwind_protect_cleanup |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
114 closeread = fclose (fid); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
115 if (closeread < 0) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
116 error ("savepath: could not close savefile after reading, %s", |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
117 savefile); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
118 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
119 end_unwind_protect |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
120 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
121 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
122 ## Extract the path specifiation. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
123 if (startline > endline || (startline > 0 && endline == 0)) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
124 error ("savepath: unable to parse file, %s", savefile); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
125 elseif (startline > 0) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
126 ## Undo doubling of single quote characters performed by savepath. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
127 specifiedpath = strrep (regexprep (cstrcat (filelines(startline:endline){:}), |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
128 " *path *\\('(.*)'\\); *", "$1"), |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
129 "''", "'"); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
130 else |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
131 specifiedpath = ""; |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
132 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
133 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
134 endfunction |