mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
fileUtils.cpp
Go to the documentation of this file.
1/** \file fileUtils.cpp
2 * \brief Definitions of utilities for working with files
3 *
4 * \author Jared R. Males (jaredmales@gmail.com)
5 *
6 * \ingroup fileutils
7 *
8 */
9
10//***********************************************************************//
11// Copyright 2020 Jared R. Males (jaredmales@gmail.com)
12//
13// This file is part of mxlib.
14//
15// mxlib is free software: you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation, either version 3 of the License, or
18// (at your option) any later version.
19//
20// mxlib is distributed in the hope that it will be useful,
21// but WITHOUT ANY WARRANTY; without even the implied warranty of
22// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23// GNU General Public License for more details.
24//
25// You should have received a copy of the GNU General Public License
26// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
27//***********************************************************************//
28
29#include "ioutils/fileUtils.hpp"
30
31#include <iostream>
32#include <string>
33#include <vector>
34#include <sstream>
35#include <libgen.h>
36#include <cmath>
37
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <unistd.h>
41
42#include <filesystem>
43
44
45namespace mx
46{
47namespace ioutils
48{
49
50template bool exists<verbose::d>( const std::string &, error_t &);
51
52template bool dir_exists_is<verbose::d>( const std::string &, error_t &);
53
54error_t createDirectories( const std::string &path )
55{
56 // Use the non throwing version and silently ignore EEXIST errors
57 std::error_code ec;
58 std::filesystem::create_directories( path, ec );
59 if( ec.value() != 0 && ec.value() != EEXIST )
60 {
61 mx::error_t errc = errno2error_t(ec.value());
62 if(errc == error_t::error)
63 {
65 }
66 return errc;
67 }
68
69 return error_t::noerror;
70}
71
72std::string pathStem( const std::string &fname )
73{
74 std::filesystem::path p( fname );
75 return p.stem().string();
76}
77
78std::string pathFilename( const std::string &fname )
79{
80 std::filesystem::path p( fname );
81 return p.filename().string();
82}
83
84std::string parentPath( const std::string &fname )
85{
86 std::filesystem::path p( fname );
87 return p.parent_path().string();
88}
89
90
91
92
93template
94error_t getFileNames<verbose::d>( std::vector<std::string> &fileNames,
95 const std::string &directory,
96 const std::string &prefix,
97 const std::string &substr,
98 const std::string &extension );
99
100
101
102std::string fileNamePrependAppend( const std::string &fname, const std::string &prepend, const std::string &append )
103{
104 std::string dir, base, ext;
105
106 std::filesystem::path p = fname;
107 dir = p.parent_path().string();
108 base = p.stem().string();
109 ext = p.extension().string();
110
111 return dir + '/' + prepend + base + append + ext;
112}
113
114std::string fileNameAppend( const std::string &fname, const std::string &append )
115{
116 return fileNamePrependAppend( fname, "", append );
117}
118
119std::string fileNamePrepend( const std::string &fname, const std::string &prepend )
120{
121 return fileNamePrependAppend( fname, prepend, "" );
122}
123
124std::string
125getSequentialFilename( const std::string &basename, const std::string &extension, const int startat, const int ndigit )
126{
127 // int maxdig = 1;
128 // for(int j=0;j<ndigit;++j) maxdig *= 10;
129 int maxdig = pow( 10, ndigit );
130
131 char formstr[64];
132 snprintf( formstr, sizeof( formstr ), "%%0%dd", ndigit );
133
134 char digstr[64];
135 int i = startat;
136
137 std::stringstream outn;
138
139 snprintf( digstr, sizeof( digstr ), formstr, i );
140
141 outn << basename;
142 outn << digstr;
143 outn << extension;
144
145 while( std::filesystem::exists( outn.str() ) && i < maxdig )
146 {
147 ++i;
148 outn.str( "" );
149
150 snprintf( digstr, sizeof( digstr ), formstr, i );
151
152 outn << basename;
153 outn << digstr;
154
155 outn << extension;
156 }
157
158 return outn.str();
159}
160
161off_t fileSize( int fd )
162{
163 if( fd == -1 )
164 {
165 return -1;
166 }
167
168 struct stat stbuf;
169
170 if( ( fstat( fd, &stbuf ) != 0 ) || ( !S_ISREG( stbuf.st_mode ) ) )
171 {
172 return -1;
173 }
174
175 return stbuf.st_size;
176}
177
178off_t fileSize( FILE *f )
179{
180 return fileSize( fileno( f ) );
181}
182
183} // namespace ioutils
184} // namespace mx
Declarations of utilities for working with files.
error_t
The mxlib error codes.
Definition error_t.hpp:26
static constexpr error_t errno2error_t(const int &err)
Convert an errno code to error_t.
Definition error_t.hpp:2006
@ noerror
No error has occurred.
@ filesystem
A general filesystem error occurred.
@ error
A general error has occurred.
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.
error_t createDirectories(const std::string &path)
Create a directory or directories.
Definition fileUtils.cpp:54
std::string parentPath(const std::string &fname)
Get the parent path from a filename.
Definition fileUtils.cpp:84
std::string pathStem(const std::string &fname)
Get the stem of the filename.
Definition fileUtils.cpp:72
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:78
The mxlib c++ namespace.
Definition mxlib.hpp:37