function [nbytes, last_index, config] = get_config(instclass,params, cfg, file)
% read by @bb/read.m
% read the file; look for errors


% the BB and OS processors write little-endian
% the NB processor writes big-endian
if isa(instclass, 'nb')
   fid = fopen(file, 'r', 'ieee-be');  
else  %insclass is 'os' or 'bb'
   fid = fopen(file, 'r', 'ieee-le');  
end

[buf, config] = read_buf(instclass, fid, params);  % First time: read config.

if (isempty(buf)) %set to [] in read_buf ("second_set" was specified in error)
   nbytes = [];
   last_index = [];
   return;        %jump out now.
end


%otherwise, continue.   
if ischar(buf)
   d.error = buf;
   last_index = -1;
   return;
end


fseek(fid, 0, 'eof');
len_file = ftell(fid);      % total number of bytes                            
nbytes = config.nbytes + 2; % bytes remaining in record up to but not including
                            %    the checksum, plus  2 for the checksum        
last_index = floor(len_file / nbytes);  %%% This is a bug?? used to be      
fclose(fid);                                %%% floor(len_file / nbytes) - 1

if (~cfg.back_to_front)    %start at the front

   % the BB and OS processors write little-endian
   % the NB processor writes big-endian
   if isa(instclass, 'nb')
      fid = fopen(file, 'r', 'ieee-be');  
   else
      fid = fopen(file, 'r', 'ieee-le');  
   end

   % ok to keep reading, since it's the same direction   
   fseek(fid, 0, 'bof');
   keepreading = 1;
   
   last_good_index = 0;
   while keepreading
      buf = [];
      fseek(fid, nbytes * (last_good_index), 'bof'); %use ilist
      try
         [buf] = read_buf(instclass, fid, params, config);
      catch
         if (cfg.verbose)
            fprintf('exited read early: last good index is %d \n', last_index)
         end
         break
      end 
      
      if (~isstruct(buf))
         if (cfg.verbose)
            fprintf('buf returns %s\n',buf)
         end
         break
      end
      last_good_index = last_good_index + 1;
      if (last_good_index == last_index)
         break
      end
   end
   fclose(fid);
   
else     %  cfg.back_to_front is true 
   % the BB and OS processors write little-endian
   % the NB processor writes big-endian
   if isa(instclass, 'nb')
      fid = fopen(file, 'r', 'ieee-be');  
   else
      fid = fopen(file, 'r', 'ieee-le');  
   end
   
   fseek(fid, 0, 'eof');        % total number of bytes
   len_file = ftell(fid);       % bytes remaining in record up to but not 
   nbytes = config.nbytes + 2;  %  including the checksum, +2 for the checksum
   last_index = floor(len_file / nbytes); %%% This is a bug?? used to be      
                                              %%% floor(len_file / nbytes) - 1
   % revise the last good index
   for last_good_index = last_index:-1:1
      buf = [];
      fseek(fid, nbytes * (last_good_index - 1), 'bof'); %use ilist
      if (cfg.verbose), last_good_index, end
      try
         [buf] = read_buf(instclass, fid, params, config);
      catch
         if (cfg.verbose)
            fprintf('exited read early at index %d (out of %d):\n', ...
                    last_good_index,  last_index)
         end
      end 
      if (~isstruct(buf))
         if (cfg.verbose)
            fprintf('buf returns %s\n',buf)
         end
      elseif isstruct(buf)
         break
      else
         warning('buf is not struct or string')
      end
   end
   
   % update these things
   fclose(fid);
end

last_index = last_good_index;
