MeteoIODoc 20240426.aefd3c94
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
87 std::string getExtension(const std::string& filename);
88
97 std::string removeExtension(const std::string& filename);
98
103 std::string getCWD();
104
109 std::string getDateTime();
110
117 std::string getPath(const std::string& filename, const bool& resolve=false);
118
124 bool isAbsolutePath(const std::string& in_path);
125
131 std::string getFilename(const std::string& path);
132
133 char getEoln(std::istream& fin);
134
135 void skipLines(std::istream& fin, const size_t& nbLines, const char& eoln='\n');
136
137 std::map<std::string,std::string> readKeyValueHeader(std::istream& fin,
138 const size_t& linecount=1,
139 const std::string& delimiter="=", const bool& keep_case=false);
140
151 public:
152 FileIndexer() : vecIndex() {}
153
160 void setIndex(const Date& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
161 void setIndex(const std::string& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
162 void setIndex(const double& i_date, const std::streampos& i_pos, const size_t& linenr=static_cast<size_t>(-1));
163
170 std::streampos getIndex(const Date& i_date) const;
171 std::streampos getIndex(const std::string& i_date) const;
172 std::streampos getIndex(const double& i_date) const;
173
181 std::streampos getIndex(const Date& i_date, size_t& o_linenr) const;
182 std::streampos getIndex(const std::string& i_date, size_t& o_linenr) const;
183 std::streampos getIndex(const double& i_date, size_t& o_linenr) const;
184
185 const std::string toString() const;
186
187 private:
188 struct file_index {
189 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) {}
190 bool operator<(const file_index& a) const {
191 return date < a.date;
192 }
193 bool operator>(const file_index& a) const {
194 return date > a.date;
195 }
196 Date date;
197 size_t linenr;
198 std::streampos pos;
199 };
200 size_t binarySearch(const Date& soughtdate) const;
201
202 std::vector< struct file_index > vecIndex;
203 };
204
205} //end namespace FileUtils
206} //end namespace mio
207
208#endif
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
Definition: FileUtils.h:150
const std::string toString() const
Definition: FileUtils.cc:560
FileIndexer()
Definition: FileUtils.h:152
std::streampos getIndex(const Date &i_date) const
Get the file position suitable for a given date.
Definition: FileUtils.cc:501
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:470
void createDirectories(const std::string &path)
creates the directory tree for the given path, including missing intermediate directories (gives only...
Definition: FileUtils.cc:225
std::string getCWD()
returns the current working directory.
Definition: FileUtils.cc:316
std::string getPath(const std::string &filename, const bool &resolve)
returns the path preceeding a given filename.
Definition: FileUtils.cc:145
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:135
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:156
bool fileExists(const std::string &filename)
Definition: FileUtils.cc:325
std::string getExtension(const std::string &filename)
returns the extension part of a given filename.
Definition: FileUtils.cc:122
bool isWindowsPath(const std::string &path)
Definition: FileUtils.cc:220
char getEoln(std::istream &fin)
Definition: FileUtils.cc:394
std::string getDateTime()
returns the current date and time as a string.
Definition: FileUtils.cc:165
bool validFileAndPath(const std::string &filename)
Definition: FileUtils.cc:174
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:199
void skipLines(std::istream &fin, const size_t &nbLines, const char &eoln)
Definition: FileUtils.cc:427
bool isAbsolutePath(const std::string &in_path)
checks if a path is an absolute path
Definition: FileUtils.cc:190
bool directoryExists(const std::string &path)
Definition: FileUtils.cc:211
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:437
Definition: Config.cc:31