]> Creatis software - CreaPhase.git/blob - octave_packages/m/path/pathdef.m
update packages
[CreaPhase.git] / octave_packages / m / path / pathdef.m
1 ## Copyright (C) 2005-2012 Bill Denney
2 ## Copyright (C) 2007-2009 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} =} 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}
28 ##
29 ## @item @file{<octave-home>/@dots{}/<version>/m/startup/octaverc}
30 ##
31 ## @item Octave's path prior to changes by any octaverc.
32 ## @end enumerate
33 ## @seealso{path, addpath, rmpath, genpath, savepath, pathsep}
34 ## @end deftypefn
35
36 function val = pathdef ()
37
38   ## Locate the site octaverc file.
39   pathdir = octave_config_info ("localstartupfiledir");
40   site_octaverc = fullfile (pathdir, "octaverc");
41
42   ## Locate the user ~\.octaverc file.
43   user_octaverc = fullfile ("~", ".octaverc");
44
45   ## Extract the specified paths from the site and user octaverc"s.
46   site_path = __extractpath__ (site_octaverc);
47   if (exist (user_octaverc, "file"))
48     user_path = __extractpath__ (user_octaverc);
49   else
50     user_path = "";
51   endif
52
53   ## A path definition in the user octaverc has precedence over the
54   ## site.
55
56   if (! isempty (user_path))
57     val = user_path;
58   elseif (! isempty (site_path))
59     val = site_path;
60   else
61     val = __pathorig__ ();
62   endif
63
64 endfunction
65
66 ## Extact the path information from the script/function @var{file},
67 ## created by @file{savepath.m}.  If @var{file} is omitted,
68 ## @file{~/.octaverc} is used.  If successful, @code{__extractpath__}
69 ## returns the path specified in @var{file}.
70
71 ## Author: Ben Abbott <bpabbott@mac.com>
72
73 function specifiedpath = __extractpath__ (savefile)
74
75   ## The majority of this code was borrowed from savepath.m.
76   ## FIXME -- is there some way to share the common parts instead of
77   ## duplicating?
78
79   beginstring = "## Begin savepath auto-created section, do not edit";
80   endstring   = "## End savepath auto-created section";
81
82   if (nargin == 0)
83     savefile = tilde_expand ("~/.octaverc");
84   endif
85
86   ## Parse the file if it exists to see if we should replace a section
87   ## or create a section.
88   startline = 0;
89   endline = 0;
90   filelines = {};
91   if (exist (savefile) == 2)
92     ## read in all lines of the file
93     [fid, msg] = fopen (savefile, "rt");
94     if (fid < 0)
95       error ("__extractpath__: could not open savefile, %s: %s", savefile, msg);
96     endif
97     unwind_protect
98       linenum = 0;
99       while (linenum >= 0)
100         result = fgetl (fid);
101         if (isnumeric (result))
102           ## End at the end of file.
103           linenum = -1;
104         else
105           linenum++;
106           filelines{linenum} = result;
107           ## Find the first and last lines if they exist in the file.
108           if (strcmp (result, beginstring))
109             startline = linenum + 1;
110           elseif (strcmp (result, endstring))
111             endline = linenum - 1;
112           endif
113         endif
114       endwhile
115     unwind_protect_cleanup
116       closeread = fclose (fid);
117       if (closeread < 0)
118         error ("__extractpath__: could not close savefile after reading, %s",
119                savefile);
120       endif
121     end_unwind_protect
122   endif
123
124   ## Extract the path specifiation.
125   if (startline > endline || (startline > 0 && endline == 0))
126     error ("__extractpath__: unable to parse file, %s", savefile);
127   elseif (startline > 0)
128     ## Undo doubling of single quote characters performed by savepath.
129     specifiedpath = strrep (regexprep (cstrcat (filelines(startline:endline){:}),
130                                        " *path *\\('(.*)'\\); *", "$1"),
131                             "''", "'");
132   else
133     specifiedpath = "";
134   endif
135
136 endfunction