ftp.nice.ch/pub/next/unix/graphics/imtools.2.0.N.bs.tar.gz#/imtools/src/imfile.c

This is imfile.c in view mode; [Download] [Up]

/**
 **	$Header: /import/dev-vis/image/imtools/v2.0/imtools/src/RCS/imfile.c,v 1.3 91/10/03 13:20:21 nadeau Exp $
 **	Copyright (c) 1989, 1990  San Diego Supercomputer Center (SDSC)
 **		San Diego, California, USA
 **
 **	Users and possessors of this source code are hereby granted a
 **	nonexclusive, royalty-free copyright and design patent license to
 **	use this code in individual software.  License is not granted for
 **	commercial resale, in whole or in part, without prior written
 **	permission from SDSC.  This source is provided "AS IS" without express
 **	or implied warranty of any kind.
 **
 **	For further information contact:
 **		E-Mail:		info@sds.sdsc.edu
 **
 **		Surface Mail:	Information Center
 **				San Diego Supercomputer Center
 **				P.O. Box 85608
 **				San Diego, CA  92138-5608
 **				(619) 534-5000
 **/

#define HEADER	"    $Header: /import/dev-vis/image/imtools/v2.0/imtools/src/RCS/imfile.c,v 1.3 91/10/03 13:20:21 nadeau Exp $"

/**
 **  FILE
 **	imfile.c	-  Image file known format query
 **
 **  PROJECT
 **	IM		-  Image Manipulation Tools
 **
 **  DESCRIPTION
 **	imfile opens the file or files and tries to figure out what kind of
 **	image file they are.
 **
 **  PUBLIC CONTENTS
 **			d =defined constant
 **			f =function
 **			m =defined macro
 **			t =typedef/struct/union
 **			v =variable
 **			? =other
 **
 **	main		f  main program
 **
 **  PRIVATE CONTENTS
 **	toolCommand	g  command info for arg parsing pkg
 **	toolOptions	g  option info for arg parsing pkg
 **
 **  HISTORY
 **	$Log:	imfile.c,v $
 **	Revision 1.3  91/10/03  13:20:21  nadeau
 **	Update to version 2.0 of the Arg parsing package.
 **	
 **	Revision 1.2  91/03/11  14:38:43  nadeau
 **	Updated comments and updated to use new file formats table.
 **	
 **	Revision 1.1  90/07/02  13:16:37  nadeau
 **	Initial revision
 **	
 **	Revision 1.5  90/05/11  14:27:18  nadeau
 **	Removed old ifdefed dispatch table stuff and added more comments.
 **	
 **/

#include "imtools.h"





/*
 *  GLOBALS
 *	toolCommand		-  command info for arg parsing pkg
 *	toolOptions		-  option info for arg parsing pkg
 *
 *  DESCRIPTION
 *	toolCommand describes the command and gives the help text.
 *
 *	toolOptions lists the arg options accepted.
 */

private ArgCommand toolCommand =
{
	/* command name */		"imfile",

	/* major version # */		IMTOOLSMAJOR,
	/* minor version # */		IMTOOLSMINOR,
	/* subminor version # */	IMTOOLSSUBMINOR,

	/* -help pre-option list information				*/
"%command determines the type of image file format used by each file on its\n\
command line and prints it to stdout.\n\
",

	/* -help post-option list information				*/
	NULL,				/* Filled in later on		*/

	/* -fullhelp pre-option list information			*/
	NULL,				/* Use same message as for -help*/
	/* -fullhelp post-option list information			*/
	NULL,				/* Use same message as for -help*/

	ARGFNONE,			/* No special flags		*/
	"[options...] filenames...",
	"[options...] filenames...",
	"SDSC Image Tools, October 1991.",
	"Copyright (c) 1989-1991  San Diego Supercomputer Center (SDSC), CA, USA",
	NULL,				/* filled in later on		*/
	NULL,				/* filled in later on		*/
};

private char *toolHelp = "\n\
Typical Invocations:\n\
    List file formats for all files in the current directory:\n\
        %command *\n\
";

