function n = gpl_squares_count ( filename )

%*****************************************************************************80
%
%% gpl_squares_count() counts the number of squares in a GPL squares file.
%
%  Discussion:
%
%    The GPL file is assumed to consist of data defining (X,Y,Z(X,Y))
%    values for N rectangles, of the form:
%
%      ax1 ay1 az1
%      bx1 by1 bz1
%        (BLANK)
%      cx1 cy1 cz1
%      dx1 dy1 dz1    <-- End of square 1
%        (BLANK)
%        (BLANK)
%      ax2 ay2 az2
%      bx2 by2 bz2
%        (BLANK)
%      cx2 cy2 cz2
%      dx2 dy2 dz2    <-- End of square 1
%        (BLANK)
%        (BLANK)
%      ax3 ay3 az3
%      ...
%      axn ayn azn
%      bxn byn bzn
%      BLANK
%      cxn cyn czn
%      dxn dyn dzn    <-- End of square n
%
%    This function tries to determine the value of N, that is, the number
%    of sets of four data triples, contained in the file.
%  
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 July 2018
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string FILENAME, the name of the input file.
%
%    integer N, the number of sets of data
%
%  Output:
%
%    real X(4,N), Y(4,N), Z(4,N), the data.
%
  n = 0;

  file = fopen ( filename, 'rt' );

  line_num = 0;

  while ( true )
%
%  Pick up points 1 and 2.
%
    for i = 1 : 2

      while ( true )

        line_num = line_num + 1;
        s = fgetl ( file );
%
%  Unexpected end of file?
%
        if ( ~ ischar ( s ) )
          fprintf ( 1, '\n' );
          fprintf ( 1, 'GPL_SQUARES_COUNT:\n' );
          fprintf ( 1, '  Reached end of file reading line %d.\n', line_num );
          fprintf ( 1, '  Found %d rectangles.\n', n );
          fclose ( file );
          return
        end
%
%  Unexpected empty line?
%
        if ( length ( s ) < 1 )
          fprintf ( 1, '\n' );
          fprintf ( 1, 'GPL_SQUARES_COUNT - Fatal error!\n' );
          fprintf ( 1, '  Unexpected empty record, reading line %d\n', line_num );
          error ( 'GPL_SQUARES_COUNT - Fatal error!\n' );
        end
%
%  Comments are OK.
%
        if ( s(1) ~= '#' )
          break
        end

      end
%
%  Otherwise, X, Y, Z
%
      [ xyz, count ] = sscanf ( s, '%f%f%f' );
%
%  Failed to read 3 things?
%
      if ( count ~= 3 )
        fprintf ( 1, '\n' );
        fprintf ( 1, 'GPL_SQUARES_COUNT - Fatal error!\n' );
        fprintf ( 1, '  Did not get 3 data items on line %d\n', line_num );
        error ( 'GPL_SQUARES_COUNT - Fatal error!\n' );
      end

    end
%
%  Expect one blank separator line.
%
    line_num = line_num + 1;
    s = fgetl ( file );
    if ( 1 <= length ( s ) )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'GPL_SQUARES_COUNT - Fatal error!\n' );
      fprintf ( 1, '  Unexpected nonempty record at line %d\n', line_num );
      error ( 'GPL_SQUARES_COUNT - Fatal error!\n' );
    end
%
%  Pick up points 4 and 3.
%
    for i = 4 : -1 : 3

      while ( true )

        line_num = line_num + 1;
        s = fgetl ( file );
%
%  Unexpected end of file?
%
        if ( ~ ischar ( s ) )
          fprintf ( 1, '\n' );
          fprintf ( 1, 'GPL_SQUARES_COUNT - Fatal error!\n' );
          fprintf ( 1, '  Unexpected end of file reading line %d\n', line_num );
          error ( 'GPL_SQUARES_COUNT - Fatal error!\n' );
        end
%
%  Unexpected empty line?
%
        if ( length ( s ) < 1 )
          fprintf ( 1, '\n' );
          fprintf ( 1, 'GPL_SQUARES_COUNT - Fatal error!\n' );
          fprintf ( 1, '  Unexpected empty record, reading line %d\n', line_num );
          error ( 'GPL_SQUARES_COUNT - Fatal error!\n' );
        end
%
%  Comments are OK.
%
        if ( s(1) ~= '#' )
          break
        end

      end
%
%  Otherwise, X, Y, Z
%
      [ xyz, count ] = sscanf ( s, '%f%f%f' );
%
%  Failed to read 3 things?
%
      if ( count ~= 3 )
        fprintf ( 1, '\n' );
        fprintf ( 1, 'GPL_SQUARES_COUNT - Fatal error!\n' );
        fprintf ( 1, '  Did not get 3 data items on line %d\n', line_num );
        error ( 'GPL_SQUARES_COUNT - Fatal error!\n' );
      end

    end

    n = n + 1;
%
%  Read two blank separator lines.
%
    for i = 1 : 2
      line_num = line_num + 1;
      s = fgetl ( file );
      if ( 1 <= length ( s ) )
        fprintf ( 1, '\n' );
        fprintf ( 1, 'GPL_SQUARES_COUNT - Fatal error!\n' );
        fprintf ( 1, '  Unexpected nonempty record at line %d\n', line_num );
        error ( 'GPL_SQUARES_COUNT - Fatal error!\n' );
      end
    end

  end

  return
end
