! read_glyph.MBS
! Read a single Hershey glyph's data.
!
! Open the file data_glyphs_occidental.txt, which contains the Hershey 
! occidental glyph repertory in Hurt's encoding, and extract from it 
! the glyph data for the indicated glyph number.
! Place the results in one var int parameter containing the number
! of pairs and three var string parameters containing the data
! (three because the ! maximum VARKON string length is 132, 
! while the longest string in the ! glyph data is 294 bytes)
! Discard the glyph number information (as the caller already knows it).

global drawing module read_glyph(
    int    glyph_wanted;
var int    number_of_data_pairs;
var int    data_pairs(300);
var int    rc;
    int    verbosity);

file   glyph_data;
string this_glyph_string*5;
int    this_glyph;
string glyph_length_string*3; ! length in bytes, not data pairs
int    glyph_length;
int    lines_read;            ! a safety valve
int    ix, iy;

string line_part_1*128;
string line_part_2*128;
string line_part_3*128;

string homedir*132;

beginmodule

rc := 0;

! zero out the data_pairs array
for ix := 1 to 300 do
   data_pairs(ix) := 0;
endfor;

! open the data file
homedir := get_environment("HOME");
open(glyph_data, "r", homedir + "/hershey_data/data_glyphs_occidental.txt");
if iostat(glyph_data) <> 0 then
   if verbosity > 0 then
      lst_lin("read_glyph: open(" + 
              homedir + 
              "/hershey_data/data_glyphs_occidental.txt) failed, iostat = " + 
              str(iostat(glyph_data)));
   endif;
   rc := 1;
   goto fail;
endif;

! read lines until we find the glyph, then process it
this_glyph := 0;
lines_read := 0;
findloop:
   line_part_1 := instr(glyph_data,128);     ! don't use inlin()
   if iostat(glyph_data) <> 0 then
      if verbosity > 0 then
         lst_lin("read_glyph: first instr failed, iostat = " + 
                 str(iostat(glyph_data)));
      endif;
      rc := 1;
      goto fail;
   endif;

   ! is this line the glyph desired?
   this_glyph_string := substr(line_part_1,1,5);
   this_glyph        := ival(this_glyph_string);
   if this_glyph <> glyph_wanted then !try again
      ! safety check
      ! assume that the input file (hgoc, not hgocnotes) can't exceed 5000 lines
      if lines_read > 5000 then
         if verbosity > 0 then
            lst_lin("read_glyph: failed to find glyph" + str(glyph_wanted) + 
                               "after reading over 5000 lines");
         endif;
         rc := 1;
         goto fail;
      endif;
      lines_read := lines_read + 1;
      goto findloop;
   endif;

   ! we must be at the desired glyph

   ! determine how many data pairs there are
   ! (data pairs include the initial left and right side pair
   glyph_length_string  := substr(line_part_1,6,3);
   number_of_data_pairs := ival(glyph_length_string);
   glyph_length         := number_of_data_pairs * 2;

   line_part_1 := substr(line_part_1,9,length(line_part_1) - 8);

   if glyph_length < 120 then
      ! chop off the final newline
      line_part_1 := substr(line_part_1,1,length(line_part_1) - 1);
   endif;

   ! copy line_part_1 string into the data_pairs int array
   for ix := 1 to length(line_part_1) do
      data_pairs(ix) := ascii(substr(line_part_1,ix,1));
   endfor;

   if glyph_length > 120 then
      line_part_2 := instr(glyph_data,128);
      if iostat(glyph_data) <> 0 then
         if verbosity > 0 then
            lst_lin("read_glyph: second instr failed, iostat = " + 
                    str(iostat(glyph_data)));
         endif;
         rc := 1;
         goto fail;
      endif;
      ! if the last character is a newline, chop it off
      if substr(line_part_2,length(line_part_2),1) = chr(10) then
         line_part_2 := substr(line_part_2,1,length(line_part_2) - 1);
      endif;
      iy := 1;
      for ix := length(line_part_1) + 1 to 
                length(line_part_1) + 1 + length(line_part_2) do
         data_pairs(ix) := ascii(substr(line_part_2,iy,1));
         iy := iy + 1;
      endfor;
   endif;

   if glyph_length > (120 + 128) then
      line_part_3 := instr(glyph_data,128);
      if iostat(glyph_data) <> 0 then
         if verbosity > 0 then
            lst_lin("read_glyph: third instr failed, iostat = " + 
                    str(iostat(glyph_data)));
         endif;
         rc := 1;
         goto fail;
      endif;
      ! if the last character is a newline, chop it off
      if substr(line_part_3,length(line_part_3),1) = chr(10) then
         line_part_3 := substr(line_part_3,1,length(line_part_3) - 1);
      endif;
      iy := 1;
      for ix := 249 to 249 + length(line_part_3) do
         data_pairs(ix) := ascii(substr(line_part_3,iy,1));
         iy := iy + 1;
      endfor;
   endif;

fail:

close(glyph_data);

endmodule

! Copyright 2003, 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.

