1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include <rtl/ustring.hxx>
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <com/sun/star/io/IOException.hpp>
33*cdf0e10cSrcweir #include <osl/process.h>
34*cdf0e10cSrcweir using namespace rtl;
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <map>
37*cdf0e10cSrcweir #include <list>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir struct ini_NameValue
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir 	rtl::OUString sName;
42*cdf0e10cSrcweir 	rtl::OUString sValue;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir     inline ini_NameValue() SAL_THROW( () )
45*cdf0e10cSrcweir         {}
46*cdf0e10cSrcweir     inline ini_NameValue(
47*cdf0e10cSrcweir         OUString const & name, OUString const & value ) SAL_THROW( () )
48*cdf0e10cSrcweir         : sName( name ),
49*cdf0e10cSrcweir           sValue( value )
50*cdf0e10cSrcweir         {}
51*cdf0e10cSrcweir };
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir typedef std::list<
54*cdf0e10cSrcweir     ini_NameValue
55*cdf0e10cSrcweir > NameValueList;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir struct ini_Section
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir 	rtl::OUString sName;
60*cdf0e10cSrcweir 	NameValueList lList;
61*cdf0e10cSrcweir };
62*cdf0e10cSrcweir typedef std::map<rtl::OUString,
63*cdf0e10cSrcweir 				ini_Section
64*cdf0e10cSrcweir 				>IniSectionMap;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir class IniParser
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	IniSectionMap mAllSection;
70*cdf0e10cSrcweir public:
71*cdf0e10cSrcweir 	IniSectionMap * getAllSection(){return &mAllSection;};
72*cdf0e10cSrcweir 	ini_Section *  getSection(OUString const & secName)
73*cdf0e10cSrcweir 	{
74*cdf0e10cSrcweir 		if (mAllSection.find(secName) != mAllSection.end())
75*cdf0e10cSrcweir 			return &mAllSection[secName];
76*cdf0e10cSrcweir 		return NULL;
77*cdf0e10cSrcweir 	}
78*cdf0e10cSrcweir 	IniParser(OUString const & rIniName) throw(com::sun::star::io::IOException )
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		OUString curDirPth;
81*cdf0e10cSrcweir 		OUString iniUrl;
82*cdf0e10cSrcweir 		osl_getProcessWorkingDir( &curDirPth.pData );
83*cdf0e10cSrcweir 		if (osl_getAbsoluteFileURL( curDirPth.pData,	rIniName.pData, &iniUrl.pData ))
84*cdf0e10cSrcweir 			throw ::com::sun::star::io::IOException();
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
88*cdf0e10cSrcweir 		OString sFile = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
89*cdf0e10cSrcweir 		OSL_TRACE(__FILE__" -- parser() - %s\n", sFile.getStr());
90*cdf0e10cSrcweir #endif
91*cdf0e10cSrcweir 		oslFileHandle handle=NULL;
92*cdf0e10cSrcweir 		if (iniUrl.getLength() &&
93*cdf0e10cSrcweir 			osl_File_E_None == osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read))
94*cdf0e10cSrcweir 		{
95*cdf0e10cSrcweir 			rtl::ByteSequence seq;
96*cdf0e10cSrcweir 			sal_uInt64 nSize = 0;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 			osl_getFileSize(handle, &nSize);
99*cdf0e10cSrcweir 			OUString sectionName = OUString::createFromAscii("no name section");
100*cdf0e10cSrcweir 			while (true)
101*cdf0e10cSrcweir 			{
102*cdf0e10cSrcweir 				sal_uInt64 nPos;
103*cdf0e10cSrcweir 				if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
104*cdf0e10cSrcweir 					break;
105*cdf0e10cSrcweir 				if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq))
106*cdf0e10cSrcweir 					break;
107*cdf0e10cSrcweir 				OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
108*cdf0e10cSrcweir 				sal_Int32 nIndex = line.indexOf('=');
109*cdf0e10cSrcweir 				if (nIndex >= 1)
110*cdf0e10cSrcweir 				{
111*cdf0e10cSrcweir 					ini_Section *aSection = &mAllSection[sectionName];
112*cdf0e10cSrcweir 					struct ini_NameValue nameValue;
113*cdf0e10cSrcweir 					nameValue.sName = OStringToOUString(
114*cdf0e10cSrcweir 						line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US );
115*cdf0e10cSrcweir 					nameValue.sValue = OStringToOUString(
116*cdf0e10cSrcweir 						line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 					aSection->lList.push_back(nameValue);
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 				}
121*cdf0e10cSrcweir 				else
122*cdf0e10cSrcweir 				{
123*cdf0e10cSrcweir 					sal_Int32 nIndexStart = line.indexOf('[');
124*cdf0e10cSrcweir 					sal_Int32 nIndexEnd = line.indexOf(']');
125*cdf0e10cSrcweir 					if ( nIndexEnd > nIndexStart && nIndexStart >=0)
126*cdf0e10cSrcweir 					{
127*cdf0e10cSrcweir 						sectionName =  OStringToOUString(
128*cdf0e10cSrcweir 							line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US );
129*cdf0e10cSrcweir 						if (!sectionName.getLength())
130*cdf0e10cSrcweir 							sectionName = OUString::createFromAscii("no name section");
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 						ini_Section *aSection = &mAllSection[sectionName];
133*cdf0e10cSrcweir 						aSection->sName = sectionName;
134*cdf0e10cSrcweir 					}
135*cdf0e10cSrcweir 				}
136*cdf0e10cSrcweir 			}
137*cdf0e10cSrcweir 			osl_closeFile(handle);
138*cdf0e10cSrcweir 		}
139*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
140*cdf0e10cSrcweir 		else
141*cdf0e10cSrcweir 		{
142*cdf0e10cSrcweir 			OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
143*cdf0e10cSrcweir 			OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
144*cdf0e10cSrcweir 			throw ::com::sun::star::io::IOException();
145*cdf0e10cSrcweir 		}
146*cdf0e10cSrcweir #endif
147*cdf0e10cSrcweir 	}
148*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
149*cdf0e10cSrcweir 	void Dump()
150*cdf0e10cSrcweir 	{
151*cdf0e10cSrcweir 		IniSectionMap::iterator iBegin = mAllSection.begin();
152*cdf0e10cSrcweir 		IniSectionMap::iterator iEnd = mAllSection.end();
153*cdf0e10cSrcweir 		for(;iBegin != iEnd;iBegin++)
154*cdf0e10cSrcweir 		{
155*cdf0e10cSrcweir 			ini_Section *aSection = &(*iBegin).second;
156*cdf0e10cSrcweir 			OString sec_name_tmp = OUStringToOString(aSection->sName, RTL_TEXTENCODING_ASCII_US);
157*cdf0e10cSrcweir 			for(NameValueList::iterator itor=aSection->lList.begin();
158*cdf0e10cSrcweir 				itor != aSection->lList.end();
159*cdf0e10cSrcweir 				itor++)
160*cdf0e10cSrcweir 			{
161*cdf0e10cSrcweir 					struct ini_NameValue * aValue = &(*itor);
162*cdf0e10cSrcweir 					OString name_tmp = OUStringToOString(aValue->sName, RTL_TEXTENCODING_ASCII_US);
163*cdf0e10cSrcweir 					OString value_tmp = OUStringToOString(aValue->sValue, RTL_TEXTENCODING_UTF8);
164*cdf0e10cSrcweir 					OSL_TRACE(
165*cdf0e10cSrcweir 						" section=%s name=%s value=%s\n",
166*cdf0e10cSrcweir 										sec_name_tmp.getStr(),
167*cdf0e10cSrcweir 										name_tmp.getStr(),
168*cdf0e10cSrcweir 										value_tmp.getStr() );
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 			}
171*cdf0e10cSrcweir 		}
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 	}
174*cdf0e10cSrcweir #endif
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir };
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir #if (defined UNX) || (defined OS2)
179*cdf0e10cSrcweir int main( int argc, char * argv[] )
180*cdf0e10cSrcweir #else
181*cdf0e10cSrcweir int _cdecl main( int argc, char * argv[] )
182*cdf0e10cSrcweir #endif
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	IniParser parser(OUString::createFromAscii("test.ini"));
187*cdf0e10cSrcweir 	parser.Dump();
188*cdf0e10cSrcweir     return 0;
189*cdf0e10cSrcweir }
190