MeteoIODoc 20250312.660e6d76
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 <sstream>
22#include <string>
23#include <map>
24#include <vector>
25#include <list>
26
28#include <meteoio/FStream.h>
29
30namespace mio {
31namespace FileUtils {
32
39 void copy_file(const std::string& src, const std::string& dest);
40
53 void readDirectory(const std::string& path, std::list<std::string>& dirlist, const std::string& pattern="", const bool& isRecursive=false);
54
55 std::list<std::string> readDirectory(const std::string& path, const std::string& pattern="", const bool& isRecursive=false);
56
57 bool directoryExists(const std::string &path);
58 bool isWindowsPath(const std::string &path);
64 void createDirectories(const std::string &path);
65
66 bool validFileAndPath(const std::string& filename);
67
68 bool fileExists(const std::string& filename);
69
77 std::string cleanPath(std::string in_path, const bool& resolve=false, const bool& silent=false);
78
79
85 bool isSubDirOf(std::string in_path, std::string root_path);
86
95 std::string getExtension(const std::string& filename);
96
105 std::string removeExtension(const std::string& filename);
106
111 std::string getCWD();
112
117 std::string getDateTime();
118
125 std::string getPath(const std::string& filename, const bool& resolve=false);
126
132 bool isAbsolutePath(const std::string& in_path);
133
139 std::string getFilename(const std::string& path);
140
141 char getEoln(std::istream& fin);
142
143 void skipLines(std::istream& fin, const size_t& nbLines, const char& eoln='\n');
144
145 std::map<std::string,std::string> readKeyValueHeader(std::istream& fin,
146 const size_t& linecount=1,
147 const std::string& delimiter="=", const bool& keep_case=false);
148
159 public:
160 FileIndexer() : vecIndex() {}
161
168 void setIndex(const Date& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
169 void setIndex(const std::string& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
170 void setIndex(const double& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
171
178 std::streampos getIndex(const Date& i_date) const;
179 std::streampos getIndex(const std::string& i_date) const;
180 std::streampos getIndex(const double& i_date) const;
181
189 std::streampos getIndex(const Date& i_date, size_t& o_linenr) const;
190 std::streampos getIndex(const std::string& i_date, size_t& o_linenr) const;
191 std::streampos getIndex(const double& i_date, size_t& o_linenr) const;
192
193 const std::string toString() const;
194
195 private:
196 struct file_index {
197 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) {}
198 bool operator<(const file_index& a) const {
199 return date < a.date;
200 }
201 bool operator>(const file_index& a) const {
202 return date > a.date;
203 }
204 Date date;
205 size_t linenr;
206 std::streampos pos;
207 };
208 size_t binarySearch(const Date& soughtdate) const;
209
210 std::vector< struct file_index > vecIndex;
211 };
212
213} //end namespace FileUtils
214} //end namespace mio
215
216#endif
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
Definition: FileUtils.h:158
const std::string toString() const
Definition: FileUtils.cc:571
FileIndexer()
Definition: FileUtils.h:160
std::streampos getIndex(const Date &i_date) const
Get the file position suitable for a given date.
Definition: FileUtils.cc:512
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:481
void createDirectories(const std::string &path)
creates the directory tree for the given path, including missing intermediate directories (gives only...
Definition: FileUtils.cc:236
std::string getCWD()
returns the current working directory.
Definition: FileUtils.cc:327
std::string getPath(const std::string &filename, const bool &resolve)
returns the path preceeding a given filename.
Definition: FileUtils.cc:155
void copy_file(const std::string &src, const std::string &dest)
Copies a files from one location to another.
Definition: FileUtils.cc:68
std::string removeExtension(const std::string &filename)
remove the extension part of a given filename.
Definition: FileUtils.cc:145
bool isSubDirOf(std::string in_path, std::string root_path)
Check if a given path (here in_path) is a subpath of another one (here root_path)
Definition: FileUtils.cc:122
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:89
std::string getFilename(const std::string &path)
extract the file name from a path+filename string.
Definition: FileUtils.cc:166
bool fileExists(const std::string &filename)
Definition: FileUtils.cc:336
std::string getExtension(const std::string &filename)
returns the extension part of a given filename.
Definition: FileUtils.cc:132
bool isWindowsPath(const std::string &path)
Definition: FileUtils.cc:230
char getEoln(std::istream &fin)
Definition: FileUtils.cc:405
std::string getDateTime()
returns the current date and time as a string.
Definition: FileUtils.cc:175
bool validFileAndPath(const std::string &filename)
Definition: FileUtils.cc:184
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:209
void skipLines(std::istream &fin, const size_t &nbLines, const char &eoln)
Definition: FileUtils.cc:438
bool isAbsolutePath(const std::string &in_path)
checks if a path is an absolute path
Definition: FileUtils.cc:200
bool directoryExists(const std::string &path)
Definition: FileUtils.cc:221
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:448
Definition: Config.cc:31