Mercurial > hg > octave-nkf
annotate scripts/path/__extractpath__.m @ 12007:dc56a38b5a64 release-3-2-x
var.m: fix typos (thinkos?) in previous change
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 23 Jun 2009 12:57:57 +0200 |
parents | eb63fbe60fab |
children |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 Bill Denney |
7388 | 2 ## Copyright (C) 2007 Ben Abbott |
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} =} __extractpath__ (@var{file}) | |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7540
diff
changeset
|
22 ## Undocumented internal function. |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7540
diff
changeset
|
23 ## @end deftypefn |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7540
diff
changeset
|
24 |
7388 | 25 ## Extact the path information from the script/function @var{file}, |
26 ## created by @file{savepath.m}. If @var{file} is omitted, | |
27 ## @file{~/.octaverc} is used. If successful, @code{__extractpath__} | |
28 ## returns the path specified in @var{file}. | |
29 | |
30 ## Author: Ben Abbott <bpabbott@mac.com> | |
31 | |
32 function specifiedpath = __extractpath__ (savefile) | |
33 | |
7392 | 34 ## The majority of this code was borrowed from savepath.m. |
35 ## FIXME -- is there some way to share the common parts instead of | |
36 ## duplicating? | |
7388 | 37 |
38 beginstring = "## Begin savepath auto-created section, do not edit"; | |
39 endstring = "## End savepath auto-created section"; | |
40 | |
41 if (nargin == 0) | |
42 savefile = tilde_expand ("~/.octaverc"); | |
43 endif | |
44 | |
7392 | 45 ## Parse the file if it exists to see if we should replace a section |
46 ## or create a section. | |
7388 | 47 startline = 0; |
48 endline = 0; | |
49 filelines = {}; | |
50 if (exist (savefile) == 2) | |
51 ## read in all lines of the file | |
52 [fid, msg] = fopen (savefile, "rt"); | |
53 if (fid < 0) | |
54 error ("__extractpath__: could not open savefile, %s: %s", savefile, msg); | |
55 endif | |
7392 | 56 unwind_protect |
57 linenum = 0; | |
58 while (linenum >= 0) | |
59 result = fgetl (fid); | |
60 if (isnumeric (result)) | |
61 ## End at the end of file. | |
62 linenum = -1; | |
63 else | |
64 linenum++; | |
65 filelines{linenum} = result; | |
66 ## Find the first and last lines if they exist in the file. | |
67 if (strcmp (result, beginstring)) | |
68 startline = linenum + 1; | |
69 elseif (strcmp (result, endstring)) | |
70 endline = linenum - 1; | |
71 endif | |
72 endif | |
73 endwhile | |
74 unwind_protect_cleanup | |
75 closeread = fclose (fid); | |
76 if (closeread < 0) | |
77 error ("savepath: could not close savefile after reading, %s", | |
78 savefile); | |
7388 | 79 endif |
7392 | 80 end_unwind_protect |
7388 | 81 endif |
82 | |
7392 | 83 ## Extract the path specifiation. |
7388 | 84 if (startline > endline || (startline > 0 && endline == 0)) |
85 error ("savepath: unable to parse file, %s", savefile); | |
7393 | 86 elseif (startline > 0) |
87 ## Undo doubling of single quote characters performed by savepath. | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
88 specifiedpath = strrep (regexprep (cstrcat (filelines(startline:endline){:}), |
7393 | 89 " *path *\\('(.*)'\\); *", "$1"), |
90 "''", "'"); | |
7388 | 91 else |
7393 | 92 specifiedpath = ""; |
7388 | 93 endif |
94 | |
95 endfunction |