myIni v1.0b for B3D\Basic
Community Forums/Showcase/myIni v1.0b for B3D\Basic
| ||
Hi! This is a beta of my (kinda) Ini system. Find the link on the top news of my site: http://www.kochonet.com/thok This is an early beta but still functional, with only one example of myIni Creation(others will come later). The functions are described in the source and all are listed in the decls file. Feel free to try, give comments and ask questions... köcHon |
| ||
This is a good one too, you might be able to use some of the features. In fact this is all I need for a INI library...Type INI_Type Field strFilename$ = "" Field blnModified% = False Field strContents$ = "" End Type Global INI_CurrentFile.INI_Type = New INI_Type ; Core functions Function INI_OpenFile(INI_strFilename$) ; Description: Open the specified INI file for manipulation (if said file exists). ; Returns: [] n/a INI_CurrentFile\strFilename = Trim(INI_strFilename) : If Right(Upper(INI_strFilename), 4) <> ".INI" Then INI_CurrentFile\strFilename = INI_CurrentFile\strFilename + ".ini" INI_CurrentFile\blnModified = False INI_CurrentFile\strContents = "" INI_lngFileHandle% = ReadFile(INI_CurrentFile\strFilename) If INI_lngFileHandle <> 0 Then While Not(Eof(INI_lngFileHandle)) INI_CurrentFile\strContents = INI_CurrentFile\strContents + ReadLine$(INI_lngFileHandle) + Chr$($0D) + Chr$($0A) Wend CloseFile INI_lngFileHandle Else Return 1 End If Return 0 End Function Function INI_CloseFile%() ; Description: If INI modifications have been made (via INI_WriteValue), they are saved back to the original INI file. ; Returns: [boolean] True (success) or False (unable to save modifications back to the INI file, where applicable) INI_blnResult% = True If (INI_CurrentFile\blnModified = True) Then ; Commit internal changes back to the INI file INI_lngFileHandle = WriteFile(INI_CurrentFile\strFilename) If INI_lngFileHandle <> 0 Then WriteLine INI_lngFileHandle, Left$(INI_CurrentFile\strContents, Len(INI_CurrentFile\strContents) - 2) CloseFile INI_lngFileHandle INI_CurrentFile\blnModified = False Else INI_blnResult = False ; Create file failed End If End If Return INI_blnResult End Function Function INI_ReadValue$(INI_strSection$, INI_strKey$, INI_strDefault$ = "") ; Description: Retrieve the INI file value for the specified INI Section/Key combination. ; Returns: [string] INI Value - The optional INI_strDefault value is returned if no SECTION/KEY combination is found INI_strSection = "[" + Upper$(Trim$(INI_strSection)) + "]" INI_strKey = Chr$($0A) + Upper$(Trim$(INI_strKey)) INI_strUpperContents$ = Upper$(INI_CurrentFile\strContents) ; Locate the SECTION, KEY and VALUE INI_strValue$ = "" INI_lngSectionPos% = Instr(INI_strUpperContents, INI_strSection) If (INI_lngSectionPos <> 0) Then INI_lngKeyPos% = Instr(INI_strUpperContents, INI_strKey, (INI_lngSectionPos + Len(INI_strSection) + 1)) If (INI_lngKeyPos <> 0) Then INI_NextSection% = Instr(INI_strUpperContents, "[", (INI_lngSectionPos + Len(INI_strSection) + 1)) If (INI_NextSection = 0) Or (INI_lngKeyPos < INI_NextSection) Then INI_lngStartPos% = Instr(INI_strUpperContents, "=", (INI_lngKeyPos + 1)) If (INI_lngStartPos <> 0) Then INI_lngEndPos% = Instr(INI_strUpperContents, Chr$($0D), (INI_lngStartPos + 1)) If (INI_lngEndPos <> 0) Then ; We have located the required INI key and it's corresponding value INI_strValue = Trim$(Mid$(INI_CurrentFile\strContents, INI_lngStartPos + 1, (INI_lngEndPos - INI_lngStartPos - 1))) End If End If End If End If End If ; Return the appropriate value If (INI_strValue <> "") Then Return INI_strValue Else Return INI_strDefault End Function Function INI_WriteValue(INI_strSection$, INI_strKey$, INI_strValue$) ; Description: Add/update key values within the current INI file (internally). ; Note: Changes are not committed back To file until you explicitly call INI_CloseFile. ; Returns: [] n/a INI_strSection = "[" + Trim$(INI_strSection) + "]" INI_strUpperSection$ = Upper$(INI_strSection) INI_strKey = Trim$(INI_strKey) INI_strValue = Trim$(INI_strValue) ; (Re)Create the INI contents updating/adding the SECTION, KEY and VALUE INI_blnWrittenKey% = False INI_blnSectionFound% = False INI_strCurrentSection$ = "" INI_strTempContents$ = INI_CurrentFile\strContents INI_CurrentFile\strContents = "" INI_lngOldPos% = 1 INI_lngPos% = Instr(INI_strTempContents, Chr$($0D)) While (INI_lngPos <> 0) INI_strTemp$ =Trim$(Mid$(INI_strTempContents, INI_lngOldPos, (INI_lngPos - INI_lngOldPos))) If (INI_strTemp <> "") Then If Left$(INI_strTemp, 1) = "[" And Right$(INI_strTemp, 1) = "]" Then ; Process SECTION If (INI_strCurrentSection = INI_strUpperSection) And (INI_blnWrittenKey = False) Then INI_blnWrittenKey = INI_CreateKey(INI_strKey, INI_strValue) End If INI_CreateSection INI_strTemp INI_strCurrentSection = Upper$(INI_strTemp) If (INI_strCurrentSection = INI_strUpperSection) Then INI_blnSectionFound = True Else ; KEY=VALUE INI_lngEqualsPos% = Instr(INI_strTemp, "=") If (INI_lngEqualsPos <> 0) Then If (INI_strCurrentSection = INI_strUpperSection) And (Upper$(Trim$(Left$(INI_strTemp, (INI_lngEqualsPos - 1)))) = Upper$(INI_strKey)) Then INI_blnWrittenKey = INI_CreateKey(INI_strKey, INI_strValue) Else INI_CurrentFile\strContents = INI_CurrentFile\strContents + INI_strTemp + Chr$($0D) + Chr$($0A) End If End If End If End If ; Move through the INI contents... INI_lngOldPos = INI_lngPos + 1 INI_lngPos% = Instr(INI_strTempContents, Chr$($0D), INI_lngOldPos) Wend ; KEY wasn't found in the INI contents - Append a new SECTION if required and create our KEY=VALUE line If (INI_strValue <> "") And (INI_blnWrittenKey = False) Then If (INI_blnSectionFound = False) Then INI_CreateSection INI_strSection End If INI_CreateKey INI_strKey, INI_strValue End If INI_CurrentFile\blnModified = True End Function Function INI_CreateSection(INI_strNewSection$) If INI_CurrentFile\strContents <> "" Then ; Blank line between sections INI_CurrentFile\strContents = INI_CurrentFile\strContents + Chr$($0D) + Chr$($0A) End If INI_CurrentFile\strContents = INI_CurrentFile\strContents + INI_strNewSection + Chr$($0D) + Chr$($0A) End Function Function INI_CreateKey%(INI_strKey$, INI_strValue$) If (INI_strValue <> "") Then INI_CurrentFile\strContents = INI_CurrentFile\strContents + INI_strKey + "=" + INI_strValue + Chr$($0D) + Chr$($0A) End If Return True End Function |
| ||
Well, it seems to be more simply programmed than mine and will surely do the job. A myIni ini file is not as common ini and could looks like this: ----------------------------------------------------------- ->Description of the ini file (header) x = 10 ;: xpos y = 10 ;: ypos width = 320 ;: width height = 240 ;: height [ChildWindow] [Pos] x = 5 ;: child xpos y = 5 ;: child ypos width = 100 ;: child width height = 100 ;: child height [.] [Color] r = 128 ;: Red value g = 128 ;: Green value b = 255 ;: Blue value [.] [.] ----------------------------------------------------------- You can the access sections and subsection with: w = myIni_GetInt("width", "ChildWindow\Pos") or myIni_SetSection("ChildWindow\Color") r = myIni_GetInt("r") You can also deal with multiple myIni files easily... |
| ||
I'm not being negative just practical... Well personally I prefer the regular formatted INI file and I'll tell you why. My screen savers have a configuration component written in VB and it reads and writes to the INI file using the standards Windows API calls. Now a custom INI file would require loads more programming on the VB side. Perhaps your new INI format has advantages over the standard MS one but IMHO the last thing we need is different formatted INI files. In fact I would go one step further and say don't give yours a .INI extension as it's NOT an INI file. |
| ||
Did someone mentioned XML already? ;) |
| ||
Jake, good point... |
| ||
You're right boiled Sweets with the extension. The fact is that myIni_Load, myIni_Create and myIni_Save functions have no defined extension. the user may save its file as myGame.cfg or myGame.blabla any extension wanted. So I will change the extension of the demo generated file, And will choose a better extension for the future examples and help to not confusing. Thanks, köchOn |
| ||
You really should have a look into XML. I see only advantages for you: * it's existing, you don't have to code it yourself * it does all the extra functionality you want plus some more you may like * it's a standard and xml-libs exists for almost any language Don't get me wrong, I was a real .ini-fanboy but switched completly to XML as it gives me more functionality with less pain. Search this forum for MAXML and give it a try... Jake PS: Yes, LibXML exists as well, but IMHO Maxml is easier to use. |
| ||
Thanks jake, but I want to have my own system. I ll only add a .mini extensions for each files created to avoid confusions with normal ini files XML seems to be very interesting and easy to use but as there is no dll to wrap, I should have to get a large portion of code from the code archives. Then myIni system is intended to be used only for the Blitz3D/BlitzPlus community. köchOn |