Ini.au3
Last modified: Thursday, 13 November 2008
;*******************************************************************************
;
; Function List
; _INIAddSection()
; _INICopySection()
; _IniSort()
; _IniSortSectionByValue()
; _IniSortSections()
; _IniSortValues()
;
;*******************************************************************************
#include-once
#Include<array.au3>
;===============================================================================
; Function Name: _INIAddSection()
; Description: Add an empty section to an Ini file for future use
; Syntax: _INIAddSection("File Path & Name", "Section"[, $i_Sort = 1])
; Parameter(s): $i_Sort - 1 to sort the section names after the new section is added
; Requirements:
; Return Value(s):
; Author(s): George (GEOSoft) Gedye
;===============================================================================
Func _INIAddSection($i_File, $i_Section, $i_Sort = 1)
FileWriteLine($i_File, @CRLF & '[' & $i_Section & ']')
If $i_Sort = 1 Then
$S_Names = _IniSortSections($i_File)
EndIf
EndFunc ;<==> _INIAddSection($i_File, $i_Section, $i_Sort = 1)
;===============================================================================
; Function Name: _INICopySection()
; Description: Copy an INI section from one file to another.
; Syntax: _INICopySection("File Path & Name of file to copy from", "File Path & Name of file to copy to", "Section")
; Parameter(s):
; Requirements:
; Return Value(s): Copies a complete section from the input file to the output file.
; Author(s): George (GEOSoft) Gedye
;===============================================================================
Func _INICopySection($i_File, $o_File, $i_Section)
Local $I, $iArray, $oSec = "", $nFile
$nFile = FileOpen($o_File, 1)
If @Error Then MsgBox(4096, "Error", "Unable to open the output file")
$iArray = StringSplit(StringStripCR(FileRead($i_File)), @LF)
$sSec = _ArraySearch($iArray, $i_Section, 0, 0, 0, True)
If $sSec = -1 Then
MsgBox(4096, "Error", "Unable to locate the requested section")
Return SetError(1)
EndIf
$eSec = _ArraySearch($iArray, '[', $sSec +1, 0, 0, True)
If $eSec <> -1 Then
$eSec -=1
Else
$eSec = Ubound($iArray) -1
EndIf
For $I = $sSec To $eSec
FileWriteLine($nFile, $iArray[$I])
Next
FileClose($nFile)
EndFunc ;<==> _INIAddSection($i_File, $i_Section, $i_Sort = 1)
;===============================================================================
; Function Name: _IniSort()
; Description: Sort an Ini file
; Syntax: _IniSort("File Path & Name" [, $sec_Order [, $val_Order]] )
; Parameter(s): $sec_Order 0 - Sort sections Ascending (default) Else sort sections Descending
; $val_Order 0 - Sort Values Ascending (default) Else sort values Descending
; Requirements:
; Return Value(s):
; Author(s): George (GEOSoft) Gedye
;===============================================================================
Func _IniSort($i_File, $sec_Order = 0, $val_Order = 0)
Local $S_Names = _IniSortSections($i_File, $sec_Order), $I, $O_txt = ''
For $I = 1 To Ubound($S_Names) -1
$O_Txt &= _IniSortValues($i_File, $S_Names[$I], $val_Order) & @CRLF
Next
$O_File = FileOpen($i_File, 2)
FileWrite($O_File,StringStripWS($O_Txt, 2))
FileClose($O_File)
EndFunc
;===============================================================================
; Function Name: _IniSortSectionByValue()
; Description: Sort a section based on the key value
; Syntax: _IniSortSectionByValue("File Path & Name", "Section" [, $s_Order] )
; Parameter(s): $s_Order 0 - Ascending (default) else - descending
; Requirements:
; Return Value(s): Failure - Sets @Error to 1
; Author(s): George (GEOSoft) Gedye
;===============================================================================
Func _IniSortSectionByValue($i_File, $iSec, $s_Order = 0)
Local $I, $sArray
If $s_Order <> 0 Then $s_Order = 1
$sArray = INIReadSection($i_File, $iSec)
If @Error Then Return SetError(@Error)
_ArraySort($sArray, $s_Order, 1, 0, 2, 1)
If IsArray($sArray) Then; _ArrayDisplay($sArray, "First")
For $I = 1 To $sArray[0][0]
IniDelete($i_File, $iSec, $sArray[$I][0])
IniWrite($i_File, $iSec, $sArray[$i][0], $sArray[$i][1])
Next
EndIf
EndFunc
;===============================================================================
; Function Name: _IniSortSections()
; Description: Sort the section names of an Ini file
; Syntax: _IniSortSections("File Path & Name" [, $s_Order] )
; Parameter(s): $s_Order 0 - Ascending (default) else - descending
; Requirements:
; Return Value(s): Sorted array of Section names
; Author(s): George (GEOSoft) Gedye
;===============================================================================
Func _IniSortSections($i_File, $s_Order = 0)
Local $S_Names = INIReadSectionNames($i_File)
If @Error Then Return SetError(@Error)
If $s_Order <> 0 Then $s_Order = 1
_ArraySort($S_Names, $s_Order)
Return $S_Names
EndFunc ;<==> _IniSortSections($i_File)
;===============================================================================
; Function Name: _IniSortValues()
; Description: Sort the values of an Ini file section
; Syntax: _IniSortValues("File Path & Name", "Section" [, $s_Order] )
; Parameter(s): $s_Order 0 - Ascending (default) else - descending
; Requirements:
; Return Value(s): Text string of the sorted values with a blank line added at the end of each section
; Author(s): George (GEOSoft) Gedye
;===============================================================================
Func _IniSortValues($i_File, $i_Sec, $s_Order = 0)
Local $s_Val = '', $Rtn = '[' & $i_Sec & ']' & @CRLF, $I
If $s_Order <> 0 Then $s_Order = 1
$v_Array = IniReadSection($i_File, $I_Sec)
If @Error Then Return SetError(@Error)
For $I = 1 To $v_Array[0][0]
$s_Val &= $v_array[$I][0] & '|'
Next
$s_Val = StringSplit(StringTrimRight($s_Val, 1), '|')
_ArraySort($s_Val, $s_Order)
For $I = 1 To $s_Val[0]
$Rtn &= $s_Val[$I] & '=' & IniRead($I_File, $i_Sec, $s_Val[$I],'') & @CRLF
;If $I <> $s_val[0] Then $Rtn &= @CRLF
Next
Return $Rtn
EndFunc ;<==> _IniSortValues($i_File, $i_Sec)