function bSuccess = fCleanFileAlignment(vFiles)
%fCleanFileAlignment clean m-file alignment
%
% bSuccess = fCleanFileAlignment(vFiles)
%
% Function for cleaning m-file alignment.
%
% Example: bSuccess = fCleanFileAlignment({'MyFile1.m', 'MyFile2.m'});
%
% Author: Dipl.-Ing. Andr Manfred Strobel
% Copyright: 1999-2001 by DaimlerChrysler AG, FT1/AK
% Matlab: 6.0, R12 (Win)
% Date: 2001/03/12 11:00
% Version: 001.22 * Released * / beta / alpha
% Release: 01.00
% define constants
% cNewExt = ['.new']; % additional extension for saved file
cNewExt = ['']; % additional extension for saved file
% initialize outputs
bSuccess = 0; % first there is no successful operation
% create column vector
[vFiles, vSuccess] = fVectorOrientation(vFiles, 'column');
for i = 1:1:size(vFiles, 1)
% for all files do
% load file
vFile = fopen(vFiles{i}, ['r']); % open the file for read
j = 1; % initialize line counter
vLines = {}; % initialize cell array of lines
while 1
vLoadString = fgetl(vFile); % get next line
if ~isstr(vLoadString)
% current line is not a normal string -> end of lines or binary file
break; % exit while loop
else
vLines{j} = vLoadString;
j = j + 1; % increase line counter
end % of if
end % of while
fclose(vFile);
vLines = vLines'; % transpose vector
vAlignment = 0; % first there is no tab on the left side
for j = 1:1:size(vLines, 1)
% for all lines do
% remove leading blanks and tabs
vNewLine = ['']; % init changed line as empty string
for k = 1:1:size(vLines{j}, 2)
% for all characters of the current line do
if ~(isequal(vLines{j}(k), [' ']) | isequal(vLines{j}(k), char(9)))
% keep the rest
vNewLine = vLines{j}(k:size(vLines{j}, 2)); % take the rest of the line
break; % for k loop
end % of if
end % of for k
vLines{j} = vNewLine; % use the calculated result
% remove comments
vParseLine = vLines{j};
vComments = fFindStr(vParseLine, ['%']);
if (size(vComments, 2) > 0)
vParseLine = vParseLine(1:(vComments(1) - 1)); % remove comment
end % of if
% remove strings
if ~isempty(vParseLine)
bIsStringSymbol = vParseLine == ['''']; % check string for string symbols
end % of if
vNewLine = ['']; % init changed line as empty string
bInString = 0; % first there is no open string
for k = 1:1:size(vParseLine, 2)
% for all characters of the line to parse do
if bIsStringSymbol(k)
% keep the rest
bInString = ~bInString; % change in string status
elseif ~bInString
% not string symbol and not in string
vNewLine = [vNewLine, vParseLine(k)]; % take the character
end % of if
end % of for k
vParseLine = vNewLine; % use the calculated result
% check for start key words
vStartWords = 0; % init number of start words with zero
vStartStrings = {'for', 'while', 'if', 'elseif', 'else', 'switch', 'case', 'otherwise', 'try', 'catch'};
for l = 1:1:size(vStartStrings, 2)
% for all start strings do
if fIsCommandInLine(vParseLine, vStartStrings{l})
% start string is in line
vStartWords = 1; % it is one start word
break; % break for l loop
end % of if
end % of for l
% check for end key word
vEndWords = 0; % init number of end words with zero
vEndStrings = {'end', 'elseif', 'else', 'case', 'otherwise', 'catch'};
for l = 1:1:size(vEndStrings, 2)
% for all end strings do
if fIsCommandInLine(vParseLine, vEndStrings{l})
% end string is in line
vEndWords = 1; % it is one end word
break; % break for l loop
end % of if
end % of for l
% calculate new alignment
vAlignment = vAlignment - vEndWords; % take end words immediately into account
vNewAlign = ['']; % init new alignment as empty string
for k = 1:1:vAlignment
% for all alignment steps do
vNewAlign = [vNewAlign, char(9)]; % add a tab
end % of for k
vLines{j} = [vNewAlign, vLines{j}];
% disp([num2str(vStartWords), ' ', num2str(vEndWords), ' ', num2str(vAlignment), ' ', vLines{j}]);
vAlignment = vAlignment + vStartWords; % take start words in next line into account
end % of for j
% save file
vFile = fopen([vFiles{i}, cNewExt], ['w']); % open the file for write
for j = 1:1:size(vLines, 1)
% for all lines do
fprintf(vFile, ['%s%s%s'], vLines{j}, char(13), char(10)); % write next line
end % of for j
fclose(vFile);
end % of for i
bSuccess = -1; % successful operation
% end of function fCleanFileAlignment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function bIsCommandInLine = fIsCommandInLine(vLine, vCommandString)
bIsCommandInLine = 0; % first there is no command found in the line
vCommandPos = fFindStr(vLine, vCommandString); % look for the command string in the line
if ~isempty(vCommandPos)
% command string found
vCommandPos = vCommandPos(1); % only use first occurence
% check left side
bLeftSideOK = 0; % init left side O.K. with false
if isequal(vCommandPos, 1)
% command starting at first character in line
bLeftSideOK = -1; % left side is O.K.
elseif (isequal(vLine(vCommandPos - 1), [' ']) | isequal(vLine(vCommandPos - 1), char(9)))
% character left of command is space or tab
bLeftSideOK = -1; % left side is O.K.
end % of if
% check right side
bRightSideOK = 0; % init right side O.K. with false
if isequal(vCommandPos, (size(vLine, 2) - size(vCommandString, 2) + 1))
% command at the end of the line
bRightSideOK = -1; % right side is O.K.
elseif (size(vLine, 2) >= (vCommandPos + size(vCommandString, 2)))
% line is long enough for character at the end of the command
if (isequal(vLine(vCommandPos + size(vCommandString, 2)), [' ']) | isequal(vLine(vCommandPos + size(vCommandString, 2)), char(9)) | isequal(vLine(vCommandPos + size(vCommandString, 2)), [';']))
% character right of command is space or tab or semicolon
bRightSideOK = -1;
end % of if
end % of if
% evaluate checks
if (bLeftSideOK & bRightSideOK)
bIsCommandInLine = -1;
end % of if
end % of if
% end of function fIsCommandInLine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%