[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: pushd and popd for octave?
From: |
fork |
Subject: |
Re: pushd and popd for octave? |
Date: |
Fri, 30 Jul 2010 22:41:29 +0000 (UTC) |
User-agent: |
Loom/3.14 (http://gmane.org/) |
Jordi Gutiérrez Hermoso <jordigh <at> gmail.com> writes:
> Why not make both of them be wrappers to a third function that has the
> persistent variable?
Voila:
function currd = dirTraverse(op, fdir)
%% currd = dirTraverse(op, fdir)
%%
%% op's are 'pushd', 'popd', 'clears', 'show'
%% DIRSTACK will hold the directories we traverse
persistent DIRSTACK;
%% make sure we have a DIRSTACK
if ~exist(DIRSTACK)
DIRSTACK = {};
endif
%% do stuff depending on op
switch lower(op)
case 'pushd'
if nargin ~= 2
print_usage();
endif
currd = pwd();
cd(fdir); % can error out before pushing
DIRSTACK(end+1) = currd;
case 'popd'
if isequal(DIRSTACK, {})
error("dirTraverse: popd called with empty DIRSTACK\n")
endif
currd = pwd();
cd(DIRSTACK(end){:});
DIRSTACK(end) = [];
case 'clears'
DIRSTACK = {};
case 'show'
disp(DIRSTACK)
otherwise
warning("dirTraverse: bad op: %s", op)
print_usage();
endswitch
endfunction