isabsolute.m

来自「toolbox of BVQX, This is the access betw」· M 代码 · 共 104 行

M
104
字号
function [isabs, abspath] = isabsolute(tpath)
% isabsolute  - returns true if the given path is absolute
%
% FORMAT:       isabs = isabsolute(testpath)
%
% Input fields:
%
%       testpath    string to test for absolute path content
%
% Output fields:
%
%       isabs       true for absolute paths, false for relative
%
% See also EXIST

% Version:  v0.5c
% Build:    6120415
% Date:     Dec-04 2006, 3:15 PM CET
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
   ~ischar(tpath) || ...
    isempty(tpath)
    error( ...
        'BVQXtools:BadArgument', ...
        'One non-empty 1xN char path argument is required.' ...
    );
end

% make valid char argument
tpath = tpath(:)';

% get machine ostype
try
    machine = ostype('machine');
    machine = machine.machine;
catch
    if ispc
        machine = 'WIN';
    else
        machine = 'UNIX';
    end
end

% default is false
isabs = false;
abspath = tpath;

% machine dependent code
switch (machine)

    % for PCs/Windows hosts
    case {'WIN'}

        % for now, replace \'s with /'s
        tpath = strrep(tpath, '\', '/');
        % path can only be absolute for either of
        % - X:\<...>
        % - \\HOST\PATH_TO_UNC
        if (length(tpath) > 2 && ...
            tpath(1) > 64 && ...
            tpath(1) < 123 && ...
            (tpath(1) < 91 || tpath(1) > 96) && ...
            strcmp(tpath(2:3), ':/') ...
           ) || ( ...
            length(tpath) > 3 && ...
            strcmp(tpath(1:2), '//'))
            isabs = true;
        elseif tpath(1) == '/'
            pwdpath = cd; % see dbtype pwd
            abspath = [pwdpath(1) ':' tpath];
        else
            abspath = strrep([pwd filesep tpath], '//', '/');
        end

    % for Linux/Unix'ish systems
    case {'UNIX'}

        % path can only be "absolute" for either of
        % - /...
        % - ~/...
        if tpath(1) == '/' || ...
            (length(tpath) > 1 && strcmp(tpath(1:2), '~/'))
            isabs = true;
        else
            abspath = strrep([pwd filesep tpath], '//', '/');
        end

    % for Macintosh
    case {'MAC'}

        % path could be absolute for either of
        % - /...
        % - ~/...
        if tpath(1) == '/' || ...
            (length(tpath) > 1 && strcmp(tpath(1:2), '~/'))
            isabs = true;
        else
            abspath = strrep([pwd filesep tpath], '//', '/');
        end
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?