LibreOffice Module connectivity (master) 1
MNSINIParser.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "MNSINIParser.hxx"
21#include <com/sun/star/io/IOException.hpp>
22#include <osl/file.h>
23#include <rtl/byteseq.hxx>
24#include <sal/log.hxx>
25#include <o3tl/string_view.hxx>
26
27IniParser::IniParser(OUString const & rIniName)
28{
29 OUString iniUrl;
30 if (osl_File_E_None != osl_getFileURLFromSystemPath(rIniName.pData, &iniUrl.pData))
31 return;
32
33
34 oslFileHandle handle=nullptr;
35 oslFileError fileError = osl_File_E_INVAL;
36 try{
37 if (!iniUrl.isEmpty())
38 fileError = osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read);
39 }
40 catch(const css::io::IOException&)
41 {
42 SAL_WARN("connectivity.mozab", "couldn't open file: " << iniUrl );
43 }
44
45 if (osl_File_E_None == fileError)
46 {
47 rtl::ByteSequence seq;
48 sal_uInt64 nSize = 0;
49
50 osl_getFileSize(handle, &nSize);
51 OUString sectionName( "no name section" );
52 while (true)
53 {
54 sal_uInt64 nPos;
55 if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
56 break;
57 if (osl_File_E_None != osl_readLine(handle, reinterpret_cast<sal_Sequence **>(&seq)))
58 break;
59 OString line(reinterpret_cast<const char *>(seq.getConstArray()), seq.getLength() );
60 sal_Int32 nIndex = line.indexOf('=');
61 if (nIndex >= 1)
62 {
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 );
69
70 aSection->vVector.push_back(nameValue);
71
72 }
73 else
74 {
75 sal_Int32 nIndexStart = line.indexOf('[');
76 sal_Int32 nIndexEnd = line.indexOf(']');
77 if ( nIndexEnd > nIndexStart && nIndexStart >=0)
78 {
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";
83 }
84 }
85 }
86 osl_closeFile(handle);
87 }
88 else
89 {
90 SAL_INFO("connectivity.mozab", "couldn't open file: " << iniUrl );
91 }
92}
93
94/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IniSectionMap mAllSection
IniParser(OUString const &rIniName)
sal_Int32 nIndex
sal_uInt16 nPos
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
line
std::basic_string_view< charT, traits > trim(std::basic_string_view< charT, traits > str)
OUString sValue
OUString sName
NameValueVector vVector