5732
|
1 ## Copyright (C) 2005 Bill Denney |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
6440
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
5732
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} savepath (@var{file}) |
5804
|
22 ## Save the current function search path to @var{file}. If @var{file} |
|
23 ## is omitted, @file{~/.octaverc} is used. If successful, |
|
24 ## @code{savepath} returns 0. |
5814
|
25 ## @seealso{path, addpath, rmpath, genpath, pathdef, pathsep} |
5732
|
26 ## @end deftypefn |
|
27 |
|
28 ## Author: Bill Denney <bill@givebillmoney.com> |
|
29 |
5736
|
30 ##PKGADD: mark_as_command savepath |
|
31 |
5732
|
32 function varargout = savepath (savefile) |
|
33 |
|
34 retval = 1; |
|
35 |
|
36 beginstring = "## Begin savepath auto-created section, do not edit"; |
|
37 endstring = "## End savepath auto-created section"; |
|
38 |
5733
|
39 if (nargin == 0) |
5735
|
40 savefile = tilde_expand ("~/.octaverc"); |
5732
|
41 endif |
|
42 |
5733
|
43 ## parse the file if it exists to see if we should replace a section |
|
44 ## or create a section |
5732
|
45 startline = 0; |
|
46 endline = 0; |
|
47 filelines = {}; |
|
48 if (exist (savefile) == 2) |
5733
|
49 ## read in all lines of the file |
5732
|
50 [fid, msg] = fopen (savefile, "rt"); |
|
51 if (fid < 0) |
|
52 error ("savepath: could not open savefile, %s: %s", savefile, msg); |
|
53 endif |
|
54 linenum = 0; |
|
55 while (linenum >= 0) |
|
56 result = fgetl (fid); |
|
57 if (isnumeric (result)) |
5733
|
58 ## end at the end of file |
5732
|
59 linenum = -1; |
|
60 else |
|
61 linenum = linenum + 1; |
|
62 filelines{linenum} = result; |
5733
|
63 ## find the first and last lines if they exist in the file |
5732
|
64 if (strcmp (result, beginstring)) |
|
65 startline = linenum; |
|
66 elseif (strcmp (result, endstring)) |
|
67 endline = linenum; |
|
68 endif |
|
69 endif |
|
70 endwhile |
|
71 closeread = fclose (fid); |
|
72 if (closeread < 0) |
|
73 error ("savepath: could not close savefile after reading, %s", savefile); |
|
74 endif |
|
75 endif |
|
76 |
5733
|
77 if (startline > endline || (startline > 0 && endline == 0)) |
5735
|
78 error ("savepath: unable to parse file, %s", savefile); |
5732
|
79 endif |
|
80 |
5733
|
81 ## put the current savepath lines into the file |
|
82 if (isempty (filelines) |
|
83 || (startline == 1 && endline == length (filelines))) |
|
84 ## savepath is the entire file |
5732
|
85 pre = post = {}; |
|
86 elseif (endline == 0) |
5733
|
87 ## drop the savepath statements at the end of the file |
5732
|
88 pre = filelines; |
|
89 post = {}; |
|
90 elseif (startline == 1) |
|
91 pre = {}; |
|
92 post = filelines(endline+1:end); |
|
93 elseif (endline == length(filelines)) |
|
94 pre = filelines(1:startline-1); |
|
95 post = {}; |
|
96 else |
5733
|
97 ## insert in the middle |
5732
|
98 pre = filelines(1:startline-1); |
|
99 post = filelines(endline+1:end); |
|
100 endif |
|
101 |
5733
|
102 ## write the results |
5732
|
103 [fid, msg] = fopen (savefile, "wt"); |
|
104 if (fid < 0) |
|
105 error ("savepath: unable to open file for writing, %s, %s", savefile, msg); |
|
106 end |
|
107 for i = 1:length (pre) |
|
108 fprintf (fid, "%s\n", pre{i}) |
|
109 endfor |
5736
|
110 |
6807
|
111 ## Use single quotes for PATH argument to avoid string escape |
|
112 ## processing. |
|
113 fprintf (fid, "%s\n path ('%s');\n%s\n", |
5804
|
114 beginstring, path (), endstring); |
5736
|
115 |
5732
|
116 for i = 1:length (post) |
|
117 fprintf (fid, "%s\n", post{i}); |
|
118 endfor |
|
119 closeread = fclose (fid); |
|
120 if (closeread < 0) |
|
121 error ("savepath: could not close savefile after writing, %s", savefile); |
|
122 endif |
|
123 |
|
124 retval = 0; |
|
125 |
|
126 if (nargout == 1) |
|
127 varargout{1} = retval; |
|
128 endif |
|
129 |
|
130 endfunction |