1function [HDR]=sseek(HDR,offset,origin)
2% SSEEK repositions file position indicator
3%  HDR = sseek(HDR,offset,origin)
4%
5%  offset       number of samples relative to origin
6%  origin
7%        -1,'bof':   begin of file
8%         0,'cof':   current file position
9%        +1,'eof':   end of file
10%
11% See also: SOPEN, SREAD, SWRITE, SCLOSE, SSEEK, SREWIND, STELL, SEOF
12
13%	$Id$
14%	(C) 1997-2005,2006,2007 by Alois Schloegl <alois.schloegl@gmail.com>
15%    	This is part of the BIOSIG-toolbox http://biosig.sf.net/
16
17% This program is free software; you can redistribute it and/or
18% modify it under the terms of the GNU General Public License
19% as published by the Free Software Foundation; either version 2
20% of the  License, or (at your option) any later version.
21%
22% This program is distributed in the hope that it will be useful,
23% but WITHOUT ANY WARRANTY; without even the implied warranty of
24% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25% GNU General Public License for more details.
26%
27% You should have received a copy of the GNU General Public License
28% along with this program; if not, write to the Free Software
29% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
30
31
32if strcmp(origin,'bof')
33	origin = -1;
34elseif strcmp(origin,'cof')
35	origin = 0;
36elseif strcmp(origin,'eof')
37	origin = 1;
38end;
39
40if origin == -1,
41        HDR.FILE.POS = offset;
42        if strmatch(HDR.TYPE,{'ACQ','BDF','EDF','GDF','EPL','MFER','SCP','native','TMS32','WG1'}),
43	elseif HDR.FILE.FID>2,
44                POS = HDR.HeadLen+HDR.AS.bpb*offset;
45                if POS~=ceil(POS),  % for alpha format
46                        fprintf(HDR.FILE.stderr,'Error STELL (alpha): starting position is non-integer\n');
47                        return;
48                end
49                HDR.FILE.status = fseek(HDR.FILE.FID,POS,-1);
50        end
51
52elseif origin == 0,
53        HDR.FILE.POS = HDR.FILE.POS + offset;
54        if strmatch(HDR.TYPE,{'ACQ','BDF','EDF','GDF','EPL','MFER','SCP','native','TMS32','WG1'}),
55	elseif HDR.FILE.FID>2,
56                POS = HDR.AS.bpb*offset;
57                if POS~=ceil(POS),  % for alpha format
58                        fprintf(HDR.FILE.stderr,'Error STELL (alpha): starting position is non-integer\n');
59                        return;
60                end
61                HDR.FILE.status = fseek(HDR.FILE.FID,POS,0);
62        end
63
64elseif origin == 1,
65	if 0, %strmatch(HDR.TYPE,{}),
66		HDR.FILE.POS = HDR.NRec+offset;
67		HDR.FILE.status = fseek(HDR.FILE.FID,HDR.AS.bpb*offset,1);
68	elseif strmatch(HDR.TYPE,{'ACQ','BDF','EDF','GDF','EPL','Sigma'}),
69		HDR.FILE.POS = HDR.NRec*HDR.SPR+offset;
70                if HDR.FILE.POS < 0,
71                        HDR.FILE.POS = 0;
72                elseif HDR.FILE.POS > HDR.NRec*HDR.SPR,
73                        HDR.FILE.POS = HDR.NRec*HDR.SPR;
74                end;
75		HDR.FILE.status = fseek(HDR.FILE.FID,HDR.HeadLen+HDR.FILE.POS*HDR.AS.bpb,-1);
76	elseif strmatch(HDR.TYPE,{'CTF','Nicolet'}),
77		POS = HDR.AS.endpos+offset*HDR.AS.bpb;
78		HDR.FILE.status = fseek(HDR.FILE.FID,POS,-1);
79		HDR.FILE.POS = (POS-HDR.HeadLen)/HDR.AS.bpb;
80	elseif strmatch(HDR.TYPE,{'AINF','BKR','ET-MEG','ISHNE','RG64','MIT','LABVIEW','SMA','BVbinmul','BCI2000'}),
81		HDR.FILE.POS = HDR.AS.endpos+offset;
82		HDR.FILE.status = fseek(HDR.FILE.FID,HDR.AS.bpb*offset,1);
83	elseif strmatch(HDR.TYPE,{'CNT','EEG','AVG','EGI','SND','WAV','AIF','CFWB','DEMG'}),
84		HDR.FILE.POS = HDR.AS.endpos+offset;
85		HDR.FILE.status = fseek(HDR.FILE.FID,HDR.HeadLen+(HDR.AS.endpos+offset)*HDR.AS.bpb,-1);
86        elseif strmatch(HDR.TYPE,{'alpha'}),
87                POS = HDR.HeadLen+(HDR.AS.endpos+offset)*HDR.AS.bpb;
88                if POS~=ceil(POS),  % for alpha format
89                        fprintf(HDR.FILE.stderr,'Error STELL (alpha): starting position is non-integer\n');
90                        return;
91                end
92		HDR.FILE.POS = HDR.AS.endpos+offset;
93		HDR.FILE.status = fseek(HDR.FILE.FID,POS,-1);
94        elseif strmatch(HDR.TYPE,{'RDF','SIGIF'}),
95		HDR.FILE.POS = length(HDR.Block.Pos)+offset;
96        elseif strmatch(HDR.TYPE,{'BVascii','BVbinvec','EEProbe-CNT','EEProbe-AVR','FIF','native','MFER','SCP','TMS32','WG1'}),
97		HDR.FILE.POS = HDR.AS.endpos+offset;
98	else
99		fprintf(HDR.FILE.stderr,'Warning SSEEK: format %s not supported.\n',HDR.TYPE);
100	end;
101else
102        fprintf(2,'error SSEEK: 3rd argument "%s" invalid\n',origin);
103        return;
104end;
105
106