File: wisefile.c

package info (click to toggle)
wise 2.4.1-21
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 27,140 kB
  • sloc: ansic: 276,365; makefile: 1,003; perl: 886; lex: 93; yacc: 81; sh: 24
file content (370 lines) | stat: -rw-r--r-- 8,750 bytes parent folder | download
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifdef _cplusplus
extern "C" {
#endif
#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;


/* Function:  set_config_dir(path,*path)
 *
 * Descrip:    Programmatically set systemconfigdir to override
 *             any value set (or not) by env.var. WISECONFIGDIR.
 *
 *             Added by arve.
 *
 *
 * Arg:         path [UNKN ] path that WISECONFIGDIR is set to [NullString]
 * Arg:        *path [UNKN ] Undocumented argument [char]
 *
 */
# line 58 "wisefile.dy"
void set_config_dir(char *path) 
{
  if (systemconfigdir != NULL ) {
    ckfree(systemconfigdir);
  }
  systemconfigdir = stringalloc(path);
}


/* Function:  myfclose(ofp)
 *
 * Descrip:    reports the fclose type etc
 *
 *
 * Arg:        ofp [UNKN ] Undocumented argument [FILE *]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
# line 70 "wisefile.dy"
int myfclose(FILE * ofp)
{
  fprintf(stderr,"Closing %d\n",ofp);
#undef fclose
  fclose(ofp);
  return 1;
}

    
/* Function:  try_to_load(void)
 *
 * Descrip:    Loads up 'standard' path places
 *
 *
 *
 */
# line 83 "wisefile.dy"
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;
}

/* Function:  remove_file(filename)
 *
 * Descrip:    silly function to provide a boolean wrapper
 *             around remove. 
 *
 *
 * Arg:        filename [UNKN ] Undocumented argument [char *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
# line 117 "wisefile.dy"
boolean remove_file(char * filename)
{
  if( remove(filename) == 0)
    return TRUE;
  else	return FALSE;
}

/* Function:  move_file(from,to)
 *
 * Descrip:    silly function to provide a boolean wrapper
 *             around rename 
 *
 *
 * Arg:        from [UNKN ] Undocumented argument [char *]
 * Arg:          to [UNKN ] Undocumented argument [char *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
# line 128 "wisefile.dy"
boolean move_file(char * from,char * to)
{
  if( rename(from,to) == 0)
    return TRUE;
  else	return FALSE;
}

/* Function:  append_file_to_path(buffer,len,file,path)
 *
 * Descrip:    Appends file onto path in buffer
 *
 *
 * Arg:        buffer [UNKN ] Undocumented argument [char *]
 * Arg:           len [UNKN ] Undocumented argument [int]
 * Arg:          file [UNKN ] Undocumented argument [const char *]
 * Arg:          path [UNKN ] Undocumented argument [char *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
# line 139 "wisefile.dy"
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;
}

/* Function:  touchfile(filename)
 *
 * Descrip:    sees if filename exists
 *
 *
 * Arg:        filename [UNKN ] Undocumented argument [char *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
# line 166 "wisefile.dy"
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;
}

/* Function:  openfile(filename,passedprot)
 *
 * Descrip:    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 [UNKN ] filename to open for read/writing [const char *]
 * Arg:        passedprot [UNKN ] string representing standard fopen attributes [const char *]
 *
 * Return [UNKN ]  open'd filehandle, NULL on error [FILE *]
 *
 */
# line 205 "wisefile.dy"
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 direcotry 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;
  
}

/* Function:  envopenfile(envname,filename,name,env)
 *
 * Descrip:    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:         envname [READ ] environment variable to read from [NullString]
 * Arg:        filename [UNKN ] Undocumented argument [char *]
 * Arg:            name [READ ] filename to open [NullString]
 * Arg:             env [UNKN ] Undocumented argument [char *]
 *
 * Return [UNKN ]  a valid file pointer or NULL [FILE *]
 *
 */
# line 308 "wisefile.dy"
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");
}
  

# line 347 "wisefile.c"

#ifdef _cplusplus
}
#endif