1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "MNSINIParser.hxx"
21 #include <rtl/byteseq.hxx>
22 #include <osl/diagnose.h>
24 IniParser::IniParser(OUString
const & rIniName
) throw(css::io::IOException
, std::exception
)
27 if (osl_File_E_None
!= osl_getFileURLFromSystemPath(rIniName
.pData
, &iniUrl
.pData
))
31 #if OSL_DEBUG_LEVEL > 0
32 OString sFile
= OUStringToOString(iniUrl
, RTL_TEXTENCODING_ASCII_US
);
33 OSL_TRACE(__FILE__
" -- parser() - %s\n", sFile
.getStr());
35 oslFileHandle handle
=nullptr;
36 oslFileError fileError
= osl_File_E_INVAL
;
38 if (!iniUrl
.isEmpty())
39 fileError
= osl_openFile(iniUrl
.pData
, &handle
, osl_File_OpenFlag_Read
);
41 catch(const css::io::IOException
&)
43 #if OSL_DEBUG_LEVEL > 0
44 OString file_tmp
= OUStringToOString(iniUrl
, RTL_TEXTENCODING_ASCII_US
);
45 OSL_TRACE( __FILE__
" -- couldn't open file: %s", file_tmp
.getStr() );
49 if (osl_File_E_None
== fileError
)
51 rtl::ByteSequence seq
;
54 osl_getFileSize(handle
, &nSize
);
55 OUString
sectionName( "no name section" );
59 if (osl_File_E_None
!= osl_getFilePos(handle
, &nPos
) || nPos
>= nSize
)
61 if (osl_File_E_None
!= osl_readLine(handle
, reinterpret_cast<sal_Sequence
**>(&seq
)))
63 OString
line(reinterpret_cast<const char *>(seq
.getConstArray()), seq
.getLength() );
64 sal_Int32 nIndex
= line
.indexOf('=');
67 ini_Section
*aSection
= &mAllSection
[sectionName
];
68 struct ini_NameValue nameValue
;
69 nameValue
.sName
= OStringToOUString(
70 line
.copy(0,nIndex
).trim(), RTL_TEXTENCODING_ASCII_US
);
71 nameValue
.sValue
= OStringToOUString(
72 line
.copy(nIndex
+1).trim(), RTL_TEXTENCODING_UTF8
);
74 aSection
->lList
.push_back(nameValue
);
79 sal_Int32 nIndexStart
= line
.indexOf('[');
80 sal_Int32 nIndexEnd
= line
.indexOf(']');
81 if ( nIndexEnd
> nIndexStart
&& nIndexStart
>=0)
83 sectionName
= OStringToOUString(
84 line
.copy(nIndexStart
+ 1,nIndexEnd
- nIndexStart
-1).trim(), RTL_TEXTENCODING_ASCII_US
);
85 if (sectionName
.isEmpty())
86 sectionName
= "no name section";
88 ini_Section
*aSection
= &mAllSection
[sectionName
];
89 aSection
->sName
= sectionName
;
93 osl_closeFile(handle
);
95 #if OSL_DEBUG_LEVEL > 0
98 OString file_tmp
= OUStringToOString(iniUrl
, RTL_TEXTENCODING_ASCII_US
);
99 OSL_TRACE( __FILE__
" -- couldn't open file: %s", file_tmp
.getStr() );
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */