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