FUNCTION setini
	PARAMETER pcinifile, pcsection,  ;
		pcvar, pcval
	PRIVATE lasect
	PRIVATE lavars
	PRIVATE ALL LIKE j*
	DIMENSION lasect[1], lavars[ 1,3]
	jlsuccess = .T.
	IF  .NOT. EMPTY(pcinifile)
		jcfilename = IIF(AT('.',  ;
			pcinifile) > 0,  ;
			pcinifile,  ;
			pcinifile +  ;
			'.INI')
		pcsection = ALLTRIM(pcsection)
		pcvar = ALLTRIM(pcvar)
		pcval = ALLTRIM(pcval)
		IF FILE(jcfilename)
			jnhandle = FOPEN(jcfilename,  ;
				2)
			IF jnhandle < 0
				jlsuccess = .F.
				= messagebox( ;
					'Unable to open file: ' +  ;
					jcfilename, ;
					'File Open Error', ;
					0)
				RETURN jlsuccess
			ENDIF
		ELSE
			jnhandle = -1
		ENDIF
		= buildarray(jnhandle, ;
			@lasect,@lavars)
		IF jnhandle > -1
			= FCLOSE(jnhandle)
		ENDIF
		jsuccess = buildini(jcfilename, ;
			pcsection,pcvar, ;
			pcval,@lasect, ;
			@lavars)
	ELSE
		jlsuccess = .F.
	ENDIF
	RETURN jlsuccess
ENDFUNC && setini
*!*
FUNCTION buildini
	PARAMETER pcfilename, pcsection,  ;
		pcvar, pcval, pasect,  ;
		pavars
	PRIVATE ALL LIKE j*
	jlsuccess = .T.
	jnfhandle = 0
	jlfoundvar = .F.
	jnfound = 0
	IF  .NOT. EMPTY(pasect)
		FOR jncount = 1 TO  ;
				ALEN(pasect, 1)
			IF UPPER(pasect(jncount)) ==  ;
					UPPER(pcsection)
				jnfound = jncount
				EXIT
			ENDIF
		ENDFOR
	ENDIF
	IF jnfound > 0
		FOR jncount = 1 TO  ;
				ALEN(pavars, 1)
			IF pavars(jncount,1) ==  ;
					pcvar .AND.  ;
					pavars(jncount,3) ==  ;
					jnfound
				pavars[ jncount,  ;
                     2] = pcval
				jlfoundvar = .T.
				EXIT
			ENDIF
		ENDFOR
		IF  .NOT. jlfoundvar
			IF  .NOT.  ;
					EMPTY(pavars(1))
				jnlen2 = ALEN(pavars,  ;
					1) + 1
				DIMENSION pavars[  ;
                         jnlen2,  ;
                         3]
			ELSE
				jnlen2 = 1
			ENDIF
			pavars[ jnlen2, 1] =  ;
				pcvar
			pavars[ jnlen2, 2] =  ;
				pcval
			pavars[ jnlen2, 3] =  ;
				jnfound
		ENDIF
	ELSE
		IF  .NOT. EMPTY(pasect(1))
			jnlen = ALEN(pasect, 1) +  ;
				1
			DIMENSION pasect[  ;
                    jnlen]
		ELSE
			jnlen = 1
		ENDIF
		pasect[ jnlen] = pcsection
		IF  .NOT. EMPTY(pavars(1))
			jnlen2 = ALEN(pavars,  ;
				1) + 1
			DIMENSION pavars[  ;
                    jnlen2, 3]
		ELSE
			jnlen2 = 1
		ENDIF
		pavars[ jnlen2, 1] = pcvar
		pavars[ jnlen2, 2] = pcval
		pavars[ jnlen2, 3] = jnlen
	ENDIF
	IF FILE(pcfilename)
		jcoldfile = SUBSTR(pcfilename,  ;
			1, AT('.',  ;
			pcfilename) - 1) +  ;
			'.BAK'
		IF FILE(jcoldfile)
			DELETE FILE (jcoldfile)
		ENDIF
		RENAME (pcfilename) TO  ;
			(jcoldfile)
	ENDIF
	jnfhandle = FCREATE(pcfilename)
	IF  .NOT. jnfhandle == -1 .AND.   ;
			.NOT. EMPTY(pasect)
		FOR ncount = 1 TO  ;
				ALEN(pasect, 1)
			IF (';' $  ;
					pasect(ncount)) .OR.  ;
					EMPTY(pasect(ncount))
				= FPUTS(jnfhandle,  ;
					pasect(ncount))
			ELSE
				= FPUTS(jnfhandle,  ;
					'[' +  ;
					pasect(ncount) +  ;
					']')
			ENDIF
			FOR ncount2 = 1 TO  ;
					ALEN(pavars, 1)
				IF pavars(ncount2, ;
						3) == ncount
					= FPUTS(jnfhandle,  ;
						pavars(ncount2, ;
						1) + ' = ' +  ;
						pavars(ncount2, ;
						2))
				ENDIF
			ENDFOR
		ENDFOR
		= FCLOSE(jnfhandle)
	ELSE
		= messagebox( ;
			'Unable to create file: ' +  ;
			jcfilename, ;
			'File create error',0)
		jlsuccess = .F.
	ENDIF
	RETURN jlsuccess
