-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

i created my first snippet using the new snippetcreator ...


Oliver

- --
GnuPG key 0xCFD04A45: 8822 057F 4956 46D3 352C 1A06 4E2C AB40 CFD0 4A45
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEL9IUTiyrQM/QSkURAt/zAKCrGm+DVUYye8LWoVLpO54MEGWlIQCdFeOy
eBD3+42PEihqwy3l0x+VT+k=
=M72d
-----END PGP SIGNATURE-----
<?xml version="1.0"?>
<!--
$RCSfile: $
last change: $Revision: $ $Author: $ $Date: $

(c)2003 by the copyright holders listed with the author-tags.
If no explicit copyright holder is mentioned with a certain author,
the author him-/herself is the copyright holder. All rights reserved.

Public Documentation License Notice:

The contents of this Documentation are subject to the
Public Documentation License Version 1.0 (the "License");
you may only use this Documentation if you comply with
the terms of this License. A copy of the License is
available at http://www.openoffice.org/licenses/PDL.html

The Original Documentation can be found in the CVS archives
of openoffice.org at the place specified by RCSfile: in this header.

The Initial Writer(s) of the Original Documentation are listed
with the author-tags below.

The Contributor(s) are listed with the author-tags below
without the marker for being an initial author.

All Rights Reserved.
-->

<snippet language="OOBasic" application="Office">

<keywords>
	<keyword>ini</keyword>
	<keyword>file</keyword>
	<keyword>fileaccess</keyword>
	<keyword>read</keyword>
</keywords>

<authors>
</authors>

<question heading="ReadIniFile">How to read an ini file using oo basic ?


<p> </p>
</question>

<answer>
<p>the macro will read an ini file into a multidimensional array ...</p>
<p>to acces the entries within the sections use:</p>
<p></p>
<p> [DATA]	      mIni(0,0,0) -&gt; [DATA]</p>
<p> X=1		          mIni(0,0,1) -&gt; X / mIni(0,0,2) -&gt; 1</p>
<p> Y=2		          mIni(0,1,1) -&gt; Y / mIni(0,1,2) -&gt; 2</p>
<p> [HELP]	          mIni(1,0,0) -&gt; [HELP]</p>
<p> INFO=YES 	  mIni(1,0,1) -&gt; INFO /  mIni(1,0,2) -&gt; YES</p>
<p></p>
<listing>OPTION EXPLICIT

Sub Main()

	Dim mIni() as String
	Dim sMsg as String
	Dim i as Integer
	Dim j as Integer

	mIni() = ReadIniFile(&quot;E:\temp\test.ini&quot;, &quot;&quot;, 20, 50)
	
	For i = 0 To uBound(mIni())
		sMsg = sMsg &amp; mIni(i,0,0) &amp; Chr(13)
		For j = 0 To 50
			If mIni(i,j,1) &lt;&gt; &quot;&quot; Then
				sMsg = sMsg &amp; mIni(i,j,1) &amp; &quot; = &quot; &amp; mIni(i,j,2) &amp; Chr(13)
			EndIf
		Next j
		MsgBox sMsg
		sMsg = &quot;&quot;
	Next i

End Sub

Function ReadIniFile(ByVal sFile as String, ByVal sEncoding as String,_
					 ByVal nMaxSection as Integer, ByVal nMaxEntry as Integer) as Variant

	On Local Error Goto ErrorHandler

	Dim oFileAccess as Variant
	Dim oFile as Variant
	Dim oStream as Variant

	Dim mTmp() as String
	Dim sTmp as String
	Dim i as Long
	Dim j as Long
	Dim bFlag as Boolean

	Dim mArray(nMaxSection, nMaxEntry, 2) as String

	If sEncoding = &quot;&quot; Then
		sEncoding = &quot;ISO-8859-1&quot;
	EndIf

	oFileAccess = createUnoService(&quot;[EMAIL PROTECTED] com.sun.star.ucb.SimpleFileAccess}&quot;)

	If oFileAccess.exists(sFile) and Not oFileAccess.IsFolder(sFile) Then

		oFile = oFileAccess.openFileRead(sFile)
		oStream = createUnoService(&quot;[EMAIL PROTECTED] com.sun.star.io.TextInputStream}&quot;)
		oStream.setInputStream(oFile)
		oStream.setEncoding(sEncoding)

		i = 0
		j = 0

		mArray(0, 0, 0) = &quot;[]&quot;
		bFlag = True
		
		While Not oStream.isEOF
			sTmp = oStream.readLine()
			If sTmp &lt;&gt; &quot;&quot; and Left(sTmp, 1) &lt;&gt; &quot;;&quot;  Then	&apos; ignore comments ...
				If Left(sTmp, 1) = &quot;[&quot; Then
					If bFlag = True Then
						bFlag = False
					Else
						j = j + 1
						i = 0
					EndIf
					mArray(j, i, 0) = Trim(sTmp)
				Else
					mTmp() = Split(sTmp, &quot;=&quot;, 2)
					If uBound(mTmp()) = 1 Then
						mArray(j, i, 1) = Trim(mTmp(0))
						mArray(j, i, 2) = mTmp(1)
					Else
						mArray(j, i, 1) = Trim(sTmp)
					EndIf
					i = i + 1
				EndIf
			EndIf
		Wend
		oStream.closeInput()
		oFile.closeInput()
	EndIf

	Redim Preserve mArray(j, nMaxEntry, 2) as String
	ReadIniFile = mArray()
	Exit Function

ErrorHandler:
	mArray(0, 0, 0) = &quot;-1&quot;
	ReadIniFile = mArray()
	MsgBox Err() &amp; Chr(13) &amp; Error() &amp; Chr(13) &amp; Erl()
End Function</listing>
</answer>

<versions>
	<version number="1.1.x" status="tested"/>
	<version number="2.0.x" status="tested"/>
</versions>

<operating-systems>
<operating-system name="Win32"/>
</operating-systems>

<changelog>
</changelog>

</snippet>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to