-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtemp_file.cc
85 lines (65 loc) · 1.94 KB
/
temp_file.cc
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
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** Copyright UCAR (c) 1992 - 2021
// ** University Corporation for Atmospheric Research (UCAR)
// ** National Center for Atmospheric Research (NCAR)
// ** Research Applications Lab (RAL)
// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
////////////////////////////////////////////////////////////////////////
using namespace std;
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "temp_file.h"
#include "file_exists.h"
#include "vx_log.h"
////////////////////////////////////////////////////////////////////////
ConcatString make_temp_file_name(const char *prefix, const char *suffix) {
int i, pid;
ConcatString s;
const int max_tries = 1000;
//
// Retrieve the current process id
//
pid = (int) getpid();
i = -1;
do {
i++;
if(i > max_tries) {
mlog << Error << "\nmake_temp_file_name() -> "
<< "failed to make temporary file name:\n"
<< s << "\n\n";
exit(1);
}
//
// Initialize
//
s.erase();
//
// Build the file name
//
if(prefix) s << prefix << '_';
s << pid << '_' << i;
if(suffix) s << '_' << suffix;
} while(file_exists(s.c_str()));
return(s);
}
////////////////////////////////////////////////////////////////////////
void remove_temp_file(const ConcatString file_name) {
int errno;
//
// Attempt to remove the file and print out any error message
//
if((errno = remove(file_name.c_str())) != 0) {
mlog << Error << "\nremove_temp_file() -> "
<< "can't delete temporary file: \""
<< file_name << "\" ("
<< strerror(errno) << ")\n\n";
exit(1);
}
return;
}
////////////////////////////////////////////////////////////////////////