ENDFUNC && buildini
*!*
FUNCTION buildarray
	PARAMETER pnfhandle, pasect,  ;
		pavars
	PRIVATE ALL LIKE j*
	jnalen = 1
	jnvarlen = 1
	IF pnfhandle > -1
		= FSEEK(pnfhandle, 0)
		DO WHILE  .NOT.  ;
				FEOF(pnfhandle)
			jcline = FGETS(pnfhandle)
			IF ';' $ jcline .OR.  ;
					EMPTY(jcline)
				IF EMPTY(pasect(1))
					pasect[ 1] =  ;
						jcline
				ELSE
					jnalen = ALEN(pasect,  ;
						1) +  ;
						1
					DIMENSION pasect[  ;
                              jnalen]
					pasect[  ;
                          jnalen] =  ;
						jcline
				ENDIF
			ELSE
				jnfound1 = AT('[', jcline)
				jnfound2 = AT(']', jcline)
				IF jnfound1 > 0 .AND. jnfound2 > 0
					jcsection = SUBSTR(jcline,  ;
						jnfound1 +  ;
						1,  ;
						jnfound2 -  ;
						2)
					IF EMPTY(pasect(1))
						pasect[  ;
                               1] =  ;
							jcsection
					ELSE
						jnalen =  ;
							ALEN(pasect,  ;
							1) + 1
						DIMENSION  ;
							pasect[  ;
                          jnalen]
						pasect[  ;
                               jnalen] =  ;
							jcsection
					ENDIF
					jnalen = ALEN(pasect,  ;
						1)
				ELSE
					IF AT('=',  ;
							jcline) >  ;
							0
						IF EMPTY(pavars(1, ;
								1))
							pavars[  ;
                               1,  ;
                               1] =  ;
								ALLTRIM(SUBSTR(jcline,  ;
								1,  ;
								AT( ;
								'=',  ;
								jcline) -  ;
								1))
							pavars[  ;
                               1,  ;
                               2] =  ;
								ALLTRIM(SUBSTR(jcline,  ;
								AT( ;
								'=',  ;
								jcline) +  ;
								1))
							pavars[  ;
                               1,  ;
                               3] =  ;
								jnalen
						ELSE
							jnvarlen =  ;
								ALEN(pavars,  ;
								1) +  ;
								1
							DIMENSION  ;
								pavars[  ;
                               jnvarlen,  ;
                               3]
							pavars[  ;
                               jnvarlen,  ;
                               1] =  ;
								ALLTRIM(SUBSTR(jcline,  ;
								1,  ;
								AT( ;
								'=',  ;
								jcline) -  ;
								1))
							pavars[  ;
                               jnvarlen,  ;
                               2] =  ;
								ALLTRIM(SUBSTR(jcline,  ;
								AT( ;
								'=',  ;
								jcline) +  ;
								1))
							pavars[  ;
                               jnvarlen,  ;
                               3] =  ;
								jnalen
						ENDIF
					ENDIF
				ENDIF
			ENDIF
		ENDDO
	ENDIF
	RETURN .T.
ENDFUNC && buildarray
*!*
FUNCTION getini
	PARAMETER pcinifile, pcsection,  ;
		pcvar
	PRIVATE ALL LIKE j*
	jcretval = ''
	IF  .NOT. EMPTY(pcinifile)
		jcfilename = IIF(AT('.',  ;
			pcinifile) > 0,  ;
			pcinifile,  ;
			pcinifile +  ;
			'.INI')
		IF FILE(jcfilename)
			jnhandle = FOPEN(jcfilename)
			IF jnhandle < 0
				jlsuccess = .F.
				= messagebox( ;
					'Unable to open file: ' +  ;
					jcfilename, ;
					'File Open Error', ;
					0)
			ELSE
				pcsection = ALLTRIM(UPPER(pcsection))
				pcvar = ALLTRIM(UPPER(pcvar))
				jcretval = readini(@jnhandle, ;
					pcsection, ;
					pcvar)
				= FCLOSE(jnhandle)
			ENDIF
		ENDIF
	ENDIF
	RETURN jcretval
ENDFUNC && getini
*!*
FUNCTION readini
	PARAMETER pnfhandle, pcsection,  ;
		pcvar
	PRIVATE ALL LIKE j*
	jcline = ''
	jcsection = ''
	jnfound1 = 0
	jnfound2 = 0
	jnfound3 = 0
	jnalen = 0
	jcretval = ''
	IF  .NOT. EMPTY(pnfhandle)
		= FSEEK(pnfhandle, 0)
		DO WHILE  .NOT.  ;
				FEOF(pnfhandle)
			jcline = FGETS(pnfhandle)
			jnfound1 = AT('[',  ;
				jcline)
			jnfound2 = AT(']',  ;
				jcline)
			IF jnfound1 > 0 .AND.  ;
					jnfound2 > 0
				jcsection = UPPER(SUBSTR(jcline,  ;
					jnfound1 +  ;
					1,  ;
					jnfound2 -  ;
					2))
			ENDIF
			IF jcsection ==  ;
					pcsection
				jnfound3 = AT('=',  ;
					jcline)
				IF jnfound3 > 0
					IF ALLTRIM(UPPER(SUBSTR(jcline,  ;
							1,  ;
							jnfound3 -  ;
							1))) ==  ;
							pcvar
						jcretval =  ;
							ALLTRIM(SUBSTR(jcline,  ;
							jnfound3 +  ;
							1))
						EXIT
					ENDIF
				ENDIF
			ENDIF
		ENDDO
	ENDIF
	RETURN jcretval
ENDFUNC && readini

