mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
fileUtils.hpp
Go to the documentation of this file.
1
2/** \file fileUtils.hpp
3 * \brief Declarations of utilities for working with files
4 *
5 * \author Jared R. Males (jaredmales@gmail.com)
6 *
7 * \ingroup fileutils
8 *
9 */
10
11//***********************************************************************//
12// Copyright 2015-2020 Jared R. Males (jaredmales@gmail.com)
13//
14// This file is part of mxlib.
15//
16// mxlib is free software: you can redistribute it and/or modify
17// it under the terms of the GNU General Public License as published by
18// the Free Software Foundation, either version 3 of the License, or
19// (at your option) any later version.
20//
21// mxlib is distributed in the hope that it will be useful,
22// but WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24// GNU General Public License for more details.
25//
26// You should have received a copy of the GNU General Public License
27// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
28//***********************************************************************//
29
30#ifndef filtUtils_hpp
31#define filtUtils_hpp
32
33#include <string>
34#include <vector>
35
36#include "../mxlib.hpp"
37
38namespace mx
39{
40namespace ioutils
41{
42
43/** \addtogroup fileutils
44 * @{
45 */
46
47/// Check if a path exists
48/**
49 * \returns true if the path exists
50 * \returns false if it doesn't
51 */
52bool exists( const std::string &path /**< [in] the path to check for existence */ );
53
54/// Create a directory or directories
55/** This will create any directories in path that don't exist. It silently ignores already existing directories.
56 *
57 * \returns 0 on success, indicating the directories were created or already existed.
58 * \returns -1 on error
59 */
60int createDirectories( const std::string &path /**< [in] the path of the directory(ies)to create */ );
61
62/// Get the stem of the filename
63/**
64 * \returns the stem for the filename, that is without the path or extension
65 */
66std::string pathStem( const std::string &fname );
67
68/// Get the base filename
69/**
70 * \returns the filename, including the extension but without the path
71 */
72std::string pathFilename( const std::string &fname );
73
74/// Get the parent path from a filename
75/**
76 * \returns the parent path of the file
77 */
78std::string parentPath( const std::string &fname );
79
80/// Get a list of file names from the specified directory, specifying a prefix, a substring to match, and an extension
81/**
82 * \returns mx::error_t::success on success
83 * \returns mx::error_t::invalidarg if \p directory is not a directory
84 * \returns mx::error_t::dirnotfound if \p directory does not exist
85 * \returns mx::error_t::exception if an exception is thrown from the standard library
86 *
87 * \tparam verbose if true then error messages are printed as they occur
88 *
89 * \b Tests:
90 * - Getting a list of files \ref tests_ioutils_fileUtils_getFileNames "[test doc]"
91 *
92 */
93template<class verboseT=verbose::vvv>
94error_t getFileNames( std::vector<std::string> &fileNames, /** [out] The populated list of file names.*/
95 const std::string &directory, /**< [in] The path to the directory to search.
96 Can not be empty.*/
97 const std::string &prefix, /**< [in] The file name prefix (the beginning
98 characters of the file name) to search
99 for. If "" then not used.*/
100 const std::string &substr, /**< [in] A substring of the filename to search
101 for. If "" then not used. Only matches
102 after the first character.*/
103 const std::string &extension /**< [in] The file name extension to search for.
104 If "" then not used. This does not need
105 to include the ".", as in".ext".*/
106);
107
108template<>
109error_t getFileNames<verbose::o>( std::vector<std::string> &fileNames,
110 const std::string &directory,
111 const std::string &prefix,
112 const std::string &substr,
113 const std::string &extension
114);
115
116template<>
117error_t getFileNames<verbose::v>( std::vector<std::string> &fileNames,
118 const std::string &directory,
119 const std::string &prefix,
120 const std::string &substr,
121 const std::string &extension
122);
123
124template<>
125error_t getFileNames<verbose::vv>( std::vector<std::string> &fileNames,
126 const std::string &directory,
127 const std::string &prefix,
128 const std::string &substr,
129 const std::string &extension
130);
131
132template<>
133error_t getFileNames<verbose::vvv>( std::vector<std::string> &fileNames,
134 const std::string &directory,
135 const std::string &prefix,
136 const std::string &substr,
137 const std::string &extension
138);
139/// Prepend and/or append strings to a file name, leaving the directory and extension unaltered.
140/**
141 * \returns the new file name
142 */
143std::string fileNamePrependAppend( const std::string &fname, /**< [in] the original file name, possibly including a
144 directory and extension*/
145 const std::string &prepend, /**< [in] is the string to insert at the beginning of the
146 file name after the path*/
147 const std::string &append /**< [in] is the string to insert at the end of the file
148 name, before the extension*/
149);
150
151/// Append a string to a file name, leaving the directory and extension unaltered.
152/**
153 * \returns the new file name
154 */
155std::string fileNameAppend( const std::string &fname, /**< [in] the original file name, possibly including
156 a directory and extension*/
157 const std::string &append /**< [in] is the string to insert at the end
158 of the file name, before the extension*/
159);
160
161/// Prepend strings to a file name, leaving the directory and extension unaltered.
162/**
163 * \returns the new file name
164 */
165std::string fileNamePrepend( const std::string &fname, /**< [in] the original file name, possibly including
166 a directory and extension*/
167 const std::string &prepend /**< [in] is the string to insert at the beginning of
168 the file name after the path*/
169);
170
171/// Get the next file in a numbered sequence
172/** Searches for files in the path designated by basename of the form basenameXXXXextension
173 * where the number of digits in XXXX is set by the \a ndigit parameter.
174 *
175 * \warning this does not currently detect missing files in the sequence, e.g. if you have 0,1,3 in the directory this
176 * will start with 2!
177 *
178 * \todo switch to using a regex or something so we can detect the missing file.
179 *
180 * \retval std::string containing the next filename.
181 *
182 * \test Verify creation of sequential file names \ref tests_ioutils_fileUtils_getSequentialFilename "[test doc]"
183 */
184std::string getSequentialFilename( const std::string &basename, ///< [in] path and initial name of the file*/
185 const std::string &extension = "", /**< [in] [optional] extension to append after the
186 number. Default is empty.*/
187 const int startat = 0, /**< [in] [optional] number to start the
188 search from.
189 Default is 0.*/
190 int ndigit = 4 /**< [in] [optional] number of digits in string
191 representation
192 of the number.Default is 4. */
193);
194
195/// Get the size in bytes of a file
196/** Uses fstat.
197 *
198 * \returns the file size if fd is valid and no errors occur
199 * \returns -1 on an error
200 */
201off_t fileSize( int fd /**< [in] an open file descriptor */ );
202
203/// Get the size in bytes of a file pointed to by a FILE pointer
204/** Uses fileno to get the associated descriptor, then uses fstat.
205 *
206 * \returns the file size if fd is valid and no errors occur
207 * \returns -1 on an error
208 *
209 * \overload
210 */
211off_t fileSize( FILE *f /**< [in] an open file */ );
212
213///@} -fileutils
214
215} // namespace ioutils
216} // namespace mx
217
218#endif // fileUtils_hpp
error_t
The mxlib error codes.
Definition error_t.hpp:20
error_t getFileNames(std::vector< std::string > &fileNames, const std::string &directory, const std::string &prefix, const std::string &substr, const std::string &extension)
Get a list of file names from the specified directory, specifying a prefix, a substring to match,...
std::string fileNamePrepend(const std::string &fname, const std::string &prepend)
Prepend strings to a file name, leaving the directory and extension unaltered.
std::string getSequentialFilename(const std::string &basename, const std::string &extension="", const int startat=0, int ndigit=4)
Get the next file in a numbered sequence.
std::string fileNamePrependAppend(const std::string &fname, const std::string &prepend, const std::string &append)
Prepend and/or append strings to a file name, leaving the directory and extension unaltered.
std::string fileNameAppend(const std::string &fname, const std::string &append)
Append a string to a file name, leaving the directory and extension unaltered.
int createDirectories(const std::string &path)
Create a directory or directories.
Definition fileUtils.cpp:57
std::string parentPath(const std::string &fname)
Get the parent path from a filename.
Definition fileUtils.cpp:82
std::string pathStem(const std::string &fname)
Get the stem of the filename.
Definition fileUtils.cpp:70
bool exists(const std::string &path)
Check if a path exists.
Definition fileUtils.cpp:52
off_t fileSize(int fd)
Get the size in bytes of a file.
std::string pathFilename(const std::string &fname)
Get the base filename.
Definition fileUtils.cpp:76
The mxlib c++ namespace.
Definition mxError.hpp:40