1 // MethodProps.h 2 3 #ifndef __7Z_METHOD_PROPS_H 4 #define __7Z_METHOD_PROPS_H 5 6 #include "../../Common/MyString.h" 7 8 #include "../../Windows/PropVariant.h" 9 10 #include "../ICoder.h" 11 12 bool StringToBool(const UString &s, bool &res); 13 HRESULT PROPVARIANT_to_bool(const PROPVARIANT &prop, bool &dest); 14 unsigned ParseStringToUInt32(const UString &srcString, UInt32 &number); 15 HRESULT ParsePropToUInt32(const UString &name, const PROPVARIANT &prop, UInt32 &resValue); 16 17 HRESULT ParseMtProp(const UString &name, const PROPVARIANT &prop, UInt32 defaultNumThreads, UInt32 &numThreads); 18 19 struct CProp 20 { 21 PROPID Id; 22 bool IsOptional; 23 NWindows::NCOM::CPropVariant Value; CPropCProp24 CProp(): IsOptional(false) {} 25 }; 26 27 struct CProps 28 { 29 CObjectVector<CProp> Props; 30 ClearCProps31 void Clear() { Props.Clear(); } 32 AreThereNonOptionalPropsCProps33 bool AreThereNonOptionalProps() const 34 { 35 FOR_VECTOR (i, Props) 36 if (!Props[i].IsOptional) 37 return true; 38 return false; 39 } 40 41 void AddProp32(PROPID propid, UInt32 level); 42 AddProp_AsciiCProps43 void AddProp_Ascii(PROPID propid, const char *s) 44 { 45 CProp &prop = Props.AddNew(); 46 prop.IsOptional = true; 47 prop.Id = propid; 48 prop.Value = s; 49 } 50 51 HRESULT SetCoderProps(ICompressSetCoderProperties *scp, const UInt64 *dataSizeReduce) const; 52 }; 53 54 class CMethodProps: public CProps 55 { 56 HRESULT SetParam(const UString &name, const UString &value); 57 public: 58 int GetLevel() const; Get_NumThreads()59 int Get_NumThreads() const 60 { 61 int i = FindProp(NCoderPropID::kNumThreads); 62 if (i >= 0) 63 if (Props[i].Value.vt == VT_UI4) 64 return (int)Props[i].Value.ulVal; 65 return -1; 66 } 67 Get_DicSize(UInt32 & res)68 bool Get_DicSize(UInt32 &res) const 69 { 70 res = 0; 71 int i = FindProp(NCoderPropID::kDictionarySize); 72 if (i >= 0) 73 if (Props[i].Value.vt == VT_UI4) 74 { 75 res = Props[i].Value.ulVal; 76 return true; 77 } 78 return false; 79 } 80 81 int FindProp(PROPID id) const; 82 Get_Lzma_Algo()83 UInt32 Get_Lzma_Algo() const 84 { 85 int i = FindProp(NCoderPropID::kAlgorithm); 86 if (i >= 0) 87 if (Props[i].Value.vt == VT_UI4) 88 return Props[i].Value.ulVal; 89 return GetLevel() >= 5 ? 1 : 0; 90 } 91 Get_Lzma_DicSize()92 UInt32 Get_Lzma_DicSize() const 93 { 94 int i = FindProp(NCoderPropID::kDictionarySize); 95 if (i >= 0) 96 if (Props[i].Value.vt == VT_UI4) 97 return Props[i].Value.ulVal; 98 int level = GetLevel(); 99 return level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)); 100 } 101 Are_Lzma_Model_Props_Defined()102 bool Are_Lzma_Model_Props_Defined() const 103 { 104 if (FindProp(NCoderPropID::kPosStateBits) >= 0) return true; 105 if (FindProp(NCoderPropID::kLitContextBits) >= 0) return true; 106 if (FindProp(NCoderPropID::kLitPosBits) >= 0) return true; 107 return false; 108 } 109 Get_Lzma_NumThreads(bool & fixedNumber)110 UInt32 Get_Lzma_NumThreads(bool &fixedNumber) const 111 { 112 fixedNumber = false; 113 int numThreads = Get_NumThreads(); 114 if (numThreads >= 0) 115 { 116 fixedNumber = true; 117 return numThreads < 2 ? 1 : 2; 118 } 119 return Get_Lzma_Algo() == 0 ? 1 : 2; 120 } 121 Get_BZip2_NumThreads(bool & fixedNumber)122 UInt32 Get_BZip2_NumThreads(bool &fixedNumber) const 123 { 124 fixedNumber = false; 125 int numThreads = Get_NumThreads(); 126 if (numThreads >= 0) 127 { 128 fixedNumber = true; 129 if (numThreads < 1) return 1; 130 if (numThreads > 64) return 64; 131 return numThreads; 132 } 133 return 1; 134 } 135 Get_BZip2_BlockSize()136 UInt32 Get_BZip2_BlockSize() const 137 { 138 int i = FindProp(NCoderPropID::kDictionarySize); 139 if (i >= 0) 140 if (Props[i].Value.vt == VT_UI4) 141 { 142 UInt32 blockSize = Props[i].Value.ulVal; 143 const UInt32 kDicSizeMin = 100000; 144 const UInt32 kDicSizeMax = 900000; 145 if (blockSize < kDicSizeMin) blockSize = kDicSizeMin; 146 if (blockSize > kDicSizeMax) blockSize = kDicSizeMax; 147 return blockSize; 148 } 149 int level = GetLevel(); 150 return 100000 * (level >= 5 ? 9 : (level >= 1 ? level * 2 - 1: 1)); 151 } 152 Get_Ppmd_MemSize()153 UInt32 Get_Ppmd_MemSize() const 154 { 155 int i = FindProp(NCoderPropID::kUsedMemorySize); 156 if (i >= 0) 157 if (Props[i].Value.vt == VT_UI4) 158 return Props[i].Value.ulVal; 159 int level = GetLevel(); 160 return level >= 9 ? (192 << 20) : ((UInt32)1 << (level + 19)); 161 } 162 AddProp_Level(UInt32 level)163 void AddProp_Level(UInt32 level) 164 { 165 AddProp32(NCoderPropID::kLevel, level); 166 } 167 AddProp_NumThreads(UInt32 numThreads)168 void AddProp_NumThreads(UInt32 numThreads) 169 { 170 AddProp32(NCoderPropID::kNumThreads, numThreads); 171 } 172 173 HRESULT ParseParamsFromString(const UString &srcString); 174 HRESULT ParseParamsFromPROPVARIANT(const UString &realName, const PROPVARIANT &value); 175 }; 176 177 class COneMethodInfo: public CMethodProps 178 { 179 public: 180 AString MethodName; 181 UString PropsString; 182 Clear()183 void Clear() 184 { 185 CMethodProps::Clear(); 186 MethodName.Empty(); 187 PropsString.Empty(); 188 } IsEmpty()189 bool IsEmpty() const { return MethodName.IsEmpty() && Props.IsEmpty(); } 190 HRESULT ParseMethodFromPROPVARIANT(const UString &realName, const PROPVARIANT &value); 191 HRESULT ParseMethodFromString(const UString &s); 192 }; 193 194 #endif 195