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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} savepath (@var{file}) |
|
22 ## This function saves the current @code{LOADPATH} to your personal |
|
23 ## default initilization file or optionally the @var{file} that you |
|
24 ## specify. |
|
25 ## |
|
26 ## It will return 0 if it was successful. |
|
27 ## |
5733
|
28 ## @seealso{LOADPATH, addpath, rmpath} |
5732
|
29 ## @end deftypefn |
|
30 |
|
31 ## Author: Bill Denney <bill@givebillmoney.com> |
|
32 |
|
33 function varargout = savepath (savefile) |
|
34 |
|
35 retval = 1; |
|
36 |
|
37 beginstring = "## Begin savepath auto-created section, do not edit"; |
|
38 endstring = "## End savepath auto-created section"; |
|
39 |
5733
|
40 if (nargin == 0) |
5735
|
41 savefile = tilde_expand ("~/.octaverc"); |
5732
|
42 endif |
|
43 |
5733
|
44 ## parse the file if it exists to see if we should replace a section |
|
45 ## or create a section |
5732
|
46 startline = 0; |
|
47 endline = 0; |
|
48 filelines = {}; |
|
49 if (exist (savefile) == 2) |
5733
|
50 ## read in all lines of the file |
5732
|
51 [fid, msg] = fopen (savefile, "rt"); |
|
52 if (fid < 0) |
|
53 error ("savepath: could not open savefile, %s: %s", savefile, msg); |
|
54 endif |
|
55 linenum = 0; |
|
56 while (linenum >= 0) |
|
57 result = fgetl (fid); |
|
58 if (isnumeric (result)) |
5733
|
59 ## end at the end of file |
5732
|
60 linenum = -1; |
|
61 else |
|
62 linenum = linenum + 1; |
|
63 filelines{linenum} = result; |
5733
|
64 ## find the first and last lines if they exist in the file |
5732
|
65 if (strcmp (result, beginstring)) |
|
66 startline = linenum; |
|
67 elseif (strcmp (result, endstring)) |
|
68 endline = linenum; |
|
69 endif |
|
70 endif |
|
71 endwhile |
|
72 closeread = fclose (fid); |
|
73 if (closeread < 0) |
|
74 error ("savepath: could not close savefile after reading, %s", savefile); |
|
75 endif |
|
76 endif |
|
77 |
5733
|
78 if (startline > endline || (startline > 0 && endline == 0)) |
5735
|
79 error ("savepath: unable to parse file, %s", savefile); |
5732
|
80 endif |
|
81 |
5733
|
82 ## put the path into a cell array |
5732
|
83 pathlines = { beginstring, [" LOADPATH=\"", LOADPATH, "\";"], endstring }; |
|
84 |
5733
|
85 ## put the current savepath lines into the file |
|
86 if (isempty (filelines) |
|
87 || (startline == 1 && endline == length (filelines))) |
|
88 ## savepath is the entire file |
5732
|
89 pre = post = {}; |
|
90 elseif (endline == 0) |
5733
|
91 ## drop the savepath statements at the end of the file |
5732
|
92 pre = filelines; |
|
93 post = {}; |
|
94 elseif (startline == 1) |
|
95 pre = {}; |
|
96 post = filelines(endline+1:end); |
|
97 elseif (endline == length(filelines)) |
|
98 pre = filelines(1:startline-1); |
|
99 post = {}; |
|
100 else |
5733
|
101 ## insert in the middle |
5732
|
102 pre = filelines(1:startline-1); |
|
103 post = filelines(endline+1:end); |
|
104 endif |
|
105 |
5733
|
106 ## write the results |
5732
|
107 [fid, msg] = fopen (savefile, "wt"); |
|
108 if (fid < 0) |
|
109 error ("savepath: unable to open file for writing, %s, %s", savefile, msg); |
|
110 end |
|
111 for i = 1:length (pre) |
|
112 fprintf (fid, "%s\n", pre{i}) |
|
113 endfor |
|
114 for i = 1:length (pathlines) |
|
115 fprintf (fid, "%s\n", pathlines{i}); |
|
116 endfor |
|
117 for i = 1:length (post) |
|
118 fprintf (fid, "%s\n", post{i}); |
|
119 endfor |
|
120 closeread = fclose (fid); |
|
121 if (closeread < 0) |
|
122 error ("savepath: could not close savefile after writing, %s", savefile); |
|
123 endif |
|
124 |
|
125 retval = 0; |
|
126 |
|
127 if (nargout == 1) |
|
128 varargout{1} = retval; |
|
129 endif |
|
130 |
|
131 endfunction |