clc
clear all;

Allfiles=ls([pwd '\*.xlsx*']);
fn=1;
for af=1:size(Allfiles,1)
    a=Allfiles(af,:);
    b=find(isspace(a));
    a(b)=[];
    if  ~strcmp(a,'BMS_Template.xlsx') && ~strcmp(a,'DD_BMS1.xlsx') 
        filenames(fn)={a};
        fn=fn+1;
    end
end

rflag=1;
cont_flag=0;
while rflag==1
    disp('Note: Please select number of the target file:');
    for s=1:length(filenames)
        notes=strcat(num2str(s),'.',char(filenames(s)));
        disp(notes)
    end

    add=strcat(num2str(s+1),'.','ALL');
        disp(add);

    choose = input('Please input number:');
    % cont_flag=0;
    if choose>=1 && choose<=length(filenames)
       target_file=char(filenames(choose));
       rflag=0;
    elseif choose==length(filenames)+1
        target_file=char(filenames(1));        
        cont_flag=1;
        Fid=2;
        rflag=0;       
    else
        disp('The input is not correct !')
        rflag=1;
    end
end


ddflag=1;
while ddflag==1
    
    [sig_num,sig_txt,sig_raw]=xlsread(target_file,'signal');
    flag=size(sig_raw,1);
    Colum=size(sig_raw,2);
    if flag>1
        for k=2:flag
            Name=char(sig_raw(k,1));
            eval([Name '=xpt.Signal;']);

            size=char(sig_raw(k,9));
            idx=strfind(size,',');
            size_data(1)=str2num(size(1:idx));
            size_data(2)=str2num(size(idx+1:length(size)));

            BL=[1,1];
            Eone=isequal(size_data,BL);
            if Eone==1
                size_data=1;         
            end

        %     size_data=sig_num(k-1,4);

            Dim=strcat(Name,'.Dimensions');
            eval([Dim '=size_data;']);

            Comp=strcat(Name,'.Complexity');
            type='real';
            eval([Comp '=type;']);

        %     value=signal_raw(k,8);
        %     idx=strfind(value,',');
        %     idx=[0,idx];
        %     data=zeros(length(idx)-1,1);
        %         for i=1:length(idx)-1
        %             data(i)=str2num(value(idx(i)+1:idx(i+1)-1));
        %         end
        % 
        %     for i=1:size_data(1)
        %         for j=1:size_data(2)
        %             data_matrix(i,j)=data((i-1)*size_data(2)+j);
        %         end
        %     end
            if isnan(cell2mat(sig_raw(k,8)))==0
                IniValue=strcat(Name,'.InitialValue');
            %     ini_data=num2str(sig_num(k-1,3)*ones(size_data(1), size_data(2)));
                ini_data=num2str(sig_num(k-1,3));
                eval([IniValue '=ini_data;']);
            end

            DataType=strcat(Name,'.DataType');
            eval([DataType '=char(sig_raw(k,3));']);

            if isnan(cell2mat(sig_raw(k,6)))==0
                Min=strcat(Name,'.Min');
                eval([Min '=cell2mat(sig_raw(k,6));']);
            end

            if isnan(cell2mat(sig_raw(k,7)))==0
                Max=strcat(Name,'.Max');
                eval([Max '=cell2mat(sig_raw(k,7));']);
            end    


            if isnan(cell2mat(sig_raw(k,5)))==0
                Unit=strcat(Name,'.DocUnits');
                eval([Unit '=char(sig_raw(k,5));']);
            end

            if isnan(cell2mat(sig_raw(k,4)))==0
                Des=strcat(Name,'.Description');
                eval([Des '=char(sig_raw(k,4));']);
            end
            
            if ~isempty(cell2mat(strfind(sig_raw(k,1),'_strMsg')))
                
                if strcmp(char(sig_raw(k,2)),'input')|| strcmp(char(sig_raw(k,2)),'Input') || strcmp(char(sig_raw(k,2)),'INPUT')
                    Sto=strcat(Name,'.CoderInfo.StorageClass');
                    storagetype1 = 'ImportedExtern';
                    eval([Sto '=storagetype1;']);
                elseif strcmp(char(sig_raw(k,2)),'output')|| strcmp(char(sig_raw(k,2)),'Output') || strcmp(char(sig_raw(k,2)),'OUTPUT') 
                    Sto=strcat(Name,'.CoderInfo.StorageClass');
                    storagetype1 = 'ExportedGlobal';
                    eval([Sto '=storagetype1;'])
                end 
                
            else
                
                Sto=strcat(Name,'.CoderInfo.StorageClass');
                StoC=strcat(Name,'.CoderInfo.CustomStorageClass');
                
                if strcmp(char(sig_raw(k,2)),'input') || strcmp(char(sig_raw(k,2)),'Input') || strcmp(char(sig_raw(k,2)),'INPUT')
            %         storagetype='ImportedExtern';
            %         eval([Sto '=storagetype;']);
                    storagetype1 = 'Custom';
                    storagetype2 = 'InputSignal';
                    eval([Sto '=storagetype1;']);
                    eval([StoC '=storagetype2;']);
                elseif strcmp(char(sig_raw(k,11)),'NVM')
                    storagetype1 = 'Custom';
                    storagetype2 = 'Volatile';
                    eval([Sto '=storagetype1;']);
                    eval([StoC '=storagetype2;']);
                elseif  strcmp(char(sig_raw(k,2)),'output')|| strcmp(char(sig_raw(k,2)),'Output') || strcmp(char(sig_raw(k,2)),'OUTPUT') || strcmp(char(sig_raw(k,2)),'signal') 
                        storagetype1 = 'Custom';
                        storagetype2 = 'OutputSignal';
                        eval([Sto '=storagetype1;']);
                        eval([StoC '=storagetype2;']);                    
                    
                else 
        %         storagetype='ExportedGlobal';
        %         eval([Sto '=storagetype;']);
                storagetype1 = 'Custom';
                storagetype2 = 'OutputSignal';
                eval([Sto '=storagetype1;']);
                eval([StoC '=storagetype2;']);         
                end
            end
        end
        clear DataType BL Eone Des Dim idx ini_data IniValue k Max min Min Name sig_txt size size_data Unit type Sto storagetype Comp storagetype1 storagetype2 StoClass StoC
        clc
        clear sig_raw sig_num datasize flag
    end  
    
        [par_num,par_txt,par_raw]=xlsread(target_file,'parameters');
        flag=size(par_raw,1);
        if flag>1
            for k=2:flag
            size=char(par_raw(k,6));
            idx=strfind(size,',');
            size_data(1)=str2num(size(1:idx));
            size_data(2)=str2num(size(idx+1:length(size)));

            Name=char(par_raw(k,1));
            eval([Name '=xpt.Parameter;']);

            value=char(cell2mat(par_raw(k,2)));
            idx=strfind(value,',');
            idx=[0,idx];
            data=zeros(length(idx)-1,1);
            for i=1:length(idx)-1
                    data(i)=str2num(value(idx(i)+1:idx(i+1)-1));
                end

            for i=1:size_data(1)
                for j=1:size_data(2)
                    data_matrix(i,j)=data((i-1)*size_data(2)+j);
                end
            end

            Value=strcat(Name,'.Value');
            eval([Value '=data_matrix;']);

            DataType=strcat(Name,'.DataType');
            eval([DataType '=char(par_raw(k,5));']);


            Min=strcat(Name,'.Min');
            eval([Min '=cell2mat(par_raw(k,3));']);


            Max=strcat(Name,'.Max');
            eval([Max '=cell2mat(par_raw(k,4));']);

            if isnan(cell2mat(par_raw(k,7)))==0
                Unit=strcat(Name,'.DocUnits');
                eval([Unit '=char(par_raw(k,7));']);
            end

            if isnan(cell2mat(par_raw(k,10)))==0
                Des=strcat(Name,'.Description');
                eval([Des '=char(par_raw(k,10));']);
            end

            StoClass='Const (Custom)';
            Sto=strcat(Name,'.StorageClass');
            eval([Sto '=StoClass;']); 

            if isnan(cell2mat(par_raw(k,8)))==0
                HF=strcat(Name,'.CoderInfo.CustomAttributes.HeaderFile');
                eval([HF '=char(par_raw(k,8));']);
            end

            if isnan(cell2mat(par_raw(k,9)))==0
                DF=strcat(Name,'.CoderInfo.CustomAttributes.DefinitionFile');
                eval([DF '=char(par_raw(k,9));']);
            end  

        %     Mem=strcat(Name,'.CoderInfo.CustomAttributes.MemorySection');
        %     MemSec='MemConst';
        %     eval([Mem '=MemSec;']);
            clear data data_matrix DataType Des DF HF i idx j k Max min Min Name par_num par_txt Unit value Value size size_data StorageClass Sto StoClass StoC
            clc
            end
            clear par_raw datasize flag par_num par_txt
        end
    clc
    if cont_flag==0 
        disp('Dou you want to continue?');
        disp('1. Yes');
        disp('2. No');
        choose = input('Please input number:');

        if choose==1
            rflag=1;
            while rflag==1
                disp('Note: Please select number of the target file:');
                for s=1:length(filenames)
                    notes=strcat(num2str(s),'.',char(filenames(s)));
                    disp(notes)
                end
                choose2 = input('Please input number:');
                if choose2>=1 && choose2<=length(filenames)
                    target_file=char(filenames(choose2));
                    rflag=0;
                else
                    disp('The input is not correct !')
                    rflag=1;
                end
            end                
        elseif choose==2
            ddflag=0;
        end
    elseif cont_flag==1 && Fid<= length(filenames)
        notes=strcat('The file',32,num2str(Fid-1),32,'has been done!');
        disp(notes)            
        target_file=char(filenames(Fid));
        Fid=Fid+1;       
    else
         ddflag=0;
         disp('The DD files have been done!')
    end

end

clear par_raw datasize flag par_num par_txt
clear DataType BL Eone Des Dim idx ini_data IniValue k Max min Min Name sig_txt size size_data Unit type Sto storagetype Comp storagetype1 storagetype2 StoClass StoC
clc
clear a add Allfiles b choose choose2 cont_flag filenames fn notes rflag s target_file 
clear af ddflag Fid Colum





%pcode dd2mpt.m





