-
Notifications
You must be signed in to change notification settings - Fork 15
/
Utility.cpp
155 lines (132 loc) · 3.84 KB
/
Utility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// ****************************************************************************
// File: Utility.cpp
// Desc: Utility stuff
//
// ****************************************************************************
#include "stdafx.h"
#define I2TIME(_int) ((double) (_int) * (double) ((double) 1.0 / (double) 1000.0))
// Single heap instance
INSTANCE_EZHeap(Heap);
// ****************************************************************************
// Func: GetTimeSamp()
// Desc: Get elapsed factional seconds
//
// ****************************************************************************
TIMESTAMP getTimeStamp()
{
LARGE_INTEGER tLarge;
QueryPerformanceCounter(&tLarge);
static ALIGN(16) TIMESTAMP s_ClockFreq;
if(s_ClockFreq == 0.0)
{
LARGE_INTEGER tLarge;
QueryPerformanceFrequency(&tLarge);
s_ClockFreq = (TIMESTAMP) tLarge.QuadPart;
}
return((TIMESTAMP) tLarge.QuadPart / s_ClockFreq);
}
void trace(LPCSTR format, ...)
{
if(format)
{
va_list vl;
char str[4096];
va_start(vl, format);
_vsnprintf(str, (sizeof(str) - 1), format, vl);
str[(sizeof(str) - 1)] = 0;
va_end(vl);
OutputDebugString(str);
}
}
long fsize(FILE *fp)
{
long psave, endpos;
long result = -1;
if((psave = ftell(fp)) != -1L)
{
if(fseek(fp, 0, SEEK_END) == 0)
{
if((endpos = ftell(fp)) != -1L)
{
fseek(fp, psave, SEEK_SET);
result = endpos;
}
}
}
return(result);
}
char *replaceNameInPath(char *pszPath, char *pszNewName)
{
char szDrive[_MAX_DRIVE];
char szDir[_MAX_DIR];
_splitpath(pszPath, szDrive, szDir, NULL, NULL);
_makepath(pszPath, szDrive, szDir, pszNewName, NULL);
return(pszPath);
}
// Get a pretty delta time string for output
LPCSTR timeString(TIMESTAMP time)
{
static char szBuff[64];
if(time >= HOUR)
_snprintf(szBuff, SIZESTR(szBuff), "%.2f hours", (time / (TIMESTAMP) HOUR));
else
if(time >= MINUTE)
_snprintf(szBuff, SIZESTR(szBuff), "%.2f minutes", (time / (TIMESTAMP) MINUTE));
else
if(time < (TIMESTAMP) 0.01)
_snprintf(szBuff, SIZESTR(szBuff), "%.2f milliseconds", (time * (TIMESTAMP) 1000.0));
else
_snprintf(szBuff, SIZESTR(szBuff), "%.2f seconds", time);
return(szBuff);
}
// Returns a pretty factional byte size string for given input size
LPCSTR byteSizeString(UINT64 bytes)
{
static const UINT64 KILLOBYTE = 1024;
static const UINT64 MEGABYTE = (KILLOBYTE * 1024); // 1048576
static const UINT64 GIGABYTE = (MEGABYTE * 1024); // 1073741824
static const UINT64 TERABYTE = (GIGABYTE * 1024); // 1099511627776
#define BYTESTR(_Size, _Suffix) \
{ \
double fSize = ((double) bytes / (double) _Size); \
double fIntegral; double fFractional = modf(fSize, &fIntegral); \
if(fFractional > 0.05) \
_snprintf(buffer, SIZESTR(buffer), ("%.1f " ## _Suffix), fSize); \
else \
_snprintf(buffer, SIZESTR(buffer), ("%.0f " ## _Suffix), fIntegral); \
}
static char buffer[32];
ZeroMemory(buffer, sizeof(buffer));
if (bytes >= TERABYTE)
BYTESTR(TERABYTE, "TB")
else
if (bytes >= GIGABYTE)
BYTESTR(GIGABYTE, "GB")
else
if (bytes >= MEGABYTE)
BYTESTR(MEGABYTE, "MB")
else
if (bytes >= KILLOBYTE)
BYTESTR(KILLOBYTE, "KB")
else
_snprintf(buffer, SIZESTR(buffer), "%u byte%c", bytes, (bytes == 1) ? 0 : 's');
return(buffer);
}
// Return a comma formatted string for a given number
LPSTR prettyNumberString(UINT64 n, __bcount(32) LPSTR buffer)
{
std::string s;
int c = 0;
do
{
s.insert(0, 1, char('0' + (n % 10)));
n /= 10;
if ((c += (3 && n)) >= 3)
{
s.insert(0, 1, ',');
c = 0;
}
} while (n);
strncpy(buffer, s.c_str(), 31);
return(buffer);
}