summaryrefslogtreecommitdiff
path: root/elpa/irony-20220110.849/server/src/support/TemporaryFile.cpp
blob: e7393e1f8b1b3748aaa21c7a779066ac50d5bc05 (plain)
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
/**
 * \file
 * \author Guillaume Papin <guillaume.papin@epitech.eu>
 *
 * This file is distributed under the GNU General Public License. See
 * COPYING for details.
 */

#include "TemporaryFile.h"

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <random>

static std::string getTemporaryFileDirectory() {
  const char *temporaryDirEnvVars[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};

  for (const char *envVar : temporaryDirEnvVars) {
    if (const char *dir = std::getenv(envVar))
      return dir;
  }

  return "/tmp";
}

TemporaryFile::TemporaryFile(const std::string &prefix,
                             const std::string &suffix)
  : pathOrPattern_(prefix + "-%%%%%%" + suffix) {
}

TemporaryFile::~TemporaryFile() {
  if (openedFile_) {
    openedFile_.reset();
    std::remove(pathOrPattern_.c_str());
  }
}

const std::string &TemporaryFile::getPath() {
  if (!openedFile_) {
    openedFile_.reset(new std::fstream);

    std::random_device rd;
    std::default_random_engine e(rd());
    std::uniform_int_distribution<int> dist(0, 15);
    std::string pattern = pathOrPattern_;
    std::string tmpDir = getTemporaryFileDirectory() + "/";
    int i = 0;

    do {
      // exiting is better than infinite loop
      if (++i > TemporaryFile::MAX_ATTEMPS) {
        std::cerr << "error: couldn't create temporary file, please check your "
                     "temporary file directory (" << tmpDir << ")\n";
        exit(EXIT_FAILURE);
      }

      // make the filename based on the pattern
      std::transform(pattern.begin(),
                     pattern.end(),
                     pathOrPattern_.begin(),
                     [&e, &dist](char ch) {
        return ch == '%' ? "0123456789abcdef"[dist(e)] : ch;
      });
      // create the file
      openedFile_->open(tmpDir + pathOrPattern_, std::ios_base::out);
    } while (!openedFile_->is_open());
    pathOrPattern_ = tmpDir + pathOrPattern_;
  }

  return pathOrPattern_;
}