[go: up one dir, main page]

Menu

[r201]: / mia2 / mia / core / filetools.cc  Maximize  Restore  History

Download this file

223 lines (182 with data), 5.5 kB

  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
/* -*- mia-c++ -*-
* Copyright (c) 2006 Max-Planck-Institute of Evolutionary Anthropology
*
* 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 2 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sstream>
#include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
#include "boost/filesystem/fstream.hpp" // ditto
#include <mia/core/msgstream.hh>
#include <mia/core/filetools.hh>
NS_MIA_BEGIN
using namespace std;
using namespace mia;
namespace bfs = ::boost::filesystem;
#ifndef MAX_PATH
#define MAX_PATH 4096
#endif
vector<string> get_consecutive_numbered_files_from_pattern(string const& in_filename, int start, int end)
{
char buffer[MAX_PATH];
int num = start;
vector<string> result;
snprintf(buffer, MAX_PATH, in_filename.c_str(), num);
string first_filename(buffer);
while (!bfs::exists(buffer) && num < end) {
++num;
snprintf(buffer, MAX_PATH, in_filename.c_str(), num);
// no file found
if (first_filename == string(buffer))
return result;
}
while (bfs::exists(buffer) && num < end) {
result.push_back(string(buffer));
++num;
snprintf(buffer, MAX_PATH, in_filename.c_str(), num);
// run through all possible file names
if (first_filename == string(buffer))
break;
}
return result;
}
const std::string get_filename_pattern_and_range(std::string const& in_filename, size_t& start_filenum, size_t& end_filenum, size_t& format_width)
{
string base_name;
size_t nwidth = format_width = fname_to_cformat(in_filename.c_str(), base_name, false);
if (nwidth > 0) { // probably more then one slice
size_t max_num = 1;
while (nwidth--)
max_num *= 10;
start_filenum = end_filenum = 0;
cvdebug() << "trying files of pattern " << base_name << " n=" << nwidth << "\n";
// search for the first existing file
string filename = create_filename(base_name.c_str(), start_filenum);
while (!bfs::exists(filename) && start_filenum < max_num) {
++start_filenum;
filename = create_filename(base_name.c_str(), start_filenum);
}
end_filenum = start_filenum;
while (bfs::exists(filename) && end_filenum < max_num) {
++end_filenum;
filename = create_filename(base_name.c_str(), end_filenum);
if (end_filenum == 0)
break;
}
}
return base_name;
}
vector<string> get_consecutive_numbered_files(string const& in_filename)
{
vector<string> src_names;
string base_name;
// get all possible file names
size_t nwidth = fname_to_cformat(in_filename.c_str(), base_name, false);
if (nwidth > 0) { // probably more then one slice
// check, how many consecutive files we have
int n_files = 0;
size_t start_filenum = 0;
cvdebug() << "trying files of pattern " << base_name << " n="
<< nwidth << "\n";
// search for the first existing file
string filename = create_filename(base_name.c_str(), start_filenum);
string first_filename = filename;
size_t max_num = 1;
while (nwidth--)
max_num *= 10;
while (!bfs::exists(filename) && start_filenum < max_num) {
++start_filenum;
filename = create_filename(base_name.c_str(), start_filenum);
}
while (bfs::exists(filename) && start_filenum < max_num) {
src_names.push_back(filename);
++start_filenum;
++n_files;
filename = create_filename(base_name.c_str(), start_filenum);
if (filename == src_names[0])
break;
}
cvdebug() << "Found " << n_files << " input files\n";
}else{
src_names.push_back(in_filename);
}
return src_names;
}
size_t fname_to_cformat(const char *fname, string& base, bool wildcard)
{
char *help = strdup(fname);
char *suffix = strrchr(help, '.');
if (suffix == help) {
base.assign(fname);
free(help);
return 0;
}
char *num = suffix ? suffix : help + strlen(help);
--num;
size_t nwidth = 0;
while (isdigit(*num)) {
++nwidth;
if (num == help)
break;
--num;
}
if (nwidth == 0) {
base.assign(fname);
free(help);
return 0;
}
stringstream result;
if (num != help) {
num[1] = '\0'; // cut of the tail
result << help;
}
if (wildcard) {
for (size_t i = 0; i < nwidth; ++i)
result << "?";
}else {
result << "%0" << nwidth << "d";
}
if (suffix)
result << suffix;
base = result.str();
free(help);
return nwidth;
}
void split_dir_fname(const char *in_name, std::string& dir, std::string& fname)
{
char *help = strdup(in_name);
char *filename = strrchr(help, '/');
if (filename) {
*filename = 0;
++filename;
dir.assign(help);
fname.assign(filename);
}else {
dir.assign("./");
fname.assign(help);
}
free(help);
}
// ugly
string create_filename(const char *cformat, size_t num)
{
char buf[FILENAME_MAX];
snprintf(buf, FILENAME_MAX, cformat, num);
return string(buf);
}
NS_MIA_END