comparison srf2fastq/io_lib-1.12.2/io_lib/files.c @ 0:d901c9f41a6a default tip

Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
author dawe
date Tue, 07 Jun 2011 17:48:05 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d901c9f41a6a
1 /*
2 * Copyright (c) Medical Research Council 1994. All rights reserved.
3 *
4 * Permission to use, copy, modify and distribute this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * this copyright and notice appears in all copies.
7 *
8 * This file was written by James Bonfield, Simon Dear, Rodger Staden,
9 * as part of the Staden Package at the MRC Laboratory of Molecular
10 * Biology, Hills Road, Cambridge, CB2 2QH, United Kingdom.
11 *
12 * MRC disclaims all warranties with regard to this software.
13 */
14
15 #include "io_lib/misc.h"
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 /* Alliant's Concentrix <sys/stat.h> is hugely deficient */
20 /* Define things we require in this program */
21 /* Methinks S_IFMT and S_IFDIR aren't defined in POSIX */
22 #ifndef S_ISDIR
23 #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
24 #endif /*!S_ISDIR*/
25 #ifndef S_ISREG
26 #define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
27 #endif /*!S_ISREG*/
28
29 int is_directory(char * fn)
30 {
31 struct stat buf;
32 if ( stat(fn,&buf) ) return 0;
33 return S_ISDIR(buf.st_mode);
34 }
35
36 int is_file(char * fn)
37 {
38 struct stat buf;
39 if ( stat(fn,&buf) ) return 0;
40 return S_ISREG(buf.st_mode);
41 }
42
43 int file_exists(char * fn)
44 {
45 struct stat buf;
46 return ( stat(fn,&buf) == 0);
47 }
48
49 int compressed_file_exists(char *fname)
50 {
51 struct stat buf;
52 char fn[2048];
53
54 if (stat(fname, &buf) == 0) return 1;
55
56 sprintf(fn, "%s.gz", fname);
57 if (stat(fn, &buf) == 0) return 1;
58
59 sprintf(fn, "%s.bz", fname);
60 if (stat(fn, &buf) == 0) return 1;
61
62 sprintf(fn, "%s.bz2", fname);
63 if (stat(fn, &buf) == 0) return 1;
64
65 sprintf(fn, "%s.Z", fname);
66 if (stat(fn, &buf) == 0) return 1;
67
68 sprintf(fn, "%s.z", fname);
69 if (stat(fn, &buf) == 0) return 1;
70
71 return 0;
72 }
73
74 int file_size(char * fn)
75 {
76 struct stat buf;
77 if ( stat(fn,&buf) != 0) return 0;
78 return buf.st_size;
79 }
80
81 /*
82 * ---------------------------------------------------------------------------
83 * File of filename management
84 */
85
86 FILE *open_fofn(char *files) {
87 return fopen(files, "r");
88 }
89
90 char *read_fofn(FILE *fp) {
91 char line[256];
92 static char name[256];
93
94 while (fgets(line, 254, fp)) {
95 if (1 == sscanf(line, "%s", name))
96 return name;
97 }
98
99 return NULL;
100 }
101
102 void close_fofn(FILE *fp) {
103 fclose(fp);
104 }
105
106
107 /*
108 * ---------------------------------------------------------------------------
109 * Temporary file handling.
110 */
111
112 #ifdef _WIN32
113 /*
114 * On UNIX systems we use tmpfile().
115 *
116 * On windows this is broken because it always attempts to create files in
117 * the root directory of the current drive, and fails if the user does not
118 * have write permission.
119 *
120 * We can't wrap up mkstemp() either as that doesn't exist under windows.
121 * Instead we roll our own tmpfile() using the native windows API.
122 */
123 #include <windows.h>
124 #include <winbase.h>
125 #include <stdio.h>
126 #include <io.h>
127 #include <fcntl.h>
128 #define _POSIX_ /* needed to get PATH_MAX */
129 #include <limits.h>
130
131 static void display_win_error(char *msg) {
132 LPVOID lpMsgBuf;
133 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
134 FORMAT_MESSAGE_FROM_SYSTEM |
135 FORMAT_MESSAGE_IGNORE_INSERTS,
136 NULL,
137 GetLastError(),
138 0, /* Default language */
139 (LPTSTR)&lpMsgBuf, /* Got to love void* to str casts! */
140 0, NULL);
141 fprintf(stderr, "%s: error #%d, %s", msg, GetLastError(), lpMsgBuf);
142 LocalFree(lpMsgBuf);
143 }
144
145 /*
146 * Creates a temporary file and returns a FILE pointer to it.
147 * The file will be automatically deleted when it is closed or the
148 * applicaton exits.
149 *
150 * Returns NULL on failure.
151 */
152 FILE *tmpfile(void) {
153 DWORD ret;
154 char tmp_path[PATH_MAX], shrt_path[PATH_MAX];
155 int fd;
156 FILE *fp;
157
158 /* The Windows Way: get the temp directory and a file within it */
159 ret = GetTempPath(PATH_MAX, tmp_path);
160 if (ret == 0 || ret > PATH_MAX) {
161 display_win_error("GetTempPath()");
162 return NULL;
163 }
164
165 if (0 == GetTempFileName(tmp_path, "fubar", 0, shrt_path)) {
166 display_win_error("GetTempFileName()");
167 return NULL;
168 }
169
170 /*
171 * O_TRUNC incase anyone has managed to inject data in the newly created
172 * file already via race-conditions.
173 *
174 * O_EXCL to (in theory) stop anyone else opening it and to die if someone
175 * beat us to it - although this appears to not actually work on Windows.
176 *
177 * O_TEMPORARY so that the file is removed on close.
178 */
179 if (-1 == (fd = _open(shrt_path, O_RDWR | O_TRUNC | O_EXCL |
180 O_BINARY | O_TEMPORARY, 0600))) {
181 perror(shrt_path);
182 }
183
184 /* Replace fd with FILE*. No need to close fd */
185 if (NULL == (fp = _fdopen(fd, "r+b"))) {
186 perror(shrt_path);
187 }
188
189 return fp;
190 }
191
192 #endif /* _WIN32 */