Advertisement
Guest User

CPropBag.cls

a guest
Feb 12th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Option Explicit
  3.  
  4. ' CPropBag.cls
  5. '
  6. ' simple class to store/retrieve settings and persist
  7. ' them using a standard propertybag object, the class
  8. ' allows to store variables of any type, including the
  9. ' objects (e.g StdFont, classes and so on); the class
  10. ' also implements simple encryption to allow encrypting
  11. ' the data saved to disk and decrypting them upon load
  12. '
  13.  
  14. ' private workareas
  15. Private msFileName    As String
  16. Private mbAutoSave    As Boolean
  17. Private mbEncrypt     As Boolean
  18. Private mlSeed        As Long
  19. Private mobjPB        As PropertyBag
  20. Private mlErrNum      As Long
  21. Private msErrMsg      As String
  22.  
  23.  
  24. ' instance
  25. Private Sub Class_Initialize()
  26.   Dim sTemp, nPos
  27.  
  28.   ' set a reasonable default
  29.  ' for the storage file name
  30.  sTemp = App.EXEName
  31.   nPos = InStrRev(sTemp, ".")
  32.   If nPos > 0 Then
  33.     sTemp = Mid(sTemp, 1, nPos - 1)
  34.   End If
  35.   msFileName = App.Path & "\" & sTemp & ".cfg"
  36.    
  37.   ' initialize the PB
  38.  Set mobjPB = New PropertyBag
  39.  
  40.   ' set defaults
  41.  mbAutoSave = False
  42.   mbEncrypt = False
  43.   mlSeed = 0
  44.    
  45.   ' reset error infos
  46.  SetError 0, ""
  47. End Sub
  48.  
  49. ' destroy
  50. Private Sub Class_Terminate()
  51.   Set mobjPB = Nothing
  52. End Sub
  53.  
  54. ' set persist file pathname
  55. Public Property Let FileName(ByVal sPathName As String)
  56.   msFileName = sPathName
  57. End Property
  58.  
  59. ' get persist file pathname
  60. Public Property Get FileName() As String
  61.   FileName = msFileName
  62. End Property
  63.  
  64. ' set autosave flag
  65. Public Property Let AutoSave(ByVal bYesNo As Boolean)
  66.   mbAutoSave = bYesNo
  67. End Property
  68.  
  69. ' get autosave flag
  70. Public Property Get AutoSave() As Boolean
  71.   AutoSave = mbAutoSave
  72. End Property
  73.  
  74. ' set encrypt/decrypt password
  75. Public Property Let Password(ByVal sPwd As String)
  76.   If Len(sPwd) < 1 Then
  77.     ' disable encryption
  78.    mbEncrypt = False
  79.     mlSeed = 0
  80.     Exit Property
  81.   End If
  82.   ' calculate the seed and enable encryption
  83.  mlSeed = CalcSeed(sPwd)
  84.   mbEncrypt = True
  85. End Property
  86.  
  87. ' get the propertybag object
  88. Public Property Get PropBag() As PropertyBag
  89.   Set PropBag = mobjPB
  90. End Property
  91.  
  92. ' get the last error number
  93. Public Property Get ErrNum() As Long
  94.   ErrNum = mlErrNum
  95. End Property
  96.  
  97. ' get the last error message
  98. Public Property Get ErrMsg() As Long
  99.   ErrMsg = msErrMsg
  100. End Property
  101.  
  102. ' stores a value
  103. Public Sub PutValue(ByVal sKeyName As String, ByVal vData As Variant)
  104.   mobjPB.WriteProperty sKeyName, vData
  105.   If mbAutoSave = True Then
  106.     SaveSettings
  107.   End If
  108. End Sub
  109.  
  110. ' retrieves a value, if not present, returns the default
  111. Public Function GetValue(ByVal sKeyName As String, _
  112.                          ByVal vDefault As Variant) As Variant
  113.   Dim vValue As Variant
  114.  
  115.   vValue = mobjPB.ReadProperty(sKeyName, vDefault)
  116.   If IsObject(vValue) Or IsObject(vDefault) Then
  117.     Set GetValue = vValue
  118.   Else
  119.     GetValue = vValue
  120.   End If
  121. End Function
  122.  
  123. ' Saves the PB to disk
  124. Public Function SaveSettings() As Boolean
  125.   Dim fp As Long
  126.   Dim cbContents() As Byte
  127.   Dim bRet As Boolean
  128.    
  129.   On Local Error GoTo Catch
  130.   SetError 0, ""
  131.   bRet = False
  132.   SaveSettings = bRet
  133.   If Len(msFileName) < 1 Then
  134.     SetError vbObjectError, "Missing storage file name"
  135.     Exit Function
  136.   End If
  137.  
  138.   ' get the PB contents and saves it to binary file
  139.  cbContents = mobjPB.Contents
  140.   If mbEncrypt Then
  141.     ' encrypt the data
  142.    EncDec cbContents
  143.   End If
  144.   fp = FreeFile()
  145.   Open msFileName For Binary Access Write As #fp
  146.   Put #fp, , cbContents
  147.   Close #fp
  148.   bRet = True
  149.  
  150. BailOut:
  151.   ' common exit point
  152.  On Local Error Resume Next
  153.   Close #fp
  154.   SaveSettings = bRet
  155.   Exit Function
  156.  
  157. Catch:
  158.   ' error handler
  159.  SetError Err.Number, Err.Description
  160.   bRet = False
  161.   Resume BailOut
  162. End Function
  163.  
  164. ' Loads the PB from disk
  165. Public Function LoadSettings() As Boolean
  166.   Dim fp As Long
  167.   Dim cbContents() As Byte
  168.   Dim bRet As Boolean
  169.  
  170.   On Local Error GoTo Catch
  171.   SetError 0, ""
  172.   bRet = False
  173.   LoadSettings = bRet
  174.   If Len(msFileName) < 1 Then
  175.     SetError vbObjectError, "Missing storage file name"
  176.     Exit Function
  177.   End If
  178.  
  179.   ' loads PB data from binary file
  180.  fp = FreeFile()
  181.   Open msFileName For Binary Access Read As #fp
  182.   ReDim cbContents(LOF(fp) - 1)
  183.   Get #fp, , cbContents
  184.   Close #fp
  185.   If mbEncrypt Then
  186.     ' decrypt the data
  187.    EncDec cbContents
  188.   End If
  189.   mobjPB.Contents = cbContents
  190.   bRet = True
  191.  
  192. BailOut:
  193.   ' common exit point
  194.  On Local Error Resume Next
  195.   Close #fp
  196.   LoadSettings = bRet
  197.   Exit Function
  198.  
  199. Catch:
  200.   ' error handler
  201.  SetError Err.Number, Err.Description
  202.   bRet = False
  203.   Resume BailOut
  204. End Function
  205.  
  206. ' encrypt/decrypt a byte array
  207. Private Sub EncDec(ByRef cbContents() As Byte)
  208.   Dim lIdx As Long
  209.  
  210.   ' initialize the random generator
  211.  InitRNG
  212.  
  213.   ' loop over the array and encrypt/decrypt bytes
  214.  ' using a random numbers sequence depending from
  215.  ' the given password
  216.  For lIdx = LBound(cbContents) To UBound(cbContents)
  217.     cbContents(lIdx) = cbContents(lIdx) Xor RndByte()
  218.   Next lIdx
  219.  
  220.   ' reset the random generator
  221.  Randomize Timer
  222. End Sub
  223.  
  224. ' initialize the random generator
  225. Private Sub InitRNG()
  226.   Call Rnd(-1) ' <-- see randomize help
  227.  Randomize mlSeed
  228. End Sub
  229.  
  230. ' get a random byte value
  231. Private Function RndByte() As Byte
  232.   RndByte = Int(256 * Rnd)
  233. End Function
  234.  
  235. ' calculate the seed for a password
  236. Private Function CalcSeed(ByVal sPwd As String) As Long
  237.   Dim cbPass() As Byte
  238.   Dim lHash As Long
  239.   Dim lIdx As Long
  240.  
  241.   On Local Error Resume Next
  242.   lHash = 0
  243.   cbPass = StrConv(sPwd, vbFromUnicode)
  244.   For lIdx = LBound(cbPass) To UBound(cbPass)
  245.     lHash = lHash + (lIdx * cbPass(lIdx))
  246.   Next lIdx
  247.   CalcSeed = lHash
  248. End Function
  249.  
  250. ' set last error informations
  251. Private Sub SetError(ByVal lNum As Long, _
  252.                      ByVal sMsg As String)
  253.  
  254.   mlErrNum = lNum
  255.   msErrMsg = sMsg
  256. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement