mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
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 
38 namespace mx
39 {
40 namespace ioutils
41 {
42 
43 /** \addtogroup fileutils
44  * @{
45  */
46 
47 /// Create a directory or directories
48 /** This will create any directories in path that don't exist. It silently ignores already existing directories.
49  *
50  * \returns 0 on success, indicating the directories were created or already existed.
51  * \returns -1 on error
52  */
53 int createDirectories( const std::string & path /**< [in] the path of the directory(ies)to create */);
54 
55 /// Get the stem of the filename
56 /**
57  * \returns the stem for the filename, that is without the path or extension
58  */
59 std::string pathStem(const std::string & fname);
60 
61 /// Get the base filename
62 /**
63  * \returns the filename, including the extension but without the path
64  */
65 std::string pathFilename( const std::string & fname);
66 
67 /// Get the parent path from a filename
68 /**
69  * \returns the parent path of the file
70  */
71 std::string parentPath(const std::string & fname);
72 
73 ///Get a list of file names from the specified directory, specifying a prefix, a substring to match, and an extension
74 /**
75  * \returns a std::vector<std::string> which contains the matching file names.
76  */
77 std::vector<std::string> getFileNames( const std::string & directory, ///< [in] the path to the directory to search. Can not be empty.
78  const std::string & prefix, ///< [in] the file name prefix (the beginning characters of the file name) to search for, if "" then not used.
79  const std::string & substr, ///< [in] a substring of the filename to earch for, if "" then not used.
80  const std::string & extension ///< [in] the file name extension to search for, if "" then not used. Note that this must include the ".", as in".ext".
81  );
82 
83 ///Get a list of file names from the specified directory, specifying the extension
84 /** \overload
85  *
86  * \returns a std::vector<std::string> which contains the matching file names.
87  */
88 std::vector<std::string> getFileNames( const std::string & directory, ///< [in] the path to the directory to search. Can not be empty.
89  const std::string & extension ///< [in] the file name extension to search for, if "" then not used. Note that this must include the ".", as in ".ext".
90  );
91 
92 ///Get a list of file names from the specified directory
93 /** \overload
94  *
95  * \returns a std::vector<std::string> which contains the matching file names.
96  */
97 std::vector<std::string> getFileNames( const std::string & directory /**< [in] the path to the directory to search. Can not be empty. */);
98 
99 
100 ///Prepend and/or append strings to a file name, leaving the directory and extension unaltered.
101 /**
102  * \returns the new file name
103  */
104 std::string fileNamePrependAppend( const std::string & fname, ///< [in] the original file name, possibly including a directory and extension
105  const std::string & prepend, ///< [in] is the string to insert at the beginning of the file name after the path
106  const std::string & append ///< [in] is the string to insert at the end of the file name, before the extension
107  );
108 
109 ///Append a string to a file name, leaving the directory and extension unaltered.
110 /**
111  * \returns the new file name
112  */
113 std::string fileNameAppend( const std::string & fname, ///< [in] the original file name, possibly including a directory and extension
114  const std::string & append ///< [in] is the string to insert at the end of the file name, before the extension
115  );
116 
117 ///Prepend strings to a file name, leaving the directory and extension unaltered.
118 /**
119  * \returns the new file name
120  */
121 std::string fileNamePrepend( const std::string & fname, ///< [in] the original file name, possibly including a directory and extension
122  const std::string & prepend ///< [in] is the string to insert at the beginning of the file name after the path
123  );
124 
125 ///Get the next file in a numbered sequence
126 /** Searches for files in the path designated by basename of the form basenameXXXXextension
127  * where the number of digits in XXXX is set by the \a ndigit parameter.
128  *
129  * \warning this does not currently detect missing files in the sequence, e.g. if you have 0,1,3 in the directory this will start with 2!
130  *
131  * \todo switch to using a regex or something so we can detect the missing file.
132  *
133  * \retval std::string containing the next filename.
134  *
135  * \test Verify creation of sequential file names \ref tests_ioutils_fileUtils_getSequentialFilename "[test doc]"
136  */
137 std::string getSequentialFilename( const std::string & basename, ///< [in] path and initial name of the file
138  const std::string & extension = "", ///< [in] [optional] extension to append after the number. Default is empty.
139  const int startat = 0, ///< [in] [optional] number to start the search from. Default is 0.
140  int ndigit = 4 ///< [in] [optional] number of digits in string representation of the number. Default is 4.
141  );
142 
143 /// Get the size in bytes of a file
144 /** Uses fstat.
145  *
146  * \returns the file size if fd is valid and no errors occur
147  * \returns -1 on an error
148  */
149 off_t fileSize( int fd /**< [in] an open file descriptor */);
150 
151 /// Get the size in bytes of a file pointed to by a FILE pointer
152 /** Uses fileno to get the associated descriptor, then uses fstat.
153  *
154  * \returns the file size if fd is valid and no errors occur
155  * \returns -1 on an error
156  *
157  * \overload
158  */
159 off_t fileSize( FILE * f /**< [in] an open file */);
160 
161 ///@} -fileutils
162 
163 } //namespace ioutils
164 } //namespace mx
165 
166 #endif //fileUtils_hpp
std::string fileNamePrepend(const std::string &fname, const std::string &prepend)
Prepend strings to a file name, leaving the directory and extension unaltered.
Definition: fileUtils.cpp:206
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.
Definition: fileUtils.cpp:213
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.
Definition: fileUtils.cpp:181
std::string fileNameAppend(const std::string &fname, const std::string &append)
Append a string to a file name, leaving the directory and extension unaltered.
Definition: fileUtils.cpp:199
int createDirectories(const std::string &path)
Create a directory or directories.
Definition: fileUtils.cpp:52
off_t fileSize(FILE *f)
Get the size in bytes of a file pointed to by a FILE pointer.
Definition: fileUtils.cpp:271
std::string parentPath(const std::string &fname)
Get the parent path from a filename.
Definition: fileUtils.cpp:77
std::string pathStem(const std::string &fname)
Get the stem of the filename.
Definition: fileUtils.cpp:65
std::vector< std::string > getFileNames(const std::string &directory)
Get a list of file names from the specified directory.
Definition: fileUtils.cpp:175
std::string pathFilename(const std::string &fname)
Get the base filename.
Definition: fileUtils.cpp:71
The mxlib c++ namespace.
Definition: mxError.hpp:107