home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD14417222001.psc / modFileIO.bas < prev    next >
Encoding:
BASIC Source File  |  2001-01-19  |  11.4 KB  |  304 lines

  1. Attribute VB_Name = "modFileIO"
  2. Option Explicit
  3.  
  4. '
  5. ' File I\O Module (modFileIO.bas)
  6. '
  7. ' Part of the Amazon Book Searcher Project.
  8. ' --------------------------------------------
  9. '
  10. ' Purpose: Holds the Subs and Functions related to
  11. ' file I\O (Opening, Saving and Merging Book List Files).
  12. '
  13.  
  14. '
  15. ' Public Constants.
  16. '
  17.  
  18. ' The number of items to store in history (Meaning,
  19. ' the number of author, title and subject fields being
  20. ' stored in the history).
  21. Public Const HISTORY_COUNT As Integer = 10
  22. ' The Settings filename.
  23. Public Const SETTINGS_FILENAME As String = "Settings.dat"
  24.  
  25.  
  26. '
  27. ' API Declarations.
  28. '
  29.  
  30. ' INI manipulation API's.
  31. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
  32. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
  33.  
  34.  
  35. '
  36. ' Public Subs.
  37. '
  38.  
  39.  
  40. '
  41. ' Open the settings file.
  42. '
  43. Public Sub OpenSettings(Filename As String)
  44. ' A buffer variable used for opening the INI file.
  45. Dim strBuffer As String
  46. Dim s As String
  47. Dim i As Integer
  48.  
  49.     If (Dir(Filename) = "") Then Exit Sub
  50.  
  51.     ' Create the Buffer.
  52.     strBuffer = String(750, Chr(0))
  53.     
  54.     frmMain.Width = CInt(Left$(strBuffer, GetPrivateProfileString("WindowSettings", "Width", "7380", strBuffer, Len(strBuffer), Filename)))
  55.     frmMain.Height = CInt(Left$(strBuffer, GetPrivateProfileString("WindowSettings", "Height", "6870", strBuffer, Len(strBuffer), Filename)))
  56.     frmMain.Top = CInt(Left$(strBuffer, GetPrivateProfileString("WindowSettings", "Top", Str((Screen.Height - frmMain.Height) / 2), strBuffer, Len(strBuffer), Filename)))
  57.     frmMain.Left = CInt(Left$(strBuffer, GetPrivateProfileString("WindowSettings", "Left", Str((Screen.Width - frmMain.Width) / 2), strBuffer, Len(strBuffer), Filename)))
  58.     frmMain.WindowState = CInt(Left$(strBuffer, GetPrivateProfileString("WindowSettings", "WindowState", "0", strBuffer, Len(strBuffer), Filename)))
  59.     
  60.     For i = 1 To HISTORY_COUNT
  61.         frmMain.cmbAuthor.AddItem Left$(strBuffer, GetPrivateProfileString("History", "Author" & i, "", strBuffer, Len(strBuffer), Filename))
  62.     Next i
  63.  
  64.     For i = 1 To HISTORY_COUNT
  65.         frmMain.cmbTitle.AddItem Left$(strBuffer, GetPrivateProfileString("History", "Title" & i, "", strBuffer, Len(strBuffer), Filename))
  66.     Next i
  67.  
  68.     For i = 1 To HISTORY_COUNT
  69.         frmMain.cmbSubject.AddItem Left$(strBuffer, GetPrivateProfileString("History", "Subject" & i, "", strBuffer, Len(strBuffer), Filename))
  70.     Next i
  71. End Sub
  72.  
  73.  
  74. '
  75. ' Save the settings in a file.
  76. '
  77. Public Sub SaveSettings(Filename As String)
  78. Dim s As String
  79. Dim i As Integer
  80. Dim F As Integer
  81.  
  82.     F = FreeFile
  83.     Open Filename For Output Access Write As F
  84.         Print #F, "[WindowSettings]"
  85.         Print #F, "WindowState = " & frmMain.WindowState
  86.         
  87.         If (frmMain.WindowState = 0) Then
  88.             Print #F, "Width = " & frmMain.Width
  89.             Print #F, "Height = " & frmMain.Height
  90.             Print #F, "Top = " & frmMain.Top
  91.             Print #F, "Left = " & frmMain.Left
  92.         End If
  93.         
  94.         Print #F, "[History]"
  95.         For i = 0 To frmMain.cmbAuthor.ListCount - 1
  96.             Print #F, "Author" & (i + 1) & " = " & frmMain.cmbAuthor.List(i)
  97.         Next i
  98.         
  99.         For i = 0 To frmMain.cmbTitle.ListCount - 1
  100.             Print #F, "Title" & (i + 1) & " = " & frmMain.cmbTitle.List(i)
  101.         Next i
  102.         
  103.         For i = 0 To frmMain.cmbSubject.ListCount - 1
  104.             Print #F, "Subject" & (i + 1) & " = " & frmMain.cmbSubject.List(i)
  105.         Next i
  106.     Close F
  107. End Sub
  108.  
  109.  
  110. '
  111. ' Merges 2 booklist files.
  112. '
  113. Public Sub MergeBooklists(FileName1 As String, FileName2 As String)
  114. Dim i As Integer
  115. Dim clsBooks() As clsBook
  116. Dim clsOldBooks() As clsBook
  117.  
  118.     ' Exit the sub if one of the files doesn't exists.
  119.     If ((Dir(FileName1) = "") Or (Dir(FileName2) = "")) Then Exit Sub
  120.     
  121.     If (UBound(gclsBooks) > 0) Then
  122.         ' Save the old book list.
  123.         ReDim clsOldBooks(1 To UBound(gclsBooks))
  124.         
  125.         For i = 1 To UBound(gclsBooks)
  126.             Set clsOldBooks(i) = gclsBooks(i)
  127.         Next i
  128.     Else
  129.         ReDim clsOldBooks(0 To 0)
  130.     End If
  131.     
  132.     ' Open the first booklist file.
  133.     OpenBooklist (FileName1)
  134.     
  135.     If (UBound(gclsBooks) = 0) Then Exit Sub
  136.     
  137.     ' Copy gclsBooks array into clsBooks array.
  138.     ReDim clsBooks(1 To UBound(gclsBooks))
  139.     
  140.     For i = 1 To UBound(gclsBooks)
  141.         Set clsBooks(i) = gclsBooks(i)
  142.     Next i
  143.     
  144.     ' Open the second booklist file.
  145.     OpenBooklist (FileName2)
  146.     
  147.     ' Append the first book list to the second booklist.
  148.     For i = 1 To UBound(clsBooks)
  149.         If (BookIndex(clsBooks(i).Title) = 0) Then
  150.             ReDim Preserve gclsBooks(1 To UBound(gclsBooks) + 1)
  151.             Set gclsBooks(UBound(gclsBooks)) = clsBooks(i)
  152.         End If
  153.     Next i
  154.     
  155.     ' Save the merged booklist into the second file.
  156.     SaveBooklist (FileName2)
  157.     
  158.     ' Return to the original book list.
  159.     If (UBound(clsOldBooks) > 0) Then
  160.         ReDim gclsBooks(1 To UBound(clsOldBooks))
  161.         For i = 1 To UBound(clsOldBooks)
  162.             Set gclsBooks(i) = clsOldBooks(i)
  163.         Next i
  164.     End If
  165. End Sub
  166.  
  167.  
  168. '
  169. ' Saves the global variable array gclsBooks into
  170. ' a specific file.
  171. '
  172. Public Sub SaveBooklist(Filename As String)
  173. Dim F As Integer
  174. Dim i As Integer, c As Integer
  175.  
  176.     If (UBound(gclsBooks) = 0) Then Exit Sub
  177.     
  178.     F = FreeFile
  179.     Open Filename For Output Access Write As F
  180.         For i = 1 To UBound(gclsBooks)
  181.             Print #F, "[Book" & i & "]"
  182.             For c = 1 To gclsBooks(i).AuthorCount
  183.                 Print #F, "Author" & c & " = " & gclsBooks(i).Authors(c)
  184.             Next c
  185.             If (gclsBooks(i).AverageRating <> 0) Then Print #F, "AverageRating = " & gclsBooks(i).AverageRating
  186.             If (gclsBooks(i).Cover <> "") Then Print #F, "Cover = " & gclsBooks(i).Cover
  187.             If (gclsBooks(i).Dimensions(1) <> 0) Then
  188.                 Print #F, "Dimensions1 = " & gclsBooks(i).Dimensions(1)
  189.                 Print #F, "Dimensions2 = " & gclsBooks(i).Dimensions(2)
  190.                 Print #F, "Dimensions3 = " & gclsBooks(i).Dimensions(3)
  191.             End If
  192.             If (gclsBooks(i).ISBN <> "") Then Print #F, "ISBN = " & gclsBooks(i).ISBN
  193.             If (gclsBooks(i).Pages <> 0) Then Print #F, "Pages = " & gclsBooks(i).Pages
  194.             If (gclsBooks(i).Price <> 0) Then Print #F, "Price = " & gclsBooks(i).Price
  195.             If (gclsBooks(i).Publishers <> "") Then Print #F, "Publishers = " & gclsBooks(i).Publishers
  196.             If (gclsBooks(i).PublishingDate <> "") Then Print #F, "PublishingDate = " & gclsBooks(i).PublishingDate
  197.             If (gclsBooks(i).SalesRank <> 0) Then Print #F, "SalesRank = " & gclsBooks(i).SalesRank
  198.             If (gclsBooks(i).Title <> "") Then Print #F, "Title = " & gclsBooks(i).Title
  199.             If (gclsBooks(i).URL <> "") Then Print #F, "URL = " & gclsBooks(i).URL
  200.         Next i
  201.     Close F
  202. End Sub
  203.  
  204.  
  205. '
  206. ' Opens a booklist file into the global variable
  207. ' array gclsBooks.
  208. '
  209. Public Sub OpenBooklist(Filename As String)
  210. Dim F As Integer
  211. Dim i As Integer, c As Integer
  212. ' A buffer variable used for opening the INI file.
  213. Dim strBuffer As String
  214. Dim s As String
  215. Dim clsB As clsBook
  216.  
  217.     ' Exit the sub if the file doesn't exists.
  218.     If (Dir$(Filename) = "") Then Exit Sub
  219.     
  220.     ReDim gclsBooks(0 To 0)
  221.         
  222.     i = 1
  223.     
  224.     Do
  225.         ' Create a new instance of a Book class.
  226.         Set clsB = New clsBook
  227.         
  228.         ' Create the Buffer.
  229.         strBuffer = String(750, Chr(0))
  230.         
  231.         ' Retrieve the Authors from the INI file.
  232.         c = 1
  233.         Do
  234.             s = Left$(strBuffer, GetPrivateProfileString("Book" & i, "Author" & c, "", strBuffer, Len(strBuffer), Filename))
  235.             
  236.             If (s <> "") Then
  237.                 ' Add the Author.
  238.                 clsB.AddAuthor s
  239.             End If
  240.             
  241.             c = c + 1
  242.         Loop Until (s = "")
  243.         
  244.         ' Retrieve the Average Rating from the INI file.
  245.         clsB.AverageRating = CInt(Left$(strBuffer, GetPrivateProfileString("Book" & i, "AverageRating", "0", strBuffer, Len(strBuffer), Filename)))
  246.         ' Retrieve the Cover from the INI file.
  247.         clsB.Cover = Left$(strBuffer, GetPrivateProfileString("Book" & i, "Cover", "", strBuffer, Len(strBuffer), Filename))
  248.         ' Retrieve the Dimensions from the INI file.
  249.         clsB.Dimensions(1) = CInt(Left$(strBuffer, GetPrivateProfileString("Book" & i, "Dimensions1", "0", strBuffer, Len(strBuffer), Filename)))
  250.         clsB.Dimensions(2) = CInt(Left$(strBuffer, GetPrivateProfileString("Book" & i, "Dimensions2", "0", strBuffer, Len(strBuffer), Filename)))
  251.         clsB.Dimensions(3) = CInt(Left$(strBuffer, GetPrivateProfileString("Book" & i, "Dimensions3", "0", strBuffer, Len(strBuffer), Filename)))
  252.         ' Retrieve the ISBN from the INI file.
  253.         clsB.ISBN = Left$(strBuffer, GetPrivateProfileString("Book" & i, "ISBN", "", strBuffer, Len(strBuffer), Filename))
  254.         ' Retrieve the Pages from the INI file.
  255.         clsB.Pages = CInt(Left$(strBuffer, GetPrivateProfileString("Book" & i, "Pages", "0", strBuffer, Len(strBuffer), Filename)))
  256.         ' Retrieve the Price from the INI file.
  257.         clsB.Price = CInt(Left$(strBuffer, GetPrivateProfileString("Book" & i, "Price", "0", strBuffer, Len(strBuffer), Filename)))
  258.         ' Retrieve the Publishers from the INI file.
  259.         clsB.Publishers = Left$(strBuffer, GetPrivateProfileString("Book" & i, "Publishers", "", strBuffer, Len(strBuffer), Filename))
  260.         ' Retrieve the Pusblishing Date from the INI file.
  261.         clsB.PublishingDate = Left$(strBuffer, GetPrivateProfileString("Book" & i, "PublishingDate", "", strBuffer, Len(strBuffer), Filename))
  262.         ' Retrieve the Sales Rank from the INI file.
  263.         clsB.SalesRank = CInt(Left$(strBuffer, GetPrivateProfileString("Book" & i, "SalesRank", "0", strBuffer, Len(strBuffer), Filename)))
  264.         ' Retrieve the Title from the INI file.
  265.         clsB.Title = Left$(strBuffer, GetPrivateProfileString("Book" & i, "Title", "", strBuffer, Len(strBuffer), Filename))
  266.         ' Retrieve the URL from the INI file.
  267.         clsB.URL = Left$(strBuffer, GetPrivateProfileString("Book" & i, "URL", "", strBuffer, Len(strBuffer), Filename))
  268.         
  269.         ' Add the book to the book array.
  270.         If (UBound(gclsBooks) = 0) Then
  271.             ReDim gclsBooks(1 To 1)
  272.         Else
  273.             ReDim Preserve gclsBooks(1 To UBound(gclsBooks) + 1)
  274.         End If
  275.         
  276.         Set gclsBooks(UBound(gclsBooks)) = clsB
  277.         
  278.         i = i + 1
  279.     Loop Until (GetPrivateProfileString("Book" & i, "Title", "", strBuffer, Len(strBuffer), Filename) = 0)
  280. End Sub
  281.  
  282.  
  283. '
  284. ' Public functions.
  285. '
  286.  
  287.  
  288. '
  289. ' Returns the Index of a book in the gclsBooks
  290. ' array by its title. Returns zero if book wasn't found.
  291. '
  292. Public Function BookIndex(Title As String) As Integer
  293. Dim i As Integer
  294.     For i = 1 To UBound(gclsBooks)
  295.         If (gclsBooks(i).Title = Title) Then
  296.             ' Found the book.
  297.             BookIndex = i
  298.             Exit Function
  299.         End If
  300.     Next i
  301.     
  302.     BookIndex = 0
  303. End Function
  304.