Developer documentation
Version 3.0.3-105-gd3941f44
name_parser.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __file_name_parser_h__
18#define __file_name_parser_h__
19
20#include <memory>
21
22#include "mrtrix.h"
23#include "memory.h"
24#include "file/path.h"
25
26namespace MR
27{
28 namespace File
29 {
30
33 public:
35 public:
36 Item () : seq_length (0) { }
37
38 void set_str (const std::string& s) {
39 clear ();
40 str = s;
41 }
42
43 void set_seq (const std::string& s) {
44 clear ();
45 if (s.size()) seq = parse_ints<uint32_t> (s);
46 seq_length = 1;
47 }
48
49 void clear () {
50 str.clear();
51 seq.clear();
52 seq_length = 0;
53 }
54
55 std::string string () const {
56 return (str);
57 }
58
59 const vector<uint32_t>& sequence () const {
60 return (seq);
61 }
62
64 return (seq);
65 }
66
67 bool is_string () const {
68 return (seq_length == 0);
69 }
70
71 bool is_sequence () const {
72 return (seq_length != 0);
73 }
74
75 size_t size () const {
76 return (seq_length ? seq_length : str.size());
77 }
78
79 void calc_padding (size_t maxval = 0);
80
81 friend std::ostream& operator<< (std::ostream& stream, const Item& item);
82
83 protected:
84 size_t seq_length;
85 std::string str;
87 };
88
89
90 void parse (const std::string& imagename, size_t max_num_sequences = std::numeric_limits<size_t>::max());
91
92 size_t num () const {
93 return (array.size());
94 }
95
96 std::string spec () const {
97 return (specification);
98 }
99
100 const Item& operator[] (size_t i) const {
101 return (array[i]);
102 }
103
104 const vector<uint32_t>& sequence (size_t index) const {
105 return (array[seq_index[index]].sequence());
106 }
107
108 size_t ndim () const {
109 return (seq_index.size());
110 }
111
112 size_t index_of_sequence (size_t number = 0) const {
113 return (seq_index[number]);
114 }
115
116 bool match (const std::string& file_name, vector<uint32_t>& indices) const;
117 void calculate_padding (const vector<uint32_t>& maxvals);
118 std::string name (const vector<uint32_t>& indices);
119 std::string get_next_match (vector<uint32_t>& indices, bool return_seq_index = false);
120
121 friend std::ostream& operator<< (std::ostream& stream, const NameParser& parser);
122
123 private:
124 vector<Item> array;
125 vector<size_t> seq_index;
126 std::string folder_name, specification, current_name;
127 std::unique_ptr<Path::Dir> folder;
128
129 void insert_str (const std::string& str) {
130 Item item;
131 item.set_str (str);
132 array.insert (array.begin(), item);
133 }
134
135 void insert_seq (const std::string& str) {
136 Item item;
137 item.set_seq (str);
138 array.insert (array.begin(), item);
139 seq_index.push_back (array.size()-1);
140 }
141 };
142
143
144
145
146
147
148
149
152 public:
153 ParsedName (const std::string& name, const vector<uint32_t>& index) : indices (index), filename (name) { }
154
157 public:
158 vector<uint32_t> parse_scan_check (const std::string& specifier,
159 size_t max_num_sequences = std::numeric_limits<size_t>::max());
160
161 void scan (NameParser& parser);
162
164
165 size_t biggest_filename_size () const {
166 return max_name_size;
167 }
168
169 size_t size () const { return list.size(); }
170
171 const ParsedName& operator[] (size_t index) const { return *(list[index]); }
172
173 friend std::ostream& operator<< (std::ostream& stream, const List& list);
174
175 protected:
177 void count_dim (vector<uint32_t>& dim, size_t& current_entry, size_t current_dim) const;
179 };
180
181
182
183 std::string name () const {
184 return filename;
185 }
186 size_t ndim () const {
187 return indices.size();
188 }
189 uint32_t index (size_t num) const {
190 return indices[num];
191 }
192
193 bool operator< (const ParsedName& pn) const;
194 friend std::ostream& operator<< (std::ostream& stream, const ParsedName& pin);
195
196 protected:
198 std::string filename;
199
200 };
201
202 }
203}
204
205#endif
206
const vector< uint32_t > & sequence() const
Definition: name_parser.h:59
vector< uint32_t > seq
Definition: name_parser.h:86
std::string string() const
Definition: name_parser.h:55
vector< uint32_t > & sequence()
Definition: name_parser.h:63
void calc_padding(size_t maxval=0)
friend std::ostream & operator<<(std::ostream &stream, const Item &item)
void set_str(const std::string &s)
Definition: name_parser.h:38
void set_seq(const std::string &s)
Definition: name_parser.h:43
a class to interpret numbered filenames
Definition: name_parser.h:32
bool match(const std::string &file_name, vector< uint32_t > &indices) const
std::string get_next_match(vector< uint32_t > &indices, bool return_seq_index=false)
friend std::ostream & operator<<(std::ostream &stream, const NameParser &parser)
size_t index_of_sequence(size_t number=0) const
Definition: name_parser.h:112
void calculate_padding(const vector< uint32_t > &maxvals)
size_t ndim() const
Definition: name_parser.h:108
void parse(const std::string &imagename, size_t max_num_sequences=std::numeric_limits< size_t >::max())
const vector< uint32_t > & sequence(size_t index) const
Definition: name_parser.h:104
std::string spec() const
Definition: name_parser.h:96
const Item & operator[](size_t i) const
Definition: name_parser.h:100
size_t num() const
Definition: name_parser.h:92
std::string name(const vector< uint32_t > &indices)
a class to hold a set of parsed image filenames
Definition: name_parser.h:156
friend std::ostream & operator<<(std::ostream &stream, const List &list)
vector< std::shared_ptr< ParsedName > > list
Definition: name_parser.h:176
void scan(NameParser &parser)
const ParsedName & operator[](size_t index) const
Definition: name_parser.h:171
vector< uint32_t > count() const
size_t biggest_filename_size() const
Definition: name_parser.h:165
void count_dim(vector< uint32_t > &dim, size_t &current_entry, size_t current_dim) const
vector< uint32_t > parse_scan_check(const std::string &specifier, size_t max_num_sequences=std::numeric_limits< size_t >::max())
a class to hold a parsed image filename
Definition: name_parser.h:151
size_t ndim() const
Definition: name_parser.h:186
bool operator<(const ParsedName &pn) const
uint32_t index(size_t num) const
Definition: name_parser.h:189
std::string filename
Definition: name_parser.h:198
ParsedName(const std::string &name, const vector< uint32_t > &index)
Definition: name_parser.h:153
std::string name() const
Definition: name_parser.h:183
friend std::ostream & operator<<(std::ostream &stream, const ParsedName &pin)
vector< uint32_t > indices
Definition: name_parser.h:197
#define NOMEMALIGN
Definition: memory.h:22
Definition: base.h:24
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247
Item item
size_t index
size_t num
Definition: thread.h:216