function [strings, sizes, message] = busread(busfilename)
%BUSREAD Returns the Strings and Sizes for a given bus file.
%
%For use only with the SIMULINK Bus connection blocks.
%
%See also 'help simbus'
%Copyright (c) 1995 by The MathWorks, Inc.
%$Revision: 1.1 $ $Date: 1997/02/11 20:50:15 $
strings = '';
message = '';
sizes = [];
busfilename=lower(busfilename);
% Make sure there is no crud in the filename
if any(busfilename == '.')
[busfilename,ext] = strtok(busfilename,'.');
if ~strcmp(ext,'txt')
message = ['Unknown bus file format ' busfilename '.' ext];
return;
end
end
busfilename = [busfilename '.txt'];
% First check in the current directory
directory = cd;
if strtok(version,'.') < '5'
c = computer;
if isunix,
sep = '/';
elseif isvms
sep = '.';
elseif strcmp(c(1:2),'PC')
sep = '\';
elseif strcmp(c(1:2),'MA')
sep = ':';
end
else
sep = filesep;
end
busfile = [directory sep busfilename];
exists = exist(busfile);
if ~exists
%
% Can't find file in current directory - try using fwhich
%
busfile = fwhich(busfilename,'file','first');
end
if isempty(busfile)
%
% Can't find file using fwhich - return error message
%
message = ['Can''t locate file ' busfilename];
return;
end
fid = fopen(busfile,'rt');
if fid < 0
%
% Can't read file - return error message
%
message = ['Can''t read file ' busfilename];
return;
end
%
% Read in whole file
%
[txt,count] = fread(fid,'char');
fclose(fid);
if count == 0
%
% Empty file
%
message=['The file ' busfilename ' is empty.'];
return;
end
txt = setstr(txt);
%
BLANK = 32;
CRETURN = 13;
comp = computer;
if comp(1:3) == 'MAC'
NEWLINE = 13;
else
NEWLINE = 10;
end
%
nl = find(txt == NEWLINE);
lnl = length(nl);
%
% Be sure that the last line is covered in case NEWLINE
% is missing
%
if isempty(nl)
nl = count;
lnl = 1;
elseif nl(lnl) ~= count
nl = [ nl; count ];
lnl = lnl + 1;
end
%
% Skip comments and blank lines
%
line = txt(1:nl(1)-1);
if isempty(line)
flag=1;
elseif txt(nl(1)-1) == CRETURN
line = txt(1:nl(1)-2);
if isempty(line)
flag = 1;
else
flag = strcmp(line(1),'%');
end
else
flag = strcmp(line(1),'%');
end
i = 1;
while flag
i = i + 1;
if i > lnl
%
% No strings in file
%
message = ['File ' busfilename ' is not in a valid bus file.'];
return;
end
line = txt(nl(i-1)+1:nl(i)-1);
if isempty(line)
flag = 1;
elseif txt(nl(i)-1) == CRETURN
line = txt(nl(i-1)+1:nl(i)-2);
if isempty(line)
flag = 1;
else
flag = strcmp(line(1),'%');
end
else
flag = strcmp(line(1),'%');
end
end
%
astrings = '';
lstrings = [];
while 1
[text,count] = sscanf(line, '%s %d%*');
if count > 0
len = length(text);
lstrings = [lstrings; len-1];
astrings = [astrings setstr(text(1:len-1)')];
sizes = [sizes; text(len:len)];
end
i = i + 1;
if i > lnl
%
% Finished:
% Build strings from size vector and one long string
%
len = length(lstrings);
mx = max(lstrings);
strings = setstr(ones(1,len * mx) * BLANK);
ssum = 1;
asum = 1;
for i=1:len
strings(ssum:ssum+lstrings(i)-1) = astrings(asum:asum+lstrings(i)-1);
ssum = ssum + mx;
asum = asum + lstrings(i);
end
strings = reshape(strings,mx,len)';
return;
end
if txt(nl(i)-1) ~= CRETURN
line = txt(nl(i-1)+1:nl(i)-1);
else
line = txt(nl(i-1)+1:nl(i)-2);
end
end