Main Page   Compound List   File List   Compound Members   File Members  

rr_libs/rr_iopen.c

Go to the documentation of this file.
00001 /* rr_iopen(), rr_iclose():  a generalized input file opener */
00002 /*=====================================================================
00003                 =======   COPYRIGHT NOTICE   =======
00004 Copyright (C) 1994, Carnegie Mellon University and Ronald Rosenfeld.
00005 All rights reserved.
00006 
00007 This software is made available for research purposes only.  It may be
00008 redistributed freely for this purpose, in full or in part, provided
00009 that this entire copyright notice is included on any copies of this
00010 software and applications and derivations thereof.
00011 
00012 This software is provided on an "as is" basis, without warranty of any
00013 kind, either expressed or implied, as to any matter including, but not
00014 limited to warranty of fitness of purpose, or merchantability, or
00015 results obtained from use of this software.
00016 ======================================================================*/
00017 
00022 /* Open for input a file which may potentially be Z compressed */
00023 /* If pathanme ends in ".Z", assume the file is compressed.
00024    Otherwise, look for it and assume it is uncompressed.
00025               If not found, append .Z and look again, assuming compressed */
00026 /* If pathname is "-", use stdin and assume it is uncompressed */
00027 
00028 /* Unforgiving: quit if open() fails */
00029 /* Also entry point for closing the associated stream */
00030 
00031 /*****************************************************************
00032 
00033   Modified by Philip Clarkson 1/10/96 to allow gzipped files also. 
00034 
00035 *****************************************************************/
00036 
00037 /* Edited by Philip Clarkson, March 1997 to prevent compilation warnings */
00038 
00039 #include <stdio.h>
00040 #include "general.h"
00041 #include "strings.h"
00042 char  RRi_is_Z[100];
00043 
00044 FILE *rr_iopen(char *path)
00045 {
00046   static char rname[]="rr_iopen";
00047   FILE *fp;
00048   char pipe[256], is_Z;
00049   int  lpath;
00050 
00051   if (strcmp(path,"-")==0) return(stdin);
00052 
00053   lpath = strlen(path);
00054   if (lpath > sizeof(pipe) - strlen("cat | gunzip ") - 4)
00055     quit(-1,"%s: pathname '%s' is too long\n",rname,path);
00056 
00057   if (strcmp(&path[lpath-2],".Z")==0) {
00058      /* popen() does not report error if file doesn't exist, so: */
00059      if (!rr_fexists(path)) quit(-1,"%s: file '%s' not found\n",rname,path);
00060      sprintf(pipe,"zcat %s",path);
00061      goto Z;
00062   }
00063 
00064   else if (strcmp(&path[lpath-3],".gz")==0) {
00065      /* popen() does not report error if file doesn't exist, so: */
00066      if (!rr_fexists(path)) quit(-1,"%s: file '%s' not found\n",rname,path);
00067      sprintf(pipe,"cat %s | gunzip",path);
00068      goto Z;
00069   }
00070 
00071   else if (!rr_fexists(path)) {
00072      sprintf(pipe,"%s.Z",path);
00073      /* popen() does not report error if file doesn't exist, so: */
00074      if (!rr_fexists(pipe)) {
00075        sprintf(pipe,"%s.gz",path);
00076        if (!rr_fexists(pipe)) {
00077          quit(-1,"%s: None of '%s' '%s.Z' or '%s.gz' exist.\n",rname,path,path,path);
00078        }
00079        sprintf(pipe,"cat %s.gz | gunzip",path);
00080        goto Z;
00081      }
00082      sprintf(pipe,"zcat %s.Z",path);
00083      goto Z;
00084   }
00085   else {
00086      fp = rr_fopen(path,"r");
00087      is_Z = 0;
00088      goto record;
00089   }
00090 
00091 Z:
00092   fp = popen(pipe,"r");
00093   if (!fp) quit(-1,"%s: problems opening the pipe '%s' for input.\n", rname,pipe);
00094   is_Z = 1;
00095 
00096 record:
00097   if (fileno(fp) > sizeof(RRi_is_Z)-1) quit(-1,"%s: fileno = %d is too large\n",rname,fileno(fp));
00098   RRi_is_Z[fileno(fp)] = is_Z;
00099 
00100   return(fp);
00101 }
00102 
00103 void *rr_iclose(FILE *fp)
00104 {
00105   if (fp==stdin) return(0);
00106   else if (RRi_is_Z[fileno(fp)]) pclose(fp);
00107   else fclose(fp);
00108 
00109   return(0); /* Not relevant, but stops compilation warnings. */
00110 
00111 }

Generated on Tue Dec 21 13:54:46 2004 by doxygen1.2.18