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 <com/sun/star/io/IOException.hpp>
23 #include <rtl/byteseq.hxx>
24 #include <sal/log.hxx>
25 #include <o3tl/string_view.hxx>
27 IniParser::IniParser(OUString
const & rIniName
)
30 if (osl_File_E_None
!= osl_getFileURLFromSystemPath(rIniName
.pData
, &iniUrl
.pData
))
34 oslFileHandle handle
=nullptr;
35 oslFileError fileError
= osl_File_E_INVAL
;
37 if (!iniUrl
.isEmpty())
38 fileError
= osl_openFile(iniUrl
.pData
, &handle
, osl_File_OpenFlag_Read
);
40 catch(const css::io::IOException
&)
42 SAL_WARN("connectivity.mozab", "couldn't open file: " << iniUrl
);
45 if (osl_File_E_None
== fileError
)
47 rtl::ByteSequence seq
;
50 osl_getFileSize(handle
, &nSize
);
51 OUString
sectionName( u
"no name section"_ustr
);
55 if (osl_File_E_None
!= osl_getFilePos(handle
, &nPos
) || nPos
>= nSize
)
57 if (osl_File_E_None
!= osl_readLine(handle
, reinterpret_cast<sal_Sequence
**>(&seq
)))
59 OString
line(reinterpret_cast<const char *>(seq
.getConstArray()), seq
.getLength() );
60 sal_Int32 nIndex
= line
.indexOf('=');
63 ini_Section
*aSection
= &mAllSection
[sectionName
];
64 struct ini_NameValue nameValue
;
65 nameValue
.sName
= OStringToOUString(
66 o3tl::trim(line
.subView(0,nIndex
)), RTL_TEXTENCODING_ASCII_US
);
67 nameValue
.sValue
= OStringToOUString(
68 o3tl::trim(line
.subView(nIndex
+1)), RTL_TEXTENCODING_UTF8
);
70 aSection
->vVector
.push_back(nameValue
);
75 sal_Int32 nIndexStart
= line
.indexOf('[');
76 sal_Int32 nIndexEnd
= line
.indexOf(']');
77 if ( nIndexEnd
> nIndexStart
&& nIndexStart
>=0)
79 sectionName
= OStringToOUString(
80 o3tl::trim(line
.subView(nIndexStart
+ 1,nIndexEnd
- nIndexStart
-1)), RTL_TEXTENCODING_ASCII_US
);
81 if (sectionName
.isEmpty())
82 sectionName
= "no name section";
86 osl_closeFile(handle
);
90 SAL_INFO("connectivity.mozab", "couldn't open file: " << iniUrl
);
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */