-
Notifications
You must be signed in to change notification settings - Fork 23
/
File_Loader.cc
436 lines (387 loc) · 17.9 KB
/
File_Loader.cc
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//File_Loader.cc - A part of DICOMautomaton 2019, 2021. Written by hal clark.
#include <exception>
//#include <functional>
#include <iostream>
#include <list>
#include <map>
//#include <memory>
#include <string>
#include <vector>
//#include <cfenv> //Needed for std::feclearexcept(FE_ALL_EXCEPT).
#include <initializer_list>
#include <filesystem>
//#include <cstdlib> //Needed for exit() calls.
//#include <utility> //Needed for std::pair.
#include "YgorFilesDirs.h" //Needed for Does_File_Exist_And_Can_Be_Read(...), etc..
#include "YgorMisc.h" //Needed for FUNCINFO, FUNCWARN, FUNCERR macros.
#include "YgorString.h" //Needed for GetFirstRegex(...)
#include "Structs.h"
#include "Boost_Serialization_File_Loader.h"
#include "DICOM_File_Loader.h"
#include "FITS_File_Loader.h"
#include "XYZ_File_Loader.h"
#include "XIM_File_Loader.h"
#include "SNC_File_Loader.h"
#include "OFF_File_Loader.h"
#include "STL_File_Loader.h"
#include "OBJ_File_Loader.h"
#include "PLY_File_Loader.h"
#include "3ddose_File_Loader.h"
#include "Line_Sample_File_Loader.h"
#include "Transformation_File_Loader.h"
#include "TAR_File_Loader.h"
#include "DVH_File_Loader.h"
#include "CSV_File_Loader.h"
#include "Script_Loader.h"
#include "Contour_Collection_File_Loader.h"
using loader_func_t = std::function<bool(std::list<std::filesystem::path>&)>;
struct file_loader_t {
std::list<std::string> exts;
long int priority;
loader_func_t f;
};
// This routine loads files. In order for it to return true, all files need to be successfully read.
// If a file cannot be read, all others are tried before returning false.
bool
Load_Files( Drover &DICOM_data,
std::map<std::string,std::string> &InvocationMetadata,
const std::string &FilenameLex,
std::list<OperationArgPkg> &Operations,
std::list<std::filesystem::path> &Paths ){
// Generate a priority list of file loaders.
// Note that some file loaders are extremely generous in what they accept, so feeding them generic files could
// result in false-positives and invalid data. The following default order was determined heuristically.
const auto get_default_loaders = [&](){
std::list<file_loader_t> loaders;
long int priority = 0;
//Standalone file loading: TAR files.
loaders.emplace_back(file_loader_t{{".tar", ".gz", ".tar.gz", ".tgz"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_TAR_Files( DICOM_data, InvocationMetadata, FilenameLex, Operations, p )){
FUNCWARN("Failed to load TAR file");
return false;
}
return true;
}});
//Standalone file loading: Boost.Serialization archives.
loaders.emplace_back(file_loader_t{{".gz", ".tar", ".tar.gz", ".tgz", ".xml", ".xml.gz", ".txt", ".txt.gz"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_Boost_Serialization_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load Boost.Serialization archive");
return false;
}
return true;
}});
//Standalone file loading: DICOM files.
loaders.emplace_back(file_loader_t{{".dcm"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_DICOM_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load DICOM file");
return false;
}
return true;
}});
//Standalone file loading: XIM files.
loaders.emplace_back(file_loader_t{{".xim"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_XIM_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load XIM file");
return false;
}
return true;
}});
//Standalone file loading: SNC files.
loaders.emplace_back(file_loader_t{{".snc"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_SNC_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load ASCII SNC file");
return false;
}
return true;
}});
//Standalone file loading: (ASCII or binary) PLY (mesh or point cloud) files.
loaders.emplace_back(file_loader_t{{".ply"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_PLY_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load ASCII/binary PLY mesh or point cloud file");
return false;
}
return true;
}});
//Standalone file loading: ASCII STL mesh files.
//
// Note: should preceed 'tabular DVH' line sample files.
loaders.emplace_back(file_loader_t{{".stl"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_Mesh_From_ASCII_STL_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load ASCII STL mesh file");
return false;
}
return true;
}});
//Standalone file loading: binary STL mesh files.
loaders.emplace_back(file_loader_t{{".stl"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_Mesh_From_Binary_STL_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load binary STL mesh file");
return false;
}
return true;
}});
//Standalone file loading: plaintext contour collection files.
loaders.emplace_back(file_loader_t{{".dat", ".txt"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_Contour_Collection_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load contour collection file");
return false;
}
return true;
}});
//Standalone file loading: 'tabular DVH' line sample files.
loaders.emplace_back(file_loader_t{{".dvh", ".txt", ".dat"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_DVH_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load DVH file");
return false;
}
return true;
}});
//Standalone file loading: script files.
loaders.emplace_back(file_loader_t{{".dcma", ".dsc", ".dscr", ".scr", ".txt"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_Script_Files( Operations, p )){
FUNCWARN("Failed to load script file");
return false;
}
return true;
}});
//Standalone file loading: FITS files.
loaders.emplace_back(file_loader_t{{".fit", ".fits"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_FITS_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load FITS file");
return false;
}
return true;
}});
//Standalone file loading: DOSXYZnrc 3ddose files.
loaders.emplace_back(file_loader_t{{".3ddose"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_3ddose_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load 3ddose file");
return false;
}
return true;
}});
//Standalone file loading: OFF point cloud files.
//
// Note: should preceed the OFF mesh loader.
loaders.emplace_back(file_loader_t{{".off"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_Points_From_OFF_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load OFF point cloud file");
return false;
}
return true;
}});
//Standalone file loading: OFF mesh files.
loaders.emplace_back(file_loader_t{{".off"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_Mesh_From_OFF_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load OFF mesh file");
return false;
}
return true;
}});
//Standalone file loading: OBJ point cloud files.
//
// Note: should preceed the OBJ mesh loader.
loaders.emplace_back(file_loader_t{{".obj"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_Points_From_OBJ_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load OBJ point cloud file");
return false;
}
return true;
}});
//Standalone file loading: OBJ mesh files.
loaders.emplace_back(file_loader_t{{".obj"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_Mesh_From_OBJ_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load OBJ mesh file");
return false;
}
return true;
}});
//Standalone file loading: XYZ point cloud files.
//
// Note: XYZ can be confused with many other formats, so it should be near the end.
loaders.emplace_back(file_loader_t{{".xyz", ".txt"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_XYZ_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load XYZ file");
return false;
}
return true;
}});
//Standalone file loading: transformation files.
//
// Note: this file can be confused with many other formats, so it should be near the end.
loaders.emplace_back(file_loader_t{{".trans", ".txt"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_Transforms_From_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load transformation file");
return false;
}
return true;
}});
//Standalone file loading: line sample files.
//
// Note: this file can be confused with many other formats, so it should be near the end.
loaders.emplace_back(file_loader_t{{".lsamp", ".lsamps", ".txt"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_Line_Sample_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load line sample file");
return false;
}
return true;
}});
//Standalone file loading: CSV files.
//
// Note: this file can be confused with many other formats, so it should be near the end.
loaders.emplace_back(file_loader_t{{".csv", ".tsv"}, ++priority, [&](std::list<std::filesystem::path> &p) -> bool {
if(!p.empty()
&& !Load_From_CSV_Files( DICOM_data, InvocationMetadata, FilenameLex, p )){
FUNCWARN("Failed to load CSV/TSV file");
return false;
}
return true;
}});
return loaders;
};
const auto has_recognized_extension = [=](const std::filesystem::path &p) -> bool {
const auto loaders = get_default_loaders();
const auto ext = p.extension().string();
const auto recognized = std::any_of( std::begin(loaders), std::end(loaders),
[ext](const file_loader_t &l){
return std::any_of( std::begin(l.exts),
std::end(l.exts),
[ext](const std::string &l_ext){ return icase_str_eq(ext, l_ext); });
});
return recognized;
};
// Convert directories to filenames and remove non-existent filenames and directories.
bool contained_unresolvable = false;
{
auto loaders = get_default_loaders();
std::list<std::filesystem::path> recursed_Paths;
std::list<std::filesystem::path> l_Paths;
while(!recursed_Paths.empty() || !Paths.empty()){
const auto is_orig = !Paths.empty();
auto p = (is_orig) ? Paths.front() : recursed_Paths.front();
if(is_orig){
Paths.pop_front();
}else{
recursed_Paths.pop_front();
}
try{
// Resolve the file to an absolute path if possible. This is useful for resolving symbolic links.
// Note that this won't be possible for Windows UNC paths like '\\server\share_name\path\to\a\file.dcm',
// so we have to confirm whether the absolute path can still be accessed.
const auto l_p = std::filesystem::absolute(p);
if( std::filesystem::exists(l_p) ) p = l_p;
if(! std::filesystem::exists(p) ){
throw std::runtime_error("Unable to resolve file or directory "_s + p.string() + "'");
}
if( std::filesystem::is_directory(p) ){
for(const auto &rp : std::filesystem::directory_iterator(p)){
recursed_Paths.push_back(rp);
}
}else{
// Only include files with recognized file extensions.
if( is_orig
|| has_recognized_extension(p)){
l_Paths.push_back(p);
}else{
FUNCWARN("Ignoring file '" << p.string() << "' because extension is not recognized. Specify explicitly to attempt loading");
}
}
}catch(const std::filesystem::filesystem_error &e){
FUNCWARN(e.what());
contained_unresolvable = true;
}
}
Paths = l_Paths;
}
// Partition the paths by file extension.
icase_map_t<std::list<std::filesystem::path>> extensions(icase_str_lt);
for(auto &p : Paths){
const auto ext = p.extension().string();
extensions[ext].push_back(p);
}
Paths.clear();
for(auto &ep : extensions){
const auto ext = ep.first;
auto &&l_Paths = ep.second;
// Warn if the file extension is not recognized.
for(const auto &p : l_Paths){
if(!has_recognized_extension(p)){
FUNCWARN("Unrecognized file extension '" << ext << "'. Attempting to load because it was explicitly specified");
}
}
// Boost the priority of any loaders whose extensions match this bunch of files.
auto loaders = get_default_loaders();
for(auto &l : loaders){
if(std::any_of( std::begin(l.exts),
std::end(l.exts),
[ext](const std::string &l_ext){ return icase_str_eq(ext, l_ext); })){
l.priority -= 1000;
}
}
// For select 'unique' extensions, exclude all other loaders that are likely to be irrelevant.
if( !ext.empty()
&& ( icase_str_eq(ext, ".dcm")
|| icase_str_eq(ext, ".xim")
|| icase_str_eq(ext, ".tar")
|| icase_str_eq(ext, ".tgz")
|| icase_str_eq(ext, ".gz")
|| icase_str_eq(ext, ".tar.gz")
|| icase_str_eq(ext, ".3ddose")
|| icase_str_eq(ext, ".stl")
|| icase_str_eq(ext, ".obj")
|| icase_str_eq(ext, ".off")
|| icase_str_eq(ext, ".ply")
|| icase_str_eq(ext, ".xyz")
|| icase_str_eq(ext, ".scr")
|| icase_str_eq(ext, ".dscr")
|| icase_str_eq(ext, ".csv")
|| icase_str_eq(ext, ".tsv")
|| icase_str_eq(ext, ".lsamps") ) ){
loaders.remove_if( [ext](const file_loader_t &l){
return std::none_of( std::begin(l.exts),
std::end(l.exts),
[&](const std::string &l_ext){ return icase_str_eq(ext, l_ext); });
} );
}
// Re-sort using the altered priorities.
loaders.sort( [](const file_loader_t &l, const file_loader_t &r){
return (l.priority < r.priority);
});
// Attempt to load the files.
for(const auto &l : loaders){
if(l_Paths.empty()) break;
std::stringstream ss;
for(const auto &e : l.exts) ss << (ss.str().empty() ? "" : ", ") << "'" << e << "'";
FUNCINFO("Trying loader for extensions: " << ss.str() << " for file(s) with extension '" << ext << "'");
if(!l_Paths.empty() && !l.f(l_Paths)){
return false;
}
}
// Return any remaining files to the user's container.
Paths.splice( std::end(Paths), l_Paths );
}
if(!Paths.empty()){
for(const auto &p : Paths) FUNCWARN("Unloaded file: '" << p.string() << "'");
}
return (Paths.empty() && !contained_unresolvable);
}