! check_table.MBS
! Check the "list_tables_occidental.txt" file 
! for the existence of a single table, and get its name.

global drawing module check_table(
    int    table_wanted;
var int    rc;
var string table_name*132;
    int    verbosity);

string homedir*132;
file   list_tables_occidental;
int    lines_read;
int    first_comma_position;
string line*132;
string field*132;

beginmodule

if verbosity >= 5 then
   lst_lin("check_table: table wanted is " + str(table_wanted));
endif;

table_name := "NULL";

! Open the list_tables_occidental.txt file.
homedir := get_environment("HOME");
open(list_tables_occidental, "r", 
     homedir + "/hershey_data/list_tables_occidental.txt");
rc := iostat(list_tables_occidental);
if rc <> 0 then
   if verbosity > 0 then
      lst_lin("check_table: open of file " + 
               homedir + "/hershey_data/list_tabless_occidental.txt" + " failed");
   endif;
   goto fail;
endif;

! assume the worst
rc := 1;

lines_read := 0;

findloop:
   ! Make sure we're not reading forever, as a failsafe.
   ! Assume the file is never more than 500 lines long,
   ! including all comment and blank lines.
   if lines_read > 500 then
      if verbosity > 0 then
         lst_lin("check_table: table " + str(table_wanted) + " not found " +
                 "read > 500 lines");
      endif;
      goto fail;
   endif;

   ! read a line
   line := inlin(list_tables_occidental);
   ! see if we ran off the end of the file
   ! if so, then the table doesn't exist
   if iostat(list_tables_occidental) = -1 then
      if verbosity > 0 then
         lst_lin("check_table: table " + str(table_wanted,1,0) + " not found " +
                 "(read past end of table data file)");
      endif;
      goto fail;
   endif;
   ! see if we encountered some other read error
   if iostat(list_tables_occidental) <> 0 then
      if verbosity > 0 then
         lst_lin("check_table: inlin() failed, iostat = " + 
                 str(iostat(list_tables_occidental)));
      endif;
      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;

   ! figure out the first field's length, and check it for sensibility
   ! VARKON strings start at 1
   first_comma_position := finds(line,",");
   if first_comma_position > 2 then
      if verbosity > 0 then
         lst_lin("check_table: bad line format, first comma at " + 
                            str(first_comma_position));
      endif;
      goto fail;
   endif;

   ! the second field (table name) is the last, and goes to the end of the line
   ! There's not really a lot of error checking to do on a name.
   ! I could check to ensure that it's alphanumeric, but don't.
   table_name := substr(line,first_comma_position +1, length(line));

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

fail:

close(list_tables_occidental);

endmodule


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

