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 <rtl/ustring.hxx>
21 #include <sal/log.hxx>
22 #include <com/sun/star/io/IOException.hpp>
23 #include <osl/process.h>
34 inline ini_NameValue()
37 OUString
const & name
, OUString
const & value
)
50 NameValueVector vVector
;
52 typedef std::map
<OUString
,
59 IniSectionMap mAllSection
;
61 IniSectionMap
* getAllSection(){return &mAllSection
;};
62 ini_Section
* getSection(OUString
const & secName
)
64 if (mAllSection
.find(secName
) != mAllSection
.end())
65 return &mAllSection
[secName
];
68 explicit IniParser(OUString
const & rIniName
) throw(css::io::IOException
)
72 osl_getProcessWorkingDir( &curDirPth
.pData
);
73 if (osl_getAbsoluteFileURL( curDirPth
.pData
, rIniName
.pData
, &iniUrl
.pData
))
74 throw css::io::IOException();
77 oslFileHandle handle
=NULL
;
78 if (iniUrl
.getLength() &&
79 osl_File_E_None
== osl_openFile(iniUrl
.pData
, &handle
, osl_File_OpenFlag_Read
))
81 rtl::ByteSequence seq
;
84 osl_getFileSize(handle
, &nSize
);
85 OUString
sectionName( "no name section" );
89 if (osl_File_E_None
!= osl_getFilePos(handle
, &nPos
) || nPos
>= nSize
)
91 if (osl_File_E_None
!= osl_readLine(handle
, (sal_Sequence
**) &seq
))
93 OString
line( (const char *) seq
.getConstArray(), seq
.getLength() );
94 sal_Int32 nIndex
= line
.indexOf('=');
97 ini_Section
*aSection
= &mAllSection
[sectionName
];
98 struct ini_NameValue nameValue
;
99 nameValue
.sName
= OStringToOUString(
100 line
.copy(0,nIndex
).trim(), RTL_TEXTENCODING_ASCII_US
);
101 nameValue
.sValue
= OStringToOUString(
102 line
.copy(nIndex
+1).trim(), RTL_TEXTENCODING_UTF8
);
104 aSection
->vVector
.push_back(nameValue
);
109 sal_Int32 nIndexStart
= line
.indexOf('[');
110 sal_Int32 nIndexEnd
= line
.indexOf(']');
111 if ( nIndexEnd
> nIndexStart
&& nIndexStart
>=0)
113 sectionName
= OStringToOUString(
114 line
.copy(nIndexStart
+ 1,nIndexEnd
- nIndexStart
-1).trim(), RTL_TEXTENCODING_ASCII_US
);
115 if (!sectionName
.getLength())
116 sectionName
= "no name section";
118 ini_Section
*aSection
= &mAllSection
[sectionName
];
119 aSection
->sName
= sectionName
;
123 osl_closeFile(handle
);
125 #if OSL_DEBUG_LEVEL > 1
128 SAL_WARN("connectivity", "couldn't open file: " << iniUrl
);
129 throw css::io::IOException();
133 #if OSL_DEBUG_LEVEL > 1
136 for(auto& rEntry
: mAllSection
)
138 ini_Section
*aSection
= &rEntry
.second
;
139 for(const auto& rValue
: aSection
->vVector
)
141 SAL_WARN("connectivity",
142 " section=" << aSection
->sName
<< " name=" << rValue
.sName
<< " value=" << rValue
.sValue
);
152 int main( int argc
, char * argv
[] )
155 IniParser
parser(OUString("test.ini"));
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */