January 19, 2021, 05:49:09 AM

Author Topic: [bb] INI File Functions v2.2 by Murilo [ 1+ years ago ]  (Read 526 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] INI File Functions v2.2 by Murilo [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : INI File Functions v2.2
Author : Murilo
Posted : 1+ years ago

Description : INI file reading/writing made easy ;)

All manipulation if now handled in memory - Far more efficient that the previous release (1.22). INI changes are not committed back to file until a call to INI_CloseFile is made.

Simply call INI_OpenFile([file]) to begin working with an INI file. Then perform all read from (INI_ReadValue) and write to (WriteValue) operations. Finally call INI_CloseFile to finish up (and commit any changes to file).


Code :
Code: BlitzBasic
  1. ; Title:        INI file manipulation functions
  2. ; Version:      2.2
  3. ; Revised:      June 16th 2004
  4. ; Author:       Leigh Bowers
  5.  
  6. ; Email:        leigh.bowers@curvesoftware.co.uk
  7. ; Web:          http://www.curvesoftware.co.uk/blitz
  8.  
  9. ; History:
  10.  
  11. ; 2.2           Fixed issue with locating keys.
  12. ; 2.01          Removed superfluous CR LF from file.
  13. ; 2.00          Redesigned and rewritten.
  14.  
  15. ; Example usage:
  16.  
  17. ;Include "Ini2.2.bb"
  18. ;
  19. ;INI_OpenFile("Archon.ini")
  20. ;strValue$ = INI_ReadValue("Config", "Mode", "C64")
  21. ;INI_WriteValue("Config", "Mode", "Atari")
  22. ;If Not(INI_CloseFile()) Then
  23. ;       Print "Unable to commit INI file changes to file (" + INI_CurrentFilestrFilename + ")."
  24. ;End If
  25.  
  26. Type INI_Type
  27.         Field strFilename$ = ""
  28.         Field blnModified% = False
  29.         Field strContents$ = ""
  30. End Type
  31. Global INI_CurrentFile.INI_Type = New INI_Type
  32.  
  33. ; Core functions
  34.  
  35. Function INI_OpenFile(INI_strFilename$)
  36.  
  37. ; Description:  Open the specified INI file for manipulation (if said file exists).
  38. ; Returns:              [] n/a
  39.  
  40.         INI_CurrentFilestrFilename = Trim(INI_strFilename) : If Right(Upper(INI_strFilename), 4) <> ".INI" Then INI_CurrentFilestrFilename = INI_CurrentFilestrFilename + ".ini"
  41.         INI_CurrentFilelnModified = False
  42.         INI_CurrentFilestrContents = ""
  43.  
  44.         INI_lngFileHandle% = ReadFile(CurrentDir$() + ""  + INI_CurrentFilestrFilename)
  45.         If INI_lngFileHandle <> 0 Then
  46.                 While Not(Eof(INI_lngFileHandle))
  47.                         INI_CurrentFilestrContents = INI_CurrentFilestrContents + ReadLine$(INI_lngFileHandle) + Chr$($0D) + Chr$($0A)
  48.                 Wend
  49.                 CloseFile INI_lngFileHandle
  50.         End If
  51.  
  52. End Function
  53.  
  54. Function INI_CloseFile%()
  55.  
  56. ; Description:  If INI modifications have been made (via INI_WriteValue), they are saved back to the original INI file.
  57. ; Returns:              [boolean] True (success) or False (unable to save modifications back to the INI file, where applicable)
  58.  
  59.         INI_blnResult% = True
  60.  
  61.         If (INI_CurrentFilelnModified = True) Then
  62.                 ; Commit internal changes back to the INI file
  63.                 INI_lngFileHandle = WriteFile(CurrentDir$() + "" + INI_CurrentFilestrFilename)
  64.                 If INI_lngFileHandle <> 0 Then
  65.                         WriteLine INI_lngFileHandle, Left$(INI_CurrentFilestrContents, Len(INI_CurrentFilestrContents) - 2)
  66.                         CloseFile INI_lngFileHandle
  67.                         INI_CurrentFilelnModified = False
  68.                 Else
  69.                         INI_blnResult = False ; Create file failed
  70.                 End If
  71.         End If
  72.        
  73.         Return INI_blnResult
  74.  
  75. End Function
  76.  
  77. Function INI_ReadValue$(INI_strSection$, INI_strKey$, INI_strDefault$ = "")
  78.  
  79. ; Description:  Retrieve the INI file value for the specified INI Section/Key combination.
  80. ; Returns:              [string] INI Value - The optional INI_strDefault value is returned if no SECTION/KEY combination is found
  81.  
  82.         INI_strSection = "[" + Upper$(Trim$(INI_strSection)) + "]"
  83.         INI_strKey = Chr$($0A) + Upper$(Trim$(INI_strKey))
  84.  
  85.         INI_strUpperContents$ = Upper$(INI_CurrentFilestrContents)
  86.  
  87. ; Locate the SECTION, KEY and VALUE
  88.  
  89.         INI_strValue$ = ""
  90.  
  91.         INI_lngSectionPos% = Instr(INI_strUpperContents, INI_strSection)
  92.         If (INI_lngSectionPos <> 0) Then
  93.                 INI_lngKeyPos% = Instr(INI_strUpperContents, INI_strKey, (INI_lngSectionPos + Len(INI_strSection) + 1))
  94.                 If (INI_lngKeyPos <> 0) Then
  95.                         INI_NextSection% = Instr(INI_strUpperContents, "[", (INI_lngSectionPos + Len(INI_strSection) + 1))
  96.                         If (INI_NextSection = 0) Or (INI_lngKeyPos < INI_NextSection) Then
  97.                                 INI_lngStartPos% = Instr(INI_strUpperContents, "=", (INI_lngKeyPos + 1))
  98.                                 If (INI_lngStartPos <> 0) Then
  99.                                         INI_lngEndPos% = Instr(INI_strUpperContents, Chr$($0D), (INI_lngStartPos + 1))
  100.                                         If (INI_lngEndPos <> 0) Then
  101.                                                 ; We have located the required INI key and it's corresponding value
  102.                                                 INI_strValue = Trim$(Mid$(INI_CurrentFilestrContents, INI_lngStartPos + 1, (INI_lngEndPos - INI_lngStartPos - 1)))
  103.                                         End If
  104.                                 End If
  105.                         End If
  106.                 End If
  107.         End If
  108.  
  109. ; Return the appropriate value
  110.  
  111.         If (INI_strValue <> "") Then Return INI_strValue Else Return INI_strDefault
  112.  
  113. End Function
  114.  
  115. Function INI_WriteValue(INI_strSection$, INI_strKey$, INI_strValue$)
  116.  
  117. ; Description:  Add/update key values within the current INI file (internally).
  118. ;                               Note: Changes are not committed back To file until you explicitly call INI_CloseFile.
  119. ; Returns:              [] n/a
  120.  
  121.         INI_strSection = "[" + Trim$(INI_strSection) + "]"
  122.         INI_strUpperSection$ = Upper$(INI_strSection)
  123.         INI_strKey = Trim$(INI_strKey)
  124.         INI_strValue = Trim$(INI_strValue)
  125.  
  126. ; (Re)Create the INI contents updating/adding the SECTION, KEY and VALUE
  127.  
  128.         INI_blnWrittenKey% = False
  129.         INI_blnSectionFound% = False
  130.         INI_strCurrentSection$ = ""
  131.  
  132.         INI_strTempContents$ = INI_CurrentFilestrContents
  133.         INI_CurrentFilestrContents = ""
  134.  
  135.         INI_lngOldPos% = 1
  136.         INI_lngPos% = Instr(INI_strTempContents, Chr$($0D))
  137.        
  138.         While (INI_lngPos <> 0)
  139.  
  140.                 INI_strTemp$ =Trim$(Mid$(INI_strTempContents, INI_lngOldPos, (INI_lngPos - INI_lngOldPos)))
  141.                
  142.                 If (INI_strTemp <> "") Then
  143.                         If Left$(INI_strTemp, 1) = "[" And Right$(INI_strTemp, 1) = "]" Then
  144.                                 ; Process SECTION
  145.                                 If (INI_strCurrentSection = INI_strUpperSection) And (INI_blnWrittenKey = False) Then
  146.                                         INI_blnWrittenKey = INI_CreateKey(INI_strKey, INI_strValue)
  147.                                 End If
  148.                                 INI_CreateSection INI_strTemp
  149.                                 INI_strCurrentSection = Upper$(INI_strTemp)
  150.                                 If (INI_strCurrentSection = INI_strUpperSection) Then INI_blnSectionFound = True
  151.                         Else
  152.                                 ; KEY=VALUE
  153.                                 INI_lngEqualsPos% = Instr(INI_strTemp, "=")
  154.                                 If (INI_lngEqualsPos <> 0) Then
  155.                                         If (INI_strCurrentSection = INI_strUpperSection) And (Upper$(Trim$(Left$(INI_strTemp, (INI_lngEqualsPos - 1)))) = Upper$(INI_strKey)) Then
  156.                                                 INI_blnWrittenKey = INI_CreateKey(INI_strKey, INI_strValue)
  157.                                         Else
  158.                                                 INI_CurrentFilestrContents = INI_CurrentFilestrContents + INI_strTemp + Chr$($0D) + Chr$($0A)
  159.                                         End If
  160.                                 End If
  161.                         End If
  162.                 End If
  163.  
  164.                 ; Move through the INI contents...
  165.  
  166.                 INI_lngOldPos = INI_lngPos + 1
  167.                 INI_lngPos% = Instr(INI_strTempContents, Chr$($0D), INI_lngOldPos)
  168.  
  169.         Wend
  170.  
  171.         ; KEY wasn't found in the INI contents - Append a new SECTION if required and create our KEY=VALUE line
  172.  
  173.         If (INI_strValue <> "") And (INI_blnWrittenKey = False) Then
  174.                 If (INI_blnSectionFound = False) Then
  175.                         INI_CreateSection INI_strSection
  176.                 End If
  177.                 INI_CreateKey INI_strKey, INI_strValue
  178.         End If
  179.        
  180.         INI_CurrentFilelnModified = True
  181.  
  182. End Function
  183.  
  184. Function INI_CreateSection(INI_strNewSection$)
  185.  
  186.         If INI_CurrentFilestrContents <> "" Then
  187.                 ; Blank line between sections
  188.                 INI_CurrentFilestrContents = INI_CurrentFilestrContents + Chr$($0D) + Chr$($0A)
  189.         End If
  190.         INI_CurrentFilestrContents = INI_CurrentFilestrContents + INI_strNewSection + Chr$($0D) + Chr$($0A)
  191.  
  192. End Function
  193.  
  194. Function INI_CreateKey%(INI_strKey$, INI_strValue$)
  195.  
  196.         If (INI_strValue <> "") Then
  197.                 INI_CurrentFilestrContents = INI_CurrentFilestrContents + INI_strKey + "=" + INI_strValue + Chr$($0D) + Chr$($0A)
  198.         End If
  199.        
  200.         Return True
  201.  
  202. End Function


Comments :


boomboom(Posted 1+ years ago)

 Brilliant. Thanks :)


Beaker(Posted 1+ years ago)

 I've never really understood why you don't use the INI system calls like this one:<a href="codearcs4878.html?code=1139" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=1139[/url]


grable(Posted 1+ years ago)

 <div class="quote"> I've never really understood why you don't use the INI system calls </div>Because they suck? The problem with them is that they have to open the file for reading/writing on every call. Not very efficient for big ini files.


N(Posted 1+ years ago)

 <div class="quote"> Because they suck? The problem with them is that they have to open the file for reading/writing on every call. Not very efficient for big ini files. </div>This is to ensure that a) the file is not locked in case other code needs to access it and b) changes made by any process will be immediately available upon next reading.  Might not be necessary for you or me, but it's a better-safe-than-sorry approach that works.Also, if you have a gigantic INI file, consider splitting it up into smaller component-specific INI files.  And for that matter, consider not using INI files.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal