Home > atmlab > files > create_tmpfolder.m

create_tmpfolder

PURPOSE ^

CREATE_TMPFOLDER Creation of a temporary folder in the work area.

SYNOPSIS ^

function tmpfolder = create_tmpfolder

DESCRIPTION ^

 CREATE_TMPFOLDER   Creation of a temporary folder in the work area.

    The function creates a folder in the folder set as work area in
    *atmlab*. See CONFIGURE for information about *atmlab*. A work area 
    is a required setting.

    The folder name is generated by matlab's tempname function and a
    -atmlab is prepended to it.

    

 FORMAT   tmpfolder = create_tmpfolder

 OUT      tmpfolder   Full path to created folder.
 ATMLAB   WORK_AREA   The work area is required as a personal setting.

 See also: onCleanup (e.g. c = onCleanup(@()delete(tmpfolder))

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

create_tmpfolder.m

SOURCE CODE ^

0001 % CREATE_TMPFOLDER   Creation of a temporary folder in the work area.
0002 %
0003 %    The function creates a folder in the folder set as work area in
0004 %    *atmlab*. See CONFIGURE for information about *atmlab*. A work area
0005 %    is a required setting.
0006 %
0007 %    The folder name is generated by matlab's tempname function and a
0008 %    -atmlab is prepended to it.
0009 %
0010 %
0011 %
0012 % FORMAT   tmpfolder = create_tmpfolder
0013 %
0014 % OUT      tmpfolder   Full path to created folder.
0015 % ATMLAB   WORK_AREA   The work area is required as a personal setting.
0016 %
0017 % See also: onCleanup (e.g. c = onCleanup(@()delete(tmpfolder))
0018 
0019 % 2011-04-04  Updated by Oliver Lemke. Use matlab's tempname to generate
0020 %             folder name. Append -atmlab to tmpfolder name.
0021 % 2002-12-20  Created by Patrick Eriksson, based on older version
0022 %             in AMI (part of arts-1).
0023 
0024 
0025 function tmpfolder = create_tmpfolder
0026 
0027 %=== Require that a work area is set as a personal setting
0028 %
0029 atmlab( 'require', {'WORK_AREA'} );
0030 workarea = atmlab( 'WORK_AREA' );
0031 %
0032 if ~ischar(workarea)
0033     error( 'atmlab:create_tmpfolder', 'WORKAREA must be a string' );
0034 elseif length(workarea) == 1 && strcmp(workarea,'.')
0035     workarea = pwd();
0036 elseif length(workarea) > 1 && strcmp(workarea(1:2),'./')
0037     workarea = atmlab('WORK_AREA',[pwd(),workarea(2:end)]);
0038 elseif length(workarea) > 1 && strcmp(workarea(1:2),'~/')
0039     workarea = atmlab('WORK_AREA',[getenv('HOME'),workarea(2:end)]);
0040 elseif ~strcmp(workarea(1), '/')
0041     error( 'atmlab:create_tmpfolder', 'WORKAREA must be an absolute path' );
0042 end
0043 
0044 if ~isdir(workarea)
0045     error( 'atmlab:create_tmpfolder', ...
0046            'The given work area (%s) does not exist.', workarea );
0047 end
0048 
0049 %=== Loop until a new folder with unique name has been created
0050 %
0051 ready = 0;
0052 count = 100;
0053 %
0054 username = getenv('USER');
0055 if (~isempty(username)), username = [username '-']; end
0056 tmpfolderparts = regexp(tempname(workarea), ...
0057     ['(?<dirname>.*' filesep ')(?<basename>[^' filesep ']*)$'], ...
0058     'names');
0059 tmpfolder = [tmpfolderparts.dirname 'atmlab-' username tmpfolderparts.basename];
0060 
0061 % Has happened that whitespace has entered, at least from username
0062 tmpfolder = tmpfolder( ~isspace(tmpfolder) );
0063 
0064 while count && ~ready
0065   if ~exist( tmpfolder, 'dir' )
0066     [a,b] = mkdir(tmpfolder);      %#ok<ASGLU>
0067     if isempty(b)           % Check again if folder exist, for extra safety
0068       ready = 1;
0069     end
0070   end
0071   count = count - 1;
0072 end
0073 
0074 if ~ready
0075     error( 'atmlab:tmpfolder', ...
0076            'Unable to create temporary folder in %s', workarea );
0077 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005