Ooops
March 01, 2021, 10:35:03 PM

Author Topic: [bb] Random Access File Utilities by TygerWulf [ 1+ years ago ]  (Read 571 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Random Access File Utilities
Author : TygerWulf
Posted : 1+ years ago

Description : Allows you to randomly access a file by using identifiers to locate data in a file.  It needs an encryption code or some other such to be truly effective, though, but it operates well the way it is.  If you don't mind people being able to read the files, then go ahead and use it.

Code :
Code: BlitzBasic
  1. Type Datafile
  2.         Field FName$
  3.         Field Size
  4.         Field ID
  5. End Type
  6.  
  7. ;FWriteS(File.DataFile, Key$, KeyVal$)
  8. ;File.DataFile--A file opened with FOpen
  9. ;Key$--The identifier that will be used to retrieve the data later
  10. ;KeyVal$--The Value to be stored for retrieval later
  11. ;Writes a string to a file or updates a string value in the file.
  12.  
  13. Function FWriteS(File.DataFile, Key$,KeyVal$)
  14.         Key$ = "$" +Key$
  15.         If FileSize = 0 Then
  16.                 X$ = Key$+"="+KeyVal
  17.                 WriteLine(FileID,X$)
  18.                 QSFile(File)
  19.                 Return 2
  20.         End If
  21.         P = KeyExists(File, Key$)
  22.         If P <> -1 Then
  23.  
  24.                 SeekFile(FileID,P)
  25.                 X$ = ReadLine(FileID)
  26.                 SeekFile(FileID,P)
  27.                 L = Len(X$)
  28.                 X$ = Key$+"="+KeyVal
  29.                 If Len(X$) < L Then X$ = LSet$(X$,L)
  30.                 WriteLine(FileID, X$)
  31.                 QSFile(File)
  32.                 Return 1 ;Updated an existing value
  33.         Else
  34.                 SeekFile(FileID,FileSize)
  35.                 X$ = Key$+"="+KeyVal
  36.                 WriteLine(FileID,X$)
  37.                 QSFile(File)
  38.                 Return 2 ;Created a new value
  39.         End If
  40.        
  41.         Return 0
  42.  
  43. End Function
  44.  
  45.  
  46. ;FReadS$(File.DataFile, Key$)
  47. ;File.DataFile--A File Opened with FOpen
  48. ;Key$--The identifier to look for in the file, where the information is stored
  49. ;Returns the string value found at identifier Key$ or "" if Key$ was not found
  50.  
  51. Function FReadS$(File.DataFile, Key$)
  52.         Key$ = "$" +Key$
  53.         P = KeyExists(File, Key$)
  54.         If P <> -1 Then
  55.                 SeekFile(FileID,P)
  56.                 X$ = ReadLine(FileID)
  57.                 L = Instr(X$, "=")
  58.                 If L = 0 Then Return 0
  59.                 L = Len(X$) - (L)
  60.                 Return Right$(X$, L)
  61.  
  62.         End If
  63.         Return ""
  64.        
  65. End Function
  66.  
  67.  
  68. ;FWriteI(File.DataFile, Key$, KeyVal%)
  69. ;File.DataFile--A file opened with FOpen
  70. ;Key$--The identifier that will be used to retrieve the data later
  71. ;KeyVal%--The Value to be stored for retrieval later
  72. ;Writes an integer to a file or updates an integer value in the file.
  73.  
  74. Function FWriteI(File.DataFile, Key$,KeyVal%)
  75.         Key$ = "%" +Key$
  76.         If FileSize = 0 Then
  77.                 X$ = Key$+"="+Str(KeyVal)
  78.                 WriteLine(FileID,X$)
  79.                 QSFile(File)
  80.                 Return 2
  81.         End If
  82.         P = KeyExists(File, Key$)
  83.         If P <> -1 Then
  84.                 SeekFile(FileID,P)
  85.                 X$ = ReadLine(FileID)
  86.                 SeekFile(FileID,P)
  87.                 L = Len(X$)
  88.                 X$ = Key$+"="+Str(KeyVal)
  89.                 If Len(X$) < L Then X$ = LSet$(X$,L)
  90.                 WriteLine(FileID, X$)
  91.                 QSFile(File)
  92.                 Return 1 ;Updated an existing value
  93.         Else
  94.                 SeekFile(FileID,FileSize)
  95.                 X$ = Key$+"="+Str(KeyVal)
  96.                 WriteLine(FileID,X$)
  97.                 QSFile(File)
  98.                 Return 2 ;Created a new value
  99.         End If
  100.        
  101.         Return 0
  102.  
  103. End Function
  104.  
  105.  
  106. ;FReadI%(File.DataFile, Key$)
  107. ;File.DataFile--A File Opened with FOpen
  108. ;Key$--The identifier to look for in the file, where the information is stored
  109. ;Returns the intiger value found at identifier Key$ or 0 if Key$ was not found
  110.  
  111. Function FReadI%(File.DataFile, Key$)
  112.  
  113.         Key$ = "%" +Key$
  114.         P = KeyExists(File, Key$)
  115.         If P <> -1 Then
  116.                 SeekFile(FileID,P)
  117.                 X$ = ReadLine(FileID)
  118.                 L = Instr(X$, "=")
  119.                 If L = 0 Then Return 0
  120.                 L = Len(X$) - (L)
  121.                 X$ = Right$(X$, L)
  122.                 L = X$
  123.                 Return L
  124.  
  125.         End If
  126.         Return 0
  127.        
  128. End Function
  129.  
  130.  
  131. ;FWriteF(File.DataFile, Key$, KeyVal#)
  132. ;File.DataFile--A file opened with FOpen
  133. ;Key$--The identifier that will be used to retrieve the data later
  134. ;KeyVal#--The Value to be stored for retrieval later
  135. ;Writes a float to a file or updates a float value in the file.
  136.  
  137. Function FWriteF(File.DataFile, Key$,KeyVal#)
  138.         Key$ = "#" +Key$
  139.         If FileSize = 0 Then
  140.                 X$ = Key$+"="+Str(KeyVal)
  141.                 WriteLine(FileID,X$)
  142.                 QSFile(File)
  143.                 Return 2
  144.         End If
  145.         P = KeyExists(File, Key$)
  146.         If P <> -1 Then
  147.                 SeekFile(FileID,P)
  148.                 X$ = ReadLine(FileID)
  149.                 SeekFile(FileID,P)
  150.                 L = Len(X$)
  151.                 X$ = Key$+"="+Str(KeyVal)
  152.                 If Len(X$) < L Then X$ = LSet$(X$,L)
  153.                 WriteLine(FileID, X$)
  154.                 QSFile(File)
  155.                 Return 1 ;Updated an existing value
  156.         Else
  157.                 SeekFile(FileID,FileSize)
  158.                 X$ = Key$+"="+Str(KeyVal)
  159.                 WriteLine(FileID,X$)
  160.                 QSFile(File)
  161.                 Return 2 ;Created a new value
  162.         End If
  163.        
  164.         Return 0
  165.  
  166. End Function
  167.  
  168.  
  169. ;FReadF#(File.DataFile, Key#)
  170. ;File.DataFile--A File Opened with FOpen
  171. ;Key$--The identifier to look for in the file, where the information is stored
  172. ;Returns the float value found at identifier Key$ or 0.0 if Key$ was not found
  173.  
  174. Function FReadF#(File.DataFile, Key$)
  175.  
  176.         Key$ = "#" +Key$
  177.         P = KeyExists(File, Key$)
  178.         If P <> -1 Then
  179.                 SeekFile(FileID,P)
  180.                 X$ = ReadLine(FileID)
  181.                 L = Instr(X$, "=")
  182.                 If L = 0 Then Return 0
  183.                 L = Len(X$) - (L)
  184.                 X$ = Right$(X$, L)
  185.                 L = X$
  186.                 Return L
  187.  
  188.         End If
  189.         Return 0
  190.        
  191. End Function
  192.  
  193.  
  194. ;KeyExists(File.DataFile, Key$)
  195. ;File.DataFile--A File opened with FOpen
  196. ;Key$--The identifier to search for.
  197. ;Returns the location in the file where Key$ is found, or -1 if Key$ was not found
  198.  
  199. Function KeyExists(File.DataFile, Key$)
  200.  
  201.         L = Len(Key$)
  202.         SeekFile(FileID,0)
  203.         While Not Eof(FileID)
  204.                 N = FilePos(FileID)
  205.                 X$ = ReadLine(FileID)
  206.                 If Lower$(Key$) = Lower$(Left$(X$,L)) Then
  207.                         Return N
  208.                 End If
  209.         Wend
  210.         Return -1
  211.  
  212. End Function
  213.  
  214.  
  215. ;QSFile(File.DataFile)
  216. ;File.Datafile--A file opened with FOpen
  217. ;Closes and reopens a file to save data changes to disk.
  218.  
  219. Function QSFile(File.DataFile)
  220.  
  221.         CloseFile(FileID)
  222.         FileID = OpenFile(FileFName$)
  223.         UpdateFile(File)
  224.         Return
  225.  
  226. End Function
  227.  
  228.  
  229. ;UpdateFile(File.DataFile)
  230. ;File.Datafile--A file opened with FOpen
  231. ;Updates the information of a Datafile type.  Currently only updates size.
  232.  
  233. Function UpdateFile(File.DataFile)
  234.  
  235.         FileSize = FileSize(FileFName$)
  236.         Return
  237.  
  238. End Function
  239.  
  240. ;FOpen.DataFile(Filename$,RC)
  241. ;Filename$--a string containing the file you want to open
  242. ;RC--a flag indicating if the file should be created when not found
  243. ;Opens a file and returns a Datafile custom type with information and a
  244. ;pointer to the file in its fields.
  245.  
  246. Function FOpen.DataFile(Filename$,RC)
  247.  
  248. File.DataFile = New DataFile
  249. Select RC
  250.         Case 0 ;Read/Write, create if not found
  251.                 If ff_is_file(Filename$) <> True Then
  252.                         CurFile = WriteFile(FileName$)  ;Creating Unfound File
  253.                         CloseFile(CurFile)
  254.                 End If
  255.                 FileID = OpenFile(Filename$)
  256.                 FileFName$ = Filename$
  257.                 FileSize = FileSize(Filename$)
  258.                 Return File
  259.                
  260.                
  261.         Case 1 ;Read/Write, Return Error on no file found
  262.                 If ff_is_file(Filename$) <> True Then
  263.                         Return Null ;File not found
  264.                 End If
  265.                 FileID = OpenFile(Filename$)
  266.                 FileFName$ = Filename$
  267.                 FileSize = FileSize(Filename$)
  268.                 Return File
  269.  
  270.         Default
  271.                 Delete File
  272.                 Return Null ;Unexpected error
  273.        
  274. End Select
  275.  
  276. End Function
  277.  
  278.  
  279. ;FClose(File.DataFile)
  280. ;File.DataFile--A File opened with FOpen
  281. ;Closes a file previously opened with FOpen
  282.  
  283. Function FClose(File.DataFile)
  284.  
  285.         CloseFile(FileID)
  286.         Delete File
  287.  
  288. End Function
  289.  
  290. ;A function to check if a file exists
  291. ;Not mine.
  292.  
  293. Function ff_is_file(file$)
  294.         If FileType(file$) = 1 Then Return True
  295. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal