annotate ezBAMQC/src/htslib/cram/open_trace_file.c @ 8:82bb8c455761

Uploaded
author cshl-bsr
date Wed, 30 Mar 2016 12:03:10 -0400
parents dfa3745e5fd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2 Author: James Bonfield
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4 Copyright (c) 2000-2001 MEDICAL RESEARCH COUNCIL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 All rights reserved
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7 Redistribution and use in source and binary forms, with or without
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 modification, are permitted provided that the following conditions are met:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10 1. Redistributions of source code must retain the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 this list of conditions and the following disclaimer.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 2. Redistributions in binary form must reproduce the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14 this list of conditions and the following disclaimer in the documentation
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 and/or other materials provided with the distribution.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 3. Neither the name of the MEDICAL RESEARCH COUNCIL, THE LABORATORY OF
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18 MOLECULAR BIOLOGY nor the names of its contributors may be used to endorse or
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 promote products derived from this software without specific prior written
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 permission.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 Copyright (c) 2008, 2009, 2013, 2014 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 Author: James Bonfield <jkb@sanger.ac.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 Redistribution and use in source and binary forms, with or without
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 modification, are permitted provided that the following conditions are met:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 1. Redistributions of source code must retain the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 this list of conditions and the following disclaimer.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 2. Redistributions in binary form must reproduce the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45 this list of conditions and the following disclaimer in the documentation
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 and/or other materials provided with the distribution.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 Institute nor the names of its contributors may be used to endorse or promote
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 products derived from this software without specific prior written permission.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 #include <stdlib.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 #include <stdio.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 #include <string.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 #include <unistd.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 #include <ctype.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 #include <limits.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 #include <sys/types.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 #include <sys/stat.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 #include "cram/os.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 #ifndef PATH_MAX
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 # define PATH_MAX 1024
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 #include "cram/open_trace_file.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 #include "cram/misc.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 #include "htslib/hfile.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 * Tokenises the search path splitting on colons (unix) or semicolons
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 * (windows).
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 * We also explicitly add a "./" to the end of the search path
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 * Returns: A new search path with items separated by nul chars. Two nul
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87 * chars in a row represent the end of the tokenised path.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 * Returns NULL for a failure.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 * The returned data has been malloced. It is up to the caller to free this
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 * memory.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 char *tokenise_search_path(char *searchpath) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 char *newsearch;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 unsigned int i, j;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96 size_t len;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 #ifdef _WIN32
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98 char path_sep = ';';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 #else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 char path_sep = ':';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 if (!searchpath)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 searchpath="";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 newsearch = (char *)malloc((len = strlen(searchpath))+5);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 if (!newsearch)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 for (i = 0, j = 0; i < len; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 /* "::" => ":". Used for escaping colons in http://foo */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 if (i < len-1 && searchpath[i] == ':' && searchpath[i+1] == ':') {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 newsearch[j++] = ':';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 i++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 /* Handle http:// and ftp:// too without :: */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 if (path_sep == ':') {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 if ((i == 0 || (i > 0 && searchpath[i-1] == ':')) &&
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 (!strncmp(&searchpath[i], "http:", 5) ||
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 !strncmp(&searchpath[i], "ftp:", 4) ||
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 !strncmp(&searchpath[i], "|http:", 6) ||
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 !strncmp(&searchpath[i], "|ftp:", 5) ||
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 !strncmp(&searchpath[i], "URL=http:", 9) ||
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 !strncmp(&searchpath[i], "URL=ftp:", 8))) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 do {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 newsearch[j++] = searchpath[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 } while (i<len && searchpath[i++] != ':');
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 if (searchpath[i] == ':')
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 i++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 if (searchpath[i]=='/')
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 newsearch[j++] = searchpath[i++];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134 if (searchpath[i]=='/')
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 newsearch[j++] = searchpath[i++];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 // Look for host:port
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 do {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138 newsearch[j++] = searchpath[i++];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 } while (i<len && searchpath[i] != ':' && searchpath[i] != '/');
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 newsearch[j++] = searchpath[i++];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 if (searchpath[i] == ':')
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 i++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146 if (searchpath[i] == path_sep) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 /* Skip blank path components */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 if (j && newsearch[j-1] != 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 newsearch[j++] = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 newsearch[j++] = searchpath[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 if (j)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 newsearch[j++] = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 newsearch[j++] = '.';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 newsearch[j++] = '/';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 newsearch[j++] = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 newsearch[j++] = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 return newsearch;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 mFILE *find_file_url(char *file, char *url) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 char buf[8192], *cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 mFILE *mf = NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 int maxlen = 8190 - strlen(file), len;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 hFILE *hf;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
171 /* Expand %s for the trace name */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
172 for (cp = buf; *url && cp - buf < maxlen; url++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
173 if (*url == '%' && *(url+1) == 's') {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
174 url++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
175 cp += strlen(strcpy(cp, file));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
176 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
177 *cp++ = *url;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
178 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
179 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
180 *cp++ = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
181
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
182 if (!(hf = hopen(buf, "r")))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
183 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
184
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
185 if (NULL == (mf = mfcreate(NULL, 0)))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
186 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
187 while ((len = hread(hf, buf, 8192)) > 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
188 if (mfwrite(buf, len, 1, mf) <= 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
189 hclose_abruptly(hf);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
190 mfdestroy(mf);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
191 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
192 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
193 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
194 if (hclose(hf) < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
195 mfdestroy(mf);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
196 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
197 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
198
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
199 mrewind(mf);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
200 return mf;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
201 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
202
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
203 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
204 * Searches for file in the directory 'dirname'. If it finds it, it opens
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
205 * it. This also searches for compressed versions of the file in dirname
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
206 * too.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
207 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
208 * Returns mFILE pointer if found
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
209 * NULL if not
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
210 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
211 static mFILE *find_file_dir(char *file, char *dirname) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
212 char path[PATH_MAX+1];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
213 size_t len = strlen(dirname);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
214 char *cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
215
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
216 if (dirname[len-1] == '/')
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
217 len--;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
218
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
219 /* Special case for "./" or absolute filenames */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
220 if (*file == '/' || (len==1 && *dirname == '.')) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
221 sprintf(path, "%s", file);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
222 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
223 /* Handle %[0-9]*s expansions, if required */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
224 char *path_end = path;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
225 *path = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
226 while ((cp = strchr(dirname, '%'))) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
227 char *endp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
228 long l = strtol(cp+1, &endp, 10);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
229 if (*endp != 's') {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
230 strncpy(path_end, dirname, (endp+1)-dirname);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
231 path_end += (endp+1)-dirname;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
232 dirname = endp+1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
233 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
234 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
235
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
236 strncpy(path_end, dirname, cp-dirname);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
237 path_end += cp-dirname;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
238 if (l) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
239 strncpy(path_end, file, l);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
240 path_end += MIN(strlen(file), l);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
241 file += MIN(strlen(file), l);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
242 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
243 strcpy(path_end, file);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
244 path_end += strlen(file);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
245 file += strlen(file);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
246 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
247 len -= (endp+1) - dirname;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
248 dirname = endp+1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
249 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
250 strncpy(path_end, dirname, len);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
251 path_end += MIN(strlen(dirname), len);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
252 *path_end = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
253 if (*file) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
254 *path_end++ = '/';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
255 strcpy(path_end, file);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
256 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
257
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
258 //fprintf(stderr, "*PATH=\"%s\"\n", path);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
259 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
260
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
261 if (is_file(path)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
262 return mfopen(path, "rb");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
263 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
264
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
265 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
266 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
267
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
268 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
269 * ------------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
270 * Public functions below.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
271 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
272
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
273 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
274 * Opens a trace file named 'file'. This is initially looked for as a
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
275 * pathname relative to a file named "relative_to". This may (for
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
276 * example) be the name of an experiment file referencing the trace
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
277 * file. In this case by passing relative_to as the experiment file
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
278 * filename the trace file will be picked up in the same directory as
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
279 * the experiment file. Relative_to may be supplied as NULL.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
280 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
281 * 'file' is looked for at relative_to, then the current directory, and then
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
282 * all of the locations listed in 'path' (which is a colon separated list).
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
283 * If 'path' is NULL it uses the RAWDATA environment variable instead.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
284 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
285 * Returns a mFILE pointer when found.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
286 * NULL otherwise.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
287 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
288 mFILE *open_path_mfile(char *file, char *path, char *relative_to) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
289 char *newsearch;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
290 char *ele;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
291 mFILE *fp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
292
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
293 /* Use path first */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
294 if (!path)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
295 path = getenv("RAWDATA");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
296 if (NULL == (newsearch = tokenise_search_path(path)))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
297 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
298
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
299 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
300 * Step through the search path testing out each component.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
301 * We now look through each path element treating some prefixes as
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
302 * special, otherwise we treat the element as a directory.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
303 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
304 for (ele = newsearch; *ele; ele += strlen(ele)+1) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
305 char *ele2;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
306
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
307 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
308 * '|' prefixing a path component indicates that we do not
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
309 * wish to perform the compression extension searching in that
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
310 * location.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
311 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
312 * NB: this has been removed from the htslib implementation.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
313 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
314 if (*ele == '|') {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
315 ele2 = ele+1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
316 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
317 ele2 = ele;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
318 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
319
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
320 if (0 == strncmp(ele2, "URL=", 4)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
321 if ((fp = find_file_url(file, ele2+4))) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
322 free(newsearch);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
323 return fp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
324 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
325 } else if (!strncmp(ele2, "http:", 5) ||
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
326 !strncmp(ele2, "ftp:", 4)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
327 if ((fp = find_file_url(file, ele2))) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
328 free(newsearch);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
329 return fp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
330 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
331 } else if ((fp = find_file_dir(file, ele2))) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
332 free(newsearch);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
333 return fp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
334 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
335 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
336
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
337 free(newsearch);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
338
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
339 /* Look in the same location as the incoming 'relative_to' filename */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
340 if (relative_to) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
341 char *cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
342 char relative_path[PATH_MAX+1];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
343 strcpy(relative_path, relative_to);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
344 if ((cp = strrchr(relative_path, '/')))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
345 *cp = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
346 if ((fp = find_file_dir(file, relative_path)))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
347 return fp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
348 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
349
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
350 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
351 }