MeteoIODoc 20250905.f67af007
 
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
94 std::string getExtension(const std::string& filename);
95
104 std::string removeExtension(const std::string& filename);
105
110 std::string getCWD();
111
116 std::string getDateTime();
117
124 std::string getPath(const std::string& filename, const bool& resolve=false);
125
131 bool isAbsolutePath(const std::string& in_path);
132
138 std::string getFilename(const std::string& path);
139
140 char getEoln(std::istream& fin);
141
142 void skipLines(std::istream& fin, const size_t& nbLines, const char& eoln='\n');
143
144 std::map<std::string,std::string> readKeyValueHeader(std::istream& fin,
145 const size_t& linecount=1,
146 const std::string& delimiter="=", const bool& keep_case=false);
147
158 public:
159 FileIndexer() : vecIndex() {}
160
167 void setIndex(const Date& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
168 void setIndex(const std::string& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
169 void setIndex(const double& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
170
177 std::streampos getIndex(const Date& i_date) const;
178 std::streampos getIndex(const std::string& i_date) const;
179 std::streampos getIndex(const double& i_date) const;
180
188 std::streampos getIndex(const Date& i_date, size_t& o_linenr) const;
189 std::streampos getIndex(const std::string& i_date, size_t& o_linenr) const;
190 std::streampos getIndex(const double& i_date, size_t& o_linenr) const;
191
192 const std::string toString() const;
193
194 private:
195 struct file_index {
196 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) {}
197 bool operator<(const file_index& a) const {
198 return date < a.date;
199 }
200 bool operator>(const file_index& a) const {
201 return date > a.date;
202 }
203 Date date;
204 size_t linenr;
205 std::streampos pos;
206 };
207 size_t binarySearch(const Date& soughtdate) const;
208
209 std::vector< struct file_index > vecIndex;
210 };
211
212} //end namespace FileUtils
213} //end namespace mio
214
215#endif
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition Date.h:87
Definition FileUtils.h:157
const std::string toString() const
Definition FileUtils.cc:574
FileIndexer()
Definition FileUtils.h:159
std::streampos getIndex(const Date &i_date) const
Get the file position suitable for a given date.
Definition FileUtils.cc:515
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:484
void createDirectories(const std::string &path)
creates the directory tree for the given path, including missing intermediate directories (gives only...
Definition FileUtils.cc:239
std::string getCWD()
returns the current working directory.
Definition FileUtils.cc:330
std::string getPath(const std::string &filename, const bool &resolve)
returns the path preceeding a given filename.
Definition FileUtils.cc:158
void copy_file(const std::string &src, const std::string &dest)
Copies a files from one location to another.
Definition FileUtils.cc:70
std::string removeExtension(const std::string &filename)
remove the extension part of a given filename.
Definition FileUtils.cc:148
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:91
std::string getFilename(const std::string &path)
extract the file name from a path+filename string.
Definition FileUtils.cc:169
bool fileExists(const std::string &filename)
Definition FileUtils.cc:339
std::string getExtension(const std::string &filename)
returns the extension part of a given filename.
Definition FileUtils.cc:135
bool isWindowsPath(const std::string &path)
Definition FileUtils.cc:233
char getEoln(std::istream &fin)
Definition FileUtils.cc:408
std::string getDateTime()
returns the current date and time as a string.
Definition FileUtils.cc:178
bool validFileAndPath(const std::string &filename)
Definition FileUtils.cc:187
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:212
void skipLines(std::istream &fin, const size_t &nbLines, const char &eoln)
Definition FileUtils.cc:441
bool isAbsolutePath(const std::string &in_path)
checks if a path is an absolute path
Definition FileUtils.cc:203
bool directoryExists(const std::string &path)
Definition FileUtils.cc:224
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:125
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:451
Definition Config.cc:34