testdir.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This file is part of NS2DDV. %
% %
% Copyright(C) 2011-2018 C. Calgaro (caterina.calgaro@math.univ-lille1.fr) %
% E. Creusé (emmanuel.creuse@math.univ-lille1.fr) %
% T. Goudon (thierry.goudon@inria.fr) %
% A. Mouton (alexandre.mouton@math.univ-lille1.fr) %
% %
% NS2DDV 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 3 of the License, or (at your option) any later version. %
% %
% NS2DDV 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 %
% NS2DDV. If not, see <http://www.gnu.org/licenses/>. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[] = testdir(expr)
% INPUT
% expr Path to be tested (existence of results that can induce conflicts)
q = strfind(expr, '_*');
k = dir(expr);
if (numel(q) == 0)
% We test the existence of a file
a = dir(expr);
if (numel(a) > 0)
% File already exist --> Stop the code
error(sprintf('File %s already exists. Please modify at least one of the following parameters in the setup file:\nPARAMETERS.OUTPUT.DIRECTORY_NAME\nPARAMETERS.OUTPUT.FILE_NAME\nor\nPARAMETERS.BACKUP.DIRECTORY_NAME\n', expr));
end
else
% We test the existence of some files with a generic name
qf = strsplit(expr(1:q(1)-1), '/');
qf = qf(numel(qf));
qf = qf(1:numel(qf));
a = dir(expr);
if (numel(a) == 0)
return;
end
for i=1:numel(a)
thefile = a(i).name;
% Cut the prefix part of the file name
extension = thefile(numel(qf{1})+2:numel(thefile));
message = sprintf('File %s already exists.', strcat(a(i).folder, '/', thefile));
message = strcat(message, sprintf('\nPlease modify at least one of the following parameters in the setup file:'));
message = strcat(message, sprintf('\nPARAMETERS.OUTPUT.DIRECTORY_NAME'));
message = strcat(message, sprintf('\nPARAMETERS.OUTPUT.FILE_NAME'));
message = strcat(message, sprintf('\nor'));
message = strcat(message, sprintf('\nPARAMETERS.BACKUP.DIRECTORY_NAME'));
message = strcat(message, sprintf('\nor clean the directory %s (you can use the routine clean_outputs)', a(i).folder));
if (numel(extension) > 3)
if strcmp(extension(numel(extension)-2:numel(extension)), '.h5')
% It is a .h5 file
strtotest = str2num(extension(1:numel(extension)-3));
if (numel(strtotest) == 1)
% A data file corresponds to the pattern --> Stop the code
error(message);
end
strtotest = str2num(extension(6:numel(extension)-3));
if (numel(strtotest) == 1)
% A mesh file corresponds to the pattern --> Stop the code
error(message);
end
end
end
if (numel(extension) > 4)
if strcmp(extension(numel(extension)-3:numel(extension)), '.mat')
% It is a .mat file
strtotest = extension(1:numel(extension)-4);
if (numel(strtotest) == 1)
% A data file corresponds to the pattern --> Stop the code
error(message);
end
strtotest = extension(6:numel(extension)-4);
if (numel(strtotest) == 1)
% A mesh file corresponds to the pattern --> Stop the code
error(message);
end
end
end
if (numel(extension) > 5)
if strcmp(extension(numel(extension)-3:numel(extension)), '.xdmf')
% It is a .xdmf file
strtotest = extension(1:numel(extension)-5);
if (numel(strtotest) == 1)
% A data file corresponds to the pattern --> Stop the code
error(message);
end
end
if strcmp(extension(numel(extension)-2:numel(extension)), 'log')
% It is a log file
error(message);
end
if strcmp(extension, 'parameters.mat')
% It is a parameters binary file
error(message);
end
end
end
end