Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkUIDGenerator.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include <mitkLogMacros.h>
14 #include <mitkUIDGenerator.h>
15 
16 #include <iomanip>
17 #include <sstream>
18 
19 mitk::UIDGenerator::UIDGenerator(const char *prefix, unsigned int lengthOfRandomPart)
20  : m_Prefix(prefix),
21  m_LengthOfRandomPart(lengthOfRandomPart),
22  m_Distribution(std::uniform_int_distribution<unsigned long>(0, std::numeric_limits<unsigned long>::max()))
23 {
24 }
25 
27 {
28  std::ostringstream s;
29  s << m_Prefix;
30  auto time = std::time(nullptr);
31  auto tm = std::localtime(&time);
32 
33  s << std::put_time(tm, "%Y%m%d%H%M%S");
34 
35  std::ostringstream rs;
36 
37  static std::random_device rd; // Will be used to obtain a seed for the random number engine
38  static std::mt19937 generator(rd()); // Standard mersenne_twister_engine seeded with rd()
39 
40  while (rs.str().length() < m_LengthOfRandomPart)
41  {
42  rs << m_Distribution(generator);
43  }
44 
45  auto randomString = rs.str();
46 
47  if (randomString.length() > m_LengthOfRandomPart)
48  {
49  randomString = randomString.substr(randomString.length() - m_LengthOfRandomPart);
50  }
51 
52  s << randomString;
53 
54  return s.str();
55 }
STL namespace.
static T max(T x, T y)
Definition: svm.cpp:56
UIDGenerator(const char *prefix="UID_", unsigned int lengthOfRandomPart=8)