[Gambas-user] Write-Read from text file

Jose J. Rodriguez joe1962 at ...626...
Wed Jun 28 01:09:44 CEST 2006


On 6/27/06, Eilert <eilert-sprachen at ...221...> wrote:
> Jose J. Rodriguez schrieb:
> > On 6/26/06, LinuxSeeker <linuxseeker at ...626...> wrote:
> >> I am writing a program that needs to interact with the user and save the
> >> user's preferences (name, number of textboxes etc) in a text file (named
> >> "conf") at the same directory where the executable is located.
> >>
> > [snip]
> >>  Any suggestions on how to make this work? How can I update the
> >> configuration file without re-writing the entire configuration file? How do
> >> I select which line to read from?
> >>
> >
> > You should consider using the Gambas component gb.settings
> > (Application settings management):
> > http://www.gambasdoc.org/help/comp/gb.settings
> >
> > Regards,
> > Joe1962
> >
>
>
> Hi Jose,
>
> Thanks for this tip - never knew about this thing. But it's heavily
> undocumented, I doubt he'll get it to work as even I have some problems
> understanding how it would work.
>
> If you know how to handle it, shouldn't we develop some easy examples?
>
> Just like
>
> - initiating a new ini-file
> - reading from the ini-file
> - writing values into it
> - replacing values
>

Well, I haven't actually used this in Gambas2, as I only just
switched. Here's a snippet of something I did a while back in Gambas1
for vl-qemu (front-end for qemu, you can check it out at Gambas
Forge). As you may guess, the Settings stuff is used through a VB
ini-like concept.


In the Global module:

' Gambas module file

PUBLIC FUNCTION ReadIni(IniKey AS String, vDEFAULT AS Variant) AS Variant
DIM hSettings AS Settings
DIM aVariant AS Variant
   hSettings = NEW Settings(System.Home & "/" & ".vl-qemu.conf")
   aVariant = hSettings [ IniKey , vDEFAULT ]
   RETURN aVariant
END

PUBLIC SUB WriteIni(IniKey AS String, aVariant AS Variant)
DIM hSettings AS Settings
   hSettings = NEW Settings(System.Home & "/" & ".vl-qemu.conf")
   hSettings [ IniKey ] = aVariant
END

PUBLIC SUB SaveConfig()
   'Write config to ini file:
   WriteIni("Setting.Boot", frmMain.cmbBoot.Index)
   WriteIni("Setting.KeyLang", frmMain.cmbKeyLang.Index)
[snip]
   WriteIni("ImagePath.HDD", frmMain.txtHDD.Text)
   WriteIni("Network.Samba", frmMain.chkSamba.Value )
   WriteIni("Network.SambaPath", frmMain.txtSamba.Text )
END

PUBLIC SUB ReadConfig()
   'Read config from ini file:
   frmMain.cmbBoot.Index = ReadIni("Setting.Boot", 0)
   frmMain.cmbKeyLang.Index = ReadIni("Setting.KeyLang", 5)
   frmMain.cmbRTCMode.Index = ReadIni("Setting.RTCMode", 0)
[snip]
   frmMain.txtHDD.Text = ReadIni("ImagePath.HDD", "")
   frmMain.chkSamba.Value = ReadIni("Network.Samba", 0)
   frmMain.txtSamba.Text = ReadIni("Network.SambaPath", "")
END


And in frmMain:

' Gambas class file

PUBLIC SUB Form_Open()
[snip]
      'Read config from ini file:
      Global.ReadConfig
[snip]
END

PUBLIC SUB ButtonExit_Click()
   'Write config to ini file:
   Global.SaveConfig
   'Go away...
   ME.Close
END


Hope that helps.

Regards,
Joe1962




More information about the User mailing list