19 cout <<
"DEBUG test_access: " << filename << endl;
21 if ( access(filename.
c_str(), F_OK) == 0 )
return filename;
31 cout <<
"DEBUG test_access: " << filename << endl;
33 if ( access(filename.c_str(), F_OK) == 0 )
return filename;
56 if ( filename.
length() < 1 )
return filename;
58 if ( filename ==
"?" )
return filename;
60 if ( filename.
empty() ) {
66 if ( path[-1] !=
'/' ) path +=
"/";
72 if ( foundfile.
length() ) {
74 }
else if ( filename.
contains(
"://") ) {
75 i = 3 + filename.
find(
"://", 0);
76 i = filename.
find(
"/", i);
81 testpath = filename.
pre_rev(
'/') +
"/";
94 foundfile =
test_access(path + testpath + testfile);
98 if ( foundfile.
empty() ) {
100 cerr <<
"Warning: File " << filename <<
" not found!" << endl;
102 foundfile = filename;
104 cerr <<
tab <<
" deleting file name!" << endl;
111 string find_file(
string filename,
string path,
int flag)
113 if ( filename.length() < 1 )
return filename;
115 if ( filename ==
"?" )
return filename;
117 if ( filename.length() < 1 ) {
123 if ( path[-1] !=
'/' ) path +=
"/";
127 string testfile, testpath;
129 if ( foundfile.length() ) {
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()));
136 if ( filename.find(
"/") != string::npos ) {
137 testfile = filename.substr(filename.rfind(
'/'));
138 testpath = filename.substr(0, filename.rfind(
'/')) +
"/";
144 if ( foundfile.length() < 1 && path.length() )
147 if ( foundfile.length() < 1 && testpath.length() )
150 if ( foundfile.length() < 1 && path.length() && testpath.length() ) {
151 foundfile =
test_access(path + testpath + testfile);
155 if ( foundfile.length() < 1 ) {
157 cerr <<
"Warning: File " << filename <<
" not found!" << endl;
159 foundfile = filename;
161 cerr <<
tab <<
" deleting file name!" << endl;
188 if ( filename.
length() < 1 )
return type;
194 int i, check_flag(0);
200 cleanname = filename.
pre(
':');
201 ext = filename.
post(
':');
207 cout <<
"DEBUG file_type: ext = " << ext << endl;
209 if ( ext ==
"star" ) {
210 f.open(cleanname.
c_str());
216 cout <<
"DEBUG file_type: file " << cleanname <<
" opened" << endl;
220 for ( aptr = (
char *) sline.c_str(); isspace(aptr[0]); aptr++ ) ;
221 if ( strncmp(aptr,
"data_", 5) == 0 ) check_flag = 1;
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;
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;
234 }
else if ( ext ==
"xml" ) {
235 f.open(cleanname.
c_str());
241 cout <<
"DEBUG file_type: file " << cleanname <<
" opened" << endl;
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;
249 }
else if ( ext ==
"emx" ) {
251 }
else if ( ext ==
"imod" ) {
253 }
else if ( ext ==
"mdoc" ) {
256 f.open(cleanname.
c_str());
262 cout <<
"DEBUG file_type: file " << cleanname <<
" opened" << endl;
264 if ( strncmp(aline,
"REMARK", 6) == 0 && strstr(aline,
"Model") ) type =
Model;
302 else if ( ext.
contains(
"mrc") || ext ==
"stk" ) type =
Image;
303 else if ( ext ==
"st" || ext.
contains(
"ali") || ext == (
"rec") ) type =
Image;
312 f.open(cleanname.
c_str());
318 cout <<
"DEBUG file_type: file " << cleanname <<
" opened" << endl;
322 while ( aptr[0] && isspace(aptr[0]) ) aptr++;
331 else cerr <<
"Error: File type with extension \"" << ext <<
"\" not supported!" << endl;
347 int fread_large(
unsigned char* aptr,
size_t pagesize,
size_t offset, ifstream* fimg)
349 size_t j, readsize, pagemax = 1073741824;
352 cout <<
"DEBUG fread_large: pagesize=" << pagesize <<
" offset=" << offset << endl;
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;
379 long max_line_len = 10240;
380 char aline[max_line_len];
382 ifstream fin(filename);
388 fin.getline(aline, max_line_len);
392 if ( !strchr(aline,
'\r') )
return 0;
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;
400 cerr <<
"Warning: Fixing carriage returns in " << filename << endl;
404 ofstream fout(
"t.t");
407 while ( !fin.eof() ) {
408 fin.getline(aline, max_line_len);
409 for ( pin = pout = aline; *pin != 0; pin++ ) {
410 if ( *pin !=
'\r' ) {
416 fout << aline << endl;
422 rename(
"t.t", filename);