⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pvalidatestructure.m

📁 利用Stateflow 进行嵌入式代码开发很好用
💻 M
字号:
function oStruct = pValidateStructure(iStruct)
%PVALIDATESTRUCTURE Validate the input strucuture and return it with 
%   missing fields added if needed.
%

%   Copyright 2005 The MathWorks, Inc.
%   $File: $
%   $Revision: $
%   $Date:  $

%Initialize some variables
initStruct = legacy_code_initialize;
nameFields = fieldnames(initStruct);

% Initialize ouput structure
oStruct = iStruct;

% validate fieldname
sizesFields=fieldnames(oStruct);
for ii=1:length(sizesFields),
    idx = strmatch(sizesFields{ii}, nameFields, 'exact');
    if isempty(idx)
        error('LegacyCode:badFieldName','Invalid field name ''%s''.', sizesFields{ii});
    end
end

% Add non existing field
idx = find(~ismember(nameFields, sizesFields));
for ii=1:length(idx)
    oStruct.(nameFields{idx(ii)}) = initStruct.(nameFields{idx(ii)});
end

% Validate field class type
for ii = 1:length(nameFields)
    refClass = class(initStruct.(nameFields{ii}));
    tstClass = class(oStruct.(nameFields{ii}));
    if ~strcmp(tstClass, refClass)
        error('LegacyCode:badFieldName',...
            'The field ''%s'' is of class ''%s'' and must be of class ''%s''.',...
            nameFields{ii}, tstClass, refClass);
    end
end

% Validate SFunctionName field
if isempty(oStruct.SFunctionName)
    error('LegacyCode:badSFunctionName',...
        'SFunctionName field must be a non-empty string.');
end

% Remove leading and trailing space
[p, SFunctionName, e] = fileparts(oStruct.SFunctionName);
SFunctionName = regexprep(SFunctionName, '^\s*|\s*$','');

% Validate SFunction Name
if ~isempty(regexp(SFunctionName,'\W', 'once'))
    error('LegacyCode:invalidCharacter',...
        'SFunctionName contains non-valid characters ''%s''.', SFunctionName);
end
oStruct.SFunctionName = SFunctionName;

% Validate output function Spec field
if ~isempty(oStruct.OutputFcnSpec)
    if ~ischar(oStruct.OutputFcnSpec)
        error('LegacyCode:badFcnSpec',...
            'OutputFcnSpec field must be a non-empty string.');
    end
end

% Validate start function Spec field
if ~isempty(oStruct.StartFcnSpec)
    if ~ischar(oStruct.StartFcnSpec)
        error('LegacyCode:badStartFcnSpec',...
            'StartFcnSpec field must be a non-empty string.');
    end
end

% Validate terminate function Spec field
if ~isempty(oStruct.TerminateFcnSpec)
    if ~ischar(oStruct.TerminateFcnSpec)
        error('LegacyCode:badTerminateFcnSpec',...
            'TerminateFcnSpec field must be a non-empty string.');
    end
end

% Validate header + add ".h"
if ~isempty(oStruct.HeaderFiles)
    if ~iscellstr(oStruct.HeaderFiles)
        error('LegacyCode:badHeaderFiles',...
            'HeaderFiles field must be a cell of string.');
    end
    % force to be a column vector
    oStruct.HeaderFiles = oStruct.HeaderFiles(:);

    % Remove empty string
    idx = find(cellfun('isempty',oStruct.HeaderFiles));
    oStruct.HeaderFiles(idx) = [];
    
    % Verify file name and extension
    for ii = 1:length(oStruct.HeaderFiles)
        [p, HeaderFiles, e] = fileparts(oStruct.HeaderFiles{ii});
        if ~isempty(p)
            error('LegacyCode:badHeaderFiles',...
                ['HeaderFiles field accepts only file name. ',...
                'You must add the path ''%s'' in the IncPaths field.'], p);
        end
        if ~isempty(HeaderFiles)
            if isempty(e)
                e = '.h';
            end
            oStruct.HeaderFiles{ii} = [HeaderFiles, e];
        else
            error('LegacyCode:badHeaderFiles',...
                'Bad header file ''%s''.', oStruct.HeaderFiles{ii});
        end
    end
end

% Validate Include Path
if ~isempty(oStruct.IncPaths)
    if ~iscellstr(oStruct.IncPaths)
        error('LegacyCode:badIncludePaths',...
            'IncPaths field must be a cell of string.');
    end
    % force to be a column vector
    oStruct.IncPaths = oStruct.IncPaths(:);

    % Remove empty string
    idx = find(cellfun('isempty',oStruct.IncPaths));
    oStruct.IncPaths(idx) = [];        
end

% Validate source + add ".c"
if ~isempty(oStruct.SourceFiles)
    if ~iscellstr(oStruct.SourceFiles)
        error('LegacyCode:badSourceFiles',...
            'SourceFiles field must be a cell of string.');
    end
    
    % force to be a column vector
    oStruct.SourceFiles = oStruct.SourceFiles(:);
    
    % Remove empty string
    idx = find(cellfun('isempty',oStruct.SourceFiles));
    oStruct.SourceFiles(idx) = [];
    
    % Verify file name and extension
    for ii = 1:length(oStruct.SourceFiles)
        [p, SourceFiles, e] = fileparts(oStruct.SourceFiles{ii});
        if ~isempty(p)
            error('LegacyCode:badSourceFiles',...
                ['SourceFiles field accepts only file name. ',...
                'You must add the path ''%s'' in the SrcPaths field.'], p);
        end        
        if ~isempty(SourceFiles)
            if isempty(e)
                e = '.c';
            end
            oStruct.SourceFiles{ii} = [SourceFiles, e];
        else
            error('LegacyCode:badSourceFiles',...
                'Bad source file ''%s''.', oStruct.SourceFiles{ii});
        end
    end
end

% Validate Source Path
if ~isempty(oStruct.SrcPaths)
    if ~iscellstr(oStruct.SrcPaths)
        error('LegacyCode:badSourcePaths',...
            'SrcPaths field must be a cell of string.');
    end
    % force to be a column vector
    oStruct.SrcPaths = oStruct.SrcPaths(:);

    % Remove empty string
    idx = find(cellfun('isempty',oStruct.SrcPaths));
    oStruct.SrcPaths(idx) = [];        
end


% Check if a non-cmex file as the same name as the cmex
if iCheckIfCfileExist(oStruct)
    warning('LegacyCode:fileConflict',...
        ['The file ''%s.c'' already exists and isn''t a CMEX file.',...
        ' This file must be moved in another directory, or renamed, or ',...
        'the SFunctionName must be different.'],...
        oStruct.SFunctionName);
end


%--------------------------------------------------------------------------
function bool = iCheckIfCfileExist(iStruct)
% Internal function

bool = 0;

% the file we are looking for
cFile = fullfile(pwd,[iStruct.SFunctionName,'.c']);

if exist(cFile, 'file')
    % Read the file's content
    fid = fopen(cFile, 'r');
    if fid == -1
        error(['Unable to open c file: ', cFile]);
    end
    cFileContents = fread(fid, [1, inf], '*char');
    fclose(fid);

    % Look for a CMEX file's content
    s1 = regexp(cFileContents,...
        ['(#define\s+S_FUNCTION_NAME\s+',iStruct.SFunctionName,')']);

    s2 = regexp(cFileContents, '(#define\s+S_FUNCTION_LEVEL\s+2)');

    % If it's not a CMEX file, then an already existing file has the same
    % name as the CMEX
    if isempty(s1) || isempty(s2)
        bool = 1;
    end
end

⌨️ 快捷键说明

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