bes  Updated for version 3.20.6
TempFile.h
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of the BES, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2018 OPeNDAP, Inc.
7 // Author: Nathan Potter <ndp@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #ifndef DAP_TEMPFILE_H_
26 #define DAP_TEMPFILE_H_
27 
28 #include <unistd.h>
29 
30 #include <vector>
31 #include <string>
32 #include <map>
33 
34 namespace bes {
35 
36 const std::string default_tmp_file_template = "/tmp/opendapXXXXXX";
37 
46 class TempFile {
47 private:
48  int d_fd;
49  std::string d_fname;
50  bool d_keep_temps;
51 
52  static std::map<std::string, int> *open_files;
53  static struct sigaction cached_sigpipe_handler;
54 
55  friend class TemporaryFileTest;
56 
57 public:
58  // Odd, but even with TemporaryFileTest declared as a friend, the tests won't
59  // compile unless this is declared public.
60  static void sigpipe_handler(int signal);
61 
62  TempFile(const std::string &path_template = default_tmp_file_template, bool keep_temps = false);
63 
64  ~TempFile();
65 
67  int get_fd() const { return d_fd; }
68 
70  std::string get_name() const { return d_fname; }
71 };
72 
73 } // namespace bes
74 
75 #endif /* DAP_TEMPFILE_H_ */
bes::TempFile::TempFile
TempFile(const std::string &path_template=default_tmp_file_template, bool keep_temps=false)
Get a new temporary file.
Definition: TempFile.cc:82
bes::TempFile::~TempFile
~TempFile()
Free the temporary file.
Definition: TempFile.cc:121
bes::TempFile::get_name
std::string get_name() const
Definition: TempFile.h:70
bes::TempFile::sigpipe_handler
static void sigpipe_handler(int signal)
Definition: TempFile.cc:54
bes::TempFile::get_fd
int get_fd() const
Definition: TempFile.h:67
bes::TempFile
Get a new temporary file.
Definition: TempFile.h:46