November 24, 2020, 06:18:08 AM

Author Topic: [bb] Ini File Updater by Rob Farley [ 1+ years ago ]  (Read 735 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Ini File Updater by Rob Farley [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Ini File Updater
Author : Rob Farley
Posted : 1+ years ago

Description : One command, Ini_File, this allows you to add to your ini file, append ini file values and get values back.

Usage:

To get a value out of an ini file:
Code: [Select]
value= ini_file(Filename,Command)So if your ini file has a line
Code: [Select]
Particles=200Then you would do something like
Code: [Select]
particles=ini_file("setup.ini","Particles")This would then put the value to the varible particles.

Okay, now updated the value:
Code: [Select]
ini_file("setup.ini","Particles",50)This will update the particles entry on the ini file to
Code: [Select]
Particles=50On top of this if you do
Code: [Select]
ini_file("setup.ini","Particles",50,trueIt will do the same but date and time stamp the change.

Further to this if you try to edit a value that is not present in the ini
Code: [Select]
ini_file("setup.ini","Fred",20)This will add the entry of
Code: [Select]
Fred=20If your value you want to amend or add is a string simply add a $ at the beginning of the value
Code: [Select]
ini_file("setup.ini","Fred","$Wilma")This will then put " marks around the value, this is really useful if you're adding more of a message ie something with spaces in it
Code: [Select]
ini_file ("setup.ini","Gameovermes","$Game Over, You're dead and stuff")
I hope this all makes sense, give me a shout if you find it handy.

Cheers, Rob. [/i]

Code :
Code: BlitzBasic
  1. ;ini file updater
  2. ; 2003 Mental Illusion
  3. ; http://www.mentalillusion.co.uk
  4. ; rob@mentalillusion.co.uk
  5.  
  6. ; Usage:
  7. ; value of command = ini_file(Filename,Command)
  8. ;
  9. ; or
  10. ;
  11. ; ini_file(Filename,Command,New Value,[Date Stamp])
  12.  
  13.  
  14. Function ini_file$(file$,cmd$,setting$="Null",date_stamp=False)
  15.  
  16. cmd=Lower(cmd)
  17. found=False
  18.  
  19.  
  20.  
  21. filein = ReadFile(file$)
  22.  
  23. If setting<>"Null"
  24.         fileout= WriteFile("temp.ini")
  25.         If Left(setting,1)="$" Then setting=Chr(34)+Right(setting,Len(setting)-1)+Chr(34)
  26.         If date_stamp Then setting=setting+" ; Updated "+CurrentDate()+" "+CurrentTime()
  27.         EndIf
  28.  
  29. return_value$="NULL"
  30.  
  31. While Not Eof(filein)
  32.  
  33. temp$=ReadLine(filein)
  34.  
  35. If temp<>"" And Left(temp,1)<>";"
  36.         command$=Lower(Left$(temp,Instr(temp,"=")-1)) ;extract command
  37.         value$=Mid(temp,Instr(temp,"=")+1,(Instr(temp,";")-1)-(Instr(temp,"=")+1)) ;extract value
  38.         If Left(value,1)=Chr(34)
  39.                 value=Mid(value,2,Instr(value,Chr(34),2)-2) ;extract a string if it is one
  40.                 Else
  41.                 If Instr(value," ")>0 Then value=Left(value,Instr(value," ")-1) ;trim spaces off value
  42.                 If Instr(value,Chr(9))>0 Then value=Left(value,Instr(value,Chr(9))-1) ;trim tabs off value
  43.                 EndIf
  44.  
  45.         If command=cmd
  46.                 found=True
  47.                 If setting="Null"
  48.                         return_value=value
  49.                         Else
  50.                         WriteLine(fileout,command+"="+setting)
  51.                         EndIf
  52.                 Else
  53.                 If setting<>"Null" Then WriteLine(fileout,temp)        
  54.                 EndIf
  55.         Else
  56.         If setting<>"Null" Then WriteLine(fileout,temp)
  57.         EndIf
  58.  
  59. Wend
  60.  
  61. CloseFile (filein)
  62.  
  63. If setting="Null"
  64.         Return return_value
  65.         Else
  66.         If found=False Then WriteLine(fileout,cmd+"="+setting)
  67.         CloseFile (fileout)
  68.         DeleteFile file
  69.         CopyFile "temp.ini",file
  70.         DeleteFile "temp.ini"
  71.         EndIf
  72.  
  73. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal