libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
temp_file.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_TEMP_FILE_HPP
2 #define LIBJMMCG_CORE_TEMP_FILE_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 /*
23  Wrap up into a class creating a temporary file, so that it is deleted upon object
24  destruction. This class is completely ANSI. Note that the temporary file is created
25  in the root of the current drive. Not ideal, but the ANSI function "tmpnam(...)"
26  doesn't work any other way.
27 */
28 
29 #include "file.hpp"
30 #include "syscall_wrapper.hpp"
31 
32 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
33 
34  template<typename E_, ppd::generic_traits::api_type API, typename Mdl>
35  class temp_file : public file<E, API, Mdl> {
36  public:
37  typedef typename file<E, API, Mdl>::exception_type exception_type;
38 
39  inline temp_file(const std::ios_base::openmode &om = (std::ios_base::openmode)0);
40  inline temp_file(const temp_file &);
41  inline ~temp_file();
42  inline temp_file &operator=(const temp_file &);
43 
44  private:
45  static inline tstring get_file_name();
46  };
47 
48  template<typename E, ppd::generic_traits::api_type API, typename Mdl> inline
49  temp_file<E, API, Mdl>::temp_file(const std::ios_base::openmode &om)
50  : file<E, API, Mdl>(get_file_name(), om|std::ios_base::in|std::ios_base::out|std::ios_base::trunc) {
51  }
52 
53  template<typename E, ppd::generic_traits::api_type API, typename Mdl> inline
54  temp_file<E, API, Mdl>::temp_file(const temp_file &tf)
55  : file<E, API, Mdl>(get_file_name(), std::ios_base::in|std::ios_base::out|std::ios_base::trunc) {
56  file<E, API, Mdl>::operator<<(tf.rdbuf());
57  }
58 
59  template<typename E, ppd::generic_traits::api_type API, typename Mdl> inline
60  temp_file<E, API, Mdl>::~temp_file() {
61  }
62 
63  template<typename E, ppd::generic_traits::api_type API, typename Mdl> inline temp_file<E, API, Mdl> &
64  temp_file<E, API, Mdl>::operator=(const temp_file<E, API, Mdl> &tf) {
65  file<E, API, Mdl>::remove();
66  create(tf.object);
67  file<E, API, Mdl>::file_type::operator<<(tf.rdbuf());
68  return *this;
69  }
70 
71  template<typename E, ppd::generic_traits::api_type API, typename Mdl> inline tstring
72  temp_file<E, API, Mdl>::get_file_name() noexcept(false) {
73  char n[L_tmpnam+1];
74  JMMCG_SYSCALL_WRAPPER("Failed to create the temporary name.", _T(LIBJMMCG_VERSION_NUMBER), ::tmpnam, n);
75  return StringToTString(n);
76  }
77 
78 } }
79 
80 #endif