TPIE

v1.1rc1-6-g0c97303
tempname.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2008, 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef _TPIE_TEMPNAM_H
25 #define _TPIE_TEMPNAM_H
26 
27 // Get definitions for working with Unix and Windows
28 #include <tpie/portability.h>
29 #include <tpie/stats.h>
30 #include <stdexcept>
31 #include <boost/utility.hpp>
32 // The name of the environment variable pointing to a tmp directory.
33 #define TMPDIR_ENV "TMPDIR"
34 
35 // The name of the environment variable to consult for default device
36 // descriptions.
37 #define AMI_SINGLE_DEVICE_ENV "AMI_SINGLE_DEVICE"
38 
39 namespace tpie {
40 
41  struct tempfile_error: public std::runtime_error {
42  explicit tempfile_error(const std::string & what): std::runtime_error(what) {}
43  };
44 
49  class tempname {
50  public:
68  static std::string tpie_name(const std::string& post_base = "",
69  const std::string& dir = "",
70  const std::string& ext = "");
71 
78  static std::string tpie_dir_name(const std::string& post_base = "",
79  const std::string& dir = "");
80 
84  static std::string get_system_path();
85 
92  static void set_default_path(const std::string& path, const std::string& subdir="");
93 
98  static void set_default_base_name(const std::string& name);
99 
104  static void set_default_extension(const std::string& ext);
105 
111  static const std::string& get_default_path();
112 
118  static const std::string& get_default_base_name();
119 
125  static const std::string& get_default_extension();
126 
127 
144  static std::string get_actual_path();
145  };
146 
152  class temp_file : boost::noncopyable {
153  private:
154  std::string m_path;
155  bool m_persist;
156  stream_size_type m_recordedSize;
157  public:
162  inline bool is_persistent() const {return m_persist;}
163 
168  inline void set_persistent(bool p) {m_persist=p;}
169 
173  temp_file(const std::string & path, bool persist=false);
174 
178  void set_path(const std::string & path, bool persist=false);
179 
180  void update_recorded_size(stream_size_type size) {
181  increment_temp_file_usage(static_cast<stream_offset_type>(size)-
182  static_cast<stream_offset_type>(m_recordedSize));
183  m_recordedSize=size;
184 
185  }
186 
191  temp_file();
192 
197  ~temp_file();
198 
202  const std::string & path();
203 
207  void free();
208  };
209 
210 }
211 
212 #endif // _TPIE_TEMPNAM_H
static std::string tpie_name(const std::string &post_base="", const std::string &dir="", const std::string &ext="")
Generate path for a new temporary file.
temp_file()
Create a temp_file and generate a random temporary file name.
void free()
If not persistent, unlink the associated file.
This file contains a few deprecated definitions for legacy code.
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:168
static std::string tpie_dir_name(const std::string &post_base="", const std::string &dir="")
Generate path for a new temporary directory.
I/O statistics.
static void set_default_base_name(const std::string &name)
Set default base name for temporary files.
void set_path(const std::string &path, bool persist=false)
Associate with a specific file.
const std::string & path()
Get the path of the associated file.
Class representing the existence of a temporary file.
Definition: tempname.h:152
Static methods for generating temporary file names and finding temporary file directories.
Definition: tempname.h:49
static const std::string & get_default_base_name()
Get default base name for temporary files if one is set using set_default_base_name.
static std::string get_system_path()
Get the default path for temporary files on the system.
static std::string get_actual_path()
Return The actual path used for temporary files taking environment variables into account...
bool is_persistent() const
Definition: tempname.h:162
static const std::string & get_default_path()
Get default path for directory containing temporary files if one is set using set_default_path.
static void set_default_extension(const std::string &ext)
Set default extension for temporary files.
static const std::string & get_default_extension()
Get default extension for temporary files if one is set using set_default_extension.
void increment_temp_file_usage(stream_offset_type delta)
Increment (possibly by a negative amount) the number of bytes being used by temporary files...
~temp_file()
temp_file destructor.
static void set_default_path(const std::string &path, const std::string &subdir="")
Sets the default temporary path.