1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: main.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include <rtl/ustring.hxx>
35 #include <com/sun/star/io/IOException.hpp>
36 #include <osl/process.h>
47 inline ini_NameValue() SAL_THROW( () )
50 OUString
const & name
, OUString
const & value
) SAL_THROW( () )
65 typedef std::map
<rtl::OUString
,
72 IniSectionMap mAllSection
;
74 IniSectionMap
* getAllSection(){return &mAllSection
;};
75 ini_Section
* getSection(OUString
const & secName
)
77 if (mAllSection
.find(secName
) != mAllSection
.end())
78 return &mAllSection
[secName
];
81 IniParser(OUString
const & rIniName
) throw(com::sun::star::io::IOException
)
85 osl_getProcessWorkingDir( &curDirPth
.pData
);
86 if (osl_getAbsoluteFileURL( curDirPth
.pData
, rIniName
.pData
, &iniUrl
.pData
))
87 throw ::com::sun::star::io::IOException();
90 #if OSL_DEBUG_LEVEL > 1
91 OString sFile
= OUStringToOString(iniUrl
, RTL_TEXTENCODING_ASCII_US
);
92 OSL_TRACE(__FILE__
" -- parser() - %s\n", sFile
.getStr());
94 oslFileHandle handle
=NULL
;
95 if (iniUrl
.getLength() &&
96 osl_File_E_None
== osl_openFile(iniUrl
.pData
, &handle
, osl_File_OpenFlag_Read
))
98 rtl::ByteSequence seq
;
101 osl_getFileSize(handle
, &nSize
);
102 OUString sectionName
= OUString::createFromAscii("no name section");
106 if (osl_File_E_None
!= osl_getFilePos(handle
, &nPos
) || nPos
>= nSize
)
108 if (osl_File_E_None
!= osl_readLine(handle
, (sal_Sequence
**) &seq
))
110 OString
line( (const sal_Char
*) seq
.getConstArray(), seq
.getLength() );
111 sal_Int32 nIndex
= line
.indexOf('=');
114 ini_Section
*aSection
= &mAllSection
[sectionName
];
115 struct ini_NameValue nameValue
;
116 nameValue
.sName
= OStringToOUString(
117 line
.copy(0,nIndex
).trim(), RTL_TEXTENCODING_ASCII_US
);
118 nameValue
.sValue
= OStringToOUString(
119 line
.copy(nIndex
+1).trim(), RTL_TEXTENCODING_UTF8
);
121 aSection
->lList
.push_back(nameValue
);
126 sal_Int32 nIndexStart
= line
.indexOf('[');
127 sal_Int32 nIndexEnd
= line
.indexOf(']');
128 if ( nIndexEnd
> nIndexStart
&& nIndexStart
>=0)
130 sectionName
= OStringToOUString(
131 line
.copy(nIndexStart
+ 1,nIndexEnd
- nIndexStart
-1).trim(), RTL_TEXTENCODING_ASCII_US
);
132 if (!sectionName
.getLength())
133 sectionName
= OUString::createFromAscii("no name section");
135 ini_Section
*aSection
= &mAllSection
[sectionName
];
136 aSection
->sName
= sectionName
;
140 osl_closeFile(handle
);
142 #if OSL_DEBUG_LEVEL > 1
145 OString file_tmp
= OUStringToOString(iniUrl
, RTL_TEXTENCODING_ASCII_US
);
146 OSL_TRACE( __FILE__
" -- couldn't open file: %s", file_tmp
.getStr() );
147 throw ::com::sun::star::io::IOException();
151 #if OSL_DEBUG_LEVEL > 1
154 IniSectionMap::iterator iBegin
= mAllSection
.begin();
155 IniSectionMap::iterator iEnd
= mAllSection
.end();
156 for(;iBegin
!= iEnd
;iBegin
++)
158 ini_Section
*aSection
= &(*iBegin
).second
;
159 OString sec_name_tmp
= OUStringToOString(aSection
->sName
, RTL_TEXTENCODING_ASCII_US
);
160 for(NameValueList::iterator itor
=aSection
->lList
.begin();
161 itor
!= aSection
->lList
.end();
164 struct ini_NameValue
* aValue
= &(*itor
);
165 OString name_tmp
= OUStringToOString(aValue
->sName
, RTL_TEXTENCODING_ASCII_US
);
166 OString value_tmp
= OUStringToOString(aValue
->sValue
, RTL_TEXTENCODING_UTF8
);
168 " section=%s name=%s value=%s\n",
169 sec_name_tmp
.getStr(),
171 value_tmp
.getStr() );
181 #if (defined UNX) || (defined OS2)
182 int main( int argc
, char * argv
[] )
184 int _cdecl
main( int argc
, char * argv
[] )
189 IniParser
parser(OUString::createFromAscii("test.ini"));