-
Notifications
You must be signed in to change notification settings - Fork 77
/
TFlagString.h
159 lines (130 loc) · 4.92 KB
/
TFlagString.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
/*****************************************************************************/
/* TFlagString.h Copyright (c) Ladislav Zezula 2021 */
/*---------------------------------------------------------------------------*/
/* Convertor of flags to string lists */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 22.04.21 1.00 Lad Created */
/*****************************************************************************/
#ifndef __TFLAGSTRING_H__
#define __TFLAGSTRING_H__
//-----------------------------------------------------------------------------
// Helper functions
inline const char * GetBitSeparator()
{
return " | ";
}
inline const char * GetBitSeparatorNewLine()
{
return " |\r\n";
}
inline const char * GetNewLineSeparator()
{
return "\r\n";
}
//-----------------------------------------------------------------------------
// Structure describing the flag information - name, value, mask
#define FLAGINFO_ALLSET 0xFFFFFFFF
#define FLAGINFO_BITV(value) { #value, value, value }
#define FLAGINFO_NUMV(value) { #value, value, FLAGINFO_ALLSET }
#define FLAGINFO_MASK(mask,value) { #value, value, mask }
#define FLAGINFO_SEPARATOR() { (LPCSTR)1, 0, 0 }
#define FLAGINFO_END() { (LPCSTR)0, 0, 0 }
struct TFlagInfo
{
bool IsValuePresent(ULONG64 dwBitMask)
{
return (IsSeparator() == false) && ((dwBitMask & dwMask) == (dwValue & dwMask));
}
bool IsSeparator()
{
return (szFlagText == (LPCSTR)1);
}
bool IsTerminator()
{
return (szFlagText == NULL);
}
const char * szFlagText;
unsigned int dwValue;
unsigned int dwMask;
};
//-----------------------------------------------------------------------------
// Extended version of TFlagInfo for dialogs
struct TDlgFlagInfo : public TFlagInfo
{
TDlgFlagInfo(const char * text, unsigned int value, unsigned int mask, unsigned int btn)
{
szFlagText = text;
dwValue = value;
dwMask = mask;
dwButtonType = btn;
}
unsigned int dwButtonType;
};
//-----------------------------------------------------------------------------
// The TFlagString class
class TFlagString : public TFastString<TCHAR>
{
public:
TFlagString(TFlagInfo * pFlags, ULONG64 dwBitMask, const char * szNextSep = NULL) : TFastString<TCHAR>()
{
// Reset the number of flags that were converted to text
dwFlagsConverted = 0;
// Only if we have some flags given
if(pFlags != NULL)
{
// Configure separators
const char * szNextSeparator = (szNextSep != NULL) ? szNextSep : GetBitSeparator();
const char * szSeparator = "";
// Parse all flags and add them to the buffer
for(size_t i = 0; !pFlags->IsTerminator(); i++, pFlags++)
{
if(pFlags->IsValuePresent(dwBitMask))
{
ULONG64 dwMask64 = pFlags->dwMask;
AppendSeparatorAndText(szSeparator, pFlags->szFlagText);
szSeparator = szNextSeparator;
dwBitMask = dwBitMask & ~dwMask64;
dwFlagsConverted++;
}
}
// Is there a nonzero value left?
if(dwBitMask != 0)
{
char szLeftOver[0x20];
StringCchPrintfA(szLeftOver, _countof(szLeftOver), "0x%08x", dwBitMask);
AppendSeparatorAndText(szSeparator, szLeftOver);
}
}
// Nothing appended?
if(IsEmpty())
{
AppendString(_T("0"));
}
}
ULONG GetConvertedFlagsCount()
{
return dwFlagsConverted;
}
protected:
void AppendSeparatorAndText(const char * szSeparator, const char * szFlagText)
{
size_t nLength1 = strlen(szSeparator);
size_t nLength2 = strlen(szFlagText);
// Append separator, if needed
if(EnsureSpace(Length() + nLength1 + nLength2))
{
#ifdef _UNICODE
MultiByteToWideChar(CP_ACP, 0, szSeparator, -1, m_pBufferPtr, (int)nLength1);
MultiByteToWideChar(CP_ACP, 0, szFlagText, -1, m_pBufferPtr+nLength1, (int)nLength2);
#else
memcpy(m_pBufferPtr, szSeparator, nLength1);
memcpy(m_pBufferPtr+nLength1, szFlagText, nLength2);
#endif
m_pBufferPtr += (nLength1 + nLength2);
}
}
ULONG dwFlagsConverted; // Number of flags that was converted to string
};
#endif // __TFLAGSTRING_H__