7017
|
1 ## Copyright (C) 2005, 2006, 2007 Bill Denney |
5732
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5732
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5732
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} savepath (@var{file}) |
5804
|
21 ## Save the current function search path to @var{file}. If @var{file} |
|
22 ## is omitted, @file{~/.octaverc} is used. If successful, |
|
23 ## @code{savepath} returns 0. |
5814
|
24 ## @seealso{path, addpath, rmpath, genpath, pathdef, pathsep} |
5732
|
25 ## @end deftypefn |
|
26 |
|
27 ## Author: Bill Denney <bill@givebillmoney.com> |
|
28 |
5736
|
29 ##PKGADD: mark_as_command savepath |
|
30 |
5732
|
31 function varargout = savepath (savefile) |
|
32 |
|
33 retval = 1; |
|
34 |
|
35 beginstring = "## Begin savepath auto-created section, do not edit"; |
|
36 endstring = "## End savepath auto-created section"; |
|
37 |
5733
|
38 if (nargin == 0) |
5735
|
39 savefile = tilde_expand ("~/.octaverc"); |
5732
|
40 endif |
|
41 |
5733
|
42 ## parse the file if it exists to see if we should replace a section |
|
43 ## or create a section |
5732
|
44 startline = 0; |
|
45 endline = 0; |
|
46 filelines = {}; |
|
47 if (exist (savefile) == 2) |
5733
|
48 ## read in all lines of the file |
5732
|
49 [fid, msg] = fopen (savefile, "rt"); |
|
50 if (fid < 0) |
|
51 error ("savepath: could not open savefile, %s: %s", savefile, msg); |
|
52 endif |
|
53 linenum = 0; |
|
54 while (linenum >= 0) |
|
55 result = fgetl (fid); |
|
56 if (isnumeric (result)) |
5733
|
57 ## end at the end of file |
5732
|
58 linenum = -1; |
|
59 else |
|
60 linenum = linenum + 1; |
|
61 filelines{linenum} = result; |
5733
|
62 ## find the first and last lines if they exist in the file |
5732
|
63 if (strcmp (result, beginstring)) |
|
64 startline = linenum; |
|
65 elseif (strcmp (result, endstring)) |
|
66 endline = linenum; |
|
67 endif |
|
68 endif |
|
69 endwhile |
|
70 closeread = fclose (fid); |
|
71 if (closeread < 0) |
|
72 error ("savepath: could not close savefile after reading, %s", savefile); |
|
73 endif |
|
74 endif |
|
75 |
5733
|
76 if (startline > endline || (startline > 0 && endline == 0)) |
5735
|
77 error ("savepath: unable to parse file, %s", savefile); |
5732
|
78 endif |
|
79 |
5733
|
80 ## put the current savepath lines into the file |
|
81 if (isempty (filelines) |
|
82 || (startline == 1 && endline == length (filelines))) |
|
83 ## savepath is the entire file |
5732
|
84 pre = post = {}; |
|
85 elseif (endline == 0) |
5733
|
86 ## drop the savepath statements at the end of the file |
5732
|
87 pre = filelines; |
|
88 post = {}; |
|
89 elseif (startline == 1) |
|
90 pre = {}; |
|
91 post = filelines(endline+1:end); |
|
92 elseif (endline == length(filelines)) |
|
93 pre = filelines(1:startline-1); |
|
94 post = {}; |
|
95 else |
5733
|
96 ## insert in the middle |
5732
|
97 pre = filelines(1:startline-1); |
|
98 post = filelines(endline+1:end); |
|
99 endif |
|
100 |
5733
|
101 ## write the results |
5732
|
102 [fid, msg] = fopen (savefile, "wt"); |
|
103 if (fid < 0) |
|
104 error ("savepath: unable to open file for writing, %s, %s", savefile, msg); |
|
105 end |
|
106 for i = 1:length (pre) |
|
107 fprintf (fid, "%s\n", pre{i}) |
|
108 endfor |
5736
|
109 |
6807
|
110 ## Use single quotes for PATH argument to avoid string escape |
|
111 ## processing. |
|
112 fprintf (fid, "%s\n path ('%s');\n%s\n", |
5804
|
113 beginstring, path (), endstring); |
5736
|
114 |
5732
|
115 for i = 1:length (post) |
|
116 fprintf (fid, "%s\n", post{i}); |
|
117 endfor |
|
118 closeread = fclose (fid); |
|
119 if (closeread < 0) |
|
120 error ("savepath: could not close savefile after writing, %s", savefile); |
|
121 endif |
|
122 |
|
123 retval = 0; |
|
124 |
|
125 if (nargout == 1) |
|
126 varargout{1} = retval; |
|
127 endif |
|
128 |
|
129 endfunction |