*.ini Konfigurationsdateien parsen und schreiben
Übersicht

OJayBetreff: *.ini Konfigurationsdateien parsen und schreiben |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
dies ist die funktions-bibliothek, um ini-dateien zu speichern und einlesen zu können. den umgang damit bekommt ihr in unserem FAQ und Tutorial Bereich im Thread *.ini Konfigurationsdateien parsen und schreiben beigebracht ![]() BlitzBasic: [AUSKLAPPEN]
|
||
![]() |
Markus2 |
![]() Antworten mit Zitat ![]() |
---|---|---|
Hmm,
so vom ansehen würde ich sagen das ist nicht ganz richtig ... EDIT: OK , so wie im TUT versteht man es besser ![]() |
||
- Zuletzt bearbeitet von Markus2 am Do, Mai 27, 2004 21:22, insgesamt einmal bearbeitet
![]() |
Ctuchik |
![]() Antworten mit Zitat ![]() |
---|---|---|
Wieso? Sieht doch gut aus! Muss ich mal testen, aber net jetzt! | ||
![]() |
Markus2 |
![]() Antworten mit Zitat ![]() |
---|---|---|
Gleiche Keys können doch an verschiedene Gruppen hängen und
davon sehe ich nix ... |
||
OJay |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Markus2 hat Folgendes geschrieben: Gleiche Keys können doch an verschiedene Gruppen hängen und
davon sehe ich nix ... bitte tutorial lesen! danke ![]() |
||
![]() |
Markus2 |
![]() Antworten mit Zitat ![]() |
---|---|---|
@OJay
Habe mir dein Tutorial also mal angesehen , ist aber nicht wirklich für eine echte INI Datei aber für BB ist es ok . Kann es sein das du immer die ganze INI neu schreiben mußt wenn man nur ein Wert ändern will ? Da könntest du noch was verbessern . INI = ReadFile(game\IniFile$) If INI then ... CloseFile INI EndIf Geht natürlich so nicht und müßte in den Funktionen rein falls mal keine INI da ist oder ? |
||
OJay |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
wenn keine ini da ist, setzt man einfach default-werte:
[syntax="bb":1:ff8496096c]INI = ReadFile(game\IniFile$) If INI then ... CloseFile INI Else defaultvar1 = 1234 defaultvar2 = 5678 EndIf [/syntax:1:ff8496096c] aber soweit wollte ich in dem tut garnicht gehen...sicher geht es besser. aber ich wollte es den leuten so einfach wie möglich machen ![]() |
||
Dreamora |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Ist jetzt net gegen das hier gerichtet, hab nur ma in einem der existierenden Codearchive folgende Lib gefunden die ich für Ini wärmstens empfehlen kann ![]() *wer weiss wo man sie finden kann, kann gern den link hinzufügen und ich ersetz es ... der homepagelink im code existiert leider nimmer* [syntax="bb":1:0519d0393e] ;***************************************************************************************** ;** ;** grbIniFilev2.bb - Ini File Library ;** ;** author: ;** me@grable.cjb.net ;** http://grable.cjb.net ;** ;** 06.09.2002 ;** Usage ; ; ; create or load a ini file ; Global Ini.IniFile=NewIniFile( "default_filename.ini") ; Global Ini.IniFile=LoadIniFile( "filename.ini") ; ; ; read / write keys ; SetIniFile( Ini) ; must be set before using read/write ; IniWriteString( "Section", "Key", "Value") ; IniReadString( "Section", "Key") ; ; ; when done ; SaveIniFile( Ini) ; FreeIniFile( Ini) ; ; comments are the sane as blitz ";" and "//" at the beginig of a line Const MaxIniFileSections=64 Const MaxIniSectionKeys=64 ; global IniFile settings ;Global INI_ValueConversion=False ; not implemented Global INI_AutoCreateKeys=True Global INI_AutoCreateSections=True ;Global INI_ReadComments=False ; not implemented Global INI_WriteComments=False ; current IniFile ; used by ini Read/Write functions Global INI_File.IniFile Global IsIniFileSet=False Global resSection.IniSection Global resKey.IniKey ; Default Ini Syntax Const INI_KeySeperatorStr$="=" Const INI_SectionStartStr$="[" Const INI_SectionEndStr$="]" Const INI_NumCommentsStr=2 Global INI_CommentsStr$[ INI_NumCommentsStr-1] INI_CommentsStr[ 0]=";" INI_CommentsStr[ 1]="/" Global INI_BoolStr$[ 1] INI_BoolStr[ 0]="False" INI_BoolStr[ 1]="True" ;***************************************************************************************** ;** ;** IniFile Type ;** Type IniFile Field FileName$ Field Sections.IniSection[ MaxIniFileSections-1] Field NumSections Field Changed ; boolean Field Comment$ End Type Function NewIniFile.IniFile( FileName$="") Local ini.IniFile=New IniFile ini\FileName=FileName ini\Changed=False Return ini End Function Function ClearIniFile( Ini.IniFile) Local i,a For i=0 To Ini\NumSections-1 For a=0 To Ini\Sections[ i]\NumKeys-1 Delete Ini\Sections[ i]\Keys[ a] Next Delete Ini\Sections[ i] Next Ini\NumSections=0 End Function Function FreeIniFile( Ini.IniFile) ClearIniFile( Ini) Delete Ini End Function ;***************************************************************************************** ;** ;** IniSection Type ;** Type IniSection Field Name$ Field Owner.IniFile Field Keys.IniKey[ MaxIniSectionKeys-1] Field NumKeys Field Comment$ End Type Function NewIniSection.IniSection( Ini.IniFile, Name$) Local section.IniSection=New IniSection section\Name=Name section\Owner=Ini ; add to owner section list section\Owner\Sections[ section\Owner\NumSections]=section section\Owner\NumSections=section\Owner\NumSections+1 Return section End Function Function FindIniSection( Ini.IniFile, Section$) Local i For i=0 To Ini\NumSections-1 If Lower( Ini\Sections[ i]\Name)=Section Then resSection=Ini\Sections[ i] Return True EndIf Next Return False End Function ;***************************************************************************************** ;** ;** IniKey Type ;** Type IniKey Field Name$ Field Value$ Field Owner.IniSection Field Comment$ End Type Function NewIniKey.IniKey( Section.IniSection, Name$, Value$="") Local key.IniKey=New IniKey key\Name=Name key\Value=Value key\Owner=Section ; add to owner key list key\Owner\Keys[ key\Owner\NumKeys]=key key\Owner\NumKeys=key\Owner\NumKeys+1 Return key End Function Function FindIniKey( Section.IniSection, Key$) Local i For i=0 To Section\NumKeys-1 If Lower( Section\Keys[ i]\Name)=Key Then resKey=Section\Keys[ i] Return True EndIf Next Return False End Function ;***************************************************************************************** ;** ;** Misc IniFile Functions ;** Function SetIniFile( Ini.IniFile) INI_File=Ini IsIniFileSet=( INI_File<>Null) End Function ;***************************************************************************************** ;** ;** IniFile Write Functions ( to currently set IniFile) ;** Function IniWriteString( Section$, Key$, Value$) If Not IsIniFileSet Then Return False If Section="" Or Key="" Then Return False If FindIniSection( INI_File, Lower( Section)) Then If FindIniKey( resSection, Lower( Key)) Then ; modify the key resKey\Value=Value resKey\Owner\Owner\Changed=True Return True Else ; key not found If INI_AutoCreateKeys Then ; create a new key resKey=NewIniKey( resSection, Key, Value) resKey\Owner\Owner\Changed=True Return True EndIf EndIf Else ; section not found If INI_AutoCreateSections Then ; create a new section resSection=NewIniSection( INI_File, Section) resSection\Owner\Changed=True ; run function again for the key Return IniWriteString( Section, Key, Value) EndIf EndIf Return DefValue End Function Function IniWriteInt( Section$, Key$, Value) Return IniWriteString( Section, Key, Str( Value)) End Function Function IniWriteFloat( Section$, Key$, Value#) Return IniWriteString( Section, Key, Str( Value)) End Function Function IniWriteBool( Section$, Key$, Value) Return IniWriteString( Section, Key, INI_BoolStr[ Value]) End Function ;***************************************************************************************** ;** ;** IniFile Read Functions ( to currently set IniFile) ;** Function IniReadString$( Section$, Key$, DefValue$="") If Not IsIniFileSet Then Return DefValue If Section="" Or Key="" Then Return DefValue If FindIniSection( INI_File, Lower( Section)) Then If FindIniKey( resSection, Lower( Key)) Then ; return the key value Return resKey\Value EndIf EndIf Return DefValue End Function Function IniReadInt( Section$, Key$, DefValue=0) Return Int( IniReadString( Section, Key, Str( DefValue))) End Function Function IniReadFloat#( Section$, Key$, DefValue#=0.0) Return Float( IniReadString( Section, Key, Str( DefValue))) End Function Function IniReadBool( Section$, Key$, DefValue=False) Select Lower( IniReadString( Section, Key, INI_BoolStr[ DefValue])) ; compare to default boolean strings Case Lower( INI_BoolStr[ False]) Return False Case Lower( INI_BoolStr[ True]) Return True ; debg ; extra booleans READ ONLY Case "0" Return False Case "1" Return True Case "off" Return False Case "on" Return True End Select Return DefValue End Function ;***************************************************************************************** ;** ;** IniSection / IniKey Commenting ;** Function SetIniSectionComment( Section$, Comment$="") If Not IsIniFileSet Then Return False If FindIniSection( INI_File, Lower( Section)) Then resSection\Comment=Comment Return True EndIf Return False End Function Function SetIniKeyComment( Section$, Key$, Comment$="") If Not IsIniFileSet Then Return False If FindIniSection( INI_File, Lower( Section)) Then If FindIniKey( resSection, Lower( Key)) Then resKey\Comment=Comment Return True EndIf EndIf Return False End Function ;***************************************************************************************** ;** ;** IniFile Stream Reading/Writing Functions ;** Function IniIsComment( S$) Local i For i=0 To INI_NumCommentsStr-1 If INI_CommentsStr[ i]=S Then Return True EndIf Next Return False End Function ; IniGetKeyName( "Key=Value")="Key" Function IniGetKeyName$( S$) Local index=Instr( S, INI_KeySeperatorStr) If index Then Return Trim( Mid( S, 1, index-1)) Return "" End Function ; IniGetKeyValue( "Key=Value")="Value" Function IniGetKeyValue$( S$) Local index=Instr( S, INI_KeySeperatorStr) If index Then Return Trim( Mid( S, index+1, Len( S))) Return "" End Function ; IniGetSectionName( "[Section]")="Section" Function IniGetSectionName$( S$) Return Trim( Mid( S, 2, Len( S)-2)) End Function Function IniParseStream( Ini.IniFile, Stream) Local s$,c$, linecount, i Local key.IniKey, section.IniSection While Not Eof( Stream) s=Trim( ReadLine( Stream)) ; skip empty lines If Len( s)>0 Then c=Left( s,1) ; skip line breaks If c<>Chr( 13) And c<>Chr( 10) Then If Not IniIsComment( c) Then ; is it a section? If c=INI_SectionStartStr Then ; create the section section=NewIniSection( Ini, IniGetSectionName( s)) Else ; is it realy a key? If Instr( s, INI_KeySeperatorStr) Then ; create the key key=NewIniKey( section, IniGetKeyName( s), IniGetKeyValue( s)) EndIf EndIf EndIf EndIf EndIf linecount=linecount+1 Wend End Function Function IniWriteStream( Ini.IniFile, Stream, IgnoreChangedFlag=True) ; If Not ( Not IgnoreChangedFlag And Ini\Changed) Then Return False Local i,a ; write ini comment If INI_WriteComments And Len( Ini\Comment)>0 Then WriteLine Stream, INI_CommentsStr[ 0]+Ini\Comment WriteLine Stream, "" EndIf ; loop sections For i=0 To Ini\NumSections-1 ; write section comment If INI_WriteComments And Len( Ini\Sections[ i]\Comment)>0 Then WriteLine Stream, INI_CommentsStr[ 0]+Ini\Sections[ i]\Comment ; write section WriteLine Stream, INI_SectionStartStr+" "+Ini\Sections[ i]\Name+" "+INI_SectionEndStr ; loop keys For a=0 To Ini\Sections[ i]\NumKeys-1 ; write key comment If INI_WriteComments And Len( Ini\Sections[ i]\Keys[ a]\Comment)>0 Then WriteLine Stream, INI_CommentsStr[ 0]+Ini\Sections[ i]\Keys[ a]\Comment ; write key WriteLine Stream, Ini\Sections[ i]\Keys[ a]\Name+INI_KeySeperatorStr+Ini\Sections[ i]\Keys[ a]\Value Next WriteLine Stream, "" Next End Function ;***************************************************************************************** ;** ;** IniFile Loading / Saving ;** Function LoadIniFile.IniFile( FileName$, AutoSet=True) Local ini.IniFile=NewIniFile( FileName) Local stream=ReadFile( FileName) ; error loading file If Not stream Then DebugLog "grbIniFile::LoadIniFile() error loading "+FileName Delete ini Return Null EndIf IniParseStream( ini, stream) CloseFile stream If AutoSet Then SetIniFile( ini) Return ini End Function Function SaveIniFile( Ini.IniFile, FileName$="") If FileName<>"" Then Ini\FileName=FileName Local stream=WriteFile( Ini\FileName) ; error creating file If Not stream Then DebugLog "grbIniFile::SaveIniFile() error saving "+FileName Return False EndIf IniWriteStream( Ini, stream) CloseFile stream Ini\Changed=False Return True End Function [/syntax:1:0519d0393e] |
||
OJay |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
lol? ist das nicht ein "bischen" übertrieben? ich meine: ich möchte nur eine ini-einlesen, keine eigene registry entwerfen ![]() ich denke, für kleine spiele ist meine routine durchaus wärmstens zu empfehlen, da hier auch keinerleich globals, consts oder types benötigt werden... hinzu kommt: erklär doch mal jemanden, der gerade mit blitz angefangen, wie man mit dem haufen funktionen umgehen soll... |
||
Dreamora |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
War auch nicht als Konkurrenz zu deiner sondern speziell zum "selben key in mehreren sektionen" Problem und dergleichen
Zudem: wer sagt dass die Codebase für totale Anfänger sein muss? Die Lib ist auf jeden Fall für grössere und gut strukturierte Ini wärmstens zu empfehlen, für kleine einfache wird sie natürlich nicht umbedingt gebraucht |
||
.Yukito. |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
meiner meinung ist die lib da (die Dreamora gepostet hat) ganz schön groß... da ich gestern nicht ans netz konnte musste ich mir selber eine schreiben... hat 2 befehle und die reichen...
Value$ = INI_GetValue(file$,group$,key$) INI_SetValue(file$,group$,key$,value$) ![]() |
||
AMD 2000+, 512MB DDR, ATI Radeon 9000, 80 GB HDD, DX9.0, C4D 8.0, Adobe Photoshop 7.0 |
![]() |
jonnyboy |
![]() Antworten mit Zitat ![]() |
---|---|---|
EDIT: Aaah, ich hab gerade den Fehler gefunden, es dürfen keine leerzeichen zwiehscne "uzanto=Jonny" in der Ini-File sein...
Hallo! Leider funktioniert der Code bei mir nicht... Alles steht in der Hauptdatei, der Pfad ist zu 100 Prozent richtig... Leider bekomme ich immer nur "fehler" zurück, also er kriegt das einfach nicht gelesen... Hier auszüge aus dem Code: ; --> Benutzer einlesen BlitzBasic: [AUSKLAPPEN] Global ini_uzanto = ReadFile("C:\Dokumente und Einstellungen\meinName\Desktop\Projekte\projekt\System\sistemo\uzanto\uzanto.ini") In der ComboBox steht immer "fehler"... Die .ini sieht so aus: Code: [AUSKLAPPEN] #aministranto
[informo] uzantoj = 1 [admin] uzanto = Jonny pasvorto = jjj Bitte, ich ahbe schon alles, was mir einfällt ausprobiert, es klappt einfach nicht... ![]() Ich verstehe nicht, woran das liegt, ich habe trotz dem auskommentierten Closefile eine leere "ini_uzanto" Variable bzw. (0), obwohl dort der Stream drin sein müsste... |
||
FWeinbehemals "ich" |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
(Ich glaube das ist ein neuer Rekord der Thread ist ~6 Jahre alt !) ![]() MfG ich |
||
"Wenn die Menschen nur über das sprächen, was sie begreifen, dann würde es sehr still auf der Welt sein." Albert Einstein (1879-1955)
"If you live each day as if it was your last, someday you'll most certainly be right." Steve Jobs |
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group