1function [IFF]=openiff(fid,LEN) 2% OPENIFF is an auxillary function to SOPEN for 3% opening of IFF files 4% 5% Use SOPEN instead of OPENIFF 6% 7% See also: fopen, SOPEN 8% 9% References: 10 11% This program is free software; you can redistribute it and/or 12% modify it under the terms of the GNU General Public License 13% as published by the Free Software Foundation; either version 3 14% of the License, or (at your option) any later version. 15 16% $Id$ 17% Copyright (C) 2004,2005,2007 by Alois Schloegl <alois.schloegl@gmail.com> 18% This is part of the BIOSIG-toolbox http://biosig.sf.net/ 19 20 21FLAG.CLOSE = 0; 22if ischar(fid) 23 fid = fopen(fid,'r'); 24 FLAG.CLOSE = 1; 25end; 26 27if nargin<2, 28 LEN = 8; 29end; 30 31IFF = []; 32K = 0; 33K1 = 0; 34 35[tmp,c] = fread(fid,[1,4],'uint8'); 36while ((LEN>0) | isnan(LEN)) & (c>0), 37 tag = char(tmp); 38 tagsize = fread(fid,1,'uint32'); % which size 39 tagsize0= tagsize + rem(tagsize,2); 40 filepos = ftell(fid); 41 LEN = LEN - 8; 42 43% fprintf(1,'tag: %6s\tpos: %8i\tsize: %8i\n',tag,filepos,tagsize); 44 45 if 0, 46 VAL = openiff(fid,tagsize); 47 elseif strcmp(tag,'FORM') 48 [tmp,c] = fread(fid,[1,4],'uint8');tag, 49 VAL = setfield([],char(tmp),openiff(fid,tagsize-4)); 50 elseif 0,strcmp(tag,'RIFF') 51 VAL = openiff(fid,tagsize); 52 elseif strcmp(tag,'RIFF') 53 [tmp,c] = fread(fid,[1,4],'uint8'); 54 %val = fread(fid,tagsize-4,'uint8'); 55 val = openiff(fid,tagsize-4); 56 VAL = setfield([],char(tmp),val); 57 elseif strcmp(tag,'MThd') 58 VAL.MThd = fread(fid,tagsize,'uchar'); 59 VAL.MIDI = openiff(fid,NaN); 60 %LEN = NaN; 61 %VAL.MIDI = tmp; 62 63 elseif strcmp(tag,'LIST'); % CNT_RIFF (EEP 3.1) 64 [tmp,c] = fread(fid,[1,4],'uint8'); 65 VAL = setfield([],char(tmp),openiff(fid,tagsize-4)); 66 67 elseif strcmp(tag,'chan') % CNT_RIFF (EEP 3.1) 68 VAL = fread(fid,[1,tagsize/2],'uint16'); 69 elseif strcmp(tag,'info') % CNT_RIFF (EEP 3.1) 70 VAL = fread(fid,[1,tagsize],'*char'); 71 elseif strcmp(tag,'eeph') % CNT_RIFF (EEP 3.1) 72 VAL = fread(fid,[1,tagsize],'*char'); 73 elseif strncmp(tag,'ep ',4), % CNT_RIFF (EEP 3.1) 74 VAL = fread(fid,[1,tagsize/4],'uint32'); 75 elseif strcmp(tag,'hdrl') 76 [tmp,c] = fread(fid,[1,4],'uint8'); 77 VAL = setfield([],char(tmp),openiff(fid,tagsize-4)); 78 79 elseif strcmp(tag,'CAT ') 80 VAL = openiff(fid,tagsize); 81 82 elseif strcmp(tag,'data') 83 % LEN = fread(fid,1,'uint32'); 84 VAL = fread(fid,[1,LEN],'*uint8'); 85 %[tmp,c] = fread(fid,[1,4],'*char'); 86 %LEN = fread(fid,1,'int32'); 87 %VAL.data = fread(fid,LEN,'uint8'); 88 %VAL = openiff(fid,tagsize); 89 elseif strncmp(tag,'(c)',3) 90 tag = 'Copyright'; 91 VAL = char(fread(fid,tagsize,'uchar')'); 92 %VAL.CopyRight = char(VAL); 93 else 94 if 0,tagsize<1024*2, 95 VAL = fread(fid,tagsize,'uchar'); 96 else 97 VAL = []; 98 VAL.handle = ftell(fid); 99 VAL.size = tagsize; 100 status = fseek(fid,tagsize,'cof'); 101 if status, return; end; 102 end 103 end; 104 105 if strcmp(tag(3:4),'dc') 106 K = K+1; 107 ix = (tag(1:2)-48)*[16;1]+1; 108 IFF.dc{K,ix} = VAL; 109 elseif strcmp(tag(3:4),'wb') 110 K1 = K1+1; 111 ix = (tag(1:2)-48)*[16;1]+1; 112 IFF.wb{K1,ix} = VAL; 113 else 114 %try, 115 IFF = setfield(IFF,deblank(tag),VAL); 116 %catch, 117 %end; 118 end; 119 status = fseek(fid,filepos+tagsize0,'bof'); 120 LEN = LEN - tagsize0; 121 [tmp,c] = fread(fid,[1,4],'uint8'); 122end; 123 124if ~isfield(IFF,'MThd'), % do not check MIDI files 125 if ((LEN ~= 0) & c), 126 fprintf(2,'Warning OPENIFF: LEN=%i %i %i %i %s %i\n',LEN,filepos,tagsize0,ftell(fid),char(tmp),c); 127 end; 128end; 129 130if FLAG.CLOSE, 131 fclose(fid); 132end; 133