home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / sightmap / site.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-02-24  |  18.5 KB  |  688 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 = "Site"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'CLASS -- Site -- Site.cls
  17.  
  18. '--------------------------------------------------------------------------
  19. '<Purpose>
  20. '   Encapsulate the functions and data needed to build a site's map.
  21. '
  22. '--------------------------------------------------------------------------
  23.  
  24. Private strFiles() As String                'holds files for the site
  25. Private strSmallFiles() As String           'holds small (short) file names
  26. Private strUKFiles() As String              'Unknown files found
  27. Private strFilters() As String              'Filters to look for
  28. Private strFilterLen() As String            'lengths of each filter
  29. Private Matrix() As Integer                 'The adjacency matrix
  30. Private strDirs() As String                 'The directories to search
  31. Private intFilterCt As Integer              'amt of filters
  32. Private intUKFileCt As Integer              'amt of unknown files found
  33. Private intFileCt As Integer                'amt of files
  34. Private intDirCt As Integer                 'amt of directories
  35. Private strName As String                   'name of this site
  36. Private strMainDirectory As String          'main directory to start parsing
  37. Private strID As String                     'internal name for this site
  38. Private blnChooseRoot As Boolean            'did user provide start point
  39. Private strRoot As String                   'start point for map
  40. Private Visited() As Boolean                'Used by traversal algorithms
  41.  
  42. '*******************************************************************************
  43.  
  44. Private Function MapName(ByVal Vertex As String) As Integer
  45. Dim i As Integer
  46.  
  47.     Vertex = UCase$(Vertex)
  48.  
  49.     For i = 1 To intFileCt
  50.         If strSmallFiles(i) = Vertex Then
  51.             MapName = i
  52.             Exit For
  53.         End If
  54.     Next
  55.  
  56. End Function
  57. '*******************************************************************************
  58.  
  59. Public Sub RemoveFilter(intIndex As Integer)
  60. Dim i As Integer
  61.  
  62.  
  63.     strFilters(intIndex) = strFilters(intFilterCt)
  64.     ReDim Preserve strFilters(intFilterCt - 1)
  65.     intFilterCt = intFilterCt - 1
  66.     
  67. End Sub
  68.  
  69. '*******************************************************************************
  70.  
  71. Private Sub ResetVisited()
  72. Dim i As Integer
  73.  
  74.     For i = 1 To intFileCt
  75.         Visited(i) = False
  76.     Next
  77.     
  78. End Sub
  79.  
  80. '*******************************************************************************
  81. Public Sub SaveSite(strPath As String)
  82. On Error GoTo SaveSite_Error
  83.  
  84. Dim i As Integer        'lcv
  85. Dim j As Integer        'lcv
  86.  
  87.     Open strPath For Output As #1
  88.      
  89.     If intFileCt < 1 Then
  90.         Err.Raise vbObjectError + 1, "SaveSite", "Site not fully defined.  Cannot save at this time."
  91.     End If
  92.     
  93.     'write site id and name
  94.     Write #1, strID, strName
  95.     
  96.     'Write the number of files
  97.     Write #1, intFileCt
  98.     
  99.     'write the files and short file names
  100.     For i = 1 To intFileCt
  101.         Write #1, strFiles(i), strSmallFiles(i)
  102.     Next
  103.     
  104.     'write the adjacency matrix, row by row
  105.     For i = 1 To intFileCt
  106.         For j = 1 To intFileCt
  107.             Write #1, Matrix(i, j)
  108.         Next j
  109.     Next i
  110.     
  111.     'Write the number of filters
  112.     Write #1, intFilterCt
  113.     
  114.     'write the filters and filter lengths
  115.     For i = 1 To intFilterCt
  116.         Write #1, strFilters(i), strFilterLen(i)
  117.     Next
  118.     
  119.     'write the unknown file amt
  120.     Write #1, intUKFileCt
  121.     
  122.     'write out the uk files
  123.     For i = 1 To intUKFileCt
  124.         Write #1, strUKFiles(i)
  125.     Next
  126.     
  127.     'Write out number of directories
  128.     Write #1, intDirCt
  129.     
  130.     'Write out the directories holding the site
  131.     For i = 1 To intDirCt
  132.         Write #1, strDirs(i)
  133.     Next
  134.     
  135.     'write out the site root definition
  136.     Write #1, strMainDirectory, blnChooseRoot, strRoot
  137.     
  138.     
  139.     
  140.     Close #1
  141. Exit Sub
  142. SaveSite_Error:
  143.  
  144.     Close #1
  145.     Err.Raise vbObjectError + 1, "Save Site", CStr(Err.Number) & " -- " + Err.Description
  146.     
  147.  
  148. End Sub
  149.  
  150. '*******************************************************************************
  151. Public Sub OpenSite(strPath As String)
  152. On Error GoTo OpenSite_Error
  153.  
  154. Dim i As Integer        'lcv
  155. Dim j As Integer
  156.  
  157.     Open strPath For Input As #1
  158.     
  159.     Input #1, strID, strName
  160.     
  161.     'input the number of files
  162.     Input #1, intFileCt
  163.     
  164.     ReDim strFiles(intFileCt)
  165.     ReDim strSmallFiles(intFileCt)
  166.     ReDim Visited(intFileCt)
  167.     
  168.     'input the files and short file names
  169.     For i = 1 To intFileCt
  170.         Input #1, strFiles(i), strSmallFiles(i)
  171.     Next
  172.     
  173.     ReDim Matrix(intFileCt, intFileCt)
  174.     
  175.     'input the adjacency matrix, row by row
  176.     For i = 1 To intFileCt
  177.         For j = 1 To intFileCt
  178.             Input #1, Matrix(i, j)
  179.         Next j
  180.     Next i
  181.     
  182.     'input the number of filters
  183.     Input #1, intFilterCt
  184.     
  185.     ReDim strFilters(intFilterCt)
  186.     ReDim strFilterLen(intFilterCt)
  187.     
  188.     'input the filters and filter lengths
  189.     For i = 1 To intFilterCt
  190.         Input #1, strFilters(i), strFilterLen(i)
  191.     Next
  192.     
  193.     'input the unknown file amt
  194.     Input #1, intUKFileCt
  195.     
  196.     If intUKFileCt > 0 Then
  197.         ReDim strUKFiles(intFileCt)
  198.         
  199.         'input out the uk files
  200.         For i = 1 To intUKFileCt
  201.             Input #1, strUKFiles(i)
  202.         Next
  203.     End If
  204.     
  205.     'input out number of directories
  206.     Input #1, intDirCt
  207.     
  208.     If intDirCt > 0 Then
  209.         ReDim strDirs(intDirCt)
  210.         
  211.         'input out the directories holding the site
  212.         For i = 1 To intDirCt
  213.             Input #1, strDirs(i)
  214.         Next
  215.     End If
  216.     
  217.     'input out the site root definition
  218.     Input #1, strMainDirectory, blnChooseRoot, strRoot
  219.  
  220.  
  221.     Close #1
  222.  
  223. Exit Sub
  224. OpenSite_Error:
  225.  
  226.     Close #1
  227.     Err.Raise vbObjectError + 1, "Open Site", CStr(Err.Number) & " -- " + Err.Description
  228.     
  229. End Sub
  230. '*******************************************************************************
  231.  
  232. Private Function AllVisited() As Boolean
  233. Dim i As Integer
  234. Dim blnFlag As Boolean
  235.  
  236.     blnFlag = True
  237.     
  238.     For i = 1 To intFileCt
  239.         If Visited(i) = False Then
  240.             blnFlag = False
  241.             Exit For
  242.         End If
  243.     Next
  244.     
  245.     AllVisited = blnFlag
  246.  
  247. End Function
  248. '*******************************************************************************
  249.  
  250. Public Sub DrawTree(tv As TreeView)
  251. Dim n As Node
  252. Dim idx As Integer
  253. Dim j As Integer
  254. Dim intNodeIdx As Integer
  255. Dim blnAllVisited As Boolean
  256.  
  257.     blnAllVisited = False
  258.     
  259.     If blnChooseRoot Then
  260.         'Supplied root
  261.         idx = MapName(strRoot)
  262.         If idx = 0 Then
  263.             Exit Sub
  264.         End If
  265.     Else
  266.         'Divine the root!
  267.         idx = DivineRoot()
  268.         If idx > 0 Then
  269.             strRoot = strSmallFiles(idx)
  270.         Else
  271.             Exit Sub
  272.         End If
  273.     End If
  274.     
  275.     Call ResetVisited
  276.     
  277.     Set n = tv.Nodes.Add
  278.     n.Text = strName
  279.     intNodeIdx = 1
  280.     Visited(idx) = True
  281.     Set n = tv.Nodes.Add(intNodeIdx, tvwChild)
  282.     n.Text = strRoot
  283.     
  284.         
  285.     Call FillBranch(tv, n, idx, n.Index)
  286.    
  287. End Sub
  288. '*******************************************************************************
  289.  
  290. Private Sub FillBranch(tv As TreeView, n As Node, idx As Integer, intNodeIdx)
  291. 'Recursive
  292. Dim j As Integer
  293.     
  294.     
  295.     For j = 1 To intFileCt
  296.         If (Matrix(idx, j) = 1) And (idx <> j) And (Not Visited(j)) Then
  297.             'We have an edge
  298.             Set n = tv.Nodes.Add(intNodeIdx, tvwChild)
  299.             n.Text = strSmallFiles(j)
  300.             Visited(j) = True
  301.             If Not AllVisited() Then
  302.                 Call FillBranch(tv, n, j, n.Index)
  303.             End If
  304.         End If
  305.     Next
  306.     
  307. End Sub
  308. '*******************************************************************************
  309.  
  310. Function DivineRoot() As Integer
  311.  
  312. Dim i As Integer
  313. Dim j As Integer
  314. Dim intFreq As Integer
  315. Dim intCurFreq As Integer
  316. Dim intFreqIdx As Integer
  317.  
  318.     'Examine matrix.  The row with the most 1's (higest frequency) will be our root.
  319.     For i = 1 To intFileCt
  320.         intCurFreq = 0
  321.         For j = 1 To intFileCt
  322.             If Matrix(i, j) > 0 Then
  323.                 intCurFreq = intCurFreq + 1
  324.             End If
  325.         Next j
  326.         If intCurFreq > intFreq Then
  327.             intFreq = intCurFreq
  328.             intFreqIdx = i
  329.         End If
  330.     Next i
  331.         
  332.     DivineRoot = intFreqIdx
  333.  
  334. End Function
  335. '*******************************************************************************
  336.  
  337. Public Sub PrintMatrix()
  338. Dim iRow As Integer
  339. Dim iCol As Integer
  340. Dim i As Integer
  341. Dim strRow As String
  342. Dim blnHasLink As Boolean
  343.  
  344.  
  345.     Open "c:\matrix.xls" For Output As #1
  346.     
  347.     Print #1, "Site:"; strName
  348.     Print #1, vbCrLf
  349.     Print #1, "Files (vertices)"
  350.     For i = 1 To intFileCt
  351.         Print #1, i; Tab; strSmallFiles(i); vbCrLf
  352.     Next
  353.     
  354.     Print #1, vbCrLf
  355.     Print #1, vbCrLf
  356.     
  357.     Print #1, "Unknown files"
  358.     For i = 1 To intUKFileCt - 1
  359.         Print #1, i; Tab; strUKFiles(i); vbCrLf
  360.     Next
  361.     
  362.     blnHasLink = False
  363.     Print #1, "Files with no links"
  364.     For iRow = 1 To intFileCt
  365.         blnHasLink = False
  366.         For iCol = 1 To intFileCt
  367.             If Matrix(iRow, iCol) = 1 Then
  368.                 blnHasLink = True
  369.                 Exit For
  370.             End If
  371.         Next
  372.         If Not blnHasLink Then
  373.             Print #1, iRow; Tab; strSmallFiles(iRow)
  374.         End If
  375.     Next
  376.     
  377.     Print #1, vbCrLf
  378.     Print #1, vbCrLf
  379.     
  380.     Print #1, "Adjacency Matrix"
  381.     
  382.     strRow = ""
  383.     For i = 1 To intFileCt
  384.         strRow = strRow & vbTab & CStr(i)
  385.     Next
  386.     Print #1, strRow
  387.     For iRow = 1 To intFileCt
  388.         strRow = CStr(iRow) & vbTab
  389.         For iCol = 1 To intFileCt
  390.             strRow = strRow & CStr(Matrix(iRow, iCol)) & vbTab
  391.         Next
  392.         Print #1, strRow
  393.     Next
  394.     
  395.     Print #1, vbCrLf
  396.     Print #1, vbCrLf
  397.     
  398.     Print #1, "Adjacency List"
  399.     For iRow = 1 To intFileCt
  400.         Print #1, iRow; strSmallFiles(iRow) & ":"
  401.         For iCol = 1 To intFileCt
  402.             If Matrix(iRow, iCol) = 1 Then
  403.                 Print #1, Tab; strSmallFiles(iCol)
  404.             End If
  405.         Next
  406.         Print #1, vbCrLf
  407.     Next
  408.     
  409.     
  410.     Close #1
  411.  
  412. End Sub
  413. '*******************************************************************************
  414.  
  415. Private Sub MakeSmallFiles()
  416.  
  417. Dim i As Integer
  418. Dim ch As String
  419. Dim j As Integer
  420.  
  421.     For i = 1 To intFileCt
  422.         For j = Len(strFiles(i)) To 1 Step -1
  423.             ch = Mid$(strFiles(i), j, 1)
  424.             If ch = "\" Then
  425.                 strSmallFiles(i) = UCase$(Right$(strFiles(i), Len(strFiles(i)) - j))
  426.                 Exit For
  427.             End If
  428.         Next
  429.     Next
  430.  
  431.  
  432. End Sub
  433. '*******************************************************************************
  434.  
  435. Public Sub BuildMatrix()
  436. On Error GoTo BuildMatrix_Error
  437.  
  438. Dim i As Integer
  439. Dim j As Integer
  440. Dim strLine As String
  441.  
  442.  
  443.     'Resize working structures
  444.  
  445.     ReDim Matrix(intFileCt, intFileCt)
  446.     ReDim Visited(intFileCt)
  447.     UKFileCount = 0
  448.     
  449.     Call MakeSmallFiles
  450.     
  451.     'Go through list of files (verticies) and construct adjacency matrix
  452.     For i = 1 To intFileCt
  453.         Open strFiles(i) For Input As #1
  454.         Do While Not EOF(1)
  455.             Line Input #1, strLine
  456.             
  457.             For j = 1 To intFilterCt
  458.                 If InStr(1, strLine, strFilters(j), vbTextCompare) Then
  459.                     Call UnstringName(strFilters(j), strLine, i, strFilterLen(j))
  460.                 End If
  461.             Next
  462.         Loop
  463.         Close #1
  464.     Next
  465.     
  466.    Call PrintMatrix
  467.  
  468. Exit Sub
  469. BuildMatrix_Error:
  470.  
  471.     Close #1
  472.     Err.Raise vbObjectError + 1, "Build Matrix", CStr(Err.Number) & " -- " + Err.Description
  473.     
  474. End Sub
  475.  
  476. '*******************************************************************************
  477.  
  478. Private Sub UnstringName(strFilter As String, strLine As String, _
  479.     intIndex As Integer, intFiltLen)
  480. On Error GoTo UnstringName_Error
  481. Dim intStart As Integer             'Starting point for search
  482. Dim intFileEnd As Integer
  483. Dim intFileStart As Integer
  484. Dim strFile As String
  485. Dim i As Integer
  486. Dim blnMoreFiles As Boolean
  487. Dim intMappedName As Integer
  488. Dim ch As String
  489.  
  490.     blnMoreFiles = True
  491.  
  492.     'Get starting point
  493.     intStart = InStr(1, strLine, strFilter, vbTextCompare)
  494.     
  495.     Do While blnMoreFiles
  496.         intMappedName = 0
  497.         For i = intStart To 1 Step -1
  498.             ch = Mid$(strLine, i, 1)
  499.             If (ch = "/") Or (ch = "\") Or (Asc(ch) = 34) Or (ch = "?") Or (ch = "=") Then
  500.                 intFileStart = i + 1
  501.                 intFileEnd = intStart + intFiltLen - 1
  502.                 strFile = Mid$(strLine, intFileStart, (intFileEnd - intFileStart + 1))
  503.                 intMappedName = MapName(strFile)
  504.                 If intMappedName > 0 Then
  505.                     'Found a vertex
  506.                     Matrix(intIndex, intMappedName) = 1
  507.                 Else
  508.                     'This file is a dead link
  509.                     If (UCase(strFile) <> "HTM") And (UCase(strFile) <> "HTML") Then
  510.                         UKFileCount = UKFileCount + 1
  511.                         strUKFiles(UKFileCount) = strFile & " found in " & strFiles(intIndex)
  512.                     End If
  513.                 End If
  514.                 Exit For
  515.             End If
  516.         Next
  517.     
  518.         'Move start point one over to loop for another instance of the filter
  519.         intStart = intStart + 1
  520.         If InStr(intStart, strLine, strFilter, vbTextCompare) Then
  521.             intStart = InStr(intStart, strLine, strFilter, vbTextCompare)
  522.         Else
  523.             blnMoreFiles = False
  524.         End If
  525.     Loop
  526.     
  527. Exit Sub
  528. UnstringName_Error:
  529.  
  530.      Err.Raise vbObjectError + 1, "UnstringName", CStr(Err.Number) & " -- " + Err.Description
  531.      
  532. End Sub
  533.  
  534.  
  535.  
  536.  
  537. '*******************************************************************************
  538.  
  539. Public Property Get MainDirectory() As String
  540.     MainDirectory = strMainDirectory
  541. End Property
  542.  
  543. Public Property Let MainDirectory(ByVal NewDir As String)
  544.     strMainDirectory = NewDir
  545. End Property
  546.  
  547. '*******************************************************************************
  548.  
  549. Public Property Get FileEntry(intIndex As Integer) As String
  550.     FileEntry = strFiles(intIndex)
  551. End Property
  552.  
  553. Public Property Let FileEntry(intIndex As Integer, ByVal strNewFile As String)
  554.     strFiles(intIndex) = strNewFile
  555. End Property
  556. '*******************************************************************************
  557.  
  558. Public Property Get FilterEntry(intIndex As Integer) As String
  559.     FilterEntry = strFilters(intIndex)
  560. End Property
  561.  
  562. Public Property Let FilterEntry(intIndex As Integer, ByVal strNewFilterEntry As String)
  563.     strFilters(intIndex) = strNewFilterEntry
  564.     strFilterLen(intIndex) = Len(strNewFilterEntry)
  565. End Property
  566. '*******************************************************************************
  567.  
  568. Public Property Get DirEntry(intIndex As Integer) As String
  569.     DirEntry = strDirs(intIndex)
  570. End Property
  571.  
  572. Public Property Let DirEntry(intIndex As Integer, ByVal strNewDirEntry As String)
  573.     strDirs(intIndex) = strNewDirEntry
  574. End Property
  575.  
  576. '*******************************************************************************
  577.  
  578. Public Property Get DirectoryCount() As Integer
  579.     DirectoryCount = intDirCt
  580. End Property
  581.  
  582. Public Property Let DirectoryCount(ByVal NewCount As Integer)
  583.     intDirCt = NewCount
  584.     If intDirCt > 0 Then
  585.         ReDim Preserve strDirs(intDirCt)
  586.     Else
  587.         Erase strDirs
  588.     End If
  589. End Property
  590. '*******************************************************************************
  591.  
  592. Public Property Get UKFileEntry(intIndex As Integer) As String
  593.     UKFileEntry = strUKFiles(intIndex)
  594. End Property
  595.  
  596. Public Property Let UKFileEntry(intIndex As Integer, ByVal strNewFile As String)
  597.     strUKFiles(intIndex) = strNewFile
  598. End Property
  599.  
  600. '*******************************************************************************
  601.  
  602. Public Property Get UKFileCount() As Integer
  603.     UKFileCount = intUKFileCt
  604. End Property
  605.  
  606. Private Property Let UKFileCount(ByVal NewCount As Integer)
  607.     intUKFileCt = NewCount
  608.     If intUKFileCt > 0 Then
  609.         ReDim Preserve strUKFiles(intUKFileCt)
  610.     Else
  611.         Erase strUKFiles
  612.     End If
  613. End Property
  614.  
  615. '*******************************************************************************
  616.  
  617. Public Property Get FilterCount() As Integer
  618.     FilterCount = intFilterCt
  619. End Property
  620.  
  621. Public Property Let FilterCount(ByVal NewCount As Integer)
  622.     intFilterCt = NewCount
  623.     If intFilterCt > 0 Then
  624.         ReDim Preserve strFilters(intFilterCt)
  625.         ReDim Preserve strFilterLen(intFilterCt)
  626.     Else
  627.         Erase strFilters
  628.         Erase strFilterLen
  629.     End If
  630. End Property
  631. '*******************************************************************************
  632.  
  633. Public Property Get SiteName() As String
  634.     SiteName = strName
  635. End Property
  636.  
  637. Public Property Let SiteName(ByVal NewName As String)
  638.     strName = NewName
  639. End Property
  640. '*******************************************************************************
  641.  
  642. Public Property Get SiteID() As String
  643.     SiteID = strID
  644. End Property
  645.  
  646. Public Property Let SiteID(ByVal NewID As String)
  647.     strID = NewID
  648. End Property
  649. '*******************************************************************************
  650.  
  651. Public Property Get FileCount() As Integer
  652.     FileCount = intFileCt
  653. End Property
  654.  
  655. Public Property Let FileCount(ByVal NewCount As Integer)
  656.     intFileCt = NewCount
  657.     If intFileCt > 0 Then
  658.         ReDim Preserve strFiles(intFileCt)
  659.         ReDim Preserve strSmallFiles(intFileCt)
  660.         ReDim Preserve Visited(intFileCt)
  661.     Else
  662.         Erase strFiles
  663.         Erase strSmallFiles
  664.         Erase Visited
  665.     End If
  666. End Property
  667.  
  668.  
  669. '*******************************************************************************
  670.  
  671. Public Property Get ChooseRoot() As Boolean
  672.     ChooseRoot = blnChooseRoot
  673. End Property
  674.  
  675. Public Property Let ChooseRoot(ByVal NewChoice As Boolean)
  676.     blnChooseRoot = NewChoice
  677. End Property
  678.  
  679. '*******************************************************************************
  680.  
  681. Public Property Get Root() As String
  682.     Root = strRoot
  683. End Property
  684.  
  685. Public Property Let Root(ByVal NewRoot As String)
  686.     strRoot = NewRoot
  687. End Property
  688.