Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
OpenFile.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file OpenFile.c
5 
6  \version V1.23
7  \date 07.07.14
8  \brief
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1991-2014
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39 
40 **************************************************************************
41 
42  Usage:
43  ======
44 
45 **************************************************************************
46 
47  Revision History:
48  =================
49 - V1.1 08.02.91 Added KillLine()
50 - V1.2 10.02.91 Added setextn() and index()
51 - V1.3 20.03.91 Added Word()
52 - V1.4 28.05.92 ANSIed
53 - V1.5 22.06.92 Added tab check to Word(). Improved setextn().
54  Added WordN(). Documented other routines.
55 - V1.6 27.07.93 Corrected fsscanf() for double precision
56 - V1.7 07.10.93 Checks made on case before toupper()/tolower()
57  for SysV compatibility. Also index() becomes
58  chindex()
59 - V1.8 18.03.94 getc() -> fgetc()
60 - V1.9 11.05.94 Added GetFilestem(), upstrcmp(), upstrncmp() &
61  GetWord()
62 - V1.10 24.08.94 Added OpenStdFiles()
63 - V1.11 08.03.95 Corrected OpenFile() for non-UNIX
64 - V1.12 09.03.95 Added check on non-NULL filename in OpenFile()
65 - V1.13 17.07.95 Added countchar()
66 - V1.14 18.10.95 Moved YorN() to WindIO.c
67 - V1.15 06.11.95 Added StoreString(), InStringList() and FreeStringList()
68 - V1.16 22.11.95 Moved ftostr() to generam.c
69 - V1.17 15.12.95 Added QueryStrStr()
70 - V1.18 18.12.95 OpenStdFiles() treats filename of - as stdin/stdout
71 - V1.19 05.02.96 OpenStdFiles() allows NULL pointers instead if filenames
72 - V1.20 18.09.96 Added padchar()
73 - V1.21 18.06.02 Added string.h
74 - V1.22 28.07.05 Added conditionals for Mac OS/X
75 - V1.23 07.07.14 Include general.h Use bl prefix for functions By: CTP
76 
77 *************************************************************************/
78 /* Doxygen
79  -------
80  #GROUP General Programming
81  #SUBGROUP File IO
82  #FUNCTION blOpenFile()
83  Attempts to open a filename as specified. Returns a file
84  pointer. If this fails looks in a directory specified using an
85  environment variable or assign.
86 */
87 /************************************************************************/
88 /* Includes
89 */
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include "SysDefs.h"
94 #include "port.h"
95 
96 /************************************************************************/
97 /* Defines and macros
98 */
99 
100 /************************************************************************/
101 /* Globals
102 */
103 
104 /************************************************************************/
105 /* Prototypes
106 */
107 
108 
109 /************************************************************************/
110 /*>FILE *blOpenFile(char *filename, char *envvar, char *mode, BOOL *noenv)
111  -----------------------------------------------------------------------
112 *//**
113 
114  \param[in] *filename Filename to be opened
115  \param[in] *envvar Unix/MS-DOS environment variable
116  Other OS assign name (with :)
117  \param[in] *mode Mode in which to open file (r, w, etc)
118  \param[out] *noenv Set to TRUE under Unix/MS-DOS if
119  the reason for failure was that the
120  environment variable was not set.
121  \return File pointer or NULL on failure
122 
123  Attempts to open a filename as specified. Returns a file
124  pointer. If this fails:
125 
126  Under UNIX/MS-DOS:
127  gets the contents of the envvar environment variable and prepends
128  that to the filename and tries again. If envvar was not set, noenv
129  is set to TRUE and the routine returns a NULL pointer.
130 
131  Under other OSs:
132  prepends the envvar string onto the filename and tries to open the
133  file again.
134 
135  Returns the pointer returned by the open() command after all this.
136 
137 - 22.09.94 Original By: ACRM
138 - 11.09.94 Puts a : in for the assign type.
139 - 24.11.94 Added __unix define. Checks for trailing / in environment
140  variable
141 - 08.03.95 Corrected basename to filename in non-unix version
142 - 09.03.95 Checks that filename is not a NULL or blank string
143 - 28.07.05 Added conditionals for Mac OS/X: __MACH__ and __APPLE__
144 - 07.07.14 Use bl prefix for functions By: CTP
145 */
146 FILE *blOpenFile(char *filename, char *envvar, char *mode, BOOL *noenv)
147 {
148  char *datadir,
149  buffer[160];
150  FILE *fp;
151 
152  if(filename == NULL || filename[0] == '\0')
153  return(NULL);
154 
155  if(noenv != NULL) *noenv = FALSE;
156 
157  /* Try to open the filename as specified */
158  if((fp=fopen(filename,mode)) == NULL)
159  {
160  /* Failed, so build alternative directory/filename */
161 #if (unix || __unix__ || MS_WINDOWS || __unix || __MACH__ || __APPLE__)
162  if((datadir = getenv(envvar)) != NULL)
163  {
164  if(datadir[strlen(datadir)-1] == '/')
165  sprintf(buffer,"%s%s",datadir,filename);
166  else
167  sprintf(buffer,"%s/%s",datadir,filename);
168  fp = fopen(buffer,mode);
169  }
170  else
171  {
172  if(noenv != NULL) *noenv = TRUE;
173  return(NULL);
174  }
175 #else
176  sprintf(buffer,"%s:%s",envvar,filename);
177  fp = fopen(buffer,mode);
178 #endif
179  }
180 
181  return(fp);
182 }
183 
184 
short BOOL
Definition: SysDefs.h:64
#define NULL
Definition: array2.c:99
#define FALSE
Definition: macros.h:223
#define TRUE
Definition: macros.h:219
Port-specific defines to allow us to use things like popen() in a clean compile.
System-type variable type definitions.
FILE * blOpenFile(char *filename, char *envvar, char *mode, BOOL *noenv)
Definition: OpenFile.c:146