Improve MessageStringCollector
[alexxy/gromacs.git] / src / gromacs / utility / directoryenumerator.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,2019,2021, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \libinternal \file
36  * \brief
37  * Declares gmx::DirectoryEnumerator.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_utility
42  */
43 #ifndef GMX_UTILITY_DIRECTORYENUMERATOR_H
44 #define GMX_UTILITY_DIRECTORYENUMERATOR_H
45
46 #include <memory>
47 #include <string>
48 #include <vector>
49
50 namespace gmx
51 {
52
53 /*! \libinternal \brief
54  * Lists files in a directory.
55  *
56  * If multiple threads share the same DirectoryEnumerator, they must
57  * take responsibility for their mutual synchronization, particularly
58  * with regard to calling nextFile().
59  *
60  * \inlibraryapi
61  * \ingroup module_utility
62  */
63 class DirectoryEnumerator
64 {
65 public:
66     /*! \brief
67      * Convenience function to list files with certain extension from a
68      * directory.
69      *
70      * \param[in]  dirname   Path to the directory to list.
71      * \param[in]  extension List files with the given extension
72      *     (or suffix in file name).
73      * \param[in]  bThrow    Whether failure to open the directory should throw.
74      * \returns    List of files with the given extension in \p dirname.
75      * \throws std::bad_alloc if out of memory.
76      * \throws FileIOError if opening the directory fails and `bThrow == true`.
77      * \throws FileIOError if some other I/O error occurs.
78      */
79     static std::vector<std::string> enumerateFilesWithExtension(const char* dirname,
80                                                                 const char* extension,
81                                                                 bool        bThrow);
82
83     /*! \brief
84      * Opens a directory for listing.
85      *
86      * \param[in] dirname Path to the directory to list.
87      * \param[in] bThrow  Whether failure to open the directory should throw.
88      * \throws std::bad_alloc if out of memory.
89      * \throws FileIOError if opening the directory fails and `bThrow == true`
90      */
91     explicit DirectoryEnumerator(const char* dirname, bool bThrow = true);
92     //! \copydoc DirectoryEnumerator(const char *, bool)
93     explicit DirectoryEnumerator(const std::string& dirname, bool bThrow = true);
94     ~DirectoryEnumerator();
95
96     /*! \brief
97      * Gets next file in a directory.
98      *
99      * \param[out] filename  Name of the next file.
100      * \returns `false` if there were no more files.
101      * \throws  std::bad_alloc if out of memory.
102      * \throws  FileIOError if listing the next file fails.
103      *
104      * If all files from the directory have been returned (or there are no
105      * files in the directory and this is the first call), the method
106      * returns `false` and \p filename is cleared.
107      * Otherwise, the return value is `true` and the first/next file name
108      * is returned in \p filename.
109      * \p filename will not contain any path information, only the name of
110      * the file.
111      *
112      * If `bThrow` passed to the constructor was `false` and the directory
113      * was not successfully opened, the first call to this function will
114      * return `false`.
115      *
116      * This method is not thread safe when called on the same
117      * object by multiple threads. Such use requires external
118      * synchronization.
119      */
120     bool nextFile(std::string* filename);
121
122 private:
123     class Impl;
124
125     std::unique_ptr<Impl> impl_;
126 };
127
128 } // namespace gmx
129
130 #endif