function [Line,OutputIndex,NotFinished]=badxline(Instructions,InputIndex);
% [Line,OutputIndex,NotFinished]=badxline(Instructions,InputIndex)
% Look for the first line of instructions which starts at InputIndex

% (c) F. Auger, june 1999
InstructionsLength=length(Instructions);

% find the good starting point
if InputIndex>InstructionsLength,
 NotFinished=0; Line=''; OutputIndex=InputIndex; return;
end;

Index=InputIndex; 

% first loop : find the good start
% remove spaces, tabs, cariage return/line feed and comments

AsciiCode=double(Instructions(Index));
while (AsciiCode==13) | (AsciiCode==10) | ...
      (AsciiCode==39) | (AsciiCode==32) | (AsciiCode==9),
 % fprintf('%i %i\n', Index, AsciiCode);
 if AsciiCode==39,                         % start of a comment ?
  while double(Instructions(Index+1))~=13 & ...
        (Index<=InstructionsLength),
   Index=Index+1;                          % loop until end of line
  end;
  % and the next char is ... 13
  Index=Index+1; AsciiCode=double(Instructions(Index));
 else
  Index=Index+1;                           % skip that
  if Index>InstructionsLength,             % end of the file : stop immediately
   AsciiCode=0;
  else
   AsciiCode=double(Instructions(Index));  % usual case
  end;
 end;
end;

if Index>InstructionsLength,               % No line, in fact
 NotFinished=0; Line=''; OutputIndex=InputIndex; return;
end;

InputIndex=Index;

% second loop : find the end of the line
AsciiCode=double(Instructions(Index));
while AsciiCode~=13 & AsciiCode~=10 & ...
      AsciiCode~=39 & AsciiCode~=59 & ...
      Index<=InstructionsLength,
 Index=Index+1; 
 if Index>InstructionsLength,             % end of file : stop !
  AsciiCode=13;
 else 
  AsciiCode=double(Instructions(Index));  % usual case
 end;
 % fprintf('%i %s\n',Index,AsciiCode);
end;
Line=Instructions(InputIndex:Index-1);

% remove ending tabs and spaces
LengthLine=length(Line);
while LengthLine>0 & ...
      (double(Line(LengthLine))==32)|(double(Line(LengthLine))==9),
 Line=Line(1:LengthLine-1);
 LengthLine=length(Line);
end;

% finish by a comment ? skip it
if AsciiCode==39, 
 while double(Instructions(Index+1))~=13,
  Index=Index+1;
 end;
end;

OutputIndex=Index+1;
NotFinished=Index<=InstructionsLength;

