]> git.vomp.tv Git - vompclient.git/blob - util.cc
VDR connection bug fixes
[vompclient.git] / util.cc
1 /*
2     Copyright 2020 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <time.h>
21 #include <iomanip>
22
23 #ifdef WIN32
24 #include <windows.h>
25 #endif
26
27 #include "util.h"
28
29 void MILLISLEEP(ULONG a)
30 {
31 #ifndef WIN32
32   struct timespec delayTime;
33   delayTime.tv_sec = a / 1000;
34   delayTime.tv_nsec = (a % 1000) * 1000000;
35   nanosleep(&delayTime, NULL);
36 #else
37   Sleep(a);
38 #endif
39 }
40
41 std::string tp2str(const std::chrono::time_point<std::chrono::system_clock>& tp)
42 {
43   auto tms = std::chrono::time_point_cast<std::chrono::milliseconds>(tp);
44   std::chrono::milliseconds e = tms.time_since_epoch();
45   long long c = e.count();
46   time_t tt = c / 1000;
47   int ttm = c % 1000;
48   auto stm = std::localtime(&tt);
49   std::stringstream ss;
50   ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm;
51   return ss.str();
52 }
53
54 /*
55 ULLONG htonll(ULLONG a)
56 {
57   return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
58     |(ULONG)htonl(((ULONG) (a >> 32))));
59 }
60
61 ULLONG ntohll(ULLONG a)
62 {
63   return htonll(a);
64 }
65
66 ULLONG htonll(ULLONG a)
67 {
68   #if BYTE_ORDER == BIG_ENDIAN
69     return a;
70   #else
71     ULLONG b = 0;
72
73     b = ((a << 56) & 0xFF00000000000000ULL)
74       | ((a << 40) & 0x00FF000000000000ULL)
75       | ((a << 24) & 0x0000FF0000000000ULL)
76       | ((a <<  8) & 0x000000FF00000000ULL)
77       | ((a >>  8) & 0x00000000FF000000ULL)
78       | ((a >> 24) & 0x0000000000FF0000ULL)
79       | ((a >> 40) & 0x000000000000FF00ULL)
80       | ((a >> 56) & 0x00000000000000FFULL) ;
81
82     return b;
83   #endif
84 }
85
86 ULLONG ntohll(ULLONG a)
87 {
88   return htonll(a);
89 }
90
91 */