function datastruct = tst_readgsifile(filename,formatstruct)

fid = fopen(filename,'r');
datastruct = [];
datastruct.numpoints = 0;
counter = 0;
datastruct.triangulation = [];
datastruct.interpolation = [];


if(fid == -1)
  error('File does not exist in current or MATLAB search path directories.');
else
  while(~feof(fid))
    line = fgetl(fid);
    counter = counter + 1;
    if(length(line) > 1)
      ptidstr = line(1:2);
      ptid = str2num(ptidstr);
      flag = 0;
      for(i=1:length(formatstruct.Pt_code))
        if(formatstruct.Pt_code(i) == ptid);
          flag = 1;
        end
      end

      if(flag)
        datastruct = readgsiline(datastruct,line,formatstruct,counter);
      else
        fprintf('Line %i: Point code number %i is not defined in the file format structure.  Point ignored.\n',counter,ptid);
      end
    end
  end
end


%---
function datastruct = readgsiline(datastruct,line,formatstruct,counter)

remainder = line;
flag = 1;

while(length(remainder) > 0)
  [portion,remainder] = scanportion(remainder);
  datastruct = setstruct(portion,datastruct,formatstruct);
  flag = 0;
end

if((datastruct.numpoints > 0) & (flag)) 
  if(isempty(datastruct.E(end)) | isempty(datastruct.N(end)) | isempty(datastruct.H(end)))
    fprintf('Line %i: E, N, H data not imported correctly.  Line ignored.\n',counter);
  end
end

%---
function datastruct = setstruct(input,datastruct,formatstruct)

input = deblank(input);

for(i=1:length(formatstruct.Pt_code))
  if(formatstruct.Pt_code(i) == str2num(input(1:2)))
    numpoints = datastruct.numpoints + 1;
    datastruct.numpoints = numpoints;
    datastruct.point_number(numpoints) = getgsivalue(input);
  end

  flag = str2num(input(1:2));
  switch flag
    case formatstruct.EastingCode
      datastruct.E(datastruct.numpoints) = getgsivalue(input);
      datastruct.E(datastruct.numpoints) = datastruct.E(datastruct.numpoints)/1000;
    case formatstruct.NorthingCode
      datastruct.N(datastruct.numpoints) = getgsivalue(input);
      datastruct.N(datastruct.numpoints) = datastruct.N(datastruct.numpoints)/1000;
    case formatstruct.ElevationCode
      datastruct.H(datastruct.numpoints) = getgsivalue(input);
      datastruct.H(datastruct.numpoints) = datastruct.H(datastruct.numpoints)/1000;
    otherwise
  end
end


%---
function value = getgsivalue(input)

[tmp,valuestr] = strtok(input,'+-');
if(sum(isletter(valuestr)) == 0)
  value = str2num(valuestr);
else
  value = 0;
end

%--
function [outstring,remainstring] = scanportion(line)

char = line(1);
counter = 1;

while((strcmp(char,' ') | strcmp(char,'  ')) & (counter < length(line)))
  counter = counter + 1;
  char = line(counter);
end

begpos = counter;

counter = counter + 1;
if(counter > length(line))
  outstring = '';
  remainstring = line;
else
  char = line(counter);

  while(~strcmp(char,' ') & ~strcmp(char,'	') & (counter < length(line)))
    counter = counter + 1;
    char = line(counter);
  end

  endpos = counter;

  outstring = line(begpos:endpos);
  if(endpos == length(line))
    remainstring = '';
  else
    remainstring = line(endpos+1:end);
  end
end
 

