from C code comment remover by Nitin Skandan
Removes comments

removecomment(filename)
% /***********************************************************************
% * Company: TATA ELXSI
% * File: removecomment.m
% * Author :Nitin
% * Version :1.0
% * Date:4-8-2006
% * Operating Environment: 
% * Description: Remove comments in c file 
% ex: checkvariable('e:\nitin','xx.xls')
% * Dependencies : 
% * Customer Bug No./ CMF No. :
% * Brief description of the fix/enhancement :
% ***********************************************************************/
function [flag] = removecomment(filename)
 fidc = fopen(filename);
 [patstr, filename]=fileparts(filename) ;
 fidn = fopen(strcat(filename,'_nocomment.c'),'w+') ;
 flag = 0 ;
 % Read file
 buffc = fscanf(fidc,'%c');
 file_sizec=size(buffc);
 file_sizec=file_sizec(1,2);
 pntr = 1 ;
 cflag = 0 ;
 
 for i=1:file_sizec
    [status, cflag, buffc] = decision_eng(buffc, i, file_sizec, cflag) ;% call decision engine
     buffc = process_sm(status, i, buffc);      
 end

 % To remove blank lines
 j=1 ;
 nbflag=0 ;
 for i=1:file_sizec
     if(uint8(buffc(i))==10) %new line found so init all
        if(nbflag)
            fprintf(fidn,'%s',buff) ;
        end
        nbflag = 0 ;
        clear buff ;
        j=1;
     else       
         buff(j) = buffc(i) ;
         if(~(uint8(buff(j))==13 || uint8(buff(j))==10 || uint8(buff(j))==32))
             nbflag = 1 ; % Line is not blank
         end
         j=j+1 ;
     end
 end
 
 fclose(fidc);
 fclose(fidn) ;
 flag = 1 ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Decision engine
 function [status, cflag, buff] = decision_eng(buff,pntr,file_sizec, cflag)
  
 %state machine
 switch (cflag)
     case 0 %No comment
         status = 0 ;
         if (pntr == file_sizec)%EOF detect
             cflag = 3 ;                 
         elseif (buff(pntr)=='/')
             if( buff(pntr+1) == '*')
                 buff(pntr)= 32 ;
                 cflag= 1 ; % State 1 switch
             elseif (buff(pntr+1) == '/')
                  buff(pntr)= 32 ;
                 cflag = 2 ; % state 2 switch
             end        
         end         
     case 1% /*
         status = 1 ;         
         if (pntr == file_sizec)% EOF detect
             cflag = 3 ;        % 
         elseif (buff(pntr)=='*')
             if(buff(pntr+1) == '/')%End of comment
                 buff(pntr+1)= 32 ;
                 cflag= 0 ;         % State 0 swtich
             end        
         end                 
     case 2%//
         status = 1 ;
         if (pntr == file_sizec)% EOF detect
             cflag = 3 ;        % 
         elseif (uint8(buff(pntr))== 13)             
             cflag= 0 ;         % State 0 swtich             
         end                         
     case 3%EOF
         status = 2 ;         
 end
   
 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Process state m/c 
 function [buff] = process_sm(status, pntr, buff)
     switch status
         case 0 % No comments
         case 1
             buff(pntr) = 32 ;
         case 2
             
     end
     


 

Contact us at files@mathworks.com