Main Page   Compound List   File List   Compound Members   File Members  

rr_libs/rr_oopen.c

Go to the documentation of this file.
00001 /* rr_oopen(), rr_oclose():  a generalized output 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 
00018 /* Open a file for output. */
00019 /* If pathanme ends in ".Z", prepare to write thru a 'compress' pipe.  */
00020 /* If pathname is "-", prepare to write to stdout (uncompressed) */
00021 
00022 /* Unforgiving: quit if open() fails */
00023 /* Also entry point for closing the associated stream */
00024 
00025 /*****************************************************************
00026 
00027   Modified by Philip Clarkson 1/10/96 to allow gzipped files also. 
00028 
00029 *****************************************************************/
00030 
00031 /* Edited by Philip Clarkson, March 1997 to prevent compilation warnings */
00032 
00033 
00034 #include <stdio.h>
00035 #include "general.h"
00036 #include "strings.h"
00037 char  RRo_is_Z[100];
00038 
00043 FILE *rr_oopen(char *path)
00044 {
00045   static char rname[]="rr_oopen";
00046   FILE *fp;
00047   char pipe[256], is_Z;
00048   int  lpath;
00049 
00050   if (strcmp(path,"-")==0) return(stdout);
00051 
00052   lpath = strlen(path);
00053   if (strcmp(&path[lpath-2],".Z")==0) {
00054     if (lpath > sizeof(pipe) - strlen("compress >!  ") - 4)
00055       quit(-1,"%s: pathname '%s' is too long\n",rname,path);
00056      sprintf(pipe,"compress > %s",path);
00057      fp = popen(pipe,"w");
00058      if (!fp) quit(-1,"%s: problems opening the pipe '%s' for output.\n", rname,pipe);
00059      is_Z = 1;
00060   }
00061   else {
00062     if (strcmp(&path[lpath-3],".gz")==0) {
00063       if (lpath > sizeof(pipe) - strlen("gzip >!  ") -4)
00064         quit(-1,"%s: pathname '%s' is too long\n",rname,path);
00065       sprintf(pipe,"gzip > %s",path);
00066       fp = popen(pipe,"w");
00067       if (!fp) quit(-1,"%s: problems opening the pipe '%s' for output.\n", rname,pipe);
00068       is_Z = 1;
00069     }
00070     else {
00071       fp = rr_fopen(path,"w");
00072       is_Z = 0;
00073     }
00074   }
00075 
00076   if (fileno(fp) > sizeof(RRo_is_Z)-1) quit(-1,"%s: fileno = %d is too large\n",rname,fileno(fp));
00077   RRo_is_Z[fileno(fp)] = is_Z;
00078 
00079   return(fp);
00080 }
00081 
00082 void *rr_oclose(FILE *fp)
00083 {
00084   if (fp==stdout) return(0);
00085   fflush(fp); 
00086   if (RRo_is_Z[fileno(fp)]) 
00087     pclose(fp);
00088   else
00089     fclose(fp);
00090 
00091   return(0); /* Not relevant, but stops compilation warnings. */
00092 
00093 }

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