private char *toolFullHelp = "\n\
Output:\n\
    Output is a table giving the file name, its format name, and a description\n\
    of that format.  If %command cannot figure out the type of image file, the\n\
    words 'Unknown image file format' are printed beside the file name.\n\
";

private char *toolNote = "\n\
Additional Help:\n\
    This is an abbreviated help listing.  For a full listing of options,\n\
    including a list of image file formats supported, type:\n\
        %command -fullhelp\n\
";

#define TOOLNOPTIONS	1
private ArgOption toolOptions[TOOLNOPTIONS] =
{
	{ "infile", "filenames...", "Names of files to query",
	  ARGFIMPKEYWORD|ARGFREQUIRED, 1, ARGVNOMAX, ARGTSTRING },
};

#define TOOLNEQUIVS	0
#if TOOLNEQUIVS == 0
private ArgEquiv *toolEquivs;
#else
private ArgEquiv toolEquivs[TOOLNEQUIVS] =
{
};
#endif





/*
 *  FUNCTION
 *	main	-  main program
 *
 *  DESCRIPTION
 *	The incomming argument list is walked and each file checked
 *	to see what kind of image file format it uses.
 */

main( argc, argv )
	int argc;			/* Argument count		*/
	char *argv[];			/* Argument vector		*/
{
	int           i;		/* Counter			*/
	int	      numFiles;		/* Number of file names		*/
	char         *fileName;		/* Input file name		*/
	char         *formatName;	/* Input file's format		*/
	char        **pNames;		/* Format name list pointer	*/
	ImFileFormat *pFmt;		/* Format list pointer		*/
	int           fd;		/* File descriptor		*/
	char          buffer[1024];	/* Output buffer		*/
	char	     *tmp;		/* Temp string			*/


	/*
	 *  Use the standard Image Tools user registration and feedback forms.
	 */
	toolCommand.arg_register = ImToolsRegister;
	toolCommand.arg_feedback = ImToolsFeedback;


	/*
	 *  Allocate space for the total help string for the tool.  Copy the
	 *  tool-specific help in, then concatenate on the generic help text
	 *  used by most of the image tools.
	 */
	if ( (tmp = (char *)malloc( sizeof( char ) * (strlen( toolNote ) +
		strlen( toolHelp ) + 1) )) == NULL )
	{
		perror( ImToolsProgram );
		exit( 1 );
	}
	strcpy( tmp, toolHelp );
	strcat( tmp, toolNote );
	toolCommand.arg_help2 = tmp;

	if ( (tmp = (char *)malloc( sizeof( char ) * ( strlen( toolHelp ) +
		strlen( toolFullHelp ) + 1) )) == NULL )
	{
		perror( ImToolsProgram );
		exit( 1 );
	}
	strcpy( tmp, toolHelp );
	strcat( tmp, toolFullHelp );
	toolCommand.arg_fullhelp2 = tmp;


	/*
	 *  Parse arguments.
	 */
	ImToolsProgram = argv[0];
	ArgParse( argc, argv, &toolCommand, TOOLNOPTIONS, toolOptions,
		TOOLNEQUIVS, toolEquivs );
	numFiles = ArgQNValue( "infile", 0 );


	/*
	 *  List the files and the formats they use.
	 */
	for ( i = 0; i < numFiles; i++ )
	{
		fileName = ArgQValue( "infile", 0, i )->arg_s;
		sprintf( buffer, "%s:", fileName );

		if ( (fd = open( fileName, O_RDONLY )) == -1 )
		{
			printf( "%-16ss Cannot open file\n", buffer );
			continue;
		}

		formatName = ImFileQFormat( fd, fileName );
		if ( formatName == NULL )
		{
			printf( "%-16s Unknown image file format\n", buffer );
			close( fd );
			continue;
		}

		for ( pFmt = ImFileFormats; pFmt->format_names; pFmt++ )
			if ( strcmp( pFmt->format_names[0], formatName ) == 0 )
				break;
		printf( "%-16s '%s', %s, %s\n", buffer, formatName,
			pFmt->format_help, pFmt->format_creator );
		close( fd );
	}
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.