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: MNSINIParser.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 <MNSINIParser.hxx>
34 #include <rtl/byteseq.hxx>
36 ini_Section
* IniParser::getSection(OUString
const & secName
)
38 if (mAllSection
.find(secName
) != mAllSection
.end())
39 return &mAllSection
[secName
];
42 IniParser::IniParser(OUString
const & rIniName
) throw(com::sun::star::io::IOException
)
45 if (osl_File_E_None
!= osl_getFileURLFromSystemPath(rIniName
.pData
, &iniUrl
.pData
))
49 #if OSL_DEBUG_LEVEL > 0
50 OString sFile
= OUStringToOString(iniUrl
, RTL_TEXTENCODING_ASCII_US
);
51 OSL_TRACE(__FILE__
" -- parser() - %s\n", sFile
.getStr());
53 oslFileHandle handle
=NULL
;
54 oslFileError fileError
= osl_File_E_INVAL
;
56 if (iniUrl
.getLength())
57 fileError
= osl_openFile(iniUrl
.pData
, &handle
, osl_File_OpenFlag_Read
);
59 catch(::com::sun::star::io::IOException e
)
61 #if OSL_DEBUG_LEVEL > 0
62 OString file_tmp
= OUStringToOString(iniUrl
, RTL_TEXTENCODING_ASCII_US
);
63 OSL_TRACE( __FILE__
" -- couldn't open file: %s", file_tmp
.getStr() );
67 if (osl_File_E_None
== fileError
)
69 rtl::ByteSequence seq
;
72 osl_getFileSize(handle
, &nSize
);
73 OUString sectionName
= OUString::createFromAscii("no name section");
77 if (osl_File_E_None
!= osl_getFilePos(handle
, &nPos
) || nPos
>= nSize
)
79 if (osl_File_E_None
!= osl_readLine(handle
, (sal_Sequence
**) &seq
))
81 OString
line( (const sal_Char
*) seq
.getConstArray(), seq
.getLength() );
82 sal_Int32 nIndex
= line
.indexOf('=');
85 ini_Section
*aSection
= &mAllSection
[sectionName
];
86 struct ini_NameValue nameValue
;
87 nameValue
.sName
= OStringToOUString(
88 line
.copy(0,nIndex
).trim(), RTL_TEXTENCODING_ASCII_US
);
89 nameValue
.sValue
= OStringToOUString(
90 line
.copy(nIndex
+1).trim(), RTL_TEXTENCODING_UTF8
);
92 aSection
->lList
.push_back(nameValue
);
97 sal_Int32 nIndexStart
= line
.indexOf('[');
98 sal_Int32 nIndexEnd
= line
.indexOf(']');
99 if ( nIndexEnd
> nIndexStart
&& nIndexStart
>=0)
101 sectionName
= OStringToOUString(
102 line
.copy(nIndexStart
+ 1,nIndexEnd
- nIndexStart
-1).trim(), RTL_TEXTENCODING_ASCII_US
);
103 if (!sectionName
.getLength())
104 sectionName
= OUString::createFromAscii("no name section");
106 ini_Section
*aSection
= &mAllSection
[sectionName
];
107 aSection
->sName
= sectionName
;
111 osl_closeFile(handle
);
113 #if OSL_DEBUG_LEVEL > 0
116 OString file_tmp
= OUStringToOString(iniUrl
, RTL_TEXTENCODING_ASCII_US
);
117 OSL_TRACE( __FILE__
" -- couldn't open file: %s", file_tmp
.getStr() );
121 #if OSL_DEBUG_LEVEL > 0
122 void IniParser::Dump()
124 IniSectionMap::iterator iBegin
= mAllSection
.begin();
125 IniSectionMap::iterator iEnd
= mAllSection
.end();
126 for(;iBegin
!= iEnd
;iBegin
++)
128 ini_Section
*aSection
= &(*iBegin
).second
;
129 OString sec_name_tmp
= OUStringToOString(aSection
->sName
, RTL_TEXTENCODING_ASCII_US
);
130 for(NameValueList::iterator itor
=aSection
->lList
.begin();
131 itor
!= aSection
->lList
.end();
134 struct ini_NameValue
* aValue
= &(*itor
);
135 OString name_tmp
= OUStringToOString(aValue
->sName
, RTL_TEXTENCODING_ASCII_US
);
136 OString value_tmp
= OUStringToOString(aValue
->sValue
, RTL_TEXTENCODING_UTF8
);
138 " section=%s name=%s value=%s\n",
139 sec_name_tmp
.getStr(),
141 value_tmp
.getStr() );