| Bsoft 2.0.5 Bernard's software package

file_util.cpp
Go to the documentation of this file.
1
9 #include "file_util.h"
10 #include "utilities.h"
11 #include <fstream>
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
16 Bstring test_access(Bstring filename)
17 {
18 if ( verbose & VERB_DEBUG )
19 cout << "DEBUG test_access: " << filename << endl;
20
21 if ( access(filename.c_str(), F_OK) == 0 ) return filename;
22
23 Bstring empty;
24
25 return empty;
26 }
27
28 string test_access(string filename)
29 {
30 if ( verbose & VERB_DEBUG )
31 cout << "DEBUG test_access: " << filename << endl;
32
33 if ( access(filename.c_str(), F_OK) == 0 ) return filename;
34
35 string empty;
36
37 return empty;
38 }
39
54 Bstring find_file(Bstring filename, Bstring path, int flag)
55 {
56 if ( filename.length() < 1 ) return filename;
57
58 if ( filename == "?" ) return filename;
59
60 if ( filename.empty() ) {
61 filename = "?";
62 return filename;
63 }
64
65 if ( !path.empty() )
66 if ( path[-1] != '/' ) path += "/";
67
68 long i;
69 Bstring foundfile = test_access(filename);
70 Bstring testfile, testpath;
71
72 if ( foundfile.length() ) {
73 return foundfile;
74 } else if ( filename.contains("://") ) {
75 i = 3 + filename.find("://", 0);
76 i = filename.find("/", i);
77 foundfile = test_access(filename.substr(i, filename.length()));
78 } else {
79 if ( filename.contains("/") ) {
80 testfile = filename.post_rev('/');
81 testpath = filename.pre_rev('/') + "/";
82 foundfile = test_access(testfile);
83 } else {
84 testfile = filename;
85 }
86
87 if ( foundfile.empty() && path.length() )
88 foundfile = test_access(path + testfile);
89
90 if ( foundfile.empty() && testpath.length() )
91 foundfile = test_access(testpath + testfile);
92
93 if ( foundfile.empty() && path.length() && testpath.length() ) {
94 foundfile = test_access(path + testpath + testfile);
95 }
96 }
97
98 if ( foundfile.empty() ) {
99 if ( verbose && ( flag & 8 ) )
100 cerr << "Warning: File " << filename << " not found!" << endl;
101 if ( flag & 16 ) {
102 foundfile = filename;
103 if ( verbose && ( flag & 8 ) )
104 cerr << tab << " deleting file name!" << endl;
105 }
106 }
107
108 return foundfile;
109 }
110
111 string find_file(string filename, string path, int flag)
112 {
113 if ( filename.length() < 1 ) return filename;
114
115 if ( filename == "?" ) return filename;
116
117 if ( filename.length() < 1 ) {
118 filename = "?";
119 return filename;
120 }
121
122 if ( path.length() )
123 if ( path[-1] != '/' ) path += "/";
124
125 long i;
126 string foundfile = test_access(filename);
127 string testfile, testpath;
128
129 if ( foundfile.length() ) {
130 return foundfile;
131 } else if ( filename.find("://") != string::npos ) {
132 i = 3 + filename.find("://", 0);
133 i = filename.find("/", i);
134 foundfile = test_access(filename.substr(i, filename.length()));
135 } else {
136 if ( filename.find("/") != string::npos ) {
137 testfile = filename.substr(filename.rfind('/'));
138 testpath = filename.substr(0, filename.rfind('/')) + "/";
139 foundfile = test_access(testfile);
140 } else {
141 testfile = filename;
142 }
143
144 if ( foundfile.length() < 1 && path.length() )
145 foundfile = test_access(path + testfile);
146
147 if ( foundfile.length() < 1 && testpath.length() )
148 foundfile = test_access(testpath + testfile);
149
150 if ( foundfile.length() < 1 && path.length() && testpath.length() ) {
151 foundfile = test_access(path + testpath + testfile);
152 }
153 }
154
155 if ( foundfile.length() < 1 ) {
156 if ( verbose && ( flag & 8 ) )
157 cerr << "Warning: File " << filename << " not found!" << endl;
158 if ( flag & 16 ) {
159 foundfile = filename;
160 if ( verbose && ( flag & 8 ) )
161 cerr << tab << " deleting file name!" << endl;
162 }
163 }
164
165 return foundfile;
166 }
167
178 FileType file_type(const char* filename)
179 {
180 Bstring name(filename);
181 return file_type(name);
182 }
183
184 FileType file_type(Bstring& filename)
185 {
186 FileType type = Unknown_FileType;
187
188 if ( filename.length() < 1 ) return type;
189
190 ifstream f;
191 char aline[MAXLINELEN];
192 char* aptr;
193 string sline;
194 int i, check_flag(0);
195
196 Bstring cleanname = filename;
197 Bstring ext;
198
199 if ( filename.contains(":") ) {
200 cleanname = filename.pre(':');
201 ext = filename.post(':');
202 } else {
203 ext = cleanname.extension();
204 }
205
206 if ( verbose & VERB_DEBUG )
207 cout << "DEBUG file_type: ext = " << ext << endl;
208
209 if ( ext == "star" ) {
210 f.open(cleanname.c_str());
211 if ( f.fail() ) {
212 error_show(cleanname.c_str(), __FILE__, __LINE__);
213 return type;
214 }
215 if ( verbose & VERB_DEBUG )
216 cout << "DEBUG file_type: file " << cleanname << " opened" << endl;
217 while ( !f.eof() && type == Unknown_FileType ) {
218 getline(f, sline);
219 // cout << "-" << sline << "-" << endl;
220 for ( aptr = (char *) sline.c_str(); isspace(aptr[0]); aptr++ ) ;
221 if ( strncmp(aptr, "data_", 5) == 0 ) check_flag = 1;
222 // cout << check_flag << ": " << aptr << endl;
223 if ( check_flag && aptr[0] != '#' ) {
224 if ( strstr(aptr, "micrograph.id") ) type = Micrograph;
225 if ( strstr(aptr, "map.3D_reconstruction.id") ) type = Micrograph;
226 if ( strstr(aptr, "map.reference.file_name") ) type = Micrograph;
227 if ( strstr(aptr, "_rln") ) type = MgRelion; // Relion STAR
228 if ( strstr(aptr, "model.id") ) type = Model;
229 if ( strstr(aptr, "molecule.name") ) type = Molecule;
230 if ( strstr(aptr, "image.file_name") ) type = Image;
231 }
232 }
233 f.close();
234 } else if ( ext == "xml" ) {
235 f.open(cleanname.c_str());
236 if ( f.fail() ) {
237 error_show(cleanname.c_str(), __FILE__, __LINE__);
238 return type;
239 }
240 if ( verbose & VERB_DEBUG )
241 cout << "DEBUG file_type: file " << cleanname << " opened" << endl;
242 while ( !f.eof() && type == Unknown_FileType ) {
243 getline(f, sline);
244 for ( aptr = (char *) sline.c_str(); isspace(aptr[0]); aptr++ ) ;
245 if ( strncmp(aptr, "<project", 8) == 0 ) type = Micrograph;
246 if ( strncmp(aptr, "<model", 6) == 0 ) type = Model;
247 }
248 f.close();
249 } else if ( ext == "emx" ) {
250 type = Micrograph;
251 } else if ( ext == "imod" ) {
252 type = Micrograph;
253 } else if ( ext == "mdoc" ) {
254 type = Micrograph;
255 } else if ( ext.contains("pdb") ) {
256 f.open(cleanname.c_str());
257 if ( f.fail() ) {
258 error_show(cleanname.c_str(), __FILE__, __LINE__);
259 return type;
260 }
261 if ( verbose & VERB_DEBUG )
262 cout << "DEBUG file_type: file " << cleanname << " opened" << endl;
263 while ( f.getline(aline, MAXLINELEN) && type == Unknown_FileType && strncmp(aline, "ATOM", 4) ) {
264 if ( strncmp(aline, "REMARK", 6) == 0 && strstr(aline, "Model") ) type = Model;
265 }
266 f.close();
267 if ( type == Unknown_FileType ) type = Molecule;
268 } else if ( ext.contains("cmm") ) type = Model;
269 else if ( ext.contains("v3d") ) type = Model;
270 else if ( ext.contains("cif") ) type = Molecule;
271 else if ( ext.contains("txt") ) type = Molecule;
272 else if ( ext.contains("aln") ) type = Molecule;
273 else if ( ext.contains("embl") ) type = Molecule;
274 else if ( ext.contains("fasta") ) type = Molecule;
275 else if ( ext.contains("gb") || ext.contains("gen") ) type = Molecule;
276 else if ( ext.contains("gro") ) type = Molecule;
277 else if ( ext.contains("pdb") || ext.contains("ent") ) type = Molecule;
278 else if ( ext.contains("phylip") ) type = Molecule;
279 else if ( ext.contains("pir") ) type = Molecule;
280 else if ( ext.contains("wh") || ext.contains("wah") ) type = Molecule;
281 else if ( ext.contains("raw") ) type = Image;
282 else if ( ext.contains("asc") || ext.contains( "txt") ) type = Image;
283 else if ( ext.contains("bcr") ) type = Image;
284 else if ( ext.contains("pic") ) type = Image;
285 else if ( ext.contains("brx" ) || ext.contains("brix" ) ) type = Image;
286 else if ( ext.contains("dat" ) ) type = Image;
287 else if ( ext.contains("ccp") || ext.contains("map") ) type = Image;
288 else if ( ext.contains("di") ) type = Image;
289 else if ( ext.contains("dm") ) type = Image;
290 else if ( ext.contains("dx") ) type = Image;
291 else if ( ext.contains("omap" ) || ext.contains("dsn6") || ext.contains("dn6") ) type = Image;
292 else if ( ext.contains("em") ) type = Image;
293 else if ( ext.contains("pot") ) type = Image;
294 else if ( ext.contains("grd") ) type = Image;
295 else if ( ext.contains("hkl") ) type = Image;
296 else if ( ext.contains("img") || ext.contains("hed") ) type = Image;
297 else if ( ext.contains("ip") ) type = Image;
298 else if ( ext.contains("jpg") || ext.contains("jpeg") ) type = Image;
299 else if ( ext.contains("krn") ) type = Image;
300 else if ( ext.contains("mif") ) type = Image;
301 else if ( ext.contains("mff") ) type = Image;
302 else if ( ext.contains("mrc") || ext == "stk" ) type = Image;
303 else if ( ext == "st" || ext.contains("ali") || ext == ("rec") ) type = Image; // IMOD extensions
304 else if ( ext.contains("pif") || ext.contains("sf") ) type = Image;
305 else if ( ext.contains("bp") || ext.contains("bq") ) type = Image;
306 else if ( ext.contains("png") ) type = Image;
307 else if ( ext.contains("pbm") || ext.contains("pgm") || ext.contains("ppm") ) type = Image;
308 else if ( ext.contains("ser") ) type = Image;
309 else if ( ext.contains("sit") ) type = Image;
310 else if ( ext.contains("spe") ) type = Image;
311 else if ( ext.contains("spi") ) { // Spider image and parameter files
312 f.open(cleanname.c_str());
313 if ( f.fail() ) {
314 error_show(cleanname.c_str(), __FILE__, __LINE__);
315 return type;
316 }
317 if ( verbose & VERB_DEBUG )
318 cout << "DEBUG file_type: file " << cleanname << " opened" << endl;
319 for ( i=0; i<5 && type == Unknown_FileType; i++ ) {
320 f.getline(aline, MAXLINELEN);
321 aptr = aline;
322 while ( aptr[0] && isspace(aptr[0]) ) aptr++; // Remove leading spaces
323 if ( aptr[0] == ';' ) type = Micrograph;
324 }
325 f.close();
326 if ( type == Unknown_FileType ) type = Image;
327 } else if ( ext.contains("spm") || ext.contains("sup") || ext == "f" ) type = Image;
328 else if ( ext.contains("tif") ) type = Image;
329 else if ( ext.contains("tga") ) type = Image;
330 else if ( ext.contains("xpl") || ext.contains("rfl") ) type = Image;
331 else cerr << "Error: File type with extension \"" << ext << "\" not supported!" << endl;
332
333 return type;
334 }
335
347 int fread_large(unsigned char* aptr, size_t pagesize, size_t offset, ifstream* fimg)
348 {
349 size_t j, readsize, pagemax = 1073741824;
350
351 if ( verbose & VERB_DEBUG )
352 cout << "DEBUG fread_large: pagesize=" << pagesize << " offset=" << offset << endl;
353
354 fimg->seekg(offset, ios::beg);
355 for ( j=0; j<pagesize; j+=pagemax ) {
356 readsize = pagesize - j;
357 if ( readsize > pagemax ) readsize = pagemax;
358 fimg->read((char *)aptr, readsize);
359 if ( fimg->fail() ) return -1;
360 aptr += readsize;
361 }
362
363 return 0;
364 }
365
366
367
377 int detect_and_fix_carriage_return(const char* filename)
378 {
379 long max_line_len = 10240;
380 char aline[max_line_len];
381
382 ifstream fin(filename);
383 if ( fin.fail() ) {
384 error_show(filename, __FILE__, __LINE__);
385 return -1;
386 }
387
388 fin.getline(aline, max_line_len);
389
390 fin.close();
391
392 if ( !strchr(aline, '\r') ) return 0;
393
394 if ( access(filename, W_OK) ) {
395 error_show("Error in detect_and_fix_carriage_return", __FILE__, __LINE__);
396 cerr << "Warning: Carriage returns in " << filename << "! Fix with bfix." << endl;
397 return -1;
398 }
399
400 cerr << "Warning: Fixing carriage returns in " << filename << endl;
401
402 fin.open(filename);
403
404 ofstream fout("t.t");
405 char *pin, *pout;
406
407 while ( !fin.eof() ) {
408 fin.getline(aline, max_line_len);
409 for ( pin = pout = aline; *pin != 0; pin++ ) {
410 if ( *pin != '\r' ) {
411 *pout = *pin;
412 pout++;
413 }
414 }
415 *pout = 0;
416 fout << aline << endl;
417 }
418
419 fin.close();
420 fout.close();
421
422 rename("t.t", filename);
423
424 return 0;
425 }
426
FileType
FileType
File type enumeration.
Definition: file_util.h:20
Model
Definition: file_util.h:25
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:394
test_access
Bstring test_access(Bstring filename)
Definition: file_util.cpp:16
Bstring::substr
Bstring substr(long start, long len)
Definition: Bstring.cpp:324
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bstring::find
long find(const Bstring &s)
Definition: Bstring.h:103
Bstring::length
long length() const
Definition: Bstring.cpp:263
Molecule
Definition: file_util.h:24
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
Micrograph
Definition: file_util.h:23
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:381
Bstring::extension
Bstring extension()
Definition: Bstring.cpp:515
file_util.h
Header file for file checking functions.
MgRelion
Definition: file_util.h:26
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
verbose
int verbose
Bstring
Definition: Bstring.h:29
Image
Definition: file_util.h:22
fread_large
int fread_large(unsigned char *aptr, size_t pagesize, size_t offset, ifstream *fimg)
Reads blocks of memeory no larger than 1Gb.
Definition: file_util.cpp:347
Unknown_FileType
Definition: file_util.h:21
detect_and_fix_carriage_return
int detect_and_fix_carriage_return(const char *filename)
Detects carriage returns in text files and converts them to new-lines.
Definition: file_util.cpp:377
find_file
Bstring find_file(Bstring filename, Bstring path, int flag)
Searches for a file within or without the given path.
Definition: file_util.cpp:54
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
Bstring::empty
bool empty()
Definition: Bstring.h:71
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21