CCL Home Page
Up Directory CCL cfmmio
/* Copyright 1995, Columbia University, all rights reserved.
 * Permission is granted to utilize and disseminate this code or
 *  document without charge, provided that (1) this copyright notice is 
 *  not removed, and (2) all changes made by other than members of the 
 *  MacroModel Development Group at Columbia University are, if further 
 *  disseminated, (a) noted as such; for example, by means of source-code 
 *  comment lines, and (b) communicated back to the author for possible 
 *  inclusion in subsequent versions. */
/****************************************************************************
 * $RCSfile: cfmmio.c,v $
 * $Revision: 1.19 $
 * $Date: 1996/04/08 17:29:31 $
 ***************************************************************************/

/* cfmmio: read mmod files from input and copy them to output.
 * Usage: cfmmio [-cf]  
 * if the "-f" option is given, always write full CTs on output even if 
 *  compressed CTs are present in input.  This is the default, and
 *  will uncompress a compressed mmod file.
 * if "-c" is specified, then compressed CTs will be written to the output
 *  whenever they appear in the input.  This amounts to a copy of the
 *  input file.
 * cfmmio does not compress full CTs.
 * cfmmio has two purposes: to provide an example of how to use the mmio API,
 *  and to make sure that some simple facilities of the mmio library are
 *  working correctly. cfmmio always gives verbose output, which would not
 *  be desirable in a "real" application. */

#include "mmio.h"
#if defined( GETOPT_STDLIB )
	#include 
#elif defined( GETOPT_UNISTD )
	#include 
#elif defined( GETOPT_GETOPT )
	#include 
#endif

#define err() \
 printf( "ERR encountered, file %s, line %d\n", __FILE__, __LINE__ )

static void usage( void );
/************************************************************************/
main( int narg, char *arg[] )
{
	int status;
	int ict, iatom, natom;
	char title[ MMIO_L_STRLEN + 1];
	char readfname[200], writefname[200];

	/* for malloc() cleanliness: */
	char stdout_buf[BUFSIZ + 8];

	int mmod_iatom;
	int itype;
	int nbond;
	int bond_atom[ MMIO_MAXBOND ];
	int bond_order[ MMIO_MAXBOND ];
	float xyz[ 3 ];
	float charge1;
	float charge2;
	char chain;
	int color;
	int resnum;
	char resname1;
	char resname4[ 5 ];
	char pdbname[ 5 ];
	int ct_type_requested = MMIO_FULL;
	int opt;
	extern int optind;
	int iread, iwrite;

	/* line buffer stdout, in case of problems: */
	setvbuf( stdout, stdout_buf, _IOLBF, BUFSIZ );

	/* parse cmdline options: */
	while( (opt=getopt(narg,arg,"cf")) != -1 ) {
		switch( opt ) {
			case 'c':
				ct_type_requested = MMIO_COMPRESSED;
				break;
			case 'f':
				ct_type_requested = MMIO_FULL;
				break;
			default:
				usage();
				return 1;
		}
	}

	/* make sure that two cmdline args remain after any options: */
	if( narg-optind != 2 ) {
		usage();
		return 1;
	}

	/* get input filename from first remaining cmdline arg: */
	strcpy( readfname, arg[optind++] );
	printf( "cfmmio main: readfname= '%s'\n", readfname );

	/* get output filename from second remaining cmdline arg: */
	strcpy( writefname, arg[optind++] );
	printf( "cfmmio main: writefname= '%s'\n", writefname );
	
	/* tell mmio library to write its errmsgs, if any, to stdout: */
	mmio_errfile( stdout );
	printf( "cfmmio main: mmio_errfile returns %s\n", "successfully" );

	/* open input file for reading; note use of mmio_return_code() to
	 *  give ASCII equivalent of the return status: */
	status = mmio_open( &iread, readfname, MMIO_READ );
	printf( "cfmmio main: mmio_open() returns %s for %s\n",
	 mmio_return_code(status), readfname );
	if( status == MMIO_ERR ) {
		err();
		return 1;
	}

	/* open output file for writing; */
	status = mmio_open( &iwrite, writefname, MMIO_WRITE );
	printf( "cfmmio main: mmio_open() returns %s for %s\n",
	 mmio_return_code(status), writefname );
	if( status == MMIO_ERR ) {
		err();
		return 1;
	}

	ict = 0;
	/* read structures, one by one, from input file & write to output: */
	while( 1 ) {

		/* request first CT: */
		status = mmio_get_ct( iread, ct_type_requested, &natom, title );
		printf( "cfmmio main: mmio_get_ct() returns %s for ict %d;"
		 " natom= %d\n", mmio_return_code(status), ict, natom );
		if( status == MMIO_ERR ) {
			err();
			return 1;
		}
		if( status == MMIO_EOF ) {
			/* no more structures to be read: */
			break;
		}
		printf( "cfmmio main:     natom=%d, title=\n'%s'\n",
		 natom, title );

		/* write out CT with whatever status mmio_get_ct()
		 *  returned; if ct_type_requested was MMIO_FULL,
		 *  status will be MMIO_FULL;  if ct_type_requested
		 *  was MMIO_COMPRESSED, status will be MMIO_COMPRESSED if
		 *  the input file contained a compressed CT, and MMIO_FULL
		 *  if the input file contained a full CT: */
		status = mmio_put_ct( iwrite, status, natom, title );
		printf( "cfmmio main: mmio_put_ct() returns %s; natom= %d\n",
		 mmio_return_code(status), natom );
		if( status == MMIO_ERR ) {
			err();
			return 1;
		}

		/* For each atom, get the atom info from the input file
		 *  and write it to the output file.
		 * Please note that the interleaving of gets and puts is
		 *  not a requirement of MMIO, but was just the most
		 *  convenient way to code this particular application: */
		for( iatom=0; iatom \n" );
}
/************************************************************************/
Modified: Thu Oct 17 17:09:46 1996 GMT
Page accessed 4412 times since Sat Apr 17 21:57:47 1999 GMT