! get_glyph_radius.MBS
! Get a glyph's radius from the list of ranges.

global drawing module get_glyph_radius(
    int glyph_wanted;
var int rc;
var int glyph_radius;
    int verbosity);

string homedir*132;
file   list_ranges_occidental;
int    lines_read;
int    range_start_comma;
int    range_start;
int    range_stop_comma;
int    range_stop;
int    glyph_radius_comma;
int    name_comma;
string line*132;
string field*132;
int    i;

beginmodule

if verbosity >= 5 then
   lst_lin("get_glyph_radius: glyph wanted is " + str(glyph_wanted));
endif;

! open the list_ranges_occidental.txt file
homedir := get_environment("HOME");
open(list_ranges_occidental, "r", 
     homedir + "/hershey_data/list_ranges_occidental.txt");
rc := iostat(list_ranges_occidental);
if iostat(list_ranges_occidental) <> 0 then
   if verbosity > 0 then
   lst_lin("get_glyph_radius: open of file " +
            homedir + "/hershey_data/list_ranges_occidental.txt" + " failed");
   endif;
   rc := 1;
   glyph_radius := -1;
   goto fail;
endif;

! assume the worst
rc := 1;

lines_read := 0;

findloop:
   ! make sure we're not reading forever
   ! Assume the file is never more than 200 lines long,
   ! including all comment and blank lines.
   if lines_read > 200 then
      if verbosity >= 5 then  ! failure here is not really fatal overall
         lst_lin("get_glyph_radius: glyph " + 
                 str(glyph_wanted) + 
                 " not in any range " +
                 "read > 200 lines");
      endif;
      rc     := 1;
      glyph_radius := 0;
      goto fail;
   endif;

   ! read a line
   line := inlin(list_ranges_occidental);
   ! see if we ran off the end of the file
   ! if so, then the glyph doesn't exist
   if verbosity >= 9 then
      lst_lin(line);
   endif;
   if iostat(list_ranges_occidental) = -1 then
      if verbosity >= 5 then  ! failure here is not really fatal overall
      lst_lin("get_glyph_radius: glyph " + 
              str(glyph_wanted) + 
              " not in any range " +
              "read past end of file");
      endif;
      rc     := 1;
      glyph_radius := 0;
      goto fail;
   endif;
   ! see if we encountered some other read error
   if iostat(list_ranges_occidental) <> 0 then
      if verbosity > 0 then  
      lst_lin("get_glyph_radius: inlin() failed, iostat = " + 
              str(iostat(list_ranges_occidental)));
      endif;
      rc     := 1;
      glyph_radius := 0;
      goto fail;
   endif;

   ! skip this line if it is blank
   if length(line) = 0 then
      lines_read := lines_read + 1;
      goto findloop;
   endif;

   ! skip this line if it is a comment line
   ! comments are not free-form; the pound sign must be in column 1
   if substr(line,1,1) = "#" then
      lines_read := lines_read + 1;
      goto findloop;
   endif;

   ! find the range start glyph field, bypassing the range number field
   ! that is, find the location of the first comma
   range_start_comma := finds(line, ",");
   if range_start_comma = 0 then
      if verbosity > 0 then  
         lst_lin("get_glyph_radius: bad line format, first comma not found");
      endif;
      rc     := 1;
      glyph_radius := 0;
      goto fail;
   endif;

   ! find the range stop glyph field (find the second comma)
   range_stop_comma  := range_start_comma + 
                            finds(substr(line, range_start_comma + 1), ",");
   if range_stop_comma = 0 then
      if verbosity > 0 then  
         lst_lin("get_glyph_radius: bad line format, second comma not found");
      endif;
      rc     := 1;
      glyph_radius := 0;
      goto fail;
   endif;

   ! find the radius field (find the third comma)
   glyph_radius_comma  := range_stop_comma + 
                            finds(substr(line, range_stop_comma + 1), ",");
   if glyph_radius_comma = 0 then
      if verbosity > 0 then  
         lst_lin("get_glyph_radius: bad line format, third comma not found");
      endif;
      rc     := 1;
      glyph_radius := 0;
      goto fail;
   endif;

   ! find the name field (find the fourth comma)
   name_comma  := glyph_radius_comma + finds(substr(line, glyph_radius_comma + 1), ",");
   if name_comma = 0 then
      if verbosity > 0 then  
         lst_lin("get_glyph_radius: bad line format, fourth comma not found");
      endif;
      rc     := 1;
      glyph_radius := 0;
      goto fail;
   endif;

   ! read range start and stop glyph fields
   field := substr(line, range_start_comma + 1, 
                   range_stop_comma - range_start_comma - 1);
   range_start := ival(field);
   if verbosity >= 9 then  
      lst_lin("get_glyph_radius: range start = " + str(range_start));
   endif;
   ! read range stop glyph field
   field := substr(line, range_stop_comma + 1, 
                   glyph_radius_comma - range_stop_comma - 1);
   range_stop := ival(field);
   if verbosity >= 9 then  
      lst_lin("get_glyph_radius: range stop = " + str(range_stop));
   endif;

   ! see if we're in this range
   if glyph_wanted < range_start or glyph_wanted > range_stop then
      goto findloop;
   endif;
   ! we must be in this range

   ! read radius field
   field := substr(line, glyph_radius_comma + 1, 
                   name_comma - glyph_radius_comma - 1);

   ! check the radius field for sanity
   glyph_radius := ival(field);

   if glyph_radius <= 0 or glyph_radius > 50 then
      if verbosity > 0 then
         lst_lin("get_glyph_radius: invalid radius = " + field);
      endif;
      rc     := 1;
      glyph_radius := 0;
      goto fail;
   endif;
   
   ! report the radius
   glyph_radius := ival(field);

   ! correct the assumption of the worst
   rc := 0;

fail:

close(list_ranges_occidental);

endmodule


! Copyright 2004 by David M. MacMillan

! This work is free software; you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation; either version 2 of the License, or
! (at your option) any later version.

! NOTICE OF DISCLAIMER OF WARRANTY AND LIABILITY:

! This work is distributed WITHOUT ANY WARRANTY;
! without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
! See the GNU General Public License for more details.

! In no event will the author(s), editor(s), or publisher(s) of this work
! be liable to you or to any other party for damages,
! including but not limited to any general, special, incidental
! or consequential damages arising out of your use of or inability to use this
! work or the information contained in it, even if you have been advised
! of the possibility of such damages.

! In no event will the author(s), editor(s), or publisher(s) of this work
! be liable to you or to any other party for any injury, death,
! disfigurement, or other personal damage arising out of your use of
! or inability to use this work or the information
! contained in it, even if you have been advised of the
! possibility of such injury, death, disfigurement, or other
! personal damage.

! You should have received a copy of the GNU General Public License
! along with this work; if not, write to the
! Free Software Foundation, Inc., 59 Temple Place - Suite 330,
! Boston, MA  02111-1307, USA.

