News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Library to read and write INI files

Started by empty, December 01, 2004, 05:12:51 PM

Previous topic - Next topic

empty

Put this in PlayBasic's Library folder (or any other library folder you've specified) and include it with #INCLUDE to use it in any of your projects.

Download


Functions:


Result = Ini_GetSectionCount(FileName$)
FileName$ = Full path and filename of the INI file
Result = Number of sections in the INI file

Returns the number of sections in an INI file.


Result$ = Ini_GetSectionName(FileName$, Section)
FileName$ = Full path and filename of the INI file
Section = Section you want to get the name of
Result$ = The name of the section

Returns the name of a specific section.


Result = Ini_ReadSections(FileName$, Sections$())
FileName$ = Full path and filename of the INI file
Sections$() = A string array that will contain the names of the sections
Result = 1 if successful, 0 if an error occured

Writes all section names to a given string array.


Result = Ini_ReadSectionKeys(FileName$, Section$, Keys$())
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Sections$() = A string array that will contain the name of the keys
Result = 1 if successful, 0 if an error occured

Writes all keys of a specified section to an array.


Result = Ini_GetInteger(FileName$, Section$, Key$, DefaultValue)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
DefaultValue = The value that will be returned if the Key doesn't exist
Result = The returned integer value

Returns the integer value of a key.


Result# = Ini_GetFloat(FileName$, Section$, Key$, DefaultValue#)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
DefaultValue# = The value that will be returned if the Key doesn't exist
Result# = The returned float value

Returns the float value of a key.


Result$ = Ini_GetString(FileName$, Section$, Key$, DefaultValue$)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
DefaultValue$ = The value that will be returned if the Key doesn't exist
Result$ = The returned string value

Returns the string value of a key.


Ini_WriteInteger(FileName$, Section$, Key$, Value)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Value = The integer value you want to write.

Writes an integer value to a specified key.


Ini_WriteFloat(FileName$, Section$, Key$, Value#)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Value# = The float value you want to write.

Writes a float value to a specified key.


Ini_WriteString(FileName$, Section$, Key$, Value$)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Value$ = The string value you want to write.

Writes a string value to a specified key.





Example code
PlayBASIC Code: [Select]
#INCLUDE "IniFiles"

Path$ = CurrentDir$()
IniFile$ = Path$ + "Test.Ini"

Section1$ = "Section 1"
Section2$ = "Another Section"

; Create an ini file
Ini_WriteInteger(IniFile$, Section1$, "myInteger", 42)
Ini_WriteFloat(IniFile$, Section1$, "myFloat", 1.234)
Ini_WriteString(IniFile$, Section1$, "myString", "PlayBasic")

Ini_WriteInteger(IniFile$, Section2$, "Age", 33)
Ini_WriteString(IniFile$, Section2$, "Favourite Band", "King Crimson")
Ini_WriteFloat(IniFile$, Section2$, "CoolFloat", 3.33)

; Read the Ini Files Stuff
Print "Open file: " + IniFile$

; get number of sections
numsections = Ini_GetSectionCount(IniFile$)
Print "The file has " + Str$(numsections) + " Sections"

; Read all sections to an array
Dim AllSections$(0)
Ini_ReadSections(IniFile$, AllSections$())
Print "The Sections are:"
For i = 0 To GetArrayElements(AllSections$(),1)
Print AllSections$(i)
Next i


; read all entries of the first section
Dim AllEntries$(0)
Ini_ReadSectionKeys(IniFile$, Section1$, AllEntries$())
Print "The key names of the first section are:"
For i = 0 To GetArrayElements(AllEntries$(),1)
Print AllEntries$(i)
Next i

Print ""
Print "Reading the second section"
Age = Ini_GetInteger(IniFile$, Section2$, "Age", 1)
Band$ = Ini_GetString(IniFile$, Section2$, "Favourite Band", "none")
CoolFloat# = Ini_GetFloat(IniFile$, Section2$, "CoolFloat", 0.1)
Print "Age=" + Str$(Age)
Print "Band=" + Band$
Print "CoolFloat=" + Str$(CoolFloat#)


Sync
WaitKey



Cor

Thanks Just what I needed.

Good example of how to make dll calls :-)

gr.
Cor de Visser
Author of Super Guitar Chord Finder
http://www.ready4music.com
http://www.chordplanet.com

empty

Glad that you find it useful. :)

There was a bug with the returned default values, fixed now (same link).

empty

IniFiles 1.02

Renamed a few functions and added a way to write and read more than one value per key (example Rect=10,14,44,49).
Haven't had the time to create an example, but will do so soon.

empty

#4
IniFiles 1.03

Changes
- section names are returned without brackets []
- renamed functions


Functions:


Result = Ini_GetSectionCount(FileName$)
FileName$ = Full path and filename of the INI file
Result = Number of sections in the INI file

Returns the number of sections in an INI file.


Result$ = Ini_GetSectionName(FileName$, Section)
FileName$ = Full path and filename of the INI file
Section = Section you want to get the name of
Result$ = The name of the section

Returns the name of a specific section.


Result = Ini_ReadSections(FileName$, Sections$())
FileName$ = Full path and filename of the INI file
Sections$() = A string array that will contain the names of the sections
Result = 1 if successful, 0 if an error occured

Writes all section names to a given string array.


Result = Ini_ReadSectionKeys(FileName$, Section$, Keys$())
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Sections$() = A string array that will contain the name of the keys
Result = 1 if successful, 0 if an error occured

Writes all keys of a specified section to an array.


