1 // PropVariantUtils.cpp
2
3 #include "StdAfx.h"
4
5 #include "../Common/IntToString.h"
6
7 #include "PropVariantUtils.h"
8
9 using namespace NWindows;
10
AddHex(AString & s,UInt32 v)11 static void AddHex(AString &s, UInt32 v)
12 {
13 char sz[16];
14 sz[0] = '0';
15 sz[1] = 'x';
16 ConvertUInt32ToHex(v, sz + 2);
17 s += sz;
18 }
19
20
TypePairToString(const CUInt32PCharPair * pairs,unsigned num,UInt32 value)21 AString TypePairToString(const CUInt32PCharPair *pairs, unsigned num, UInt32 value)
22 {
23 char sz[16];
24 const char *p = NULL;
25 for (unsigned i = 0; i < num; i++)
26 {
27 const CUInt32PCharPair &pair = pairs[i];
28 if (pair.Value == value)
29 p = pair.Name;
30 }
31 if (!p)
32 {
33 ConvertUInt32ToString(value, sz);
34 p = sz;
35 }
36 return (AString)p;
37 }
38
PairToProp(const CUInt32PCharPair * pairs,unsigned num,UInt32 value,NCOM::CPropVariant & prop)39 void PairToProp(const CUInt32PCharPair *pairs, unsigned num, UInt32 value, NCOM::CPropVariant &prop)
40 {
41 prop = TypePairToString(pairs, num, value);
42 }
43
44
TypeToString(const char * const table[],unsigned num,UInt32 value)45 AString TypeToString(const char * const table[], unsigned num, UInt32 value)
46 {
47 char sz[16];
48 const char *p = NULL;
49 if (value < num)
50 p = table[value];
51 if (!p)
52 {
53 ConvertUInt32ToString(value, sz);
54 p = sz;
55 }
56 return (AString)p;
57 }
58
TypeToProp(const char * const table[],unsigned num,UInt32 value,NWindows::NCOM::CPropVariant & prop)59 void TypeToProp(const char * const table[], unsigned num, UInt32 value, NWindows::NCOM::CPropVariant &prop)
60 {
61 char sz[16];
62 const char *p = NULL;
63 if (value < num)
64 p = table[value];
65 if (!p)
66 {
67 ConvertUInt32ToString(value, sz);
68 p = sz;
69 }
70 prop = p;
71 }
72
73
FlagsToString(const char * const * names,unsigned num,UInt32 flags)74 AString FlagsToString(const char * const *names, unsigned num, UInt32 flags)
75 {
76 AString s;
77 for (unsigned i = 0; i < num; i++)
78 {
79 UInt32 flag = (UInt32)1 << i;
80 if ((flags & flag) != 0)
81 {
82 const char *name = names[i];
83 if (name && name[0] != 0)
84 {
85 s.Add_OptSpaced(name);
86 flags &= ~flag;
87 }
88 }
89 }
90 if (flags != 0)
91 {
92 s.Add_Space_if_NotEmpty();
93 AddHex(s, flags);
94 }
95 return s;
96 }
97
FlagsToString(const CUInt32PCharPair * pairs,unsigned num,UInt32 flags)98 AString FlagsToString(const CUInt32PCharPair *pairs, unsigned num, UInt32 flags)
99 {
100 AString s;
101 for (unsigned i = 0; i < num; i++)
102 {
103 const CUInt32PCharPair &p = pairs[i];
104 UInt32 flag = (UInt32)1 << (unsigned)p.Value;
105 if ((flags & flag) != 0)
106 {
107 if (p.Name[0] != 0)
108 s.Add_OptSpaced(p.Name);
109 }
110 flags &= ~flag;
111 }
112 if (flags != 0)
113 {
114 s.Add_Space_if_NotEmpty();
115 AddHex(s, flags);
116 }
117 return s;
118 }
119
FlagsToProp(const char * const * names,unsigned num,UInt32 flags,NCOM::CPropVariant & prop)120 void FlagsToProp(const char * const *names, unsigned num, UInt32 flags, NCOM::CPropVariant &prop)
121 {
122 prop = FlagsToString(names, num, flags);
123 }
124
FlagsToProp(const CUInt32PCharPair * pairs,unsigned num,UInt32 flags,NCOM::CPropVariant & prop)125 void FlagsToProp(const CUInt32PCharPair *pairs, unsigned num, UInt32 flags, NCOM::CPropVariant &prop)
126 {
127 prop = FlagsToString(pairs, num, flags);
128 }
129
130
Flags64ToString(const CUInt32PCharPair * pairs,unsigned num,UInt64 flags)131 AString Flags64ToString(const CUInt32PCharPair *pairs, unsigned num, UInt64 flags)
132 {
133 AString s;
134 for (unsigned i = 0; i < num; i++)
135 {
136 const CUInt32PCharPair &p = pairs[i];
137 UInt64 flag = (UInt64)1 << (unsigned)p.Value;
138 if ((flags & flag) != 0)
139 {
140 if (p.Name[0] != 0)
141 s.Add_OptSpaced(p.Name);
142 }
143 flags &= ~flag;
144 }
145 if (flags != 0)
146 {
147 {
148 char sz[32];
149 sz[0] = '0';
150 sz[1] = 'x';
151 ConvertUInt64ToHex(flags, sz + 2);
152 s.Add_OptSpaced(sz);
153 }
154 }
155 return s;
156 }
157
Flags64ToProp(const CUInt32PCharPair * pairs,unsigned num,UInt64 flags,NCOM::CPropVariant & prop)158 void Flags64ToProp(const CUInt32PCharPair *pairs, unsigned num, UInt64 flags, NCOM::CPropVariant &prop)
159 {
160 prop = Flags64ToString(pairs, num, flags);
161 }
162