Debian Codegraph @

mydir-unix.cpp

raw
1 /* BaitFisher (version 1.2.7) a program for designing DNA target enrichment baits
2 * Copyright 2013-2016 by Christoph Mayer
3 *
4 * This source file is part of the BaitFisher-package.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * For any enquiries send an Email to Christoph Mayer
21 * [email protected]
22 *
23 * When publishing work that is based on the results please cite:
24 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
25 *
26 */
27 #include <iostream>
28 #include <fstream>
29 #include <unistd.h> // Might be available for mingw. I have not verified this. (??)
30 #include <sys/stat.h> // Should be available for mingw. I have not verified this. (??)
31 #include <sys/types.h>
32 #include "faststring2.h"
33 #include <stdio.h>
34 #include <dirent.h>
35
36
37 #include "mydir-unix.h"
38
39 using namespace std;
40
41 bool file_exists(const char *fname)
42 {
43 std::ifstream is;
44 is.open(fname);
45 if (is.fail())
46 {
47 return false;
48 }
49 is.close();
50 return true;
51 }
52
53
54
55 bool file_or_dir_exists(const char *fname)
56 {
57 struct stat st;
58 if(stat(fname,&st) == 0)
59 {
60 return true;
61 }
62 else
63 return false;
64 }
65
66 // Determines the file type:
67 // 0: does not exist
68 // 1: regular file
69 // 2: directory
70 // 5: other
71
72 int file_or_dir_type(const char *fname)
73 {
74 struct stat st;
75 if(stat(fname,&st) != 0)
76 {
77 return 0;
78 }
79
80 // Link detection does not seem to work properly on my mac.
81 // if (st.st_mode & S_IFLNK)
82 // {
83 // if (st.st_mode & S_IFREG)
84 // return 3;
85 // if (st.st_mode & S_IFDIR)
86 // return 4;
87 // }
88
89 if (st.st_mode & S_IFREG)
90 return 1;
91 if (st.st_mode & S_IFDIR)
92 return 2;
93
94 return 5;
95 }
96
97 // bool directory_exists(const char *dname)
98 // {
99 // struct stat st;
100 // if(stat(dname,&st) == 0)
101 // {
102 // return true;
103 // }
104 // else
105 // return false;
106 // }
107
108
109 bool change_dir(const char *dname)
110 {
111 return (chdir(dname)==0);
112 }
113
114 // Remember: This does not work like the mv on linux. The new_name must be complete. It cannot end in an existing directory.
115 bool move_file_or_dir(const char* old_name, const char *new_name)
116 {
117 int res = rename(old_name, new_name);
118
119 return (res == 0);
120 }
121
122
123 bool make_directory(const char *dname)
124 {
125 return (mkdir(dname, 0700) == 0); // Default: Only user is allowed to use this folder.
126 }
127
128 void next_file_name_in_series(faststring &str, unsigned *z)
129 {
130 faststring fname_without_extension;
131 faststring putative_version;
132
133 str.backwards_search_divide_at_break_at('_','/',fname_without_extension, putative_version);
134
135 unsigned version = 0;
136 // cerr << "putative_version: " << putative_version << endl;
137
138 if (!putative_version.empty() && putative_version.isAnUnsigned() )
139 {
140 version = putative_version.ToUnsigned();
141 ++version;
142 }
143 str = fname_without_extension + "_" + faststring(version);
144 // cerr << "next str in series: " << str << endl;
145 if (z != NULL)
146 {
147 *z = version;
148 }
149 }
150
151
152 // faststring path = str.get_path();
153 // faststring name = str.get_FileOrDirName();
154 // faststring major_name = name;
155 // unsigned version = 0;
156 // // cerr << path << "/" << name << endl;
157 // // cerr << major_name << endl;
158
159 // unsigned pos_underscore = major_name.rfind('_');
160 // if (pos_underscore != faststring::npos)
161 // {
162 // faststring putative_version(major_name.begin()+pos_underscore+1, major_name.end());
163 // // cerr << "putative_version: " << putative_version << endl;
164 // if (putative_version.isAnUnsigned())
165 // {
166 // version = putative_version.ToUnsigned();
167 // major_name.shorten(pos_underscore);
168 // // cerr << major_name << endl;
169 // }
170 // ++version;
171 // }
172 // str = path + "/" + major_name + "_" + faststring(version);
173 // // cerr << "next str in series: " << str << endl;
174 // if (z != NULL)
175 // {
176 // *z = version;
177 // }
178
179 faststring next_unique_file_or_dir_name(faststring str, unsigned *z)
180 {
181 faststring fname_without_extension;
182 faststring putative_version;
183
184 str.backwards_search_divide_at_break_at('_','/',fname_without_extension, putative_version);
185
186
187 unsigned version = 0;
188 // cerr << "putative_version: " << putative_version << endl;
189
190 if (!putative_version.empty() && putative_version.isAnUnsigned() )
191 {
192 version = putative_version.ToUnsigned();
193 ++version;
194 }
195 str = fname_without_extension + "_" + faststring(version);
196 // cerr << "next str in series: " << str << endl;
197 if (z != NULL)
198 {
199 *z = version;
200 }
201
202 while (file_or_dir_exists(str.c_str()))
203 {
204 ++version;
205 str = fname_without_extension + "_" + faststring(version);
206 }
207 return str;
208 }
209
210 // faststring next_unique_file_or_dir_name(faststring str, unsigned *z)
211 // {
212 // faststring path = str.get_path();
213 // faststring name = str.get_FileOrDirName();
214 // faststring major_name = name;
215 // unsigned version = 0;
216 // // cerr << path << "/" << name << endl;
217 // // cerr << major_name << endl;
218
219
220 // unsigned pos_underscore = major_name.rfind('_');
221 // if (pos_underscore != faststring::npos)
222 // {
223 // faststring putative_version(major_name.begin()+pos_underscore+1, major_name.end());
224 // // cerr << "putative_version: " << putative_version << endl;
225
226 // if (putative_version.isAnUnsigned())
227 // {
228 // version = putative_version.ToUnsigned();
229 // major_name.shorten(pos_underscore);
230 // // cerr << major_name << endl;
231 // }
232 // }
233 // ++version;
234 // str = path + "/" + major_name + "_" + faststring(version);
235 // // cerr << "str: " << str << endl;
236
237
238 // while (file_or_dir_exists(str.c_str()))
239 // {
240 // ++version;
241 // str = path + "/" + major_name + "_" + faststring(version);
242 // }
243
244 // if (z != NULL)
245 // {
246 // *z = version;
247 // }
248 // return str;
249 // }
250
251 // This version does not seem to work:
252 // faststring get_working_directory()
253 // {
254 // char buf[400];
255
256 // getcwd(buf, 400);
257 // return faststring(buf);
258 // }
259
260 // faststring get_working_directory2(char* argv0)
261 // {
262 // }
263
264
265 bool get_file_names_in_dir(std::list<faststring>& container, faststring &path_name)
266 {
267 faststring filepath;
268 DIR *dp;
269 struct dirent *dirp;
270 struct stat filestat;
271
272 path_name.removeSpacesBack(" /");
273
274 dp = opendir( path_name.c_str() );
275 if (dp == NULL)
276 {
277 return false;
278 }
279
280 while ((dirp = readdir( dp )))
281 {
282 filepath = path_name + "/" + dirp->d_name;
283
284 // If the file is a directory (or is in some way invalid) we'll skip it
285 if (stat( filepath.c_str(), &filestat )) continue;
286 if (S_ISDIR( filestat.st_mode )) continue;
287
288 container.push_back(dirp->d_name);
289 }
290 closedir( dp );
291
292 return true;
293 }
294
295 bool get_file_names_in_dir(std::list<faststring>& container, faststring &path_name, faststring &extension_filter)
296 {
297 faststring filepath, extension;
298 DIR *dp;
299 struct dirent *dirp;
300 struct stat filestat;
301
302 path_name.removeSpacesBack(" /");
303
304 dp = opendir( path_name.c_str() );
305 if (dp == NULL)
306 {
307 return false;
308 }
309
310 while ((dirp = readdir( dp )))
311 {
312 filepath = path_name + "/" + dirp->d_name;
313
314 // If the file is a directory (or is in some way invalid) we'll skip it
315 if (stat( filepath.c_str(), &filestat )) continue;
316 if (S_ISDIR( filestat.st_mode )) continue;
317
318 extension = filepath.filename_extension();
319
320 if (extension_filter == extension)
321 container.push_back(dirp->d_name);
322 }
323 closedir( dp );
324
325 return true;
326 }
327
328 bool get_file_names_in_dir(std::list<faststring>& container, faststring &path_name, std::list<faststring> &extension_filter)
329 {
330 faststring filepath, extension;
331 DIR *dp;
332 struct dirent *dirp;
333 struct stat filestat;
334
335 std::list<faststring>::iterator it, it1 = extension_filter.begin();
336 std::list<faststring>::iterator it2 = extension_filter.end();
337
338 path_name.removeSpacesBack(" /");
339
340 dp = opendir( path_name.c_str() );
341 if (dp == NULL)
342 {
343 return false;
344 }
345
346 while ((dirp = readdir( dp )))
347 {
348 filepath = path_name + "/" + dirp->d_name;
349
350 // If the file is a directory (or is in some way invalid) we'll skip it
351 if (stat( filepath.c_str(), &filestat )) continue;
352 if (S_ISDIR( filestat.st_mode )) continue;
353
354 extension = filepath.filename_extension();
355
356 it = it1;
357 while (it != it2)
358 {
359 if (*it == extension)
360 container.push_back(dirp->d_name);
361 ++it;
362 }
363 }
364 closedir( dp );
365
366 return true;
367 }
368
369 long long get_file_size(faststring path_name)
370 {
371 // cout << path_name.c_str() << endl;
372 struct stat filestatus;
373 if (stat( path_name.c_str(), &filestatus )==0)
374 {
375 return filestatus.st_size;
376 }
377
378 // This value is returned if no file size could be determined.
379 // E.g. if the file does not exist.
380 return -1;
381 }
382
383
384
385
386