VERSION 1.0 CLASS BEGIN MultiUse = -1 'True Persistable = 0 'NotPersistable DataBindingBehavior = 0 'vbNone DataSourceBehavior = 0 'vbNone MTSTransactionMode = 0 'NotAnMTSObject END Attribute VB_Name = "cCache" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = False Option Explicit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Objeto responsável por carregar a cache ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Guarda o nome do objeto Private Const ST_MY_NAME As String = "cCache" ' Nome do arquivo meta-dado Private Const ST_META_DATA_FILE_NAME As String = "cache.dat" ' Nome do arquivo hash Private Const ST_HASH_TABLE_FILE_NAME As String = "cache.id" ' Vetor de posições livres Private vtln_FreeAddress() As Long ' Objeto de controle meta-dado Private md_Data As cCacheMD ' Objeto de controle hash Private ht_Hash As cCacheHT ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Retorna a lyric (se tiver) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function GetLyric(cmMusic As cMusic) As String Dim vtlnAddress() As Long Dim stFileName As String, lnHashName As Long Dim lnI As Long On Error GoTo ErrTreat: ' Inicia retorno GetLyric = ST_INVALID_VALUE ' Pega o nome do arquivo stFileName = cmMusic.Info ' Pega o hash lnHashName = GetLngHash(stFileName) ' Retorna vetor de endereços vtlnAddress = ht_Hash.GetAddress(lnHashName) ' Varre o vetor de endereços For lnI = 0 To (VtSize(vtlnAddress()) - 1) ' Verifica se o endereço bate com a letra procurada If md_Data.IsFile(vtlnAddress(lnI), stFileName) Then ' Retorna conteúdo GetLyric = wcharTOwstring(md_Data.GetData(vtlnAddress(lnI))) ' Sai do loop Exit For End If Next lnI ' Libera vetor Erase vtlnAddress() Exit Function ErrTreat: LogErrMessage Err.Description, ST_MY_NAME + ".GetLyric" End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Salva/atualiza a lyric ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function SetLyric(cmMusic As cMusic, stData As String) As Boolean Dim vtlnAddress() As Long Dim stFileName As String, lnHashName As Long Dim lnOldAddress As Long, lnNewAddress As Long Dim lnI As Long On Error GoTo ErrTreat: ' Inicia retorno SetLyric = False ' Pega o nome do arquivo stFileName = cmMusic.Info ' Pega o hash lnHashName = GetLngHash(stFileName) ' Retorna vetor de endereços vtlnAddress = ht_Hash.GetAddress(lnHashName) ' Varre o vetor de endereços For lnI = 0 To (VtSize(vtlnAddress()) - 1) ' Verifica se o endereço bate com a letra procurada If md_Data.IsFile(vtlnAddress(lnI), stFileName) Then ' Pega o endereço antigo lnOldAddress = vtlnAddress(lnI) ' Deleta arquivo md_Data.DelData lnOldAddress, vtln_FreeAddress ' Grava arquivo lnNewAddress = md_Data.SetData(vtln_FreeAddress, stFileName, wstringTOwchar(stData)) ' Verifica se não ocorreram erros If lnNewAddress > LN_NO_ERROR Then ' Atualiza vetor na tabela hash ht_Hash.FreeAddress = vtln_FreeAddress ' Atualiza na tabela hash ht_Hash.UpdateAddress lnHashName, lnOldAddress, lnNewAddress ' Retorna que gravou SetLyric = True End If ' Sai do loop GoTo Finally: Exit Function End If Next lnI ' Verifica se tem que limpar cache lnI = ht_Hash.GetFlush ' Se for deletar pelo menos um If (lnI <> LN_INVALID_VALUE) Then ' Enquanto tiver que limpar While (lnI <> LN_INVALID_VALUE) ' Deleta arquivo md_Data.DelData ht_Hash.GetAddressIndex(lnI), vtln_FreeAddress ' Deleta da tabela hash ht_Hash.DelAddressIndex lnI ' Verifica se tem que limpar cache lnI = ht_Hash.GetFlush Wend ' Atualiza vetor na tabela hash ht_Hash.FreeAddress = vtln_FreeAddress ' Salva tabela hash ht_Hash.SaveHashTable End If ' Grava arquivo lnNewAddress = md_Data.SetData(vtln_FreeAddress, stFileName, wstringTOwchar(stData)) ' Verifica se não ocorreram erros If lnNewAddress > LN_NO_ERROR Then ' Atualiza vetor na tabela hash ht_Hash.FreeAddress = vtln_FreeAddress ' Grava na tabela hash ht_Hash.SetNewAddress lnHashName, lnNewAddress ' Retorna que gravou SetLyric = True End If Finally: ' Libera vetor Erase vtlnAddress() Exit Function ErrTreat: LogErrMessage Err.Description, ST_MY_NAME + ".SetLyric" End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Deleta a lyric ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function DelLyric(cmMusic As cMusic) As Boolean Dim vtlnAddress() As Long Dim stFileName As String, lnHashName As Long Dim lnOldAddress As Long Dim lnI As Long Dim lnJ As Long On Error GoTo ErrTreat: ' Inicia retorno DelLyric = False ' Pega o nome do arquivo stFileName = cmMusic.Info ' Pega o hash lnHashName = GetLngHash(stFileName) ' Retorna vetor de endereços vtlnAddress = ht_Hash.GetAddress(lnHashName) ' Seta última posição lnJ = VtSize(vtlnAddress()) - 1 ' Varre o vetor de endereços For lnI = 0 To lnJ ' Verifica se o endereço bate com a letra procurada If md_Data.IsFile(vtlnAddress(lnI), stFileName) Then ' Pega o endereço antigo lnOldAddress = vtlnAddress(lnI) ' Deleta arquivo md_Data.DelData lnOldAddress, vtln_FreeAddress ' Atualiza vetor na tabela hash ht_Hash.FreeAddress = vtln_FreeAddress ' Deleta da tabela hash ht_Hash.DelAddress lnHashName, lnOldAddress ' Retorna que deletou DelLyric = True ' Sai do loop Exit For End If Next lnI ' Libera vetor Erase vtlnAddress() Exit Function ErrTreat: LogErrMessage Err.Description, ST_MY_NAME + ".DelLyric" End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Limpa todo o cache ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub ClearCache() On Error GoTo ErrTreat: ' Deleta toda a tabela hash e salva em arquivo ht_Hash.DelAllAddress Exit Sub ErrTreat: LogErrMessage Err.Description, ST_MY_NAME + ".ClearCache" End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Tratamentos para quando inicia / finaliza a classe ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Class_Initialize() Dim stPath As String ' Carrega objetos Set md_Data = New cCacheMD Set ht_Hash = New cCacheHT ' Limpa vetor Erase vtln_FreeAddress() ' Pega pasta de trabalho de cache stPath = getSpecialFolder(efCache) ' Carrega arquivos md_Data.FileAddress = stPath + ST_META_DATA_FILE_NAME ht_Hash.FileAddress = stPath + ST_HASH_TABLE_FILE_NAME ' Pega vetor de posições livres vtln_FreeAddress = ht_Hash.FreeAddress End Sub Private Sub Class_Terminate() ' Libera objetos Set md_Data = Nothing Set ht_Hash = Nothing ' Limpa vetor Erase vtln_FreeAddress() End Sub