// file: $USFS/class/image_analysis/v1.0/imaga_open_0.cc
//

// isip include files
//
#include "image_analysis.h"
#include <ppm.h>

#include <integral_constants.h>

// method: open_cc
//
// arguments:
//  char_1* filename: (input) name of the file
//
// return: a FILE* pointer to the file opened
//
// this method verifies and opens a file in the appropriate mode
//
FILE* Image_analysis::open_cc(char_1* filename_a, int_4 mode_a) {
  
  // declare an object of class Ppm
  //
  Ppm ppm;
  
  // expand the file name
  //
  char_1* expand_name = new char_1[ISIP_MAX_STRING_LENGTH];
  
  if (ppm.expand_cc(expand_name, filename_a) == ISIP_FALSE) {
    error_handler_cc((char_1*)"open_cc", (char_1*)"error expanding name");
    return ISIP_FALSE;
  }
  
  // open the file
  //
  FILE *fp= (FILE*)NULL;

  if (mode_a == ISIP_READ_ONLY) {
    if ((fp = fopen((char*)expand_name, "r")) == (FILE*)NULL) {
      error_handler_cc((char_1*)"open_cc", (char_1*)"error opening the file");
      return (FILE*)NULL;
    }
  }

  if (mode_a == ISIP_WRITE_ONLY) {
    if ((fp = fopen((char*)expand_name, "w")) == (FILE*)NULL) {
      error_handler_cc((char_1*)"open_cc", (char_1*)"error opening the file");
      return (FILE*)NULL;
    }
  }
  
  // save the filename
  //
  if (param_fname_d != (char_1*)NULL) {
    delete [] param_fname_d;
  }
  param_fname_d = new char_1[strlen((char*)filename_a) + 1];
  strcpy((char*)param_fname_d, (char*)filename_a);

  // delete memory
  //
  delete [] expand_name;
  
  // exit gracefully
  //
  return fp;
}
