RSC  0.17.1
langutils.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of RSC project
4  *
5  * Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "langutils.h"
28 
29 #include <cstdlib>
30 #include <cctype>
31 #include <algorithm>
32 #include <iterator>
33 
34 #ifndef _WIN32
35 #include <sys/time.h>
36 #else
37 #include <windows.h>
38 #endif
39 #include <time.h>
40 
41 namespace rsc {
42 namespace misc {
43 
44 void NullDeleter::operator()(void* /*ignored*/) const {
45 }
46 
47 #ifdef _WIN32
48 
49 boost::uint64_t windowsTime() {
50 
51  static const __int64 magic = 116444736000000000; // 1970/1/1
52  SYSTEMTIME st;
53  GetSystemTime(&st);
54  FILETIME ft;
55  SystemTimeToFileTime(&st,&ft); // in 100-nanosecs...
56  __int64 t;
57  memcpy(&t,&ft,sizeof t);
58  return t - magic;
59 
60 }
61 
62 #endif
63 
64 boost::uint64_t currentTimeMillis() {
65 
66 #ifdef _WIN32
67 
68  return windowsTime() / 10000; // scale to millis.
69 
70 #else
71 
72  timeval tv;
73  gettimeofday(&tv, NULL);
74 
75  // (second-value in milliseconds) + (microsecond value in milliseconds)
76  return (((boost::uint64_t) tv.tv_sec) * 1000l) + (tv.tv_usec / 1000l);
77 
78 #endif
79 
80 }
81 
82 boost::uint64_t currentTimeMicros() {
83 
84 #ifdef _WIN32
85 
86  return windowsTime() / 10;
87 
88 #else
89 
90  timeval tv;
91  gettimeofday(&tv, NULL);
92 
93  // (second-value in microseconds) + (microsecond value)
94  return (((boost::uint64_t) tv.tv_sec) * 1000000ull) + (tv.tv_usec);
95 
96 #endif
97 
98 }
99 
101  char c;
102  do {
103  c = (char) (rand() % ('z' - '0' + 1) + '0');
104  } while (!std::isalnum(c));
105  return c;
106 }
107 
108 std::string randAlnumStr(const std::string::size_type& length) {
109  std::string s;
110  s.reserve(length);
111  generate_n(std::back_inserter(s), length, randAlnumChar);
112  return s;
113 }
114 
115 }
116 }
std::string randAlnumStr(const std::string::size_type &length)
Generates a random alpha-numeric string with fixed length.
Definition: langutils.cpp:108
boost::uint64_t currentTimeMillis()
Returns the current system time as milliseconds.
Definition: langutils.cpp:64
char randAlnumChar()
Generates a random alpha-numeric character.
Definition: langutils.cpp:100
boost::uint64_t currentTimeMicros()
Returns the current system time as microseconds.
Definition: langutils.cpp:82
void operator()(void *ignored) const
Does nothing.
Definition: langutils.cpp:44