MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
49  void readDirectory(const std::string& path, std::list<std::string>& dirlist, const std::string& pattern="", const bool& isRecursive=false);
50 
51  std::list<std::string> readDirectory(const std::string& path, const std::string& pattern="", const bool& isRecursive=false);
52 
53  bool validFileAndPath(const std::string& filename);
54 
55  bool fileExists(const std::string& filename);
56 
63  std::string cleanPath(std::string in_path, const bool& resolve=false);
64 
72  std::string getExtension(const std::string& filename);
73 
81  std::string removeExtension(const std::string& filename);
82 
87  std::string getCWD();
88 
95  std::string getPath(const std::string& filename, const bool& resolve=false);
96 
102  bool isAbsolutePath(const std::string& in_path);
103 
109  std::string getFilename(const std::string& path);
110 
111  char getEoln(std::istream& fin);
112 
113  void skipLines(std::istream& fin, const size_t& nbLines, const char& eoln='\n');
114 
115  void readKeyValueHeader(std::map<std::string, std::string>& headermap,
116  std::istream& bs,
117  const size_t& linecount=1,
118  const std::string& delimiter="=", const bool& keep_case=false);
119 
129  class FileIndexer {
130  public:
131  FileIndexer() : vecIndex() {}
132 
138  void setIndex(const Date& i_date, const std::streampos& i_pos);
139  void setIndex(const std::string& i_date, const std::streampos& i_pos);
140  void setIndex(const double& i_date, const std::streampos& i_pos);
141 
148  std::streampos getIndex(const Date& i_date) const;
149  std::streampos getIndex(const std::string& i_date) const;
150  std::streampos getIndex(const double& i_date) const;
151 
152  const std::string toString() const;
153 
154  private:
155  struct file_index {
156  file_index(const Date& i_date, const std::streampos& i_pos) : date(i_date), pos(i_pos) {}
157  bool operator<(const file_index& a) const {
158  return date < a.date;
159  }
160  bool operator>(const file_index& a) const {
161  return date > a.date;
162  }
163  Date date;
164  std::streampos pos;
165  };
166  size_t binarySearch(const Date& soughtdate) const;
167 
168  std::vector< struct file_index > vecIndex;
169  };
170 
171 } //end namespace FileUtils
172 } //end namespace mio
173 
174 #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. The extension is defined as all the non-whitespace ch...
Definition: FileUtils.cc:99
std::streampos getIndex(const Date &i_date) const
Get the file position suitable for a given date.
Definition: FileUtils.cc:420
FileIndexer()
Definition: FileUtils.h:131
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:66
void copy_file(const std::string &src, const std::string &dest)
Copies a files from one location to another.
Definition: FileUtils.cc:45
bool isAbsolutePath(const std::string &in_path)
checks if a path is an absolute path
Definition: FileUtils.cc:158
char getEoln(std::istream &fin)
Definition: FileUtils.cc:315
std::string removeExtension(const std::string &filename)
remove the extension part of a given filename. The extension is defined as all the non-whitespace cha...
Definition: FileUtils.cc:112
bool validFileAndPath(const std::string &filename)
Definition: FileUtils.cc:142
Definition: FileUtils.h:129
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. The matching is very primitive: it only looks for the subs...
Definition: FileUtils.cc:167
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:348
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:79
std::string getPath(const std::string &filename, const bool &resolve)
returns the path preceeding a given filename.
Definition: FileUtils.cc:122
void readKeyValueHeader(std::map< std::string, std::string > &headermap, std::istream &fin, const size_t &linecount, const std::string &delimiter, const bool &keep_case)
Definition: FileUtils.cc:358
const std::string toString() const
Definition: FileUtils.cc:453
void setIndex(const Date &i_date, const std::streampos &i_pos)
Add a new position to the index.
Definition: FileUtils.cc:389
std::string getFilename(const std::string &path)
extract the file name from a path+filename string.
Definition: FileUtils.cc:133