MeteoIODoc  2.10.0
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 
29 namespace mio {
30 namespace 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 validFileAndPath(const std::string& filename);
57 
58  bool fileExists(const std::string& filename);
59 
66  std::string cleanPath(std::string in_path, const bool& resolve=false);
67 
76  std::string getExtension(const std::string& filename);
77 
86  std::string removeExtension(const std::string& filename);
87 
92  std::string getCWD();
93 
100  std::string getPath(const std::string& filename, const bool& resolve=false);
101 
107  bool isAbsolutePath(const std::string& in_path);
108 
114  std::string getFilename(const std::string& path);
115 
116  char getEoln(std::istream& fin);
117 
118  void skipLines(std::istream& fin, const size_t& nbLines, const char& eoln='\n');
119 
120  std::map<std::string,std::string> readKeyValueHeader(std::istream& fin,
121  const size_t& linecount=1,
122  const std::string& delimiter="=", const bool& keep_case=false);
123 
133  class FileIndexer {
134  public:
135  FileIndexer() : vecIndex() {}
136 
142  void setIndex(const Date& i_date, const std::streampos& i_pos);
143  void setIndex(const std::string& i_date, const std::streampos& i_pos);
144  void setIndex(const double& i_date, const std::streampos& i_pos);
145 
152  std::streampos getIndex(const Date& i_date) const;
153  std::streampos getIndex(const std::string& i_date) const;
154  std::streampos getIndex(const double& i_date) const;
155 
156  const std::string toString() const;
157 
158  private:
159  struct file_index {
160  file_index(const Date& i_date, const std::streampos& i_pos) : date(i_date), pos(i_pos) {}
161  bool operator<(const file_index& a) const {
162  return date < a.date;
163  }
164  bool operator>(const file_index& a) const {
165  return date > a.date;
166  }
167  Date date;
168  std::streampos pos;
169  };
170  size_t binarySearch(const Date& soughtdate) const;
171 
172  std::vector< struct file_index > vecIndex;
173  };
174 
175 } //end namespace FileUtils
176 } //end namespace mio
177 
178 #endif
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
Definition: FileUtils.h:133
const std::string toString() const
Definition: FileUtils.cc:468
FileIndexer()
Definition: FileUtils.h:135
std::streampos getIndex(const Date &i_date) const
Get the file position suitable for a given date.
Definition: FileUtils.cc:435
void setIndex(const Date &i_date, const std::streampos &i_pos)
Add a new position to the index.
Definition: FileUtils.cc:404
std::string cleanPath(std::string in_path, const bool &resolve)
Replace "\" by "/" in a string so that a path string is cross plateform, optionally resolve links,...
Definition: FileUtils.cc:68
std::string getCWD()
returns the current working directory.
Definition: FileUtils.cc:250
std::string getPath(const std::string &filename, const bool &resolve)
returns the path preceeding a given filename.
Definition: FileUtils.cc:124
void copy_file(const std::string &src, const std::string &dest)
Copies a files from one location to another.
Definition: FileUtils.cc:47
std::string removeExtension(const std::string &filename)
remove the extension part of a given filename.
Definition: FileUtils.cc:114
std::string getFilename(const std::string &path)
extract the file name from a path+filename string.
Definition: FileUtils.cc:135
bool fileExists(const std::string &filename)
Definition: FileUtils.cc:259
std::string getExtension(const std::string &filename)
returns the extension part of a given filename.
Definition: FileUtils.cc:101
char getEoln(std::istream &fin)
Definition: FileUtils.cc:328
bool validFileAndPath(const std::string &filename)
Definition: FileUtils.cc:144
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:169
void skipLines(std::istream &fin, const size_t &nbLines, const char &eoln)
Definition: FileUtils.cc:361
bool isAbsolutePath(const std::string &in_path)
checks if a path is an absolute path
Definition: FileUtils.cc:160
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:371
Definition: Config.cc:30