// file: $USFS/util/segment_image/v1.0/si_gfs_1.cc
//

// system include file
//
#include <string.h>

// isip include files
//
#include "segment_image.h"
#include "segment_image_constants.h"

// method: get_next_segfname_cc
//
// arguments:
//   char_1*& seg_file: (output) next segmentation file name
//   char_1* image_file: (input) the corresponding image file name
//   char_1* seg_filelist: (input) segmentation file list
//
// return:
//  a logical_1 value indicating status
//
// this method finds the segmentation file name from the segmentation file
// list according to the given image file name
//
logical_1 get_next_segfname_cc(char_1*& seg_file_a, char_1* image_file_a,
			       char_1* seg_filelist_a) {

  // declare local variables
  //
  char_1* temp_buf = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* seg_file = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* first_line = new char_1[ISIP_MAX_STRING_LENGTH];
  logical_1 stop = ISIP_FALSE;

  memset(temp_buf, (int)0, ISIP_MAX_STRING_LENGTH);
  memset(seg_file, (int)0, ISIP_MAX_STRING_LENGTH);
  memset(first_line, (int)0, ISIP_MAX_STRING_LENGTH);
    
  FILE* fp_list = fopen((char*)seg_filelist_a, "r");
  if (fp_list == (FILE*)NULL) {
    fprintf(stdout, "%s: error opening the file\n", seg_filelist_a);
    return ISIP_FALSE;
  }

  Ppm ppm;
  
  // read segmentation file name one at a time from the file list
  //
  while ((fgets((char*)temp_buf, ISIP_MAX_STRING_LENGTH, fp_list)
	 != (char*)NULL) && (stop == ISIP_FALSE)) {

    // cut off the newline char
    //
    temp_buf[strlen((char*)temp_buf)-1] = ISIP_NULL;
	
    // check for a comment line and/or blank line
    //
    char_1* nonwhitespace = new char_1[2];
	
    if ((temp_buf[0] != ISIP_POUND) &&
	(sscanf((char*)temp_buf, "%1s", nonwhitespace) > (int)0)) {	  

      // expand the file name
      //
      if (ppm.expand_cc(seg_file, temp_buf) == ISIP_FALSE) {
        fprintf(stdout, "%s: error in expanding the filename\n", temp_buf);
      }

      // open the segmentation file
      //
      FILE* fp_file = fopen((char*)seg_file, "r");
      if (fp_file == (FILE*)NULL) {
	fprintf(stdout, "%s: error opening the file\n", seg_file);
	return ISIP_FALSE;
      }

      // read the first line of the segmentation file
      //
      memset(temp_buf, (int)0, ISIP_MAX_STRING_LENGTH);
      fgets((char*)temp_buf, ISIP_MAX_STRING_LENGTH, fp_file);
      
      // cut off the newline char
      //
      temp_buf[strlen((char*)temp_buf)-1] = ISIP_NULL;

      // expand the file name
      //
      if (ppm.expand_cc(first_line, temp_buf) == ISIP_FALSE) {
        fprintf(stdout, "%s: error in expanding the filename\n", temp_buf);
      }
      
      // compare it with the image file name
      //
      if (strcmp((char*)first_line, (char*)image_file_a) == 0) {
        strcpy((char*)seg_file_a, (char*)seg_file);
        stop = ISIP_TRUE;
      }

      fclose(fp_file);
    }

    memset(temp_buf, (int)0, ISIP_MAX_STRING_LENGTH);
    memset(seg_file, (int)0, ISIP_MAX_STRING_LENGTH);
    memset(first_line, (int)0, ISIP_MAX_STRING_LENGTH);

    // delete memory
    //
    delete [] nonwhitespace;
  }

  // close file
  //
  fclose(fp_list);

  // delete memory
  //
  delete [] temp_buf;
  delete [] seg_file;
  delete [] first_line;
  
  // return
  //
  if (stop == ISIP_FALSE) {

    // no corresponding data file found
    //
    fprintf(stdout, "no corresonding data file found\n");
    return ISIP_FALSE;
  }
  else {
    
    // exit gracefully
    //
    return ISIP_TRUE;
  }
}