Result = Ini_GetInteger(FileName$, Section$, Key$, DefaultValue)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
DefaultValue = The value that will be returned if the Key doesn't exist
Result = The returned integer value

Returns the integer value of a key.


Result = Ini_ReadIntegers(FileName$, Section$, Key$, Values())
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Values() = The array all values of the given key are written to
Result = The number of values the key contains

Reads multiple integer values of one key.


Result# = Ini_GetFloat(FileName$, Section$, Key$, DefaultValue#)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
DefaultValue# = The value that will be returned if the Key doesn't exist
Result# = The returned float value

Returns the float value of a key.


Result = Ini_ReadFloats(FileName$, Section$, Key$, Values#())
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Values#() = The array all values of the given key are written to
Result = The number of values the key contains

Reads multiple float values of one key.


Result$ = Ini_GetString(FileName$, Section$, Key$, DefaultValue$)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
DefaultValue$ = The value that will be returned if the Key doesn't exist
Result$ = The returned string value

Returns the string value of a key.


Result = Ini_ReadStrings(FileName$, Section$, Key$, Values$())
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Values$() = The array all values of the given key are written to
Result = The number of values the key contains

Reads multiple string values of one key.


Ini_SetInteger(FileName$, Section$, Key$, Value)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Value = The integer value you want to write.

Writes an integer value to a specified key.


Ini_WriteIntegers(FileName$, Section$, Key$, Values())
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Values() = An array of the values to write

Writes multiple integer values to a specified key.


Ini_SetFloat(FileName$, Section$, Key$, Value#)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Value# = The float value you want to write.

Writes a float value to a specified key.


Ini_WriteFloats(FileName$, Section$, Key$, Values#())
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Values#() = An array of the values to write

Writes multiple float values to a specified key.


Ini_SetString(FileName$, Section$, Key$, Value$)
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Value$ = The string value you want to write.

Writes a string value to a specified key.


Ini_WriteStrings(FileName$, Section$, Key$, Values$())
FileName$ = Full path and filename of the INI file
Section$ = The name of the section
Key$ = The name of the key
Values$() = An array of the values to write

Writes multiple string values to a specified key.




Example
PlayBASIC Code: [Select]
#INCLUDE "IniFiles"

FName$ = "Test.ini"
IniFile$ = CurrentDir$() + FName$


Section1$ = "First Section"
Section2$ = "Second Section"


; Write single integer
Ini_SetInteger(IniFile$, Section1$, "Age", 33)

; Write single float
Ini_SetFloat(IniFile$, Section1$, "A Float Value", 1.234)

; Write single string
Ini_SetString(IniFile$, Section1$, "Cool Language", "Play Basic")


; Write multiple integers
Dim RectValues(3)
RectValues(0) = 10
RectValues(1) = 12
RectValues(2) = 134
RectValues(3) = 99
Ini_WriteIntegers(IniFile$, Section2$, "My Rect", RectValues())
UnDim RectValues()

; Write multiple floats
Dim Floats#(1)
Floats#(0) = 9.876
Floats#(1) = 5.432
Ini_WriteFloats(IniFile$, Section2$, "Floats", Floats#())
UnDim Floats#()

; Write multiple strings
Dim Names$(2)
Names$(0) = "Play Basic"
Names$(1) = "John Lennon"
Names$(2) = "Shrek"
Ini_WriteStrings(IniFile$, Section2$, "Names", Names$())
UnDim Names$()


; Analyse Ini file
; number of section
SectionCount = Ini_GetSectionCount(IniFile$)
Print FName$ + " has " + Str$(SectionCount) + " sections"

; read sections
Dim Sections$(0)
Ini_ReadSections(IniFile$, Sections$())
Print "The sections are called:"
For i = 0 To GetArrayElements(Sections$(), 1)
Print Sections$(i)
Next i
UnDim Sections$()
Print ""

; read section keys
Print "The first section has the following keys:"
Dim SectionKeys$(1)
Ini_ReadSectionKeys(IniFile$, Section1$, SectionKeys$())
For i = 0 To GetArrayElements(SectionKeys$(), 1)
Print SectionKeys$(i)
Next i
UnDim SectionKeys$()
Print ""


; Reading Values
; Read single integer
Age = Ini_GetInteger(IniFile$, Section1$, "Age", 0)
Print "The value of the key 'Age' is " + Str$(age)

; read single float value
floatval# = Ini_GetFloat(IniFile$, Section1$, "A Float Value",0)
Print "The value of the key 'A Float Value' is " + Str$(floatval#)

; read a single string value
Language$ = Ini_GetString(IniFile$, Section1$, "Cool Language", "")
Print "The value of the key 'Cool Language' is " + Language$
Print ""

; reading multiple values
; read multiple integers
Dim myInts(0)
Ini_ReadIntegers(IniFile$, Section2$, "My Rect", myInts())
Print "The key 'My Rect' has these values:"
For i = 0 To GetArrayElements(myInts(),1)
Print myInts(i)
Next i
UnDim MyInts()
Print ""

; read multiple floats
Dim myFloats#(0)
Ini_ReadFloats(IniFile$, Section2$, "Floats", myFloats#())
Print "The key 'Floats' has these values:"
For i = 0 To GetArrayElements(myFloats#(), 1)
Print myFloats#(i)
Next i
UnDim myFloats#()
Print ""

; read multiple strings
Dim myStrings$(0)
ini_ReadStrings(IniFile$, Section2$, "Names", myStrings$())
Print "The key 'Names' has these values:"
For i = 0 To GetArrayElements(myStrings$(), 1)
Print myStrings$(i)
Next i
UnDim myStrings$()




Sync
WaitKey