WSL/SLF GitLab Repository

FileUtils.h 5.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*  Copyright 2014 WSL Institute for Snow and Avalanche Research    SLF-DAVOS      */
/***********************************************************************************/
/* This file is part of MeteoIO.
    MeteoIO is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MeteoIO is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with MeteoIO.  If not, see <http://www.gnu.org/licenses/>.
*/
17
18
#ifndef __FILEUTILS_H__
#define __FILEUTILS_H__
19
20
21
22
23
24

#include <sstream>
#include <string>
#include <map>
#include <vector>
#include <list>
25
#include <unistd.h>
26

27
#include <meteoio/dataClasses/Date.h>
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

namespace mio {
namespace IOUtils {

	/**
	 * @brief Copies a files from one location to another
	 * @author Thomas Egger
	 * @param src  The filename of the file to be copied
	 * @param dest The filename of the file to copy to (will be created or overwritten)
	 */
	void copy_file(const std::string& src, const std::string& dest);

	/**
	* @brief Build a list of file in a given directory.
	* The matching is very primitive: it only looks for the substring "pattern" in the file names.
	* If this substrings exists, the file matches.
	* @param path directory containing the files
	* @param dirlist list of matching file names
	* @param pattern optional pattern that must be part of the file names
	*/
	void readDirectory(const std::string& path, std::list<std::string>& dirlist, const std::string& pattern="");

	bool validFileName(const std::string& filename);

	bool fileExists(const std::string& filename);

	/**
	* @brief Replace "\" by "/" in a string so that a path string is cross plateform, optionally resolve
	* links, convert relative paths to absolute paths, etc
	* @param in_path the path string to cleanup
	* @param resolve resolve links, convert relative paths, etc? (default=false)
	*/
	std::string cleanPath(std::string in_path, const bool& resolve=false);

	/**
	* @brief returns the extension part of a given filename.
	* The extension is defined as all the non-whitespace characters after the last '.'
	* in the filename.
	* @param filename filename to extract the extension from
	* @return extension
	*/
	std::string getExtension(const std::string& filename);

	/**
	* @brief remove the extension part of a given filename.
	* The extension is defined as all the non-whitespace characters after the last '.'
	* in the filename.
	* @param filename filename to remove the extension from
	* @return filename without extension (the '.' is also removed)
	*/
	std::string removeExtension(const std::string& filename);

80
81
82
83
84
85
	/**
	* @brief returns the current working directory.
	* @return path
	*/
	std::string getCWD();

86
87
88
89
90
91
92
93
	/**
	* @brief returns the path preceeding a given filename.
	* @param filename filename to extract the path from
	* @param resolve resolve links, convert relative paths, etc? (default=false)
	* @return path
	*/
	std::string getPath(const std::string& filename, const bool& resolve=false);

94
95
96
97
98
99
100
	/**
	* @brief checks if a path is an absolute path
	* @param in_path string containing the path to check
	* @return true if the given string represents an absolute path
	*/
	bool isAbsolutePath(const std::string& in_path);

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	/**
	* @brief extract the file name from a path+filename string.
	* @param path path to extract the true filename from
	* @return filename
	*/
	std::string getFilename(const std::string& path);

	char getEoln(std::istream& fin);

	void skipLines(std::istream& fin, const size_t& nbLines, const char& eoln='\n');

	void readKeyValueHeader(std::map<std::string, std::string>& headermap,
	                        std::istream& bs,
	                        const size_t& linecount=1,
	                        const std::string& delimiter="=", const bool& keep_case=false);

	/**
	* @class file_indexer
	* @brief helps building an index of stream positions
	* to quickly jump closer to the proper position in an file
	*
	* @ingroup plugins
	* @author Mathias Bavay
	* @date   2012-11-30
	*/
	class FileIndexer {
		public:
			FileIndexer() : vecIndex() {};

			/**
			* @brief Add a new position to the index
			* @param[in] i_date date of the new position
			* @param[in] i_pos streampos position
			*/
			void setIndex(const Date& i_date, const std::streampos& i_pos);
			void setIndex(const std::string& i_date, const std::streampos& i_pos);
			void setIndex(const double& i_date, const std::streampos& i_pos);

			/**
			* @brief Get the file position suitable for a given date
			* @param[in] i_date date for which a position is requested
			* @return closest streampos position before the requested date,
			* -1 if nothing could be found (empty index)
			*/
			std::streampos getIndex(const Date& i_date) const;
			std::streampos getIndex(const std::string& i_date) const;
			std::streampos getIndex(const double& i_date) const;

			const std::string toString() const;

		private:
			struct file_index {
				file_index(const Date& i_date, const std::streampos& i_pos) : date(i_date), pos(i_pos) {};
				bool operator<(const file_index& a) const {
					return date < a.date;
				}
				bool operator>(const file_index& a) const {
					return date > a.date;
				}
				Date date;
				std::streampos pos;
			};
			size_t binarySearch(const Date& soughtdate) const;

			std::vector< struct file_index > vecIndex;
	};

} //end namespace IOUtils
} //end namespace mio

#endif