## Copyright (C) 2005 Bill Denney ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## -*- texinfo -*- ## @deftypefn {Function File} {} savepath (@var{file}) ## This function saves the current @code{LOADPATH} to your personal ## default initilization file or optionally the @var{file} that you ## specify. ## ## It will return 0 if it was successful. ## ## @seealso{LOADPATH,addpath,rmpath} ## @end deftypefn ## Author: Bill Denney function varargout = savepath(varargin) retval = 1; beginstring = {"## Begin savepath auto-created section, do not edit", ... "try"}; endstring = {"catch", ... " warning(\"All saved paths were not loaded\");", ... "end", ... "## End savepath auto-created section"}; if (isempty(varargin)) ## FIXME: I don't know how to find the default startup file for a ## user. savefile = "/home/bill/.octaverc"; elseif (iscellstr(varargin)) && (size(varargin{1},1) == 1) %% Only do this if it's a single row string savefile = varargin{1}; else error("savepath: Invalid filename") end %% parse the file if it exists to see if we should replace a section %% or create a section startline = 0; endline = 0; filelines = {}; if (exist(savefile) == 2) %% read in all lines of the file [fid, msg] = fopen(savefile, "rt"); if (fid < 0) error(["savepath: could not open savefile, " savefile ": " msg]); end linenum = 0; while (linenum >= 0) result = fgetl(fid); if isnumeric(result) %% end at the end of file linenum = -1; else linenum = linenum + 1; filelines{linenum} = result; %% find the first and last lines if they exist in the file if (strcmp(result, beginstring{1})) startline = linenum; elseif (strcmp(result, endstring{end})) endline = linenum; end end end closeread = fclose(fid); if (closeread < 0) error(["savepath: could not close savefile after reading, " savefile]); end end if (startline > endline) || ((startline > 0) && (endline == 0)) error(["savepath: unable to parse file, " savefile ". There was " ... "probably a start line without an end line or end without start."]) end %% put the path into a cell array pathleft = LOADPATH; pathlines = {}; while (~ isempty(pathleft)) [pathpart, pathleft] = strtok(pathleft, ":"); if (~ isempty(pathpart)) pathlines{length(pathlines)+1} = sprintf(" addpath(\"%s\");", pathpart); end end %% Reverse the path lines so that they appear in the correct order in %% the restored path after loading. pathlines = [beginstring pathlines(length(pathlines):-1:1) endstring]; %% put the current savepath lines into the file if (isempty(filelines)) || ... ((startline == 1) && (endline == length(filelines))) %% savepath is the entire file filelines = pathlines; elseif (endline == 0) %% drop the savepath statements at the end of the file filelines = [filelines pathlines]; elseif (startline == 1) filelines = [pathlines filelines(endline+1:length(filelines))]; elseif (endline == length(filelines)) filelines = [filelines(1:startline-1) pathlines]; else %% insert in the middle filelines = [filelines(1:startline-1) pathlines filelines(endline+1:length(filelines))]; end %% write the results [fid, msg] = fopen(savefile, "wt"); if (fid < 0) error(["savepath: unable to open file for writing, " savefile ", " msg]); end for i = 1:length(filelines) fprintf(fid, "%s\n", filelines{i}); end closeread = fclose(fid); if (closeread < 0) error(["savepath: could not close savefile after writing, " savefile]); end retval = 0; if (nargout == 1) varargout{1} = retval; end