MeteoIODoc  MeteoIODoc-2.9.0
FileUtils.h
Go to the documentation of this file.
1 /* Copyright 2014 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
2 /***********************************************************************************/
3 /* This file is part of MeteoIO.
4  MeteoIO is free software: you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  MeteoIO is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #ifndef FILEUTILS_H
18 #define FILEUTILS_H
19 
20 #include <sstream>
21 #include <string>
22 #include <map>
23 #include <vector>
24 #include <list>
25 
27 
28 namespace mio {
29 namespace FileUtils {
30 
37  void copy_file(const std::string& src, const std::string& dest);
38 
51  void readDirectory(const std::string& path, std::list<std::string>& dirlist, const std::string& pattern="", const bool& isRecursive=false);
52 
53  std::list<std::string> readDirectory(const std::string& path, const std::string& pattern="", const bool& isRecursive=false);
54 
55  bool validFileAndPath(const std::string& filename);
56 
57  bool fileExists(const std::string& filename);
58 
65  std::string cleanPath(std::string in_path, const bool& resolve=false);
66 
75  std::string getExtension(const std::string& filename);
76 
85  std::string removeExtension(const std::string& filename);
86 
91  std::string getCWD();
92 
99  std::string getPath(const std::string& filename, const bool& resolve=false);
100 
106  bool isAbsolutePath(const std::string& in_path);
107 
113  std::string getFilename(const std::string& path);
114 
115  char getEoln(std::istream& fin);
116 
117  void skipLines(std::istream& fin, const size_t& nbLines, const char& eoln='\n');
118 
119  std::map<std::string,std::string> readKeyValueHeader(std::istream& fin,
120  const size_t& linecount=1,
121  const std::string& delimiter="=", const bool& keep_case=false);
122 
132  class FileIndexer {
133  public:
134  FileIndexer() : vecIndex() {}
135 
141  void setIndex(const Date& i_date, const std::streampos& i_pos);
142  void setIndex(const std::string& i_date, const std::streampos& i_pos);
143  void setIndex(const double& i_date, const std::streampos& i_pos);
144 
151  std::streampos getIndex(const Date& i_date) const;
152  std::streampos getIndex(const std::string& i_date) const;
153  std::streampos getIndex(const double& i_date) const;
154 
155  const std::string toString() const;
156 
157  private:
158  struct file_index {
159  file_index(const Date& i_date, const std::streampos& i_pos) : date(i_date), pos(i_pos) {}
160  bool operator<(const file_index& a) const {
161  return date < a.date;
162  }
163  bool operator>(const file_index& a) const {
164  return date > a.date;
165  }
166  Date date;
167  std::streampos pos;
168  };
169  size_t binarySearch(const Date& soughtdate) const;
170 
171  std::vector< struct file_index > vecIndex;
172  };
173 
174 } //end namespace FileUtils
175 } //end namespace mio
176 
177 #endif
bool fileExists(const std::string &filename)
Definition: FileUtils.cc:258
std::string getExtension(const std::string &filename)
returns the extension part of a given filename.
Definition: FileUtils.cc:100
FileIndexer()
Definition: FileUtils.h:134
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:67
Definition: Config.cc:28
std::streampos getIndex(const Date &i_date) const
Get the file position suitable for a given date.
Definition: FileUtils.cc:434
void copy_file(const std::string &src, const std::string &dest)
Copies a files from one location to another.
Definition: FileUtils.cc:46
bool isAbsolutePath(const std::string &in_path)
checks if a path is an absolute path
Definition: FileUtils.cc:159
char getEoln(std::istream &fin)
Definition: FileUtils.cc:327
std::string removeExtension(const std::string &filename)
remove the extension part of a given filename.
Definition: FileUtils.cc:113
bool validFileAndPath(const std::string &filename)
Definition: FileUtils.cc:143
Definition: FileUtils.h:132
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:168
const std::string toString() const
Definition: FileUtils.cc:467
std::string getCWD()
returns the current working directory.
Definition: FileUtils.cc:249
void skipLines(std::istream &fin, const size_t &nbLines, const char &eoln)
Definition: FileUtils.cc:360
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:82
std::string getPath(const std::string &filename, const bool &resolve)
returns the path preceeding a given filename.
Definition: FileUtils.cc:123
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:370
void setIndex(const Date &i_date, const std::streampos &i_pos)
Add a new position to the index.
Definition: FileUtils.cc:403
std::string getFilename(const std::string &path)
extract the file name from a path+filename string.
Definition: FileUtils.cc:134