// file: $USFS/class/image_eval/v1.0/imgev_load_0.cc
//

// system include files
//
#include <stdio.h>
#include <string.h>

// isip include files
//
#include "image_eval.h"

#include <integral_constants.h>
#include "image_eval_constants.h"

// method: load_cc
//
// arguments:
//  char_1**& fnames_a: (output) buffer containing image names 
//  float_4*& values_a: (output) buffer containing scores
//  int_4& num_item_a: (output) number of items in the buffer
//  int_4 array_size_a: (input) size of buffer
//  char_1* fname_in_a: (input) name of file from which to read data
//
// return:
//  logical_1 to indicate status
//
// This method loads in the filenames and the scores from a given
// text file into filename buffer and score buffer.  If the file doesn't
// exist, method calls error_handler and exits.
//
//
logical_1 Image_eval::load_cc(char_1**& fnames_a, float_4*& values_a,
			      int_4& num_items_a, int_4& array_size_a,
			      char_1* fname_in_a) {

  // open file for reading
  //
  FILE* fp = fopen((char*)fname_in_a, "r");
    
  // check if there is an error in opening the file
  //
  if (fp == (FILE*)NULL) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"cannot open file");
    return ISIP_FALSE;
  }

  // allocate initial space
  //
  num_items_a = (int_4)0;
  array_size_a = (int_4)IMGEV_INITIAL_ARRAY_SIZE;
  fnames_a = new (char_1*)[array_size_a];
  values_a = new (float_4)[array_size_a];

  // read the data into the buffer
  //
  char_1 tmp_buff[ISIP_MAX_STRING_LENGTH];
  while (fgets((char*)tmp_buff, ISIP_MAX_STRING_LENGTH,fp) != (char*)NULL) {

    // check for comment line and/or blank line
    //
    char_1 nonwhitespace[2];
    
    if ((tmp_buff[0] != IMGEV_COMMENT_CHAR) &&
        (sscanf((char*)tmp_buff, "%1s", nonwhitespace) > (int)0)) {

      // scan the data
      //
      char_1 tmp_fname[ISIP_MAX_STRING_LENGTH];
      float_4 tmp_value;
      
      // make sure for each number of filename there is a corresponding score
      //
      if (sscanf((char*)tmp_buff, IMGEV_ENTRY_FORMAT,
		 (char*)tmp_fname, &tmp_value) != IMGEV_NUM_ENTRIES) {
	error_handler_cc((char_1*)"load_cc",
			 (char_1*)"a filename or score is missing");
	return ISIP_FALSE;
      }
      
      // make sure we have enough space for new data
      // 
      if (num_items_a == array_size_a) {
	int_4 tmp_size = array_size_a;

	// always return ISIP_TRUE no need to check for error status
	//
	grow_arrays_cc(fnames_a, tmp_size, IMGEV_ARRAY_GROW_SIZE);
	grow_arrays_cc(values_a, array_size_a, IMGEV_ARRAY_GROW_SIZE);
      }
      
      // add the new data
      //
      fnames_a[num_items_a] = new char_1[strlen((char*)tmp_fname)+1];
      strcpy((char*)fnames_a[num_items_a], (char*)tmp_fname);
      values_a[num_items_a] = tmp_value;
      
      // check for debug level
      //
      if (debug_level_d > ISIP_DEBUG_NONE) {
	fprintf(stdout, IMGEV_DEBUG_FORMAT_A, IMAGE_EVAL_CLASS_NAME,"load_cc",
		num_items_a, fnames_a[num_items_a], values_a[num_items_a]);
      }
      
      // increment counters
      //
      num_items_a++;
    }
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}






