Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <mitkUIDGenerator.h>
00019 #include <ctime>
00020 #include <sstream>
00021 #include <math.h>
00022 #include <stdexcept>
00023 #include <iostream>
00024
00025 namespace mitk {
00026
00027 UIDGenerator::UIDGenerator(const char* prefix, unsigned int lengthOfRandomPart)
00028 :m_Prefix(prefix),
00029 m_LengthOfRandomPart(lengthOfRandomPart)
00030 {
00031 if (lengthOfRandomPart < 5)
00032 {
00033 MITK_ERROR << lengthOfRandomPart << " are not really unique, right?" << std::endl;
00034 throw std::invalid_argument("To few digits requested");
00035 }
00036
00037 srand((unsigned int) time( (time_t *)0 ));
00038 }
00039
00040 std::string UIDGenerator::GetUID()
00041 {
00042 std::ostringstream s;
00043 s << m_Prefix;
00044 time_t tt = time(0);
00045 tm* t = gmtime(&tt);
00046
00047 if (t)
00048 {
00049 s << t->tm_year + 1900;
00050
00051 if (t->tm_mon < 9) s << "0";
00052 s << t->tm_mon + 1;
00053
00054 if (t->tm_mday < 10) s << "0";
00055 s << t->tm_mday;
00056
00057 if (t->tm_hour < 10) s << "0";
00058 s << t->tm_hour;
00059
00060 if (t->tm_min < 10) s << "0";
00061 s << t->tm_min;
00062
00063 if (t->tm_sec < 10) s << "0";
00064 s << t->tm_sec;
00065
00066 std::ostringstream rs;
00067 rs << (long int)( pow(10.0, double(m_LengthOfRandomPart)) / double(RAND_MAX) * double(rand()) );
00068
00069 for (size_t i = rs.str().length(); i < m_LengthOfRandomPart; ++i)
00070 {
00071 s << "X";
00072 }
00073
00074 s << rs.str();
00075 }
00076
00077 return s.str();
00078 }
00079
00080 }
00081