Menu

[c54480]: / mkdtemp.cpp  Maximize  Restore  History

Download this file

64 lines (52 with data), 1.3 kB

 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
#include <QDir>
#include <stdint.h>
#ifndef _WIN32
#include <sys/time.h>
extern "C" {
pid_t getpid (void);
}
#else
#include <windows.h>
#define getpid GetCurrentProcessId
#include <time.h>
#include <direct.h>
#endif
QString
mkdtemp(QString tmpl)
{
static const char letters[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static uint64_t value;
const unsigned int ATTEMPTS_MIN = (62 * 62 * 62);
if (tmpl.length() < 6 || !tmpl.endsWith("XXXXXX"))
{
return QString();
}
uint64_t random_time_bits = time(NULL);
value += (random_time_bits ^ getpid());
unsigned int count;
for (count = 0; count < ATTEMPTS_MIN; value += 7777, ++count)
{
uint64_t v = value;
QString XXXXXX;
XXXXXX.append(letters[v % 62]);
v /= 62;
XXXXXX.append(letters[v % 62]);
v /= 62;
XXXXXX.append(letters[v % 62]);
v /= 62;
XXXXXX.append(letters[v % 62]);
v /= 62;
XXXXXX.append(letters[v % 62]);
v /= 62;
XXXXXX.append(letters[v % 62]);
tmpl.replace(tmpl.length()-6,6,XXXXXX);
QDir dir;
if (dir.exists(tmpl))
continue;
if (dir.mkpath(tmpl)){
return tmpl;
}
}
return QString();
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.