src/Framework/Msi/MsiFlags.h

Go to the documentation of this file.
00001 #pragma once
00002 
00003 namespace AppSecInc {
00004 namespace Msi {
00005 namespace Flags {
00006 
00007 typedef struct
00008 {
00009     LPCWSTR name;
00010     WORD value;
00011 } FlagMapEntryWORD, FlagMapEntry;
00012 
00013 typedef struct
00014 {
00015     LPCWSTR name;
00016     UINT value;
00017 } FlagMapEntryUINT;
00018 
00019 typedef struct
00020 {
00021     LPCWSTR name;
00022     DWORD value;
00023 } FlagMapEntryDWORD;
00024 
00026 template<typename T, typename R>
00027 R GetFlagValue(const std::wstring& name, const T& flagmap)
00028 {
00029     for (int i = 0; i < ARRAYSIZE(flagmap); i++)
00030     {
00031         if (flagmap[i].name == name)
00032         {
00033             return flagmap[i].value;
00034         }
00035     }
00036 
00037         std::stringstream error;
00038         error << "invalid flag name: " << AppSecInc::StringUtils::wc2mb(name);
00039         throw std::exception(error.str().c_str());
00040 }
00041 
00043 template<typename T, typename R>
00044 std::wstring GetFlagName(const R& value, const T& flagmap)
00045 {
00046     for (int i = 0; i < ARRAYSIZE(flagmap); i++)
00047     {
00048         if (flagmap[i].value == value)
00049         {
00050             return flagmap[i].name;
00051         }
00052     }
00053 
00054         std::stringstream error;
00055         error << "invalid flag value: " << AppSecInc::StringUtils::toString(value);
00056         throw std::exception(error.str().c_str());
00057 }
00058 
00060 template<typename T, typename R>
00061 R GetFlagsValue(const std::vector<std::wstring>& flags, const T& flagmap)
00062 {
00063     R result = 0;
00064 
00065     for each (const std::wstring& flag_entry in flags)
00066     {
00067         if (flag_entry.length() == 0)
00068             continue;
00069 
00070         R item_result = GetFlagValue<T, R>(flag_entry, flagmap);
00071         result |= item_result;
00072     }
00073 
00074     return result;
00075 }
00076 
00078 template <typename T, typename R>
00079 R GetStringFlagsValue(const std::wstring& flags, const T& flagmap)
00080 {
00081     std::vector<std::wstring> flags_vector;
00082         AppSecInc::StringUtils::tokenizeOnChar(flags, flags_vector, L"|,; \t\n");
00083     return GetFlagsValue<T, R>(flags_vector, flagmap);
00084 }
00085 
00087 template <typename T, typename R>
00088 R GetPropertyValue(MsiInstall& msiInstall, const std::wstring& name, const T& flagmap)
00089 {
00090         std::wstring property_value = msiInstall.GetProperty(name);
00091     return GetStringFlagsValue<T, R>(property_value, flagmap);
00092 }
00093 
00095 template<typename T>
00096 ULONG GetFlagValue(const std::wstring& name, const T& flagmap)
00097 {
00098     return GetFlagValue<T, ULONG>(name, flagmap);
00099 }
00100 
00101 template<typename T>
00102 ULONG GetFlagsValue(const std::vector<std::wstring>& flags, const T& flagmap)
00103 {
00104     return GetFlagsValue<T, ULONG>(flags, flagmap);
00105 }
00106 
00108 template <typename T>
00109 ULONG GetStringFlagsValue(const std::wstring& flags, const T& flagmap)
00110 {
00111     return GetStringFlagsValue<T, ULONG>(flags, flagmap);
00112 }
00113 
00115 template <typename T>
00116 ULONG GetPropertyValue(MsiInstall& msiInstall, const std::wstring& name, const T& flagmap)
00117 {
00118     return GetPropertyValue<T, ULONG>(msiInstall, name, flagmap);
00119 }
00120 
00121 }}}
00122         


© Application Security Inc. - All Rights Reserved http://msiext.codeplex.com