home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 45 / SuperCD45.iso / talleres / vbasic / BNodeList.cls < prev    next >
Encoding:
Visual Basic class definition  |  2000-01-30  |  6.7 KB  |  213 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "BNodeList"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"No"
  16. Attribute VB_Ext_KEY = "Member0" ,"Node"
  17. Option Explicit
  18. Private Type SECURITY_ATTRIBUTES
  19.         nLength As Long
  20.         lpSecurityDescriptor As Long
  21.         bInheritHandle As Long
  22. End Type
  23.  
  24. Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
  25. Private Declare Function GetLastError Lib "kernel32" () As Long
  26. Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
  27. Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
  28. Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
  29.  
  30. Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
  31. Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
  32.  
  33. 'local variable to hold collection
  34. Private mCol As Collection
  35. Public Function Changed(prev As BNode) As BNodeList
  36. Dim n As BNode, p As BNodeList, r As New BNodeList
  37.  
  38. For Each n In mCol
  39.     If n.attributes And vbArchive Then
  40.         Call r.Add(n.fileName, n.attributes, Nothing, n.fileName)
  41.         Set r(n.fileName).prevBNode = prev
  42.     End If
  43.         
  44.     If n.attributes And vbDirectory Then
  45.         Set p = n.nextBNode.Changed(n)
  46.         If p.Count > 0 Then
  47.             Call r.Add(n.fileName, n.attributes, p, n.fileName)
  48.             Set r(n.fileName).prevBNode = prev
  49.         End If
  50.     End If
  51. Next
  52.  
  53. Set Changed = r
  54.  
  55. End Function
  56. Public Function ResetArchiveBit(oldpath As String) As Long
  57. Dim n As BNode, total As Long, ofn As String, p As BNodeList, result As Long
  58.  
  59. total = 0
  60. For Each n In mCol
  61.     ofn = oldpath & "\" & n.fileName
  62.     If Not n.nextBNode Is Nothing Then
  63.         Set p = n.nextBNode
  64.         total = total + p.ResetArchiveBit(ofn)
  65.     Else
  66.         If SetFileAttributes(ofn, n.attributes - vbArchive) = 0 Then
  67.             result = Err.LastDllError
  68.             If MsgBox(ApiErrorText(result), vbOKCancel) = vbCancel Then
  69.                 End
  70.             End If
  71.         Else
  72.             total = total + 1
  73.         End If
  74.     End If
  75.     
  76. Next
  77. ResetArchiveBit = total
  78.  
  79. End Function
  80. Public Sub WriteCatalogue(filenum As Integer, dirLevel As Integer)
  81. Dim n As BNode, p As BNodeList, dirFlag As Boolean, pfn As String
  82.  
  83. For Each n In mCol
  84.     dirFlag = Not n.nextBNode Is Nothing
  85.     If n.prevBNode Is Nothing Then
  86.         pfn = ""
  87.     Else
  88.         pfn = n.prevBNode.fileName
  89.     End If
  90.     If dirFlag Then
  91.         Write #filenum, dirLevel, n.attributes, n.fileName, pfn, dirFlag
  92.         Set p = n.nextBNode
  93.         Call p.WriteCatalogue(filenum, dirLevel + 1)
  94.     Else
  95.         Write #filenum, dirLevel, n.attributes, n.fileName, pfn, dirFlag
  96.     End If
  97. Next
  98.  
  99. End Sub
  100. Private Function ApiErrorText(ByVal errcode As Long) As String
  101. Dim s As String, i As Long, f As Long
  102.  
  103. s = String(256, 0)
  104.  
  105. f = FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS
  106. i = FormatMessage(f, 0, errcode, 0, s, Len(s), ByVal 0)
  107. If i > 0 Then ApiErrorText = Left$(s, i)
  108.  
  109. End Function
  110. Public Sub CopyFiles(newpath As String, oldpath As String)
  111. Dim n As BNode, p As BNodeList
  112. Dim ofn As String, nfn As String, result As Long
  113. Dim sa As SECURITY_ATTRIBUTES
  114.  
  115. ' this isn't used - just set to zero
  116. sa.bInheritHandle = 0
  117. sa.lpSecurityDescriptor = 0
  118. sa.nLength = 0
  119.  
  120. If CreateDirectory(newpath, sa) = 0 Then
  121.     result = Err.LastDllError
  122.     If MsgBox(ApiErrorText(result), vbOKCancel) = vbCancel Then
  123.         End
  124.     End If
  125. End If
  126.  
  127. For Each n In mCol
  128.     ofn = oldpath & "\" & n.fileName
  129.     nfn = newpath & "\" & n.fileName
  130.     If Not n.nextBNode Is Nothing Then
  131.         Set p = n.nextBNode
  132.         Call p.CopyFiles(nfn, ofn)
  133.     Else
  134.         If CopyFile(ofn, nfn, 1) = 0 Then
  135.             result = Err.LastDllError
  136.             If MsgBox(ApiErrorText(result), vbOKCancel) = vbCancel Then
  137.                 End
  138.             End If
  139.         End If
  140.     End If
  141. Next
  142.  
  143. End Sub
  144. Public Function Add(fileName As String, attributes As Integer, nl As BNodeList, Optional sKey As String) As BNode
  145. Dim objNewMember As New BNode
  146.  
  147. 'set the properties passed into the method
  148. objNewMember.fileName = fileName
  149. Set objNewMember.nextBNode = nl
  150. objNewMember.attributes = attributes
  151. If Len(sKey) = 0 Then
  152.     mCol.Add objNewMember
  153. Else
  154.     mCol.Add objNewMember, sKey
  155. End If
  156.  
  157. 'return the object created
  158. Set Add = objNewMember
  159. Set objNewMember = Nothing
  160.  
  161. End Function
  162. Public Property Get Item(vntIndexKey As Variant) As BNode
  163. Attribute Item.VB_UserMemId = 0
  164.     'used when referencing an element in the collection
  165.     'vntIndexKey contains either the Index or Key to the collection,
  166.     'this is why it is declared as a Variant
  167.     'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
  168.   Set Item = mCol(vntIndexKey)
  169. End Property
  170. Public Function Display(indent As String) As String
  171. Dim n As BNode, s As String
  172.  
  173. For Each n In mCol
  174.     s = s & indent & n.fileName & vbCrLf
  175.     If n.attributes And vbDirectory Then
  176.         s = s & n.nextBNode.Display(" " & indent)
  177.     End If
  178. Next
  179.  
  180. Display = s
  181.  
  182. End Function
  183. Public Property Get Count() As Long
  184.     'used when retrieving the number of elements in the
  185.     'collection. Syntax: Debug.Print x.Count
  186.     Count = mCol.Count
  187. End Property
  188. Public Sub Remove(vntIndexKey As Variant)
  189.     'used when removing an element from the collection
  190.     'vntIndexKey contains either the Index or Key, which is why
  191.     'it is declared as a Variant
  192.     'Syntax: x.Remove(xyz)
  193.  
  194.  
  195.     mCol.Remove vntIndexKey
  196. End Sub
  197. Public Property Get NewEnum() As IUnknown
  198. Attribute NewEnum.VB_UserMemId = -4
  199. Attribute NewEnum.VB_MemberFlags = "40"
  200.     'this property allows you to enumerate
  201.     'this collection with the For...Each syntax
  202.     Set NewEnum = mCol.[_NewEnum]
  203. End Property
  204. Private Sub Class_Initialize()
  205.     'creates the collection when this class is created
  206.     Set mCol = New Collection
  207. End Sub
  208. Private Sub Class_Terminate()
  209.     'destroys collection when this class is terminated
  210.     Set mCol = Nothing
  211. End Sub
  212.  
  213.