forked from figment/hkxcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhkxutils.h
160 lines (129 loc) · 4.82 KB
/
hkxutils.h
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
156
157
158
159
160
#pragma once
#ifndef _WINDOWS_
# include <windows.h>
#endif
#include <tchar.h>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <map>
#include <string.h>
#include <ctype.h>
#include <locale.h>
#include <malloc.h>
#include <sstream>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
// Macro to create a dynamically allocated strdup on the stack
#define STRDUPA(p) (_tcscpy((TCHAR*)alloca((_tcslen(p)+1)*sizeof(*p)),p))
static inline float TODEG(float x) { return x * 180.0f / (float)M_PI; }
static inline float TORAD(float x) { return x * (float)M_PI / 180.0f; }
// Trim whitespace before and after a string
inline TCHAR *Trim(TCHAR*&p) {
while(_istspace(*p)) *p++ = 0;
TCHAR *e = p + _tcslen(p) - 1;
while (e > p && _istspace(*e)) *e-- = 0;
return p;
}
// Case insensitive string equivalence test for collections
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{ return stricmp(s1, s2) < 0; }
bool operator()(const string& s1, const string& s2) const
{ return stricmp(s1.c_str(), s2.c_str()) < 0; }
bool operator()(const string& s1, const char * s2) const
{ return stricmp(s1.c_str(), s2) < 0; }
bool operator()(const char * s1, const string& s2) const
{ return stricmp(s1, s2.c_str()) >= 0; }
};
// Case insensitive string equivalence but numbers are sorted together
struct NumericStringEquivalence
{
bool operator()(const TCHAR* s1, const TCHAR* s2) const
{ return numstrcmp(s1, s2) < 0; }
bool operator()(const std::string& s1, const TCHAR* s2) const
{ return numstrcmp(s1.c_str(), s2) < 0; }
bool operator()(const TCHAR* s1, const std::string& s2) const
{ return numstrcmp(s1, s2.c_str()) < 0; }
bool operator()(const std::string& s1, const std::string& s2) const
{ return numstrcmp(s1.c_str(), s2.c_str()) < 0; }
static int numstrcmp(const TCHAR *str1, const TCHAR *str2)
{
TCHAR *p1, *p2;
int c1, c2, lcmp;
for(;;)
{
c1 = tolower(*str1), c2 = tolower(*str2);
if ( c1 == 0 || c2 == 0 )
break;
else if (isdigit(c1) && isdigit(c2))
{
lcmp = strtol(str1, &p1, 10) - strtol(str2, &p2, 10);
if ( lcmp == 0 )
lcmp = (p2 - str2) - (p1 - str1);
if ( lcmp != 0 )
return (lcmp > 0 ? 1 : -1);
str1 = p1, str2 = p2;
}
else
{
lcmp = (c1 - c2);
if (lcmp != 0)
return (lcmp > 0 ? 1 : -1);
++str1, ++str2;
}
}
lcmp = (c1 - c2);
return ( lcmp < 0 ) ? -1 : (lcmp > 0 ? 1 : 0);
}
};
typedef std::map<std::string, std::string, ltstr> NameValueCollection;
typedef std::pair<std::string, std::string> KeyValuePair;
typedef std::list<std::string> stringlist;
typedef std::vector<std::string> stringvector;
extern int wildcmp(const TCHAR *wild, const TCHAR *string);
extern int wildcmpi(const TCHAR *wild, const TCHAR *string);
inline bool strmatch(const string& lhs, const std::string& rhs) {
return (0 == _tcsicmp(lhs.c_str(), rhs.c_str()));
}
inline bool strmatch(const TCHAR* lhs, const std::string& rhs) {
return (0 == _tcsicmp(lhs, rhs.c_str()));
}
inline bool strmatch(const string& lhs, const TCHAR* rhs) {
return (0 == _tcsicmp(lhs.c_str(), rhs));
}
inline bool strmatch(const TCHAR* lhs, const TCHAR* rhs) {
return (0 == _tcsicmp(lhs, rhs));
}
extern bool wildmatch(const string& match, const std::string& value);
extern bool wildmatch(const stringlist& matches, const std::string& value);
extern std::string FormatString(const TCHAR* format,...);
extern std::string replaceSubstring(std::string instr, std::string match, std::string repl);
#pragma region Enumeration support
// Enumeration support
typedef struct EnumLookupType {
int value;
const char *name;
} EnumLookupType;
extern string EnumToString(int value, const EnumLookupType *table);
extern int StringToEnum(string value, const EnumLookupType *table, int defaultValue=0);
extern int EnumToIndex(int value, const EnumLookupType *table);
extern string FlagsToString(int value, const EnumLookupType *table);
extern int StringToFlags(string value, const EnumLookupType *table, int defaultValue=0);
#pragma endregion
bool FindMatchingFiles( LPCTSTR match, stringlist& result );
void FindFiles(std::vector<string>& collection, const TCHAR *path, stringlist&excludes, bool recursive = true);
void FindFiles(std::vector<string>& collection, const TCHAR *path, bool recursive = true);
void CreateDirectories(LPCTSTR path);
extern float roundf (float x);
EXTERN_C
{
void PrintV(FILE* hFile, LPCSTR lpszFormat, va_list argptr);
void PrintLineV(FILE* hFile, LPCSTR lpszFormat, va_list argptr);
void PrintLine(FILE* hFile, LPCSTR lpszFormat, ...);
void Print(FILE* hFile, LPCSTR lpszFormat, ...);
}
string GetFileVersion(const char *fileName);