home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Basic / GridOne / setup.EXE / MODUPGRADE.BAS < prev    next >
Encoding:
BASIC Source File  |  2001-09-09  |  9.8 KB  |  336 lines

  1. Attribute VB_Name = "modUpgrade"
  2. '-----------------------------------------------------------------------------
  3. ' This is a part of the GridONE ActiveX control.
  4. ' Copyright ⌐ 2000 Stinga
  5. ' All rights reserved.
  6. '
  7. ' You have a right to use and distribute the GridONE sample files in original
  8. ' form or modified, provided that you agree that Stinga has no warranty,
  9. ' obligations, or liability for any sample application files.
  10. '-----------------------------------------------------------------------------
  11. Option Explicit
  12.  
  13. Private marFiles() As String
  14.  
  15. Private Type BEEGRID_KEAYWORDS
  16.    Old As String
  17.    New As String
  18. End Type
  19.  
  20. Private marKeywords() As BEEGRID_KEAYWORDS
  21.  
  22. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  23. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  24. Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
  25.  
  26. 'data buffer for the GetOpenFileName and GetSaveFileName functions
  27. Private Type OPENFILENAME
  28.         lStructSize As Long
  29.         hwndOwner As Long
  30.         hInstance As Long
  31.         lpstrFilter As String
  32.         lpstrCustomFilter As String
  33.         nMaxCustFilter As Long
  34.         iFilterIndex As Long
  35.         lpstrFile As String
  36.         nMaxFile As Long
  37.         lpstrFileTitle As String
  38.         nMaxFileTitle As Long
  39.         lpstrInitialDir As String
  40.         lpstrTitle As String
  41.         Flags As Long
  42.         nFileOffset As Integer
  43.         nFileExtension As Integer
  44.         lpstrDefExt As String
  45.         lCustData As Long
  46.         lpfnHook As Long
  47.         lpTemplateName As String
  48. End Type
  49.  
  50. Private Function APIFilter(sIn)
  51.     'prepares sIn for use as a filter string in API common dialog functions
  52.     Dim lChrNdx As Long
  53.     Dim sOneChr As String
  54.     Dim sOutStr As String
  55.     
  56.     'convert any | characters to nulls
  57.     For lChrNdx = 1 To Len(sIn)
  58.         sOneChr = Mid$(sIn, lChrNdx, 1)
  59.         If sOneChr = "|" Then
  60.             sOutStr = sOutStr & Chr$(0)
  61.         Else
  62.             sOutStr = sOutStr & sOneChr
  63.         End If
  64.     Next
  65.     
  66.     'add a null to the end
  67.     sOutStr = sOutStr & Chr$(0)
  68.     
  69.     'return sOutStr
  70.     APIFilter = sOutStr
  71.     
  72. End Function
  73.  
  74. Private Sub ConvertFile(sFileName As String)
  75.    Dim sData As String
  76.    Dim i As Integer
  77.  
  78.    sData = ReadFile(sFileName)
  79.    
  80.    For i = 0 To UBound(marKeywords)
  81.       sData = Replace(sData, marKeywords(i).Old, marKeywords(i).New, , , vbTextCompare)
  82.    Next
  83.    
  84.    Clipboard.SetText sData
  85.    WriteFile sFileName, sData
  86. End Sub
  87.  
  88. Private Function CreateFileList(sProjectName As String) As Boolean
  89.    Dim iFreeNum%, sFileData$, i%
  90.    Dim varKeyWords As Variant
  91.    Dim varTmp As Variant
  92.    Dim sVBFile As String, iPos%
  93.    Dim sPath As String
  94.    
  95.    On Error GoTo ReadFileError
  96.       
  97.    sPath = GetPath(sProjectName)
  98.    ReDim marFiles(0) As String
  99.    
  100.    iFreeNum = FreeFile
  101.    
  102.    varKeyWords = Array("Form=", "Class=", "Module=")
  103.    
  104.    Open sProjectName For Input As iFreeNum
  105.    
  106.    If LOF(iFreeNum) = 0 Then GoTo ReadFileError
  107.    
  108.    Do While Not EOF(iFreeNum)
  109.        Line Input #iFreeNum, sFileData
  110.        Debug.Print sFileData
  111.        For i = 0 To 2
  112.          If InStr(1, sFileData, varKeyWords(i), vbTextCompare) > 0 Then
  113.             varTmp = Split(sFileData, "=")
  114.             
  115.             iPos = InStr(varTmp(1), ";")
  116.             
  117.             If iPos > 0 Then
  118.                sVBFile = sPath & Trim(Mid(varTmp(1), iPos + 1))
  119.             Else
  120.                sVBFile = sPath & Trim(varTmp(1))
  121.             End If
  122.             
  123.             If FileDoesExist(sVBFile) Then
  124.                ReDim Preserve marFiles(UBound(marFiles) + 1) As String
  125.                marFiles(UBound(marFiles)) = sVBFile
  126.                CreateFileList = True
  127.             End If
  128.          End If
  129.        Next
  130.    Loop
  131.    
  132.    Close #iFreeNum
  133.    Exit Function
  134. ReadFileError:
  135.    MsgBox VBA.Error, vbExclamation
  136.    Close #iFreeNum
  137. End Function
  138.  
  139. Private Function GetPath(ByVal sOrig As String) As String
  140.    Dim sPath As String
  141.  
  142.    sPath = StrReverse(sOrig)
  143.    If ((Len(sPath) <> 0) And (InStr(sPath, "\") <> 0)) Then
  144.       sPath = VBA.Left(sOrig, Len(sOrig) - InStr(sPath, "\") + 1)
  145.    ElseIf ((Len(sPath) <> 0) And (InStr(sPath, "/") <> 0)) Then
  146.       sPath = VBA.Left(sOrig, Len(sOrig) - InStr(sPath, "/") + 1)
  147.    ElseIf ((Len(sPath) <> 0) And (InStr(sPath, ":") <> 0)) Then
  148.       sPath = VBA.Left(sOrig, Len(sOrig) - InStr(sPath, ":") + 1)
  149.    Else
  150.       sPath = ""
  151.    End If
  152.    GetPath = sPath
  153. End Function
  154.  
  155. Private Sub InitKeywords()
  156.    ReDim marKeywords(4) As BEEGRID_KEAYWORDS
  157.  
  158.    marKeywords(0).Old = "BeeGridOLEDB10"
  159.    marKeywords(0).New = "BeeGridOLEDB10"
  160.    marKeywords(1).Old = "BeeGrid10"
  161.    marKeywords(1).New = "BeeGrid10"
  162.    marKeywords(2).Old = "BackColor"
  163.    marKeywords(2).New = "BackColor"
  164.    marKeywords(3).Old = "BeeGd10.ocx"
  165.    marKeywords(3).New = "BeeGd10.ocx"
  166.    marKeywords(4).Old = "BeeGdo10.ocx"
  167.    marKeywords(4).New = "BeeGdo10.ocx"
  168. '   marKeywords(5).Old = ""
  169. '   marKeywords(5).New = ""
  170. End Sub
  171.  
  172. Public Function ShowOpen( _
  173.    Optional sFilter As String = "All Files *.*|*.*", _
  174.    Optional sTitle As String = "Open file") As String
  175.    'display the file dialog for ShowOpen or ShowSave
  176.    Const DLG_ACTION = 1
  177.    Const dlg_poPathMustExist = &H800
  178.    Const dlg_poFileMustExist = &H1000
  179.    Const dlg_poExplorer = &H80000
  180.    Const dlg_Flags = dlg_poPathMustExist + dlg_poFileMustExist + dlg_poExplorer
  181.    Const dlg_MaxSize = 255
  182.    Const dlg_MaxFileSize = 1024
  183.    Dim tOpenFile As OPENFILENAME
  184.    Dim sFileNameBuff As String, sFileTitleBuff As String
  185.    Dim sValue As String, lApiReturn As Long
  186.    Dim lExtendedError As Long, n As Long
  187.    Dim buf As String, dirBuf As String, sSep As String
  188.    
  189.    On Error GoTo ShowFileDialogError
  190.    
  191.    'init property buffers
  192.    lApiReturn = 0  'APIReturn property
  193.    lExtendedError = 0  'ExtendedError property
  194.        
  195.    'prepare tOpenFile data
  196.    tOpenFile.lStructSize = Len(tOpenFile)
  197.    tOpenFile.hwndOwner = 0
  198.    tOpenFile.lpstrFilter = APIFilter(sFilter)
  199.    tOpenFile.iFilterIndex = 0
  200.    
  201.    'tOpenFile.lpstrFile As Long - init from FileName property
  202.    'prepare sFileNameBuff
  203.    sFileNameBuff = ""
  204.    sFileNameBuff = Space(dlg_MaxSize - 1)
  205.    'trim to length of dlg_MaxFileSize - 1
  206.    sFileNameBuff = Mid$(sFileNameBuff, 1, dlg_MaxFileSize - 1)
  207.    'null terminate
  208.    sFileNameBuff = sFileNameBuff & Chr$(0)
  209.    tOpenFile.lpstrFile = sFileNameBuff
  210.     
  211.    'nMaxFile As Long - init from MaxFileSize property
  212.    tOpenFile.nMaxFile = dlg_MaxFileSize
  213.             
  214.    'lpstrFileTitle As String - init from FileTitle property
  215.    'prepare sFileTitleBuff
  216.    sFileTitleBuff = sTitle
  217.    'pad with spaces
  218.    sFileTitleBuff = Space(dlg_MaxSize - 1)
  219.    'trim to length of dlg_MaxFileSize - 1
  220.    sFileTitleBuff = Mid$(sFileTitleBuff, 1, dlg_MaxFileSize - 1)
  221.    'null terminate
  222.    sFileTitleBuff = sFileTitleBuff & Chr$(0)
  223.    tOpenFile.lpstrFileTitle = sFileTitleBuff
  224.         
  225.    'tOpenFile.lpstrInitialDir As String - init from InitDir property
  226.    tOpenFile.lpstrInitialDir = App.Path
  227.    
  228.    'tOpenFile.lpstrTitle As String - init from DialogTitle property
  229.    tOpenFile.lpstrTitle = "Open File"
  230.    
  231.    tOpenFile.Flags = dlg_Flags
  232.       
  233.    'tOpenFile.lpstrDefExt As String - init from DefaultExt property
  234.    tOpenFile.lpstrDefExt = ""
  235.    'call the GetOpenFileName API function
  236.    lApiReturn = GetOpenFileName(tOpenFile)
  237.     
  238.     'handle return from GetOpenFileName API function
  239.     Select Case lApiReturn
  240.         Case 0  'user canceled
  241.             'generate an error
  242.             Err.Raise (2001)
  243.             Exit Function
  244.         Case 1  'user selected or entered a file
  245.             'sFileName gets part of tOpenFile.lpstrFile to the left of first Chr$(0)
  246.             ShowOpen = Left$(tOpenFile.lpstrFile, InStr(tOpenFile.lpstrFile, Chr$(0)) - 1)
  247.         Case Else   'an error occured
  248.             lExtendedError = CommDlgExtendedError
  249.     End Select
  250.  
  251. Exit Function
  252.  
  253. ShowFileDialogError:
  254.     Exit Function
  255. End Function
  256.  
  257.  
  258.  
  259. Public Sub Main()
  260.    Dim sFileName As String
  261.    Dim sMsg As String, i%
  262.    Dim bSilent As Boolean
  263.  
  264.    sFileName = Replace(Command, Chr(34), "")
  265.    bSilent = True
  266.    
  267.    If Not FileDoesExist(sFileName) Then
  268.       sFileName = ""
  269.       bSilent = False
  270.       sFileName = ShowOpen _
  271.          ("VB Project Files *.vbp|*.vbp|All Files *.*|*.*")
  272.    End If
  273.    
  274.    If Len(sFileName) = 0 Then End
  275.    
  276.    If CreateFileList(sFileName) Then
  277.       InitKeywords
  278.       For i = 1 To UBound(marFiles)
  279.          ConvertFile marFiles(i)
  280.          sMsg = sMsg & "*" & marFiles(i) & vbCrLf
  281.       Next
  282.       
  283.       ConvertFile sFileName
  284.       sMsg = sMsg & "*" & sFileName & vbCrLf
  285.       If Not bSilent Then
  286.          sMsg = "The following files were converted: " & vbCrLf & sMsg
  287.          MsgBox sMsg, vbInformation
  288.       End If
  289.    End If
  290. End Sub
  291.  
  292. Public Function ReadFile(sFileName As String) As String
  293.    Dim iFreeNum%
  294.    
  295.    On Error Resume Next
  296.    
  297.    iFreeNum = FreeFile
  298.      
  299.    Open sFileName For Input As iFreeNum
  300.    
  301.    ReadFile = StrConv(InputB(LOF(1), iFreeNum), vbUnicode)
  302.    Close #iFreeNum
  303.    
  304. End Function
  305.  
  306. Private Sub WriteFile _
  307.    (sFileName As String, sFileData As String)
  308.    Dim iFileNr As Integer
  309.    
  310.    iFileNr = FreeFile
  311.    
  312.    Open sFileName For Output As iFileNr
  313.    
  314.    Print #iFileNr, sFileData
  315.    
  316.    Close #iFileNr
  317. End Sub
  318.  
  319. Private Function FileDoesExist(sFileName As String) As Boolean
  320.    
  321.    On Error GoTo FileExistError
  322.  
  323.    If (Len(VBA.Trim(sFileName)) = 0) Or (Len(Dir(sFileName)) = 0) Then
  324.       FileDoesExist = False
  325.    Else
  326.       FileDoesExist = True
  327.    End If
  328.    
  329.    Exit Function
  330. FileExistError:
  331.    FileDoesExist = False
  332.    Exit Function
  333. End Function
  334.  
  335.  
  336.