
/*
	file: io.lib.c
	common io routines used in VADAR 
*/


#include "stdvadar.h"

void get_seq_name(), show_msg();
FILE *open_file();


/*************************************************************/
/* Find the parameter file for this program. 
*/
FILE *get_parms_file(userFname)
char *userFname;
{
	int 	option;
	char 	aStr[MAX_LINE_LEN];
	char	parmsFile[MAX_FNAME_LEN];
	FILE	*fp;

	fp = fopen(userFname, "r");

	if (fp == NULL) {
		sprintf(parmsFile, "%s/vadar.parms", LIB_PATH);
		fp = fopen(parmsFile, "r");
	}

	while (fp == NULL) {
		show_msg("\n Program cannot find the parameter file.");
		show_msg("\n    1) I wish to enter a parameter file now.");
		show_msg("\n    2) I wish to exit this program.");
		show_msg("\n");
		show_msg("\n>> ");
		fscanf(stdin, "%s", aStr);
		option = atoi(aStr);
		switch (option) {
			case 1:		show_msg("\n Enter name of the parameters file: ");
						fscanf(stdin, "%s", parmsFile);
						fp = open_file(parmsFile, "r");
						break;
			case 2:		return(NULL);
			default:	show_msg("\n Error, illegal menu option.\n\n");
		}
	}

	return(fp);

}	/* get_parms_file */


/*************************************************************/
/* read parameter file until we come to the next prompt. 
*/
int read_prompt(fp)
FILE	*fp;
{
	int 	ch1, ch2;

	ch2 = 0;
	while (ch2 != EOF) {
		ch1 = ch2;
		ch2 = getc(fp);
		if ((ch1 == PARMS_SCH) && (ch2 == PARMS_SCH))
			return(TRUE);
	}

	return(FALSE);

}	/* read_prompt */


/*************************************************************/
/* setup input file 
*/
void get_input_fname(inputFname)
char	*inputFname;
{
char sysstr[MAX_LINE_LEN];

	strcpy(sysstr, "ls -C *.ent *.pdb 2> /dev/null");
	printf("\n**  PDB files in this directory:\n");
	system(sysstr);
	printf("\n");

	printf("\n>>  Enter file name of PDB coordinate file: ");
	fscanf(stdin, "%s", inputFname);

	while ( !checkExist(inputFname) ) {
		show_msg("\n** File does not exist.\n");
		printf(">>  Enter file name of PDB coordinate file: ");
		fscanf(stdin, "%s", inputFname);
	}

}	/* get_input_fname */


/*************************************************************/
/* check to see if a file exists
*/
int checkExist(fname)
char    *fname;
{
FILE    *fp;

	fp = fopen(fname, "r");
	if ( fp  == NULL )
		return(FALSE);
	fclose(fp);
	return(TRUE);

}	/* checkExist */


/*********************************************************************************
* PURPOSE:  to make the user hit return to continue
*/
void hold_screen()
{
char line[MAX_LINE_LEN];

	 printf("\n");
	 printf("     *******************************\n");
	 printf("     *** Press <RET> to Continue ***\n");
	 printf("     *******************************\n");
	 fgets(line, MAX_LINE_LEN, stdin);

}	/* hold_screen */
