File: temp_file.cpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (120 lines) | stat: -rw-r--r-- 2,811 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin temp_file dev}
{xrst_spell
  mkstemp
}

Create a New Empty Temporary File
#################################

Syntax
******
| *file_name* = ``temp_file`` ()

file_name
*********
Is the name of a new temporary file.
This file did not exist when ``temp_file`` was called.
It has been created and is empty upon return.

Empty
=====
This routine failed if the return value *file_name* is empty.

Thread Safe
***********
This routine is thread safe when C++17 and ``mkstemp`` are available
to CppAD.

{xrst_end temp_file}
*/
# include <vector>
# include <stdio.h>
# include <cppad/configure.hpp>
# include <cppad/local/temp_file.hpp>

# if CPPAD_HAS_MKSTEMP && CPPAD_USE_CPLUSPLUS_2017
# include <filesystem>
# else
# include <stdio.h>
# endif

# if _MSC_VER
# include <io.h>
# else
# include <unistd.h>
# endif

# ifdef _WIN32
# define DIR_SEP '\\'
# else
# define DIR_SEP '/'
# endif

namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL_NAMESPACE

std::string temp_file(void)
# if CPPAD_HAS_MKSTEMP && CPPAD_USE_CPLUSPLUS_2017
{  // path
   using std::filesystem::path;
   //
   // tmp_dir_path
   path tmp_dir_path = std::filesystem::temp_directory_path();
   //
   // tmp_dir_str
   std::string tmp_dir_str = tmp_dir_path.string();
   if( tmp_dir_str.back() != DIR_SEP )
      tmp_dir_str += DIR_SEP;
   //
   // pattern_str
   std::string pattern_str = tmp_dir_str;
   pattern_str            += "fileXXXXXX";
   //
   // pattern_vec
   std::vector<char> pattern_vec( pattern_str.size() + 1 );
   for(size_t i = 0; i < pattern_str.size(); ++i)
      pattern_vec[i] = pattern_str[i];
   pattern_vec[ pattern_str.size() ] = '\0';
   //
   // fd, pattrern_vec
   int fd = mkstemp( pattern_vec.data() );
   if( fd < 0 )
      return "";
   close(fd);
   //
   // file_name
   std::string file_name = pattern_vec.data();
   return file_name;
}
# else // CPPAD_HAS_MKSTEMP && CPPAD_USE_CPLUSPLUS_2017
{
# if CPPAD_HAS_TMPNAM_S
   char c_str[L_tmpnam_s];
   tmpnam_s(c_str, L_tmpnam_s );
# else
   char c_str[L_tmpnam];
   tmpnam(c_str);
# endif
# ifdef __MINGW32__
   // https://stackoverflow.com/questions/38868858/
   // fopen-of-file-name-created-by-tmpnam-fails-on-mingw
   std::string file_name = c_str + 1;
# else
   std::string file_name = c_str;
# endif
   FILE* fp = fopen(file_name.c_str(), "r");
   if( fp != NULL )
      return "";
   fp = fopen(file_name.c_str(), "w");
   if( fp == NULL )
      return "";
   fclose(fp);
   return file_name;
}
# endif // CPPAD_HAS_MKSTEMP && CPPAD_USE_CPLUSPLUS_2017

} } // END_CPPAD_LOCAL_NAMESPACE