]> git.vomp.tv Git - vompclient.git/blob - util.cc
Clean up screenShot() - all params, return types, function names
[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 <iostream>
22 #include <iomanip>
23 #include <sstream>
24 #include <cctype>
25
26 #ifdef WIN32
27 #include <windows.h>
28 #endif
29
30 #include "util.h"
31
32 void MILLISLEEP(ULONG a)
33 {
34 #ifndef WIN32
35   struct timespec delayTime;
36   delayTime.tv_sec = a / 1000;
37   delayTime.tv_nsec = (a % 1000) * 1000000;
38   nanosleep(&delayTime, NULL);
39 #else
40   Sleep(a);
41 #endif
42 }
43
44 std::string tp2str(const std::chrono::time_point<std::chrono::system_clock>& tp)
45 {
46   auto tms = std::chrono::time_point_cast<std::chrono::milliseconds>(tp);
47   std::chrono::milliseconds e = tms.time_since_epoch();
48   long long c = e.count();
49   time_t tt = static_cast<long>(c / 1000); // when are we going longlong for time_t ?
50   int ttm = static_cast<int>(c % 1000);
51   auto stm = std::localtime(&tt);
52   std::stringstream ss;
53   ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm;
54   return ss.str();
55 }
56
57 void dump(void* vdata, int remaining)
58 {
59   UCHAR* data = reinterpret_cast<UCHAR*>(vdata);
60   int pos{}, ntp, spaces;
61   std::stringstream ascii;
62
63   while(remaining)
64   {
65     ascii.str(std::string());
66     ntp = 16;
67     if (ntp > remaining) ntp = remaining;
68     std::cout << " " << std::hex << std::setfill('0');
69     for (int i = 0; i < ntp; i++)
70     {
71       std::cout << std::setw(2) << int(data[pos + i]) << " " << (i == 7 ? "  " : "");
72       if      (std::isspace(data[pos + i])) ascii << " ";
73       else if (std::isprint(data[pos + i])) ascii << data[pos + i];
74       else ascii << '.';
75     }
76     spaces = (16 - ntp) * 3 + 3;
77     if (ntp < 8) spaces += 2;
78     std::cout << std::setfill(' ') << std::setw(spaces) << "" << ascii.str() << std::endl;
79     pos += ntp;
80     remaining -= ntp;
81   }
82   std::cout << std::endl << std::endl;
83 }
84
85 /*
86 ULLONG htonll(ULLONG a)
87 {
88   return (((ULLONG)htonl((ULONG)((a<<32)>> 32))<<32)
89     |(ULONG)htonl(((ULONG) (a >> 32))));
90 }
91
92 ULLONG ntohll(ULLONG a)
93 {
94   return htonll(a);
95 }
96
97 ULLONG htonll(ULLONG a)
98 {
99   #if BYTE_ORDER == BIG_ENDIAN
100     return a;
101   #else
102     ULLONG b = 0;
103
104     b = ((a << 56) & 0xFF00000000000000ULL)
105       | ((a << 40) & 0x00FF000000000000ULL)
106       | ((a << 24) & 0x0000FF0000000000ULL)
107       | ((a <<  8) & 0x000000FF00000000ULL)
108       | ((a >>  8) & 0x00000000FF000000ULL)
109       | ((a >> 24) & 0x0000000000FF0000ULL)
110       | ((a >> 40) & 0x000000000000FF00ULL)
111       | ((a >> 56) & 0x00000000000000FFULL) ;
112
113     return b;
114   #endif
115 }
116
117 ULLONG ntohll(ULLONG a)
118 {
119   return htonll(a);
120 }
121
122 */