MeteoIODoc 20260616.90c51eec
Environmental timeseries pre-processing
Loading...
Searching...
No Matches
FileUtils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/* Copyright 2014 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3/***********************************************************************************/
4/* This file is part of MeteoIO.
5 MeteoIO is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 MeteoIO is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef FILEUTILS_H
19#define FILEUTILS_H
20
21#include <string>
22#include <map>
23#include <vector>
24#include <list>
25
27#include <meteoio/FStream.h>
28
29namespace mio {
30namespace FileUtils {
31
38 void copy_file(const std::string& src, const std::string& dest);
39
52 void readDirectory(const std::string& path, std::list<std::string>& dirlist, const std::string& pattern="", const bool& isRecursive=false);
53
54 std::list<std::string> readDirectory(const std::string& path, const std::string& pattern="", const bool& isRecursive=false);
55
56 bool directoryExists(const std::string &path);
57 bool isWindowsPath(const std::string &path);
63 void createDirectories(const std::string &path);
64
65 bool validFileAndPath(const std::string& filename);
66
67 bool fileExists(const std::string& filename);
68
76 std::string cleanPath(std::string in_path, const bool& resolve=false, const bool& silent=false);
77
78
84 bool isSubDirOf(const std::string& in_path, const std::string& root_path);
85
92 bool isAllowedPath(const std::string& path, const bool& is_write);
93
100 FILE* fopen_safe(const char* filename, const char* mode);
101
110 std::string getExtension(const std::string& filename);
111
120 std::string removeExtension(const std::string& filename);
121
126 std::string getCWD();
127
132 std::string getDateTime();
133
140 std::string getPath(const std::string& filename, const bool& resolve=false);
141
147 bool isAbsolutePath(const std::string& in_path);
148
154 std::string getFilename(const std::string& path);
155
156 char getEoln(std::istream& fin);
157
158 void skipLines(std::istream& fin, const size_t& nbLines, const char& eoln='\n');
159
160 std::map<std::string,std::string> readKeyValueHeader(std::istream& fin,
161 const size_t& linecount=1,
162 const std::string& delimiter="=", const bool& keep_case=false);
163
174 public:
175 FileIndexer() : vecIndex() {}
176
183 void setIndex(const Date& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
184 void setIndex(const std::string& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
185 void setIndex(const double& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
186
193 std::streampos getIndex(const Date& i_date) const;
194 std::streampos getIndex(const std::string& i_date) const;
195 std::streampos getIndex(const double& i_date) const;
196
204 std::streampos getIndex(const Date& i_date, size_t& o_linenr) const;
205 std::streampos getIndex(const std::string& i_date, size_t& o_linenr) const;
206 std::streampos getIndex(const double& i_date, size_t& o_linenr) const;
207
208 const std::string toString() const;
209
210 private:
211 struct file_index {
212 file_index(const Date& i_date, const std::streampos& i_pos, const size_t& i_linenr=static_cast<size_t>(-1)) : date(i_date), linenr(i_linenr), pos(i_pos) {}
213 bool operator<(const file_index& a) const {
214 return date < a.date;
215 }
216 bool operator>(const file_index& a) const {
217 return date > a.date;
218 }
219 Date date;
220 size_t linenr;
221 std::streampos pos;
222 };
223 size_t binarySearch(const Date& soughtdate) const;
224
225 std::vector< struct file_index > vecIndex;
226 };
227
228} //end namespace FileUtils
229} //end namespace mio
230
231#endif
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition Date.h:87
Definition FileUtils.h:173
const std::string toString() const
Definition FileUtils.cc:669
FileIndexer()
Definition FileUtils.h:175
std::streampos getIndex(const Date &i_date) const
Get the file position suitable for a given date.
Definition FileUtils.cc:610
void setIndex(const Date &i_date, const std::streampos &i_pos, const size_t &linenr=static_cast< size_t >(-1))
Add a new position to the index.
Definition FileUtils.cc:579
void createDirectories(const std::string &path)
creates the directory tree for the given path, including missing intermediate directories (gives only...
Definition FileUtils.cc:334
FILE * fopen_safe(const char *filename, const char *mode)
Open a file safely according to the sandbox restrictions.
Definition FileUtils.cc:219
std::string getCWD()
returns the current working directory.
Definition FileUtils.cc:425
std::string getPath(const std::string &filename, const bool &resolve)
returns the path preceeding a given filename.
Definition FileUtils.cc:253
void copy_file(const std::string &src, const std::string &dest)
Copies a files from one location to another.
Definition FileUtils.cc:69
std::string removeExtension(const std::string &filename)
remove the extension part of a given filename.
Definition FileUtils.cc:243
std::string cleanPath(std::string in_path, const bool &resolve, const bool &silent)
Replace "\" by "/" in a string so that a path string is cross plateform, optionally resolve links,...
Definition FileUtils.cc:90
std::string getFilename(const std::string &path)
extract the file name from a path+filename string.
Definition FileUtils.cc:264
bool fileExists(const std::string &filename)
Definition FileUtils.cc:434
bool isAllowedPath(const std::string &path, const bool &is_write)
Check if a path is safe to access according to the sandbox restrictions.
Definition FileUtils.cc:186
std::string getExtension(const std::string &filename)
returns the extension part of a given filename.
Definition FileUtils.cc:230
bool isWindowsPath(const std::string &path)
Definition FileUtils.cc:328
char getEoln(std::istream &fin)
Definition FileUtils.cc:503
std::string getDateTime()
returns the current date and time as a string.
Definition FileUtils.cc:273
bool validFileAndPath(const std::string &filename)
Definition FileUtils.cc:282
void readDirectory(const std::string &path, std::list< std::string > &dirlist, const std::string &pattern, const bool &isRecursive)
Build a list of file in a given directory.
Definition FileUtils.cc:307
void skipLines(std::istream &fin, const size_t &nbLines, const char &eoln)
Definition FileUtils.cc:536
bool isAbsolutePath(const std::string &in_path)
checks if a path is an absolute path
Definition FileUtils.cc:298
bool directoryExists(const std::string &path)
Definition FileUtils.cc:319
bool isSubDirOf(const std::string &in_path, const std::string &root_path)
Check if a given path (here in_path) is a subpath of another one (here root_path)
Definition FileUtils.cc:141
std::map< std::string, std::string > readKeyValueHeader(std::istream &fin, const size_t &linecount, const std::string &delimiter, const bool &keep_case)
Definition FileUtils.cc:546
Definition Config.cc:34