1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
%{
#include "wisebase.h"
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
#ifdef FILE_DEBUG
#define fclose myfclose
#endif
/* define here for the moment, as we always compile
with it */
#define NOERROR
#ifdef NOERROR
#define ERRORSTR "No error available"
#else
#define ERRORSTR strerror(errno)
#endif
%}
api
func openfile
endapi
%{
#include "wisefile.h"
#ifdef UNIX
static boolean isvms = FALSE;
#else
static boolean isvms= TRUE;
#endif
static char * systemconfigdir=NULL;
static char * personaldir=NULL;
static char * homedir=NULL;
static boolean hasloaded=FALSE;
%func
Programmatically set systemconfigdir to override
any value set (or not) by env.var. WISECONFIGDIR.
Added by arve.
%arg
path path that WISECONFIGDIR is set to
%%
void set_config_dir(char *path)
{
if (systemconfigdir != NULL ) {
ckfree(systemconfigdir);
}
systemconfigdir = stringalloc(path);
}
%func
reports the fclose type etc
%%
int myfclose(FILE * ofp)
{
fprintf(stderr,"Closing %d\n",ofp);
#undef fclose
fclose(ofp);
return 1;
}
%func
Loads up 'standard' path places
%type internal
%%
void try_to_load(void)
{
char * runner;
char buffer[256];
if( hasloaded == TRUE)
log_full_error(PEDANTIC,0,"Trying to reload configdirs");
if( (runner=getenv("WISECONFIGDIR")) != NULL) {
if( runner[strlen(runner)-1] == '/' )
systemconfigdir=stringalloc(runner);
else {
sprintf(buffer,"%s/",runner);
systemconfigdir=stringalloc(buffer);
}
}
if( (runner=getenv("WISEPERSONALDIR")) != NULL) {
personaldir=stringalloc(runner);
}
if( (runner=getenv("HOME")) != NULL) {
homedir=stringalloc(runner);
}
hasloaded=TRUE;
}
%func
silly function to provide a boolean wrapper
around remove.
%%
boolean remove_file(char * filename)
{
if( remove(filename) == 0)
return TRUE;
else return FALSE;
}
%func
silly function to provide a boolean wrapper
around rename
%%
boolean move_file(char * from,char * to)
{
if( rename(from,to) == 0)
return TRUE;
else return FALSE;
}
%func
Appends file onto path in buffer
%type internal
%%
boolean append_file_to_path(char * buffer,int len,const char * file,char * path)
{
char * runner;
if( 1+strlen(file)+strlen(path) > len) {
warn("Unable to expand %s with %s due to lack of buffer space",path,file);
return FALSE;
}
strcpy(buffer,path);
runner=buffer+strlen(buffer);
#ifdef UNIX
if( *runner != '/') {
*(runner++)='/';
*runner='\0';
}
#endif
strcat(buffer,file);
return TRUE;
}
%func
sees if filename exists
%%
boolean touchfile(char * filename)
{
FILE * temp;
boolean retval;
if( filename == NULL ) {
warn("Tried to touch a NULL filename");
return FALSE;
}
if( (temp=openfile(filename,"r")) == NULL) {
retval=FALSE;
}
else {
fclose(temp);
retval=TRUE;
}
return retval;
}
%func
Every file open goes through this.
It opens for reading in the following order
.
WISEPERSONALDIR
WISECONFIGDIR
For writing it opens in .
Filenames with ~'s are expanded to HOME/filename
%arg
filename filename to open for read/writing
passedprot string representing standard fopen attributes
return open'd filehandle, NULL on error
%%
FILE * openfile(const char * filename,const char * passedprot)
{
FILE * ifp;
char buffer[MAXPATHLEN];
char prot[12]; /* protection string longer than 12 ! */
boolean shouldreporterror=FALSE;
strncpy(prot,passedprot,12);
if( *prot == 'R' ) {
shouldreporterror=TRUE;
*prot = 'r';
}
if( *prot == 'W' ){
shouldreporterror=TRUE;
*prot = 'w';
}
if( filename == NULL) {
if( shouldreporterror )
info("Open for file failed due to NULL filename");
return NULL;
}
if( strcmp(filename,"-") == 0 ) {
if( *prot == 'r' )
return stdin;
else return stdout;
}
if( (ifp=fopen(filename,prot)) != NULL) {
#ifdef FILE_DEBUG
fprintf(stderr,"Succeeded in opening %s. Filehandle is %d\n",filename,ifp);
#endif
return ifp;
}
if( shouldreporterror ) {
info("Direct open for [%s,%s] failed: Error message is %s",filename,prot, ERRORSTR);
}
if( hasloaded == FALSE)
try_to_load();
if( isvms != TRUE && homedir != NULL && filename[0]== '~' && filename[1] == '/')
if( append_file_to_path(buffer,MAXPATHLEN,filename+2,homedir) != FALSE )
{
/*fprintf(stderr,"Trying to load %s\n",buffer);*/
if( (ifp=fopen(buffer,prot)) != NULL)
return ifp;
else if( shouldreporterror )
info("Expanded tilda open for[%s,%s], expanded to %s failed: Error message is %s",filename,prot,buffer, ERRORSTR);
}
if( strchr(prot,'w') != NULL)
return NULL;
/* next line ABSOLUTELY relies on order of evaluation */
if( personaldir != NULL && append_file_to_path(buffer,MAXPATHLEN,filename,personaldir) != FALSE && (ifp=fopen(buffer,prot)) != NULL)
return ifp;
else if ( shouldreporterror )
log_full_error(INFO,0,"Expanded personal directory open for[%s,%s], expanded to %s failed: Error message is %s",filename,prot,buffer,ERRORSTR);
/* next line ABSOLUTELY relies on order of evaluation */
if( systemconfigdir != NULL && append_file_to_path(buffer,MAXPATHLEN,filename,systemconfigdir) != FALSE && (ifp=fopen(buffer,prot)) != NULL)
return ifp;
else if ( shouldreporterror )
log_full_error(INFO,0,"Expanded system directory open for[%s,%s], expanded to %s failed: Error message is %s",filename,prot,buffer, ERRORSTR);
#ifdef CAREFUL
if( looks_like_vms(filename) && isvms == FALSE)
log_full_error(WARNING,0,"Filename %s looks like a VMS "
"file to me, and you've compiled as UNIX",filename);
if( looks_like_unix(filename) && isvms == TRUE)
log_full_error(WARNING,0,"Filename %s looks like a UNIX "
"file to me, and you've compiled as VMS",filename);
#endif
return NULL;
}
%func
This function basically mirrors the function in file.c
in HMMer2. You call it as
fp = Envfile(filename,envname);
where envname looks like "BLASTDB" etc.
%arg
name r filename to open
envname r environment variable to read from
return a valid file pointer or NULL
%%
FILE * envopenfile(char * filename,char * env)
{
char * envp;
char path [512];
if( filename == NULL || env == NULL ) {
warn("Passed a NULL filename or environment name into Envfile. Should trap this elsewhere");
return NULL;
}
if( (envp = getenv(env)) == NULL ) {
/* fail gracefully - somebody might query a number of environment variables */
return NULL;
}
if( strlen(filename) + strlen(envp) < 490 ) {
warn("Really long filename/environment variables [%s] [%s] Can't cope!",filename,envp);
return NULL;
}
sprintf(path,"%s/%s",filename,envp);
return fopen(path,"r");
}
%}
|