home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1995 October / SUPERCD.BIN / PCPLUS / VBWK / VBSPY / VBSPY.ZIP / FILEIO.BAS < prev    next >
Encoding:
BASIC Source File  |  1994-12-04  |  5.7 KB  |  149 lines

  1. Option Explicit
  2. DefInt A-Z
  3. '---This is a general module for file IO and INI functions
  4. Declare Function writeprivateprofilestring% Lib "kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpString$, ByVal lpFileName$)
  5. Declare Function GetPrivateProfileString Lib "Kernel" (ByVal AppName As String, ByVal KeyName As String, ByVal Default As String, ByVal ReturnedString As String, ByVal MaxSize, ByVal FileName As String)
  6. Declare Function GetPrivateProfileInt Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer
  7.  
  8. Function FileErrors (errVal As Integer) As Integer
  9. '---This covers the most common file IO errors.
  10. '---It also has a option for sweadish messages
  11. ' Return Value  Meaning             Return Value    Meaning
  12. ' 0             Resume              2               Unrecoverable error
  13. ' 1             Resume Next         3               Unrecognized error
  14. Dim MsgType As Integer
  15. Dim Response As Integer
  16. Dim Action As Integer
  17. Dim msg As String
  18. Dim smsg As String
  19.  
  20. Const Err_DeviceUnavailable = 68
  21. Const Err_DiskNotReady = 71, Err_FileAlreadyExists = 58
  22. Const Err_TooManyFiles = 67, Err_RenameAcrossDisks = 74
  23. Const Err_Path_FileAccessError = 75, Err_DeviceIO = 57
  24. Const Err_DiskFull = 61, Err_BadFileName = 64
  25. Const Err_BadFileNameOrNumber = 52, Err_FileNotFound = 53
  26. Const Err_PathDoesNotExist = 76, Err_BadFileMode = 54
  27. Const Err_FileAlreadyOpen = 55, Err_InputPastEndOfFile = 62
  28. Const MB_EXCLAIM = 48, MB_STOP = 16
  29.  
  30. MsgType = MB_EXCLAIM
  31. Select Case errVal
  32.     Case Err_DeviceUnavailable  ' Error #68
  33.         msg = "That device appears to be unavailable."
  34.         smsg = "Kan inte lΣsa frσn denna enhet"
  35.         MsgType = MB_EXCLAIM + 5
  36.     Case Err_DiskNotReady       ' Error #71
  37.         msg = "The disk is not ready."
  38.         smsg = "Disken Σr inte fΣrdig att anvΣnda."
  39.     Case Err_DeviceIO
  40.         msg = "The disk is full."
  41.         smsg = "Disken Σr full."
  42.     Case Err_BadFileName, Err_BadFileNameOrNumber   ' Errors #64 & 52
  43.         msg = "That file name is illegal."
  44.         smsg = "Ogiltigt filnamn"
  45.     Case Err_PathDoesNotExist                        ' Error #76
  46.         msg = "That path doesn't exist."
  47.         smsg = "S÷kvΣgen finns inte."
  48.     Case Err_BadFileMode                            ' Error #54
  49.         msg = "Can't open your file for that type of access."
  50.         smsg = "Kan inte ÷ppna filen f÷r denna typ av access"
  51.     Case Err_FileAlreadyOpen                        ' Error #55
  52.         msg = "That file is already open."
  53.         smsg = "Denna fil Σr redan ÷ppnen."
  54.     Case Err_InputPastEndOfFile                     ' Error #62
  55.           msg = "This file has a nonstandard end-of-file marker,"
  56.           msg = msg + "or an attempt was made to read beyond "
  57.           msg = msg + "the end-of-file marker. This is NOT a text file."
  58.           smsg = "Denna fil har en icke standard end-of-file tecken,"
  59.           smsg = smsg + "eller ett f÷rs÷k gjordes att lΣsa f÷rbi"
  60.           smsg = smsg + "end-off-file tecknet"
  61.     '---Common dialog canecelbutton
  62.     Case CDERR_CANCEL
  63.           FileErrors = 3
  64.           Exit Function
  65.     Case Else
  66.            FileErrors = 3
  67.            msg = "Unknown file I/O error"
  68.            smsg = "OkΣnt fil I/O fel"
  69.            Exit Function
  70.     End Select
  71.     Response = MsgBox(msg, MsgType, "File error")
  72.     Select Case Response
  73.         Case 4          ' Retry button.
  74.             FileErrors = 0
  75.         Case 5          ' Ignore button.
  76.             FileErrors = 1
  77.         Case 1, 2, 3    ' Ok and Cancel buttons.
  78.             FileErrors = 2
  79.         Case Else
  80.             FileErrors = 3
  81.     End Select
  82. End Function
  83.  
  84. Sub ReadFromIni_Window_Pos (Frm As Form)
  85. '---Reading window positions
  86. '---Taking the argument Frm for general use
  87. Frm.WindowState = ReadIni_Int("WindowState", 0)
  88. If Frm.WindowState = NORMAL Then
  89.    Frm.Left = ReadIni_Int("left", 1)
  90.    Frm.Top = ReadIni_Int("top", 1)
  91.    Frm.Width = ReadIni_Int("width", screen.Width \ 2)
  92.    Frm.Height = ReadIni_Int("height", screen.Height \ 2)
  93. End If
  94. End Sub
  95.  
  96. Function ReadIni (KeyName$, Default$) As String
  97. '---Reading ini files (strings)
  98. '---Just to avoid the "mastodont" API call notation
  99. '---RETURN: String (Answer$) or Default$
  100. Dim AppName$, FileName$, length%, Answer$, Size%
  101.  
  102. '---Change this according to Your application----------------
  103. AppName$ = "VB SPY 1.0"
  104. FileName$ = app.Path & "\vbspy.ini"
  105. '------------------------------------------------------------
  106. Answer$ = Space$(250)
  107. Size% = Len(Answer$)
  108.  
  109. length% = GetPrivateProfileString(AppName$, KeyName$, Trim(Default$), Answer$, Size%, FileName$)
  110. ReadIni = Left$(Answer$, length%)
  111. End Function
  112.  
  113. Function ReadIni_Int (KeyName$, Default%) As Integer
  114. '---Reading INI file (Integers)
  115. Dim AppName$, FileName$
  116. '-----------------------------------
  117. AppName$ = "VB SPY 1.0"
  118. FileName$ = app.Path & "\vbspy.ini"
  119. '------------------------------------
  120. ReadIni_Int = GetPrivateProfileInt(AppName$, KeyName$, Default%, FileName$)
  121. End Function
  122.  
  123. Sub writeIni (KeyName$, Param$)
  124. '---Writing to the INI file
  125. Dim success, AppName$, FileName$
  126. '--------------------------------------
  127. AppName$ = "VB SPY 1.0"
  128. FileName$ = app.Path & "\vbspy.ini"
  129. '--------------------------------------
  130. success = writeprivateprofilestring(AppName$, KeyName$, Param$, FileName$)
  131. End Sub
  132.  
  133. Sub writeToIni_Window_pos (Frm As Form)
  134. '---29.10.94
  135. '
  136. '---Window positions & state
  137. If Frm.WindowState = MINIMIZED Then
  138.    Frm.WindowState = NORMAL
  139. End If
  140. writeIni "WindowState", Str$(Frm.WindowState)
  141. If Frm.WindowState = NORMAL Then
  142.    writeIni "left", Str$(Frm.Left)
  143.    writeIni "top", Str$(Frm.Top)
  144.    writeIni "width", Str$(Frm.Width)
  145.    writeIni "height", Str$(Frm.Height)
  146. End If
  147. End Sub
  148.  
  149.