1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkRegularExpressionSeriesFileNames_h
19 #define itkRegularExpressionSeriesFileNames_h
20 #include "ITKIOImageBaseExport.h"
21 
22 
23 #include "itkObject.h"
24 #include "itkObjectFactory.h"
25 #include "itkMacro.h"
26 #include <vector>
27 
28 namespace itk
29 {
30 /** \class RegularExpressionSeriesFileNames
31  * \brief Generate an ordered sequence of filenames that match a
32  * regular expression.
33  *
34  * This class generates an ordered sequence of files whose filenames
35  * match a regular expression. The file names are sorted using a sub
36  * expression match selected by SubMatch. Regular expressions are a
37  * powerful,  compact mechanism for parsing strings.
38  * Expressions consist of the following metacharacters:
39  *
40  *  ^        Matches at beginning of a line
41  *
42  *  $        Matches at end of a line
43  *
44  * .         Matches any single character
45  *
46  * [ ]       Matches any character(s) inside the brackets
47  *
48  * [^ ]      Matches any character(s) not inside the brackets
49  *
50  *  -        Matches any character in range on either side of a dash
51  *
52  *  *        Matches preceding pattern zero or more times
53  *
54  *  +        Matches preceding pattern one or more times
55  *
56  *  ?        Matches preceding pattern zero or once only
57  *
58  * ()        Saves a matched expression and uses it in a  later match
59  *
60  * Note that more than one of these metacharacters can be  used
61  * in  a  single  regular expression in order to create complex
62  * search patterns. For example, the pattern [^ab1-9]  says  to
63  * match  any  character  sequence that does not begin with the
64  * characters "ab"  followed  by  numbers  in  the  series  one
65  * through nine.
66  *
67  * \ingroup IOFilters
68  *
69  * \ingroup ITKIOImageBase
70  */
71 class ITKIOImageBase_EXPORT RegularExpressionSeriesFileNames:public Object
72 {
73 public:
74   ITK_DISALLOW_COPY_AND_ASSIGN(RegularExpressionSeriesFileNames);
75 
76   /** Standard class type aliases. */
77   using Self = RegularExpressionSeriesFileNames;
78   using Superclass = Object;
79   using Pointer = SmartPointer< Self >;
80 
81   /** Method for creation through the object factory. */
82   itkNewMacro(Self);
83 
84   /** Run-time type information (and related methods). */
85   itkTypeMacro(RegularExpressionSeriesFileNames, Object);
86 
87   /* -------- Define the API for RegularExpressionSeriesFileNames ---------- */
88   /** The directory containing the files. */
89   itkSetStringMacro(Directory);
90   itkGetStringMacro(Directory);
91 
92   /** The RegularExpression. Refer to the description for valid expressions */
93   itkSetStringMacro(RegularExpression);
94   itkGetStringMacro(RegularExpression);
95 
96   /** The index of the submatch that will be used to sort the
97    * matches. */
98   itkSetMacro(SubMatch, unsigned int);
99   itkGetConstMacro(SubMatch, unsigned int);
100 
101   /** NumericSortOn changes the sort of the submatch field to a
102    * numeric sort. NumericSortOff is the default, and sorts the
103    * submatch alphabetically. */
104   itkSetMacro(NumericSort, bool);
105   itkGetConstMacro(NumericSort, bool);
106   itkBooleanMacro(NumericSort);
107 
108   /** Returns a vector containing the series' file names. The file
109     * names are sorted by the sub expression selected by the SubMatch id. */
110   const std::vector< std::string > & GetFileNames();
111 
112 protected:
RegularExpressionSeriesFileNames()113   RegularExpressionSeriesFileNames():
114     m_Directory("."),
115     m_RegularExpression(".*\\.([0-9]+)")
116   {}
117   ~RegularExpressionSeriesFileNames() override = default;
118   void PrintSelf(std::ostream & os, Indent indent) const override;
119 
120 private:
121   std::string  m_Directory;
122   unsigned int m_SubMatch{1};
123   bool         m_NumericSort{false};
124   std::string  m_RegularExpression;
125 
126   std::vector< std::string > m_FileNames;
127 };
128 } //namespace ITK
129 
130 #endif // itkRegularExpressionSeriesFileNames_h
131