Page 1 of 1

Array values to .ini file

Posted: Sat Aug 28, 2021 11:19 pm
by Claudio Ricardo
Hola, necesitaba guardar en el archivo Config.ini valores leidos de un array unidimensional, y cuando se necesite, restaurar el array desde el .ini
Para ello cree estas dos funciones, (utilicé ";" como separador pero se puede usar otro caracter), las incluí en mi libreria y las comparto.

Code: Select all

//  Function ArrayToIni (Array) return unidimensional array values in string divided by ";"

Function ArrayToIni (aArray)

LOCAL cText := ""
LOCAL Elemento

	If HMG_Len (aArray) > 0
	
		For Each Elemento In aArray
		
			If ValType ( Elemento) == "N"	  // Array only have character values but...
			
				Elemento := AllTrim ( Str ( Elemento))
				
			EndIf
			
			cText := cText + Elemento + ";"
		
		Next

	EndIf

Return cText

Code: Select all

//  Function IniToArray (Text from .ini file) restore unidimensional array from ArrayToIni () format

Function IniToArray (cIni)

LOCAL aArray := {}
LOCAL cText := ""
LOCAL nPos := 0

	If ! Empty (cIni)

		While .T.

			nPos := Hb_Uat (";",cIni)
			cText := Hb_UsubStr (cIni, 1, nPos - 1)
			
			If Empty (cText)
			
				Exit
				
			EndIf
			
			Aadd (aArray, cText)
			cIni := Stuff (cIni, 1, nPos, "")
			
		EndDo
	
	EndIf

Return aArray