[bb] simple ini file commands (broken) by dan_upright [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : simple ini file commands (broken)
Author : dan_upright
Posted : 1+ years ago

Description : *** for some reason this stuff doesn't work reliably anymore - it overwrites strings at random and can cause serious grief to blitz (it stopped recognising two identical values as equal for me) but i'll leave the code and stuff in case anyone wants to try and debug it/use it despite the warnings

set up the .decls file and then use the functions to read/write values

parameters are:
ReadIni:
----
Default is the value that will be returned if the file can't be read for some reason
String is the string that will contain the value read
size is the maximum number of characters that can be read (i use 255)

WriteIni:
----
Value is the value you want to store

Both:
----
AppName is (bizarrely) the name of the ini file section the value belongs to
KeyName is the name of the value you want
FileName is the name of the ini file (must include full path)


Code :
Code (blitzbasic) Select
kernel32.decls:
----
.lib "kernel32.dll"

ReadIni%(AppName$, KeyName$, Default$, String$, size%, FileName$):"GetPrivateProfileStringA"
WriteIni%(AppName$, KeyName$, Value$, FileName$):"WritePrivateProfileStringA"


Comments :


gman(Posted 1+ years ago)

 this works fine, you just need to size the string passed into read since GetPrivateProfileString will not resize it for you.  a simple wrapper could look like:Function read_ini$(section$,key$,dflt$,file$)   Local temp$=String(" ",1024)   ReadIni(section$,key$,dflt$,temp$,1024,file$)   Return Trim(temp$)End Function


Extron(Posted 1+ years ago)

 temp$ need to be a pointer to a buffer not a null terminated string.


Extron(Posted 1+ years ago)

 Like this.
kernel32.decls:
----
.lib "kernel32.dll"

ReadIni%(AppName$, KeyName$, Default$, Buffer*, size%, FileName$):"GetPrivateProfileStringA"
WriteIni%(AppName$, KeyName$, Value$, FileName$):"WritePrivateProfileStringA"
; Various var
Section$="Blitz Basic 3D"
Key$="Start"
Value$="2264"
File$="c:Blitz Basic 3D.ini"
Default_string$="Null"
; Write section/key/value
Write_ini$(Section$,Key$,Value$,File$)
; Read section/key
val$=Read_ini$(Section$,Key$,Default_string$,File$)
Text 0,0,val$
;
WaitKey
End
;
;*********************************************************
;
Function Write_ini$(Section$,Key$,Value$,File$)
WriteIni(Section$, Key$, Value$, File$)
End Function
;
Function Read_ini$(Section$,Key$,Default_string$,File$)
bank=CreateBank(255)
ReadIni(Section$,Key$,Default_string$,bank,BankSize(bank),File$)
If PeekByte(bank,0)=0 Then
value$=Default_string$
Return value$
EndIf
For char=0 To BankSize(bank)-1
chartemp=PeekByte(bank,char)
If chartemp=0 Then Exit
value$=value$+Chr$(chartemp)
Next
Return value$
End Function



gman(Posted 1+ years ago)

 interesting.  thx for the heads up Extron.  i guess my thinking that the userlibs functionality of Blitz was taking care of something for me somewhere down the line was incorrect.  any idea why my routine has been working for me?  puzzled now :(  and thx for the code example :)thx.


Extron(Posted 1+ years ago)

 I don't know, your code don't work for me.Just a minor correction "If PeekByte(bank,0)=0 Then" or "If Not PeekByte(bank,0) Then" after "ReadIni(Section$....".


gman(Posted 1+ years ago)

 huh.  i wonder if its a difference between BlitzPlus and Blitz3D which, given your example, looks like you are using?  in any case thx.  to be safe i will be switching.  one more Q...   how did you get the code to show up as code in your original post?  thx :)


Extron(Posted 1+ years ago)

 Blitz3D 1.87<div class="quote"> how did you get the code to show up as code in your original post? thx </div>Use code, see the <a href="../faq/faq_entry0b30.html?id=2" target="_blank">FAQ</a> ;)


gman(Posted 1+ years ago)

 lol thx for the pointer to the FAQ :)  was looking all over for it in the community area.  didnt see it there on the main page since i bookmarked the community area.  guess i need to be a bit more thorough :(  anyways, i dont have the full version of blitz3d, but the demo does not function with the code i posted and the full version of blitzplus v1.39 does.  interesting they operate differently...  in any case, the safer, more accepted and trustworthy method is the way im headed :)  nice talking with ya extron :) [/i]