MeteoIODoc 20261119.86f8ff36
Environmental timeseries pre-processing
Loading...
Searching...
No Matches
FStream.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
19#ifndef FSTREAM_H
20#define FSTREAM_H
21
22#include <fstream>
23#include <memory>
24#include <string>
25#include <meteoio/IOManager.h>
26
27namespace mio {
28
37class ofilestream : public std::ofstream
38{
39 public:
41 ofilestream(const char* filename, std::ios_base::openmode mode = std::ios_base::out);
42 ofilestream(const char* filename, const Config& cfgreader, std::ios_base::openmode mode = std::ios_base::out);
43 ofilestream(const std::string filename, std::ios_base::openmode mode = std::ios_base::out);
44 ofilestream(const std::string filename, const Config& cfgreader, std::ios_base::openmode mode = std::ios_base::out);
45
46
47 void open(const char* filename, std::ios_base::openmode mode = std::ios_base::out);
48 static void createDirectoriesOfFile(const char* filename);
49
50 static bool getDefault();
51
61 static std::string prepareWrite(const char* filename, const Config& cfgreader);
68 static std::string prepareWrite(const char* filename, bool write_directories);
69
70 private:
71 static bool write_directories_default;
72 static bool keep_old_files;
73 friend void IOManager::setOfstreamDefault(const Config& i_cfg);
74
75 static bool warn_abs_path;
76};
77
78namespace detail {
84struct ReadBufferHolder
85{
86 ReadBufferHolder() : buffer(), buffer_capacity(0) {}
87
88 std::unique_ptr<char[]> buffer;
89 size_t buffer_capacity;
90};
91}
92
106class ifilestream : private detail::ReadBufferHolder, public std::ifstream
107{
108 public:
109 static constexpr size_t default_buffer_size = 4u << 20;
110
111 ifilestream() : detail::ReadBufferHolder(), std::ifstream() {}
112 ifilestream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, const size_t& buffer_size = default_buffer_size);
113 ifilestream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in, const size_t& buffer_size = default_buffer_size);
114
115 void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, const size_t& buffer_size = default_buffer_size);
116
117 private:
118 void setBuffer(const size_t& buffer_size);
119};
120}
121
122#endif
A class that reads a key/value file. These files (typically named *.ini) follow the INI file format s...
Definition Config.h:86
void setOfstreamDefault(const Config &i_cfg)
Definition IOManager.cc:316
A class that extends std::ifstream, adding sandbox validation and a large read buffer.
Definition FStream.h:107
ifilestream()
Definition FStream.h:111
static constexpr size_t default_buffer_size
default read buffer size: 4 MiB
Definition FStream.h:109
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in, const size_t &buffer_size=default_buffer_size)
Open a file for reading.
Definition FStream.cc:179
A class that extends std::ofstream, adding some output functionality. Limiting the write access of th...
Definition FStream.h:38
static std::string prepareWrite(const char *filename, const Config &cfgreader)
Prepare the writing of a file, as done by the constructors: check that its directory is within the ou...
Definition FStream.cc:52
static bool getDefault()
Definition FStream.cc:136
ofilestream()
Definition FStream.h:40
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::out)
The actual writing function.
Definition FStream.cc:103
static void createDirectoriesOfFile(const char *filename)
Definition FStream.cc:141
Definition Config.cc:34