Home | History | Annotate | Download | only in Common
      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;
     24   CProp(): IsOptional(false) {}
     25 };
     26 
     27 struct CProps
     28 {
     29   CObjectVector<CProp> Props;
     30 
     31   void Clear() { Props.Clear(); }
     32 
     33   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 
     43   void AddPropString(PROPID propid, const wchar_t *s)
     44   {
     45     CProp prop;
     46     prop.IsOptional = true;
     47     prop.Id = propid;
     48     prop.Value = s;
     49     Props.Add(prop);
     50   }
     51 
     52   HRESULT SetCoderProps(ICompressSetCoderProperties *scp, const UInt64 *dataSizeReduce) const;
     53 };
     54 
     55 class CMethodProps: public CProps
     56 {
     57   HRESULT SetParam(const UString &name, const UString &value);
     58 public:
     59   int GetLevel() const;
     60   int Get_NumThreads() const
     61   {
     62     int i = FindProp(NCoderPropID::kNumThreads);
     63     if (i >= 0)
     64       if (Props[i].Value.vt == VT_UI4)
     65         return (int)Props[i].Value.ulVal;
     66     return -1;
     67   }
     68 
     69   bool Get_DicSize(UInt32 &res) const
     70   {
     71     res = 0;
     72     int i = FindProp(NCoderPropID::kDictionarySize);
     73     if (i >= 0)
     74       if (Props[i].Value.vt == VT_UI4)
     75       {
     76         res = Props[i].Value.ulVal;
     77         return true;
     78       }
     79     return false;
     80   }
     81 
     82   int FindProp(PROPID id) const;
     83 
     84   UInt32 Get_Lzma_Algo() const
     85   {
     86     int i = FindProp(NCoderPropID::kAlgorithm);
     87     if (i >= 0)
     88       if (Props[i].Value.vt == VT_UI4)
     89         return Props[i].Value.ulVal;
     90     return GetLevel() >= 5 ? 1 : 0;
     91   }
     92 
     93   UInt32 Get_Lzma_DicSize() const
     94   {
     95     int i = FindProp(NCoderPropID::kDictionarySize);
     96     if (i >= 0)
     97       if (Props[i].Value.vt == VT_UI4)
     98         return Props[i].Value.ulVal;
     99     int level = GetLevel();
    100     return level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26));
    101   }
    102 
    103   UInt32 Get_Lzma_NumThreads(bool &fixedNumber) const
    104   {
    105     fixedNumber = false;
    106     int numThreads = Get_NumThreads();
    107     if (numThreads >= 0)
    108     {
    109       fixedNumber = true;
    110       return numThreads < 2 ? 1 : 2;
    111     }
    112     return Get_Lzma_Algo() == 0 ? 1 : 2;
    113   }
    114 
    115   UInt32 Get_BZip2_NumThreads(bool &fixedNumber) const
    116   {
    117     fixedNumber = false;
    118     int numThreads = Get_NumThreads();
    119     if (numThreads >= 0)
    120     {
    121       fixedNumber = true;
    122       if (numThreads < 1) return 1;
    123       if (numThreads > 64) return 64;
    124       return numThreads;
    125     }
    126     return 1;
    127   }
    128 
    129   UInt32 Get_BZip2_BlockSize() const
    130   {
    131     int i = FindProp(NCoderPropID::kDictionarySize);
    132     if (i >= 0)
    133       if (Props[i].Value.vt == VT_UI4)
    134       {
    135         UInt32 blockSize = Props[i].Value.ulVal;
    136         const UInt32 kDicSizeMin = 100000;
    137         const UInt32 kDicSizeMax = 900000;
    138         if (blockSize < kDicSizeMin) blockSize = kDicSizeMin;
    139         if (blockSize > kDicSizeMax) blockSize = kDicSizeMax;
    140         return blockSize;
    141       }
    142     int level = GetLevel();
    143     return 100000 * (level >= 5 ? 9 : (level >= 1 ? level * 2 - 1: 1));
    144   }
    145 
    146   UInt32 Get_Ppmd_MemSize() const
    147   {
    148     int i = FindProp(NCoderPropID::kUsedMemorySize);
    149     if (i >= 0)
    150       if (Props[i].Value.vt == VT_UI4)
    151         return Props[i].Value.ulVal;
    152     int level = GetLevel();
    153     return level >= 9 ? (192 << 20) : ((UInt32)1 << (level + 19));
    154   }
    155 
    156   void AddLevelProp(UInt32 level)
    157   {
    158     AddProp32(NCoderPropID::kLevel, level);
    159   }
    160 
    161   void AddNumThreadsProp(UInt32 numThreads)
    162   {
    163     AddProp32(NCoderPropID::kNumThreads, numThreads);
    164   }
    165 
    166   HRESULT ParseParamsFromString(const UString &srcString);
    167   HRESULT ParseParamsFromPROPVARIANT(const UString &realName, const PROPVARIANT &value);
    168 };
    169 
    170 class COneMethodInfo: public CMethodProps
    171 {
    172 public:
    173   UString MethodName;
    174 
    175   void Clear()
    176   {
    177     CMethodProps::Clear();
    178     MethodName.Empty();
    179   }
    180   bool IsEmpty() const { return MethodName.IsEmpty() && Props.IsEmpty(); }
    181   HRESULT ParseMethodFromPROPVARIANT(const UString &realName, const PROPVARIANT &value);
    182   HRESULT ParseMethodFromString(const UString &s);
    183 };
    184 
    185 #endif
    186